Skip to content

Commit

Permalink
feat: add crd install as flag for values.yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
TrayserCassa committed Jan 29, 2025
1 parent 1a66bf1 commit 224a936
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ helm: path version manifests kustomize yq ## Undeploy controller from the K8s cl
$(KUSTOMIZE) build config/helm > $(path)/templates/operator.yaml
$(YQ) e -i '.appVersion = "$(version)"' $(path)/Chart.yaml
$(YQ) e -i '.version = "$(version)"' $(path)/Chart.yaml
python config/helm/customize_crd.py $(path)/crds/crd.yaml $(path)/templates/crd
rm -rf $(path)/crds

.PHONY: resources
resources: path manifests kustomize ## Create crd's and manager for a direct kubectl apply
Expand Down
58 changes: 58 additions & 0 deletions config/helm/customize_crd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import yaml
import sys


if len(sys.argv) != 3:
print("Missing arguments: inputFile outputDir")
sys.exit(1)

# Input and output file names
INPUT_FILE = sys.argv[1]
OUTPUT_DIR = sys.argv[2]

# Read and split CRDs by "---"
with open(INPUT_FILE, "r") as file:
crd_docs = list(yaml.safe_load_all(file))

# Process each CRD separately
modified_crds = []
for crd in crd_docs:
if not isinstance(crd, dict):
continue # Skip empty sections

# Ensure metadata and annotations exist
metadata = crd.setdefault("metadata", {})
annotations = metadata.setdefault("annotations", {})

# Inject Helm annotations
# annotations["helm.sh/resource-policy"] = "keep"

# Convert CRD back to YAML
crd_yaml = yaml.dump(crd, default_flow_style=False, sort_keys=False)

fixedYaml = ""
for line in crd_yaml.splitlines():
# if line.strip() == "helm.sh/resource-policy: keep":
# print("Modify line:", line)
# fixedYaml += " " + "{{- if .Values.crds.keep }}" + "\n"
# fixedYaml += line + "\n"
# fixedYaml += " " + "{{- end}}" + "\n"
# continue

fixedYaml += line + "\n"

# Wrap with Helm conditional for each CRD
wrapped_yaml = f"""{{{{- if .Values.crds.install }}}}
{fixedYaml}
{{{{- end }}}}
"""
modified_crds.append(
{"name": crd["spec"]["names"]["kind"], "content": wrapped_yaml}
)

# Save the modified CRD YAML
for i in range(len(modified_crds)):
with open("%s/%s.yaml" % (OUTPUT_DIR, modified_crds[i]["name"]), "w") as file:
file.write(modified_crds[i]["content"])

print("Modified CRD file saved as", modified_crds[i]["name"])
4 changes: 2 additions & 2 deletions config/helm/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
namespace: "{{ .Release.Namespace }}"
namePrefix: '{{ .Release.Name }}-'
namePrefix: "{{ .Release.Name }}-"

resources:
- ../rbac
- ../rbac
Empty file added helm/templates/crd/.gitkeep
Empty file.
5 changes: 5 additions & 0 deletions helm/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ image:
tag: ""
pullPolicy: IfNotPresent

## Custom resource configuration
crds:
# Install and upgrade CRDs
install: true

# rbac: settings for deployer RBAC creation
rbac:
# rbac.create: if false, RBAC resources should be in place
Expand Down

0 comments on commit 224a936

Please sign in to comment.