Skip to content

Commit

Permalink
fix(compose-chart-export): secret name cannot use "_"
Browse files Browse the repository at this point in the history
  • Loading branch information
EspenAlbert committed Sep 27, 2023
1 parent 6758f30 commit b43e1e7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -486,21 +486,30 @@ def service_account(spec: ChartTemplateSpec) -> str:
metadata:
labels:
{{- include "common.labels.standard" . | nindent 4 }}
name: {{ printf "%s-$SECRET_NAME" .Release.Name }}
name: {{ $SECRET_TEMPLATE_NAME }}
namespace: {{ .Release.Namespace }}
"""


def as_secret_template_name(name: str) -> str:
name = name.replace("_", "-")
return 'printf "%s-{name}" .Release.Name'.format(name=name)


def secret_with_env_vars_template(
name: str, env_vars: list[str], container_name: str, existing_value_ref: str
) -> str:
replacements = {"$EXISTING_REF": existing_value_ref, "$SECRET_NAME": name}
replacements = {
"$EXISTING_REF": existing_value_ref,
"$SECRET_TEMPLATE_NAME": as_secret_template_name(name),
}
manifest = _SECRET_OPTIONAL
for in_, out in replacements.items():
manifest = manifest.replace(in_, out)
manifest_data = ["data:"]
container_name_underscore = container_name.replace("-", "_")
manifest_data.extend(
f" {env_var}: {{{{ .Values.{container_name}.{env_var} | b64enc | quote }}}}"
f" {env_var}: {{{{ .Values.{container_name_underscore}.{env_var} | b64enc | quote }}}}"
for env_var in env_vars
)
manifest_data.append("{{- end -}}")
Expand Down
26 changes: 14 additions & 12 deletions compose_chart_export/src/compose_chart_export/chart_mods.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@
from pathlib import Path
from typing import Dict, Iterable, Mapping, Optional, Set, Union

from compose_chart_export.chart_file_templates import secret_with_env_vars_template
from compose_chart_export.chart_file_templates import (
as_secret_template_name,
secret_with_env_vars_template,
)
from compose_chart_export.ports import PrefixPort
from model_lib.serialize.yaml_serialize import edit_helm_template, edit_yaml

service_yaml = "templates/service.yaml"
values_yaml = "values.yaml"


def as_existing_secret_name(secret_name: str) -> str:
return f"existing_secret_{secret_name}"
def as_existing_secret_value_ref(secret_name: str) -> str:
return f"existing_secret_{secret_name.replace('-', '_')}"


def update_values(
Expand Down Expand Up @@ -42,7 +45,7 @@ def update_values(
if probe_values:
values.update(probe_values)
for secret_name in secret_names:
all_values[as_existing_secret_name(secret_name)] = ""
all_values[as_existing_secret_value_ref(secret_name)] = ""


def add_container(
Expand Down Expand Up @@ -117,11 +120,11 @@ def update_containers( # noqa: C901
env.append(dict(name=name, value=value_template))
env_from = []
for secret in secret_names:
existing_secret_name = as_existing_secret_name(secret)
secret_name_value = (
'{{{{ eq .Values.{} "" | ternary "{}" .Values.{} | quote }}}}'.format(
existing_secret_name, secret, existing_secret_name
)
existing_secret_name = as_existing_secret_value_ref(secret)
default_secret_name = f"( {as_secret_template_name(secret)} )"
secret_name_value = '{{{{ eq .Values.{secret_ref} "" | ternary {default_secret_name} .Values.{secret_ref} | quote }}}}'.format(
secret_ref=existing_secret_name,
default_secret_name=default_secret_name,
)
env_from.append(dict(secretRef=dict(name=secret_name_value)))
if env_from:
Expand Down Expand Up @@ -191,8 +194,7 @@ def update_services(chart_dir: Path, ports: Iterable[PrefixPort], container_name


def secret_with_env_vars(container_name: str, name: str, env_vars: list[str]) -> str:
container_name_underscore = container_name.replace("-", "_")
existing_secret_name = as_existing_secret_name(name)
existing_secret_name = as_existing_secret_value_ref(name)
return secret_with_env_vars_template(
name, env_vars, container_name_underscore, existing_secret_name
name, env_vars, container_name, existing_secret_name
)
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ spec:
value: {{ .Values.service_deployment_with_healthcheck.env | quote }}
envFrom:
- secretRef:
name: {{ eq .Values.existing_secret_name "" | ternary "name" .Values.existing_secret_name | quote }}
name: {{ eq .Values.existing_secret_name "" | ternary ( printf "%s-name" .Release.Name ) .Values.existing_secret_name | quote }}
readinessProbe: {{- toYaml .Values.service_deployment_with_healthcheck.readinessProbe | nindent 10 }}
nodeSelector:
{{- with .Values.nodeSelector }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ spec:
value: {{ .Values.docker_example.name | quote }}
envFrom:
- secretRef:
name: {{ eq .Values.existing_secret_secret1 "" | ternary "secret1" .Values.existing_secret_secret1 | quote }}
name: {{ eq .Values.existing_secret_secret1 "" | ternary ( printf "%s-secret1" .Release.Name ) .Values.existing_secret_secret1 | quote }}
- secretRef:
name: {{ eq .Values.existing_secret_secret2 "" | ternary "secret2" .Values.existing_secret_secret2 | quote }}
name: {{ eq .Values.existing_secret_secret2 "" | ternary ( printf "%s-secret2" .Release.Name ) .Values.existing_secret_secret2 | quote }}
readinessProbe: {{- toYaml .Values.docker_example.readinessProbe | nindent 10 }}
livenessProbe: {{- toYaml .Values.docker_example.livenessProbe | nindent 10 }}
startupProbe: {{- toYaml .Values.docker_example.startupProbe | nindent 10 }}
Expand Down

0 comments on commit b43e1e7

Please sign in to comment.