Skip to content

Commit f758434

Browse files
authored
Merge branch 'datreeio:main' into develop
2 parents 4752c60 + 82c9b58 commit f758434

1,485 files changed

Lines changed: 955973 additions & 45036 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/pull_request_template.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
## PR Checklist
22

3-
- [ ] I have used the [CRD Extractor](https://github.com/datreeio/CRDs-catalog?tab=readme-ov-file#crd-extractor) tool to generate these CRDs
3+
- [ ] I generated these CRs using the [CRD Extractor tool](https://github.com/datreeio/CRDs-catalog?tab=readme-ov-file#crd-extractor). If I used a different method, I have described the method in this PR.
4+
- [ ] I am updating existing schemas and have specified the updated schema version.
5+
- [ ] I am adding new schemas and included a link to the GitHub repository that contains the source of these schemas.

README.md

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ This repository also contains a handy utility that extracts all CRDs from a clus
5353

5454
### What does this utility do?
5555
1. Checks that the prerequisites are installed.
56-
2. Extracts your CRDs from your cluster using kubectl.
56+
2. Extracts your CRDs from your cluster using kubectl (optionally from a specific [Kubernetes context](#using-a-specific-kubernetes-context) via `KUBECTL_CONTEXT`).
5757
3. Using the script from [openapi2jsonschema.py from kubeconform](https://github.com/yannh/kubeconform/blob/master/scripts/openapi2jsonschema.py) to convert your CRDs from openAPI to JSON schema.
5858

5959
### Supported Platforms
@@ -65,11 +65,45 @@ The following programs are required to be installed on the machine running this
6565
* [python3](https://www.python.org/downloads/)
6666
* [kubectl](https://kubernetes.io/docs/tasks/tools/#kubectl)
6767

68+
Alternatively, you can use [Nix](https://nixos.org/) or [Devbox](https://www.jetify.com/devbox) to automatically set up all dependencies.
69+
6870
### Usage
69-
To use the CRD Extractor:
71+
To use the CRD Extractor:
7072
1. Download the [latest release](https://github.com/datreeio/CRDs-catalog/releases/latest/download/crd-extractor.zip) from this repository.
7173
2. Extract, and run the utility:
7274
```
75+
cd Utilities
76+
./crd-extractor.sh
77+
```
78+
79+
#### Using a specific Kubernetes context
80+
To extract CRDs from a specific Kubernetes cluster context, set the `KUBECTL_CONTEXT` environment variable:
81+
```
82+
cd Utilities
83+
KUBECTL_CONTEXT=staging ./crd-extractor.sh
84+
```
85+
86+
Or export it before running:
87+
```
88+
export KUBECTL_CONTEXT=staging
89+
cd Utilities
90+
./crd-extractor.sh
91+
```
92+
93+
If `KUBECTL_CONTEXT` is not set, the utility will use your default kubectl context.
94+
95+
#### Using Devbox
96+
If you have [Devbox](https://www.jetify.com/devbox) installed:
97+
```
98+
cd Utilities
99+
devbox run crd-extractor
100+
```
101+
102+
#### Using Nix
103+
If you have [Nix](https://nixos.org/) with flakes enabled:
104+
```
105+
cd Utilities
106+
nix develop
73107
./crd-extractor.sh
74108
```
75109

Utilities/crd-extractor.sh

Lines changed: 79 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
#!/usr/bin/env bash
2+
# shellcheck disable=SC2059
23

34
set -euo pipefail
45

6+
# Support kubectl context via KUBECTL_CONTEXT environment variable
7+
KUBECTL_ARGS=(kubectl)
8+
if [ -n "${KUBECTL_CONTEXT:-}" ]; then
9+
KUBECTL_ARGS=(kubectl --context="${KUBECTL_CONTEXT}")
10+
fi
11+
512
fetch_crd() {
613
filename=${1%% *}
7-
kubectl get crds "$filename" -o yaml >"$TMP/$filename.yaml" 2>&1
14+
if ! "${KUBECTL_ARGS[@]}" get crds "$filename" -o yaml >"$TMP/$filename.yaml" 2>/dev/null; then
15+
printf "Warning: Failed to extract CRD: %s\n" "$filename" >&2
16+
fi
817
}
918

1019
# Check if python3 is installed
@@ -52,19 +61,24 @@ fi
5261

5362
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
5463

55-
# Create temp folder for CRDs
64+
# Create temp folders for CRDs and conversion
5665
TMP=$(mktemp -d)
57-
trap 'rm -rf "$TMP"' EXIT
66+
CONVERSION_TMP=$(mktemp -d)
67+
trap 'rm -rf "$TMP" "$CONVERSION_TMP"' EXIT
5868

5969
# Create final schemas directory
6070
# Use current directory if OUTPUT_DIR is set, otherwise use default
6171
SCHEMAS_DIR=${OUTPUT_DIR:-$HOME/.datree/crdSchemas}
6272
mkdir -p "$SCHEMAS_DIR"
63-
cd "$SCHEMAS_DIR"
6473

6574
# Get a list of all CRDs
66-
printf "Fetching list of CRDs...\n"
67-
IFS=$'\n' read -r -d '' -a CRD_LIST < <(kubectl get crds 2>&1 | sed -n '/NAME/,$p' | tail -n +2 && printf '\0')
75+
printf "Fetching list of CRDs..."
76+
if [ -n "${KUBECTL_CONTEXT:-}" ]; then
77+
printf " (using context: %s)\n" "${KUBECTL_CONTEXT}"
78+
else
79+
printf "\n"
80+
fi
81+
IFS=$'\n' read -r -d '' -a CRD_LIST < <("${KUBECTL_ARGS[@]}" get crds 2>&1 | sed -n '/NAME/,$p' | tail -n +2 && printf '\0')
6882

6983
# If no CRDs exist in the cluster, exit
7084
if [ ${#CRD_LIST[@]} == 0 ]; then
@@ -89,40 +103,80 @@ for crd in "${CRD_LIST[@]}"; do
89103
fi
90104
((++FETCHED_CRDS))
91105
done
106+
# shellcheck disable=SC2046
92107
wait $(jobs -p)
93108

94109

95-
# Convert crds to jsonSchema
110+
# Convert crds to jsonSchema - output to temp directory to avoid mixing with existing schemas
96111
CONVERTER_SCRIPT="$SCRIPT_DIR/openapi2jsonschema.py"
97112
export FILENAME_FORMAT="{fullgroup}_{kind}_{version}"
113+
cd "$CONVERSION_TMP"
98114
python3 "$CONVERTER_SCRIPT" "$TMP"/*.yaml
99115
conversionResult=$?
100116

101-
# Copy and rename files to support kubeval
102-
rm -rf "$SCHEMAS_DIR/master-standalone"
103-
mkdir -p "$SCHEMAS_DIR/master-standalone"
104-
cp "$SCHEMAS_DIR"/*.json "$SCHEMAS_DIR/master-standalone"
105-
find "$SCHEMAS_DIR/master-standalone" -name '*json' -exec bash -c ' mv -f $0 ${0/\_/-stable-}' {} \;
106-
107-
# Organize schemas by group
108-
for schema in "$SCHEMAS_DIR"/*.json; do
109-
crdFileName=$(basename "$schema")
110-
crdGroup=$(echo "$crdFileName" | cut -d"_" -f1)
111-
outName=$(echo "$crdFileName" | cut -d"_" -f2-)
112-
mkdir -p "$crdGroup"
113-
mv "$schema" "./$crdGroup/$outName"
117+
# Track which groups we're processing in this run
118+
declare -A PROCESSED_GROUPS
119+
120+
# Find newly generated schemas in the conversion temp directory
121+
NEW_SCHEMAS=()
122+
shopt -s nullglob
123+
for schema in "$CONVERSION_TMP"/*.json; do
124+
[ -f "$schema" ] && NEW_SCHEMAS+=("$schema")
114125
done
126+
shopt -u nullglob
127+
128+
# Only proceed if we have new schemas
129+
if [ ${#NEW_SCHEMAS[@]} -gt 0 ]; then
130+
# Copy and rename files to support kubeval
131+
rm -rf "$SCHEMAS_DIR/master-standalone"
132+
mkdir -p "$SCHEMAS_DIR/master-standalone"
133+
cp "$CONVERSION_TMP"/*.json "$SCHEMAS_DIR/master-standalone/" 2>/dev/null
134+
find "$SCHEMAS_DIR/master-standalone" -name '*json' -exec bash -c ' mv -f $0 ${0/\_/-stable-}' {} \;
135+
136+
# Organize schemas by group
137+
for schema in "${NEW_SCHEMAS[@]}"; do
138+
crdFileName=$(basename "$schema")
139+
crdGroup=$(echo "$crdFileName" | cut -d"_" -f1)
140+
outName=$(echo "$crdFileName" | cut -d"_" -f2-)
141+
mkdir -p "$SCHEMAS_DIR/$crdGroup"
142+
cp "$schema" "$SCHEMAS_DIR/$crdGroup/$outName"
143+
PROCESSED_GROUPS[$crdGroup]=1
144+
done
145+
146+
# Copy organized schemas to git repository (when not using custom OUTPUT_DIR)
147+
if [ -z "${OUTPUT_DIR:-}" ]; then
148+
printf "\nCopying schemas to repository...\n"
149+
for crdGroup in "${!PROCESSED_GROUPS[@]}"; do
150+
groupDir="$SCHEMAS_DIR/$crdGroup"
151+
if [ -d "$groupDir" ]; then
152+
# Copy to parent directory of Utilities folder
153+
repoDir="$(dirname "$SCRIPT_DIR")/$crdGroup"
154+
mkdir -p "$repoDir"
155+
cp -r "$groupDir"/*.json "$repoDir/" 2>/dev/null || true
156+
printf " Copied %s schemas\n" "$crdGroup"
157+
fi
158+
done
159+
fi
160+
fi
115161

116162
CYAN='\033[0;36m'
117163
GREEN='\033[0;32m'
118164
NC='\033[0m' # No Color
119165

120-
if [ $conversionResult == 0 ]; then
121-
printf "${GREEN}Successfully converted $FETCHED_CRDS CRDs to JSON schema${NC}\n"
122-
printf "Schemas saved to: ${CYAN}$SCHEMAS_DIR${NC}\n"
166+
if [ $conversionResult == 0 ] && [ ${#NEW_SCHEMAS[@]} -gt 0 ]; then
167+
printf "\n${GREEN}Successfully converted ${FETCHED_CRDS} CRDs to JSON schema${NC}\n"
168+
printf "Schemas saved to: ${CYAN}${SCHEMAS_DIR}${NC}\n"
169+
170+
if [ -z "${OUTPUT_DIR:-}" ] && [ ${#PROCESSED_GROUPS[@]} -gt 0 ]; then
171+
printf "\n${GREEN}Schemas organized by API group and copied to:${NC}\n"
172+
for crdGroup in "${!PROCESSED_GROUPS[@]}"; do
173+
repoDir="$(dirname "$SCRIPT_DIR")/$crdGroup"
174+
printf " - %s/\n" "$repoDir"
175+
done | sort -u
176+
fi
123177

124178
printf "\nTo validate a CR using various tools, run the relevant command:\n"
125179
printf "\n- ${CYAN}datree:${NC}\n\$ datree test /path/to/file\n"
126-
printf "\n- ${CYAN}kubeconform:${NC}\n\$ kubeconform -summary -output json -schema-location default -schema-location '$SCHEMAS_DIR/{{ .Group }}/{{ .ResourceKind }}_{{ .ResourceAPIVersion }}.json' /path/to/file\n"
127-
printf "\n- ${CYAN}kubeval:${NC}\n\$ kubeval --additional-schema-locations file:\"$SCHEMAS_DIR\" /path/to/file\n\n"
180+
printf "\n- ${CYAN}kubeconform:${NC}\n\$ kubeconform -summary -output json -schema-location default -schema-location '${SCHEMAS_DIR}/{{ .Group }}/{{ .ResourceKind }}_{{ .ResourceAPIVersion }}.json' /path/to/file\n"
181+
printf "\n- ${CYAN}kubeval:${NC}\n\$ kubeval --additional-schema-locations file:\"${SCHEMAS_DIR}\" /path/to/file\n\n"
128182
fi

Utilities/devbox.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/jetify-com/devbox/0.14.2/.schema/devbox.schema.json",
3+
"packages": [
4+
"python313Packages.pyyaml@latest",
5+
"kubectl@1.32",
6+
"python313@latest"
7+
],
8+
"shell": {
9+
"init_hook": [
10+
"echo \"$(python3 --version)\"",
11+
"echo \"$(kubectl version --client)\"",
12+
"echo \"context: $(kubectl config current-context 2>/dev/null || echo '<no context>')\""
13+
],
14+
"scripts": {
15+
"crd-extractor": [
16+
"./crd-extractor.sh"
17+
]
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)