Skip to content

Commit

Permalink
add git-sync-ssh secret template (apache#39936)
Browse files Browse the repository at this point in the history
  • Loading branch information
romsharon98 authored Jun 19, 2024
1 parent 13437cb commit f81abd6
Show file tree
Hide file tree
Showing 13 changed files with 225 additions and 8 deletions.
11 changes: 8 additions & 3 deletions chart/templates/_helpers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ If release name contains chart name it will be used as a full name.
{{- define "git_sync_ssh_key_volume" }}
- name: git-sync-ssh-key
secret:
secretName: {{ .Values.dags.gitSync.sshKeySecret }}
secretName: {{ template "git_sync_ssh_key" . }}
defaultMode: 288
{{- end }}

Expand All @@ -199,7 +199,7 @@ If release name contains chart name it will be used as a full name.
imagePullPolicy: {{ .Values.images.gitSync.pullPolicy }}
securityContext: {{- include "localContainerSecurityContext" .Values.dags.gitSync | nindent 4 }}
env:
{{- if .Values.dags.gitSync.sshKeySecret }}
{{- if or .Values.dags.gitSync.sshKeySecret .Values.dags.gitSync.sshKey }}
- name: GIT_SSH_KEY_FILE
value: "/etc/git-secret/ssh"
- name: GITSYNC_SSH_KEY_FILE
Expand Down Expand Up @@ -294,7 +294,7 @@ If release name contains chart name it will be used as a full name.
volumeMounts:
- name: dags
mountPath: /git
{{- if .Values.dags.gitSync.sshKeySecret }}
{{- if or .Values.dags.gitSync.sshKeySecret .Values.dags.gitSync.sshKey }}
- name: git-sync-ssh-key
mountPath: /etc/git-secret/ssh
readOnly: true
Expand Down Expand Up @@ -427,6 +427,11 @@ If release name contains chart name it will be used as a full name.
{{- printf "%s/%s" .Values.kerberos.ccacheMountPath .Values.kerberos.ccacheFileName }}
{{- end }}

{{/* Create the name of the git sync ssh secret to use */}}
{{- define "git_sync_ssh_key" -}}
{{- default (printf "%s-ssh-secret" (include "airflow.fullname" .)) .Values.dags.gitSync.sshKeySecret }}
{{- end }}

{{- define "celery_executor_namespace" -}}
{{- if semverCompare ">=2.7.0" .Values.airflowVersion }}
{{- print "airflow.providers.celery.executors.celery_executor.app" -}}
Expand Down
2 changes: 1 addition & 1 deletion chart/templates/scheduler/scheduler-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ spec:
{{- else if .Values.dags.gitSync.enabled }}
- name: dags
emptyDir: {{- toYaml (default (dict) .Values.dags.gitSync.emptyDirConfig) | nindent 12 }}
{{- if .Values.dags.gitSync.sshKeySecret }}
{{- if or .Values.dags.gitSync.sshKeySecret .Values.dags.gitSync.sshKey}}
{{- include "git_sync_ssh_key_volume" . | indent 8 }}
{{- end }}
{{- end }}
Expand Down
34 changes: 34 additions & 0 deletions chart/templates/secrets/git-ssh-key-secret.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{{/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/}}

{{- if and .Values.dags.gitSync.sshKey .Values.dags.gitSync.enabled}}
apiVersion: v1
kind: Secret
metadata:
labels:
release: {{ .Release.Name }}
chart: {{ .Chart.Name }}
heritage: {{ .Release.Service }}
{{- with .Values.labels }}
{{- toYaml . | nindent 4 }}
{{- end }}
name: {{ template "git_sync_ssh_key" . }}
data:
gitSshKey: {{ .Values.dags.gitSync.sshKey | b64enc | quote }}
{{- end }}
4 changes: 2 additions & 2 deletions chart/templates/triggerer/triggerer-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ spec:
{{- tpl (toYaml .Values.triggerer.waitForMigrations.env) $ | nindent 12 }}
{{- end }}
{{- end }}
{{- if and (.Values.dags.gitSync.enabled) (not .Values.dags.persistence.enabled) }}
{{- if and (or .Values.dags.gitSync.enabled) (not .Values.dags.persistence.enabled) }}
{{- include "git_sync_container" (dict "Values" .Values "is_init" "true" "Template" .Template) | nindent 8 }}
{{- end }}
{{- if .Values.triggerer.extraInitContainers }}
Expand Down Expand Up @@ -273,7 +273,7 @@ spec:
{{- else if .Values.dags.gitSync.enabled }}
- name: dags
emptyDir: {{- toYaml (default (dict) .Values.dags.gitSync.emptyDirConfig) | nindent 12 }}
{{- if .Values.dags.gitSync.sshKeySecret }}
{{- if or .Values.dags.gitSync.sshKeySecret .Values.dags.gitSync.sshKey}}
{{- include "git_sync_ssh_key_volume" . | nindent 8 }}
{{- end }}
{{- end }}
Expand Down
2 changes: 1 addition & 1 deletion chart/templates/webserver/webserver-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ spec:
{{- else if .Values.dags.gitSync.enabled }}
- name: dags
emptyDir: {{- toYaml (default (dict) .Values.dags.gitSync.emptyDirConfig) | nindent 12 }}
{{- if .Values.dags.gitSync.sshKeySecret }}
{{- if or .Values.dags.gitSync.sshKeySecret .Values.dags.gitSync.sshKey}}
{{- include "git_sync_ssh_key_volume" . | indent 8 }}
{{- end }}
{{- end }}
Expand Down
2 changes: 1 addition & 1 deletion chart/templates/workers/worker-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ spec:
{{- else if .Values.dags.gitSync.enabled }}
- name: dags
emptyDir: {{- toYaml (default (dict) .Values.dags.gitSync.emptyDirConfig) | nindent 12 }}
{{- if .Values.dags.gitSync.sshKeySecret }}
{{- if or .Values.dags.gitSync.sshKeySecret .Values.dags.gitSync.sshKey}}
{{- include "git_sync_ssh_key_volume" . | indent 8 }}
{{- end }}
{{- end }}
Expand Down
8 changes: 8 additions & 0 deletions chart/values.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -8426,6 +8426,14 @@
],
"default": null
},
"sshKey": {
"description": "SSH private key",
"type": [
"string",
"null"
],
"default": null
},
"sshKeySecret": {
"description": "Name of a Secret containing the repo `sshKeySecret`.",
"type": [
Expand Down
6 changes: 6 additions & 0 deletions chart/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2609,6 +2609,12 @@ dags:
# and specify the name of the secret below
# sshKeySecret: airflow-ssh-secret
#
# Or set sshKeySecret with your key
# sshKey: |-
# -----BEGIN {OPENSSH PRIVATE KEY}-----
# ...
# -----END {OPENSSH PRIVATE KEY}-----
#
# If you are using an ssh private key, you can additionally
# specify the content of your known_hosts file, example:
#
Expand Down
42 changes: 42 additions & 0 deletions helm_tests/other/test_git_ssh_key_secret.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from __future__ import annotations

import jmespath

from tests.charts.helm_template_generator import render_chart


class TestGitSSHKeySecret:
"""Tests git-ssh secret."""

def test_create_git_ssh_key_secret(self):
docs = render_chart(
values={
"dags": {
"gitSync": {
"enabled": True,
"sshKey": "cm9tIGlzIHRoZSBraW5n",
},
"persistence": {"enabled": True},
}
},
show_only=["templates/secrets/git-ssh-key-secret.yaml"],
)

assert "release-name-ssh-secret" == jmespath.search("metadata.name", docs[0])
assert "Y205dElHbHpJSFJvWlNCcmFXNW4=" == jmespath.search("data.gitSshKey", docs[0])
30 changes: 30 additions & 0 deletions helm_tests/other/test_git_sync_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,36 @@ def test_validate_if_ssh_params_are_added(self):
"secret": {"secretName": "ssh-secret", "defaultMode": 288},
} in jmespath.search("spec.template.spec.volumes", docs[0])

def test_validate_if_ssh_params_are_added_with_git_ssh_key(self):
docs = render_chart(
values={
"dags": {
"gitSync": {
"enabled": True,
"sshKey": "dummy-ssh-key",
}
}
},
show_only=["templates/scheduler/scheduler-deployment.yaml"],
)

assert {"name": "GIT_SSH_KEY_FILE", "value": "/etc/git-secret/ssh"} in jmespath.search(
"spec.template.spec.containers[1].env", docs[0]
)
assert {"name": "GITSYNC_SSH_KEY_FILE", "value": "/etc/git-secret/ssh"} in jmespath.search(
"spec.template.spec.containers[1].env", docs[0]
)
assert {"name": "GIT_SYNC_SSH", "value": "true"} in jmespath.search(
"spec.template.spec.containers[1].env", docs[0]
)
assert {"name": "GITSYNC_SSH", "value": "true"} in jmespath.search(
"spec.template.spec.containers[1].env", docs[0]
)
assert {
"name": "git-sync-ssh-key",
"secret": {"secretName": "release-name-ssh-secret", "defaultMode": 288},
} in jmespath.search("spec.template.spec.volumes", docs[0])

def test_validate_sshkeysecret_not_added_when_persistence_is_enabled(self):
docs = render_chart(
values={
Expand Down
36 changes: 36 additions & 0 deletions helm_tests/other/test_git_sync_triggerer.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,39 @@ def test_validate_sshkeysecret_not_added_when_persistence_is_enabled(self):
show_only=["templates/triggerer/triggerer-deployment.yaml"],
)
assert "git-sync-ssh-key" not in jmespath.search("spec.template.spec.volumes[].name", docs[0])

def test_validate_if_ssh_params_are_added_with_git_ssh_key(self):
docs = render_chart(
values={
"dags": {
"gitSync": {
"enabled": True,
"sshKey": "dummy-ssh-key",
}
}
},
show_only=["templates/triggerer/triggerer-deployment.yaml"],
)

assert {"name": "GIT_SSH_KEY_FILE", "value": "/etc/git-secret/ssh"} in jmespath.search(
"spec.template.spec.containers[1].env", docs[0]
)
assert {"name": "GITSYNC_SSH_KEY_FILE", "value": "/etc/git-secret/ssh"} in jmespath.search(
"spec.template.spec.containers[1].env", docs[0]
)
assert {"name": "GIT_SYNC_SSH", "value": "true"} in jmespath.search(
"spec.template.spec.containers[1].env", docs[0]
)
assert {"name": "GITSYNC_SSH", "value": "true"} in jmespath.search(
"spec.template.spec.containers[1].env", docs[0]
)
assert {"name": "GIT_KNOWN_HOSTS", "value": "false"} in jmespath.search(
"spec.template.spec.containers[1].env", docs[0]
)
assert {"name": "GITSYNC_SSH_KNOWN_HOSTS", "value": "false"} in jmespath.search(
"spec.template.spec.containers[1].env", docs[0]
)
assert {
"name": "git-sync-ssh-key",
"secret": {"secretName": "release-name-ssh-secret", "defaultMode": 288},
} in jmespath.search("spec.template.spec.volumes", docs[0])
20 changes: 20 additions & 0 deletions helm_tests/other/test_git_sync_webserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,23 @@ def test_validate_sshkeysecret_not_added_when_persistence_is_enabled(self):
show_only=["templates/webserver/webserver-deployment.yaml"],
)
assert "git-sync-ssh-key" not in jmespath.search("spec.template.spec.volumes[].name", docs[0])

def test_validate_if_ssh_params_are_added_with_git_ssh_key(self):
docs = render_chart(
values={
"airflowVersion": "1.10.14",
"dags": {
"gitSync": {
"enabled": True,
"sshKey": "dummy-ssh-key",
},
"persistence": {"enabled": False},
},
},
show_only=["templates/webserver/webserver-deployment.yaml"],
)

assert {
"name": "git-sync-ssh-key",
"secret": {"secretName": "release-name-ssh-secret", "defaultMode": 288},
} in jmespath.search("spec.template.spec.volumes", docs[0])
36 changes: 36 additions & 0 deletions helm_tests/other/test_git_sync_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,39 @@ def test_validate_sshkeysecret_not_added_when_persistence_is_enabled(self):
)

assert "git-sync-ssh-key" not in jmespath.search("spec.template.spec.volumes[].name", docs[0])

def test_validate_if_ssh_params_are_added_with_git_ssh_key(self):
docs = render_chart(
values={
"dags": {
"gitSync": {
"enabled": True,
"sshKey": "dummy-ssh-key",
}
}
},
show_only=["templates/workers/worker-deployment.yaml"],
)

assert {"name": "GIT_SSH_KEY_FILE", "value": "/etc/git-secret/ssh"} in jmespath.search(
"spec.template.spec.containers[1].env", docs[0]
)
assert {"name": "GITSYNC_SSH_KEY_FILE", "value": "/etc/git-secret/ssh"} in jmespath.search(
"spec.template.spec.containers[1].env", docs[0]
)
assert {"name": "GIT_SYNC_SSH", "value": "true"} in jmespath.search(
"spec.template.spec.containers[1].env", docs[0]
)
assert {"name": "GITSYNC_SSH", "value": "true"} in jmespath.search(
"spec.template.spec.containers[1].env", docs[0]
)
assert {"name": "GIT_KNOWN_HOSTS", "value": "false"} in jmespath.search(
"spec.template.spec.containers[1].env", docs[0]
)
assert {"name": "GITSYNC_SSH_KNOWN_HOSTS", "value": "false"} in jmespath.search(
"spec.template.spec.containers[1].env", docs[0]
)
assert {
"name": "git-sync-ssh-key",
"secret": {"secretName": "release-name-ssh-secret", "defaultMode": 288},
} in jmespath.search("spec.template.spec.volumes", docs[0])

0 comments on commit f81abd6

Please sign in to comment.