make a better override + add more values (serviceAccount, nodeSelector...)
This commit is contained in:
@@ -81,6 +81,11 @@ func NewDeployment(service types.ServiceConfig, chart *HelmChart) *Deployment {
|
|||||||
ObjectMeta: metav1.ObjectMeta{
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
Labels: GetMatchLabels(service.Name, appName),
|
Labels: GetMatchLabels(service.Name, appName),
|
||||||
},
|
},
|
||||||
|
Spec: corev1.PodSpec{
|
||||||
|
NodeSelector: map[string]string{
|
||||||
|
"katenary.v3/node-selector": "replace",
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -161,6 +166,9 @@ func (d *Deployment) AddContainer(service types.ServiceConfig) {
|
|||||||
Name: `{{ .Values.pullSecrets | toYaml | indent __indent__ }}`,
|
Name: `{{ .Values.pullSecrets | toYaml | indent __indent__ }}`,
|
||||||
}}
|
}}
|
||||||
|
|
||||||
|
// add ServiceAccount to the deployment
|
||||||
|
d.Spec.Template.Spec.ServiceAccountName = `{{ .Values.` + service.Name + `.serviceAccount | quote }}`
|
||||||
|
|
||||||
d.AddHealthCheck(service, &container)
|
d.AddHealthCheck(service, &container)
|
||||||
|
|
||||||
d.Spec.Template.Spec.Containers = append(d.Spec.Template.Spec.Containers, container)
|
d.Spec.Template.Spec.Containers = append(d.Spec.Template.Spec.Containers, container)
|
||||||
@@ -539,24 +547,55 @@ func (d *Deployment) Yaml() ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// for impagePullSecrets, replace the name with the value from values.yaml
|
// for impagePullSecrets, replace the name with the value from values.yaml
|
||||||
inpullsecrets := false
|
|
||||||
for i, line := range content {
|
for i, line := range content {
|
||||||
if strings.Contains(line, "imagePullSecrets:") {
|
if strings.Contains(line, "imagePullSecrets:") {
|
||||||
inpullsecrets = true
|
spaces = strings.Repeat(" ", utils.CountStartingSpaces(line))
|
||||||
}
|
line = spaces + "{{- if .Values.pullSecrets }}"
|
||||||
if inpullsecrets && strings.Contains(line, "- name: ") && inpullsecrets {
|
line += "\n" + spaces + "imagePullSecrets:\n"
|
||||||
line = strings.Replace(line, "- name: ", "", 1)
|
line += spaces + "{{- .Values.pullSecrets | toYaml | nindent __indent__ }}"
|
||||||
line = strings.ReplaceAll(line, "'", "")
|
line += "\n" + spaces + "{{- end }}"
|
||||||
content[i] = line
|
content[i] = line
|
||||||
inpullsecrets = false
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find the replicas line and replace it with the value from values.yaml
|
// Find the replicas line and replace it with the value from values.yaml
|
||||||
for i, line := range content {
|
for i, line := range content {
|
||||||
|
// manage nodeSelector
|
||||||
|
if strings.Contains(line, "nodeSelector:") {
|
||||||
|
spaces = strings.Repeat(" ", utils.CountStartingSpaces(line))
|
||||||
|
pre := spaces + `{{- if .Values.` + serviceName + `.nodeSelector }}`
|
||||||
|
post := spaces + "{{- end }}"
|
||||||
|
ns := spaces + "nodeSelector:\n"
|
||||||
|
ns += spaces + ` {{- .Values.` + serviceName + `.nodeSelector | toYaml | nindent __indent__ }}`
|
||||||
|
//line = strings.Replace(line, "katenary.v3/node-selector: replace", ns, 1)
|
||||||
|
line = pre + "\n" + ns + "\n" + post
|
||||||
|
}
|
||||||
|
// manage replicas
|
||||||
if strings.Contains(line, "replicas:") {
|
if strings.Contains(line, "replicas:") {
|
||||||
line = regexp.MustCompile("replicas: .*$").ReplaceAllString(line, "replicas: {{ .Values."+serviceName+".replicas }}")
|
line = regexp.MustCompile("replicas: .*$").ReplaceAllString(line, "replicas: {{ .Values."+serviceName+".replicas }}")
|
||||||
content[i] = line
|
}
|
||||||
|
|
||||||
|
// manage serviceAccount, add condition to use the serviceAccount from values.yaml
|
||||||
|
if strings.Contains(line, "serviceAccountName:") {
|
||||||
|
spaces = strings.Repeat(" ", utils.CountStartingSpaces(line))
|
||||||
|
pre := spaces + `{{- if ne .Values.` + serviceName + `.serviceAccount "" }}`
|
||||||
|
post := spaces + "{{- end }}"
|
||||||
|
line = strings.ReplaceAll(line, "'", "")
|
||||||
|
line = pre + "\n" + line + "\n" + post
|
||||||
|
}
|
||||||
|
|
||||||
|
content[i] = line
|
||||||
|
}
|
||||||
|
|
||||||
|
// find the katenary.v3/node-selector line, and remove it
|
||||||
|
for i, line := range content {
|
||||||
|
if strings.Contains(line, "katenary.v3/node-selector") {
|
||||||
|
content = append(content[:i], content[i+1:]...)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if strings.Contains(line, "- name: '{{ .Values.pullSecrets ") {
|
||||||
|
content = append(content[:i], content[i+1:]...)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -57,6 +57,8 @@ type Value struct {
|
|||||||
Environment map[string]any `yaml:"environment,omitempty"`
|
Environment map[string]any `yaml:"environment,omitempty"`
|
||||||
Replicas *uint32 `yaml:"replicas,omitempty"`
|
Replicas *uint32 `yaml:"replicas,omitempty"`
|
||||||
CronJob *CronJobValue `yaml:"cronjob,omitempty"`
|
CronJob *CronJobValue `yaml:"cronjob,omitempty"`
|
||||||
|
NodeSelector map[string]string `yaml:"nodeSelector"`
|
||||||
|
ServiceAccount string `yaml:"serviceAccount"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// CronJobValue is a cronjob configuration that will be saved in values.yaml.
|
// CronJobValue is a cronjob configuration that will be saved in values.yaml.
|
||||||
|
Reference in New Issue
Block a user