From 9133d6f4354b3786501f55c573283dea5c23745c Mon Sep 17 00:00:00 2001 From: ilia-medvedev-codefresh Date: Thu, 30 Mar 2023 18:25:04 +0300 Subject: [PATCH] Add delete runtime hook, possibility to add arbitrary config values to app-proxy and HELM_RELEASE_NAME environment variable (#25) * add delete runtime hook, possibility to add arbitrary config values to app-proxy and HELM_RELEASE_NAME environment variable --- charts/gitops-runtime/Chart.yaml | 2 +- charts/gitops-runtime/README.md | 2 +- .../_components/cap-app-proxy/_config.yaml | 43 +++++++++++-------- .../templates/app-proxy/_app-proxy-env.yaml | 1 + .../pre-uninstall/cleanup-resources.yaml | 6 +-- .../delete-runtime-from-platform.yaml | 32 ++++++++++++++ .../templates/hooks/pre-uninstall/rbac.yaml | 37 ++++++++++++++++ .../tests/app-proxy-misc_test.yaml | 36 ++++++++++++++++ 8 files changed, 134 insertions(+), 25 deletions(-) create mode 100644 charts/gitops-runtime/templates/hooks/pre-uninstall/delete-runtime-from-platform.yaml create mode 100644 charts/gitops-runtime/templates/hooks/pre-uninstall/rbac.yaml diff --git a/charts/gitops-runtime/Chart.yaml b/charts/gitops-runtime/Chart.yaml index bbefed62..0ba3c0c5 100644 --- a/charts/gitops-runtime/Chart.yaml +++ b/charts/gitops-runtime/Chart.yaml @@ -2,7 +2,7 @@ apiVersion: v2 appVersion: 0.1.27-helm-alpha description: A Helm chart for Codefresh gitops runtime name: gitops-runtime -version: 0.2.0-16-alpha +version: 0.2.1-alpha.1 home: https://github.com/codefresh-io/gitops-runtime-helm keywords: - codefresh diff --git a/charts/gitops-runtime/README.md b/charts/gitops-runtime/README.md index c164885b..4b578133 100644 --- a/charts/gitops-runtime/README.md +++ b/charts/gitops-runtime/README.md @@ -1,6 +1,6 @@ # gitops-runtime -![Version: 0.2.0-16-alpha](https://img.shields.io/badge/Version-0.2.0--16--alpha-informational?style=flat-square) ![AppVersion: 0.1.27-helm-alpha](https://img.shields.io/badge/AppVersion-0.1.27--helm--alpha-informational?style=flat-square) +![Version: 0.2.1-alpha.1](https://img.shields.io/badge/Version-0.2.1--alpha.1-informational?style=flat-square) ![AppVersion: 0.1.27-helm-alpha](https://img.shields.io/badge/AppVersion-0.1.27--helm--alpha-informational?style=flat-square) A Helm chart for Codefresh gitops runtime diff --git a/charts/gitops-runtime/templates/_components/cap-app-proxy/_config.yaml b/charts/gitops-runtime/templates/_components/cap-app-proxy/_config.yaml index 109ab02b..4da38cfe 100644 --- a/charts/gitops-runtime/templates/_components/cap-app-proxy/_config.yaml +++ b/charts/gitops-runtime/templates/_components/cap-app-proxy/_config.yaml @@ -1,4 +1,28 @@ +{{- define "cap-app-proxy.resources.configmap-documented-configs"}} +argoCdUrl: {{ .Values.config.argoCdUrl }} +argoCdUsername: {{ .Values.config.argoCdUsername }} +argoWorkflowsInsecure: {{ .Values.config.argoWorkflowsInsecure | quote }} +env: {{ .Values.config.env | quote}} +argoWorkflowsUrl: {{ default "" .Values.config.argoWorkflowsUrl }} +runtimeName: {{ .Values.global.runtime.name | quote}} +skipGitPermissionValidation: {{ .Values.config.skipGitPermissionValidation | quote }} +logLevel: {{ .Values.config.logLevel | quote }} + {{- $enrichmentValues := get .Values "image-enrichment" }} + {{- if $enrichmentValues.enabled }} +enrichmentConcurrencyCmName: {{ $enrichmentValues.config.concurrencyCmName | quote}} +enrichmentConcurrencyCmKey: {{ $enrichmentValues.config.concurrencyCmKey | quote}} +enrichmentServiceAccountName: {{ $enrichmentValues.serviceAccount.name | quote}} +enrichmentPodGcStrategy: {{ $enrichmentValues.config.podGcStrategy | quote}} +enrichmentTtlAfterCompletionInSeconds: {{ $enrichmentValues.config.ttlAfterCompletionInSeconds | quote }} +enrichmentTtlActiveInSeconds: {{ $enrichmentValues.config.ttlActiveInSeconds | quote }} +enrichmentClientHeartbeatIntervalInSeconds: {{ $enrichmentValues.config.clientHeartbeatIntervalInSeconds | quote }} + {{- end }} +{{- end }} + {{- define "cap-app-proxy.resources.configmap" }} +{{- $documentedConfigs := (include "cap-app-proxy.resources.configmap-documented-configs" . | fromYaml ) }} +{{- $overrides := .Values.config }} +{{- $mergedConfig := mergeOverwrite $documentedConfigs $overrides }} apiVersion: v1 kind: ConfigMap metadata: @@ -6,22 +30,5 @@ metadata: labels: {{- include "cap-app-proxy.labels" . | nindent 4 }} data: - argoCdUrl: {{ .Values.config.argoCdUrl }} - argoCdUsername: {{ .Values.config.argoCdUsername }} - argoWorkflowsInsecure: {{ .Values.config.argoWorkflowsInsecure | quote }} - env: {{ .Values.config.env | quote}} - argoWorkflowsUrl: {{ default "" .Values.config.argoWorkflowsUrl }} - runtimeName: {{ .Values.global.runtime.name | quote}} - skipGitPermissionValidation: {{ .Values.config.skipGitPermissionValidation | quote }} - logLevel: {{ .Values.config.logLevel | quote }} - {{- $enrichmentValues := get .Values "image-enrichment" }} - {{- if $enrichmentValues.enabled }} - enrichmentConcurrencyCmName: {{ $enrichmentValues.config.concurrencyCmName | quote}} - enrichmentConcurrencyCmKey: {{ $enrichmentValues.config.concurrencyCmKey | quote}} - enrichmentServiceAccountName: {{ $enrichmentValues.serviceAccount.name | quote}} - enrichmentPodGcStrategy: {{ $enrichmentValues.config.podGcStrategy | quote}} - enrichmentTtlAfterCompletionInSeconds: {{ $enrichmentValues.config.ttlAfterCompletionInSeconds | quote }} - enrichmentTtlActiveInSeconds: {{ $enrichmentValues.config.ttlActiveInSeconds | quote }} - enrichmentClientHeartbeatIntervalInSeconds: {{ $enrichmentValues.config.clientHeartbeatIntervalInSeconds | quote }} - {{- end }} + {{- $mergedConfig | toYaml | nindent 2}} {{- end }} diff --git a/charts/gitops-runtime/templates/app-proxy/_app-proxy-env.yaml b/charts/gitops-runtime/templates/app-proxy/_app-proxy-env.yaml index e2fab175..8f917180 100644 --- a/charts/gitops-runtime/templates/app-proxy/_app-proxy-env.yaml +++ b/charts/gitops-runtime/templates/app-proxy/_app-proxy-env.yaml @@ -4,6 +4,7 @@ to keep the separation of components as pseudo library charts, they are defined app-proxy components are generated. */}} {{- define "codefresh-gitops-runtime.app-proxy.calculated-env-vars"}} +HELM_RELEASE_NAME: {{ .Release.Name }} USER_TOKEN: {{- include "codefresh-gitops-runtime.installation-token-env-var-value" . | nindent 2 }} GIT_INTEGRATION_PROVIDER: {{ include "codefresh-gitops-runtime.git-integration.provider" .}} diff --git a/charts/gitops-runtime/templates/hooks/pre-uninstall/cleanup-resources.yaml b/charts/gitops-runtime/templates/hooks/pre-uninstall/cleanup-resources.yaml index 2bbe27fe..562bc609 100644 --- a/charts/gitops-runtime/templates/hooks/pre-uninstall/cleanup-resources.yaml +++ b/charts/gitops-runtime/templates/hooks/pre-uninstall/cleanup-resources.yaml @@ -4,7 +4,7 @@ metadata: name: cleanup-runtime-resources annotations: "helm.sh/hook": pre-delete - "helm.sh/hook-delete-policy": hook-succeeded + "helm.sh/hook-delete-policy": hook-succeeded,before-hook-creation spec: backoffLimit: 3 template: @@ -14,14 +14,10 @@ spec: containers: - name: cleanup-runtime-resources image: "{{ .Values.installer.image.repository }}:{{ .Values.installer.image.tag | default .Chart.Version }}" - env: - - name: ISC_REPO_APPLICATION - value: {{ .Values.global.runtime.name}}-isc imagePullPolicy: {{ .Values.installer.image.pullPolicy }} command: ["sh", "-c"] args: - | - kubectl delete application $ISC_REPO_APPLICATION --cascade=foreground; kubectl patch EventBus $(kubectl get eventbus -l codefresh.io/internal=true | awk 'NR>1{print $1}' | xargs) -p '{"metadata":{"finalizers":null}}' --type=merge && \ kubectl patch Eventsource $(kubectl get EventSource -l codefresh.io/internal=true | awk 'NR>1{print $1}' | xargs) -p '{"metadata":{"finalizers":null}}' --type=merge && \ kubectl patch Sensor $(kubectl get Sensor -l codefresh.io/internal=true | awk 'NR>1{print $1}' | xargs) -p '{"metadata":{"finalizers":null}}' --type=merge ; diff --git a/charts/gitops-runtime/templates/hooks/pre-uninstall/delete-runtime-from-platform.yaml b/charts/gitops-runtime/templates/hooks/pre-uninstall/delete-runtime-from-platform.yaml new file mode 100644 index 00000000..6d38a676 --- /dev/null +++ b/charts/gitops-runtime/templates/hooks/pre-uninstall/delete-runtime-from-platform.yaml @@ -0,0 +1,32 @@ +apiVersion: batch/v1 +kind: Job +metadata: + name: delete-runtime-from-platform + annotations: + "helm.sh/hook": pre-delete + "helm.sh/hook-delete-policy": hook-succeeded,before-hook-creation,hook-failed +spec: + backoffLimit: 3 + template: + spec: + restartPolicy: Never + containers: + - name: delete-runtime-from-platform + image: "{{ .Values.installer.image.repository }}:{{ .Values.installer.image.tag | default .Chart.Version }}" + env: + - name: RUNTIME_NAME + value: {{ .Values.global.runtime.name }} + - name: PLATFORM_URL + value: {{ .Values.global.codefresh.url }} + - name: USER_TOKEN + {{ include "codefresh-gitops-runtime.installation-token-env-var-value" . | nindent 10 }} + imagePullPolicy: {{ .Values.installer.image.pullPolicy }} + command: ["sh", "-c"] + args: + - | + if [ ! -z "$USER_TOKEN" ]; then + curl ${PLATFORM_URL}/2.0/api/graphql -H "Authorization: ${USER_TOKEN}" -H "Content-Type: application/json" -d "{\"query\": \"mutation deleteRuntime(\$name: String"'!'") {\n deleteRuntime(name: \$name) }\",\"variables\": {\"name\": \"${RUNTIME_NAME}\"}}" + else + echo "User token unavailable, unable to remove runtime ${RUNTIME_NAME} from platform, please remove it from UI" + exit 1 + fi diff --git a/charts/gitops-runtime/templates/hooks/pre-uninstall/rbac.yaml b/charts/gitops-runtime/templates/hooks/pre-uninstall/rbac.yaml new file mode 100644 index 00000000..81c5e6df --- /dev/null +++ b/charts/gitops-runtime/templates/hooks/pre-uninstall/rbac.yaml @@ -0,0 +1,37 @@ +apiVersion: rbac.authorization.k8s.io/v1 +kind: Role +metadata: + name: runtime-cleanup + annotations: + "helm.sh/hook": pre-delete + "helm.sh/hook-delete-policy": hook-succeeded,before-hook-creation,hook-failed +rules: + - apiGroups: + - "*" + resources: + - "*" + verbs: + - "*" +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: RoleBinding +metadata: + name: runtime-cleanup + annotations: + "helm.sh/hook": pre-delete + "helm.sh/hook-delete-policy": hook-succeeded,before-hook-creation,hook-failed +roleRef: + apiGroup: "" + kind: Role + name: runtime-cleanup +subjects: + - kind: ServiceAccount + name: runtime-cleanup +--- +apiVersion: v1 +kind: ServiceAccount +metadata: + name: runtime-cleanup + annotations: + "helm.sh/hook": pre-delete + "helm.sh/hook-delete-policy": hook-succeeded,before-hook-creation,hook-failed \ No newline at end of file diff --git a/charts/gitops-runtime/tests/app-proxy-misc_test.yaml b/charts/gitops-runtime/tests/app-proxy-misc_test.yaml index e9394a72..eaaf93df 100644 --- a/charts/gitops-runtime/tests/app-proxy-misc_test.yaml +++ b/charts/gitops-runtime/tests/app-proxy-misc_test.yaml @@ -15,6 +15,18 @@ tests: content: name: PORT value: 8787 +- it: adding environment variables on main container + template: 'app-proxy/deployment.yaml' + values: + - ./values/mandatory-values.yaml + set: + app-proxy.env.SOME_ENV: 'test' + asserts: + - contains: + path: spec.template.spec.containers[0].env + content: + name: SOME_ENV + value: test - it: overriding of argoCD and workflows Url through values template: 'app-proxy/config.yaml' values: @@ -146,3 +158,27 @@ tests: values: - antarctica-east1 - antarctica-west1 + +- it: add some other config option to app-proxy + template: 'app-proxy/config.yaml' + values: + - ./values/mandatory-values.yaml + set: + app-proxy.config.myconf: 'test' + asserts: + - equal: + path: data.myconf + value: test + +- it: Release name exists in app-proxy env + template: 'app-proxy/deployment.yaml' + values: + - ./values/mandatory-values.yaml + release: + name: my-release + asserts: + - contains: + path: spec.template.spec.containers[0].env + content: + name: HELM_RELEASE_NAME + value: my-release