Skip to content

Commit

Permalink
Move existing annotations to values.yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
Starefossen authored and arttor committed Jun 24, 2023
1 parent d788666 commit de2e663
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 29 deletions.
3 changes: 1 addition & 2 deletions examples/operator/templates/serviceaccount.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ metadata:
labels:
{{- include "operator.labels" . | nindent 4 }}
annotations:
k8s.acme.org/some-meta-data: ACME Inc.
{{- toYaml .Values.controllerManager.serviceaccount.annotations | nindent 4 }}
{{- toYaml .Values.controllerManager.serviceAccount.annotations | nindent 4 }}
5 changes: 3 additions & 2 deletions examples/operator/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ controllerManager:
region: east
type: user-node
replicas: 1
serviceaccount:
annotations: {}
serviceAccount:
annotations:
k8s.acme.org/some-meta-data: ACME Inc.
kubernetesClusterDomain: cluster.local
managerConfig:
controllerManagerConfigYaml: |-
Expand Down
43 changes: 18 additions & 25 deletions pkg/processor/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
yamlformat "github.com/arttor/helmify/pkg/yaml"
)

const defaultMetaTemplate = `apiVersion: %[1]s
const metaTemplate = `apiVersion: %[1]s
kind: %[2]s
metadata:
name: %[3]s
Expand All @@ -21,16 +21,8 @@ metadata:
{{- include "%[4]s.labels" . | nindent 4 }}
%[6]s`

const annotationsMetaTemplate = `apiVersion: %[1]s
kind: %[2]s
metadata:
name: %[3]s
labels:
%[5]s
{{- include "%[4]s.labels" . | nindent 4 }}
annotations:
%[6]s
{{- toYaml .Values.%[7]s.%[8]s.annotations | nindent 4 }}`
const annotationsTemplate = ` annotations:
{{- toYaml .Values.%[1]s.%[2]s.annotations | nindent 4 }}`

type MetaOpt interface {
apply(*options)
Expand Down Expand Up @@ -58,10 +50,7 @@ func WithAnnotations(values helmify.Values) MetaOpt {

// ProcessObjMeta - returns object apiVersion, kind and metadata as helm template.
func ProcessObjMeta(appMeta helmify.AppMetadata, obj *unstructured.Unstructured, opts ...MetaOpt) (string, error) {
options := &options{
values: nil,
annotations: false,
}
options := &options{}
for _, opt := range opts {
opt.apply(options)
}
Expand Down Expand Up @@ -91,23 +80,27 @@ func ProcessObjMeta(appMeta helmify.AppMetadata, obj *unstructured.Unstructured,
return "", err
}
}

templatedName := appMeta.TemplatedName(obj.GetName())
apiVersion, kind := obj.GetObjectKind().GroupVersionKind().ToAPIVersionAndKind()

var metaStr string
if options.values != nil && options.annotations {
if len(obj.GetAnnotations()) != 0 {
annotations, err = yamlformat.Marshal(obj.GetAnnotations(), 4)
if err != nil {
return "", err
}
}
name := strcase.ToLowerCamel(appMeta.TrimName(obj.GetName()))
err = unstructured.SetNestedField(options.values, map[string]interface{}{}, name, strings.ToLower(kind), "annotations")
metaStr = fmt.Sprintf(annotationsMetaTemplate, apiVersion, kind, templatedName, appMeta.ChartName(), labels, annotations, name, strings.ToLower(kind))
} else {
metaStr = fmt.Sprintf(defaultMetaTemplate, apiVersion, kind, templatedName, appMeta.ChartName(), labels, annotations)
kind := strcase.ToLowerCamel(kind)
valuesAnnotations := make(map[string]interface{})
for k, v := range obj.GetAnnotations() {
valuesAnnotations[k] = v
}
err = unstructured.SetNestedField(options.values, valuesAnnotations, name, kind, "annotations")
if err != nil {
return "", err
}

annotations = fmt.Sprintf(annotationsTemplate, name, kind)
}

metaStr = fmt.Sprintf(metaTemplate, apiVersion, kind, templatedName, appMeta.ChartName(), labels, annotations)
metaStr = strings.Trim(metaStr, " \n")
metaStr = strings.ReplaceAll(metaStr, "\n\n", "\n")
return metaStr, nil
Expand Down

0 comments on commit de2e663

Please sign in to comment.