diff --git a/examples/app/templates/deployment.yaml b/examples/app/templates/deployment.yaml index 4cd0d17..67d6f29 100644 --- a/examples/app/templates/deployment.yaml +++ b/examples/app/templates/deployment.yaml @@ -7,6 +7,7 @@ metadata: {{- include "app.labels" . | nindent 4 }} spec: replicas: {{ .Values.myapp.replicas }} + revisionHistoryLimit: {{ .Values.myapp.revisionHistoryLimit }} selector: matchLabels: app: myapp diff --git a/examples/app/values.yaml b/examples/app/values.yaml index 39d362a..6f23e91 100644 --- a/examples/app/values.yaml +++ b/examples/app/values.yaml @@ -84,6 +84,7 @@ myapp: repository: gcr.io/kubebuilder/kube-rbac-proxy tag: v0.8.0 replicas: 3 + revisionHistoryLimit: 5 myappPdb: minAvailable: 2 myappService: diff --git a/pkg/processor/deployment/deployment.go b/pkg/processor/deployment/deployment.go index 6fdb59e..b96f7b0 100644 --- a/pkg/processor/deployment/deployment.go +++ b/pkg/processor/deployment/deployment.go @@ -29,6 +29,9 @@ var deploymentTempl, _ = template.New("deployment").Parse( spec: {{- if .Replicas }} {{ .Replicas }} +{{- end }} +{{- if .RevisionHistoryLimit }} +{{ .RevisionHistoryLimit }} {{- end }} selector: {{ .Selector }} @@ -57,6 +60,7 @@ func (d deployment) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstr return false, nil, nil } depl := appsv1.Deployment{} + err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &depl) if err != nil { return true, nil, fmt.Errorf("%w: unable to cast to deployment", err) @@ -74,6 +78,11 @@ func (d deployment) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstr return true, nil, err } + revisionHistoryLimit, err := processRevisionHistoryLimit(name, &depl, &values) + if err != nil { + return true, nil, err + } + matchLabels, err := yamlformat.Marshal(map[string]interface{}{"matchLabels": depl.Spec.Selector.MatchLabels}, 0) if err != nil { return true, nil, err @@ -125,19 +134,21 @@ func (d deployment) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstr return true, &result{ values: values, data: struct { - Meta string - Replicas string - Selector string - PodLabels string - PodAnnotations string - Spec string + Meta string + Replicas string + RevisionHistoryLimit string + Selector string + PodLabels string + PodAnnotations string + Spec string }{ - Meta: meta, - Replicas: replicas, - Selector: selector, - PodLabels: podLabels, - PodAnnotations: podAnnotations, - Spec: spec, + Meta: meta, + Replicas: replicas, + RevisionHistoryLimit: revisionHistoryLimit, + Selector: selector, + PodLabels: podLabels, + PodAnnotations: podAnnotations, + Spec: spec, }, }, nil } @@ -158,14 +169,31 @@ func processReplicas(name string, deployment *appsv1.Deployment, values *helmify return replicas, nil } +func processRevisionHistoryLimit(name string, deployment *appsv1.Deployment, values *helmify.Values) (string, error) { + if deployment.Spec.RevisionHistoryLimit == nil { + return "", nil + } + revisionHistoryLimitTpl, err := values.Add(int64(*deployment.Spec.RevisionHistoryLimit), name, "revisionHistoryLimit") + if err != nil { + return "", err + } + revisionHistoryLimit, err := yamlformat.Marshal(map[string]interface{}{"revisionHistoryLimit": revisionHistoryLimitTpl}, 2) + if err != nil { + return "", err + } + revisionHistoryLimit = strings.ReplaceAll(revisionHistoryLimit, "'", "") + return revisionHistoryLimit, nil +} + type result struct { data struct { - Meta string - Replicas string - Selector string - PodLabels string - PodAnnotations string - Spec string + Meta string + Replicas string + RevisionHistoryLimit string + Selector string + PodLabels string + PodAnnotations string + Spec string } values helmify.Values } diff --git a/pkg/processor/deployment/deployment_test.go b/pkg/processor/deployment/deployment_test.go index e56eb4b..d0159e2 100644 --- a/pkg/processor/deployment/deployment_test.go +++ b/pkg/processor/deployment/deployment_test.go @@ -18,6 +18,7 @@ metadata: name: my-operator-controller-manager namespace: my-operator-system spec: + revisionHistoryLimit: 5 replicas: 1 selector: matchLabels: diff --git a/test_data/k8s-operator-ci.yaml b/test_data/k8s-operator-ci.yaml index b9bc990..c488a5b 100644 --- a/test_data/k8s-operator-ci.yaml +++ b/test_data/k8s-operator-ci.yaml @@ -14,7 +14,7 @@ metadata: creationTimestamp: null name: cephvolumes.test.example.com labels: - example-label: my-app + example-label: my-app spec: group: test.example.com names: @@ -570,6 +570,7 @@ metadata: namespace: my-operator-system spec: replicas: 1 + revisionHistoryLimit: 5 selector: matchLabels: control-plane: controller-manager diff --git a/test_data/sample-app.yaml b/test_data/sample-app.yaml index 42264fc..a80e04e 100644 --- a/test_data/sample-app.yaml +++ b/test_data/sample-app.yaml @@ -7,6 +7,7 @@ metadata: namespace: my-ns spec: replicas: 3 + revisionHistoryLimit: 5 selector: matchLabels: app: myapp