From 6329cacb000c57ad795b9f8ff36f5792c1029250 Mon Sep 17 00:00:00 2001 From: ag Date: Thu, 12 Dec 2024 18:41:00 +0100 Subject: [PATCH 1/2] added deployment strategy attribute support --- pkg/processor/deployment/deployment.go | 68 +++++++++++++++++++++ pkg/processor/deployment/deployment_test.go | 2 + test_data/k8s-operator-kustomize.output | 5 ++ 3 files changed, 75 insertions(+) diff --git a/pkg/processor/deployment/deployment.go b/pkg/processor/deployment/deployment.go index ecbe0e2..b41cc23 100644 --- a/pkg/processor/deployment/deployment.go +++ b/pkg/processor/deployment/deployment.go @@ -17,6 +17,7 @@ import ( "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" + "k8s.io/apimachinery/pkg/util/intstr" ) var deploymentGVC = schema.GroupVersionKind{ @@ -33,6 +34,9 @@ spec: {{- end }} {{- if .RevisionHistoryLimit }} {{ .RevisionHistoryLimit }} +{{- end }} +{{- if .Strategy }} +{{ .Strategy }} {{- end }} selector: {{ .Selector }} @@ -84,6 +88,11 @@ func (d deployment) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstr return true, nil, err } + strategy, err := processStrategy(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 @@ -141,6 +150,7 @@ func (d deployment) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstr Meta string Replicas string RevisionHistoryLimit string + Strategy string Selector string PodLabels string PodAnnotations string @@ -149,6 +159,7 @@ func (d deployment) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstr Meta: meta, Replicas: replicas, RevisionHistoryLimit: revisionHistoryLimit, + Strategy: strategy, Selector: selector, PodLabels: podLabels, PodAnnotations: podAnnotations, @@ -218,11 +229,68 @@ func processRevisionHistoryLimit(name string, deployment *appsv1.Deployment, val return revisionHistoryLimit, nil } +func processStrategy(name string, deployment *appsv1.Deployment, values *helmify.Values) (string, error) { + if deployment.Spec.Strategy.Type == "" { + return "", nil + } + allowedStrategyTypes := map[appsv1.DeploymentStrategyType]bool{ + appsv1.RecreateDeploymentStrategyType: true, + appsv1.RollingUpdateDeploymentStrategyType: true, + } + if !allowedStrategyTypes[deployment.Spec.Strategy.Type] { + return "", fmt.Errorf("invalid deployment strategy type: %s", deployment.Spec.Strategy.Type) + } + strategyTypeTpl, err := values.Add(string(deployment.Spec.Strategy.Type), name, "strategy", "type") + if err != nil { + return "", err + } + strategyMap := map[string]interface{}{ + "type": strategyTypeTpl, + } + if deployment.Spec.Strategy.Type == appsv1.RollingUpdateDeploymentStrategyType { + if rollingUpdate := deployment.Spec.Strategy.RollingUpdate; rollingUpdate != nil { + rollingUpdateMap := map[string]interface{}{} + setRollingUpdateField := func(value *intstr.IntOrString, fieldName string) error { + var tpl string + var err error + if value.Type == intstr.Int { + tpl, err = values.Add(value.IntValue(), name, "strategy", "rollingUpdate", fieldName) + } else { + tpl, err = values.Add(value.String(), name, "strategy", "rollingUpdate", fieldName) + } + if err != nil { + return err + } + rollingUpdateMap[fieldName] = tpl + return nil + } + if rollingUpdate.MaxSurge != nil { + if err := setRollingUpdateField(rollingUpdate.MaxSurge, "maxSurge"); err != nil { + return "", err + } + } + if rollingUpdate.MaxUnavailable != nil { + if err := setRollingUpdateField(rollingUpdate.MaxUnavailable, "maxUnavailable"); err != nil { + return "", err + } + } + strategyMap["rollingUpdate"] = rollingUpdateMap + } + } + strategy, err := yamlformat.Marshal(map[string]interface{}{"strategy": strategyMap}, 2) + if err != nil { + return "", err + } + strategy = strings.ReplaceAll(strategy, "'", "") + return strategy, nil +} + type result struct { data struct { Meta string Replicas string RevisionHistoryLimit string + Strategy string Selector string PodLabels string PodAnnotations string diff --git a/pkg/processor/deployment/deployment_test.go b/pkg/processor/deployment/deployment_test.go index 10e3eb0..07a2046 100644 --- a/pkg/processor/deployment/deployment_test.go +++ b/pkg/processor/deployment/deployment_test.go @@ -20,6 +20,8 @@ metadata: spec: revisionHistoryLimit: 5 replicas: 1 + strategy: + type: Recreate selector: matchLabels: control-plane: controller-manager diff --git a/test_data/k8s-operator-kustomize.output b/test_data/k8s-operator-kustomize.output index 478c2e9..11e0d3c 100644 --- a/test_data/k8s-operator-kustomize.output +++ b/test_data/k8s-operator-kustomize.output @@ -580,6 +580,11 @@ metadata: namespace: my-operator-system spec: replicas: 1 + strategy: + type: RollingUpdate + rollingUpdate: + maxSurge: 25% + maxUnavailable: 25% selector: matchLabels: control-plane: controller-manager From ca73b65f909a297bef6f346384ccdd5c69d471ff Mon Sep 17 00:00:00 2001 From: ag Date: Tue, 17 Dec 2024 16:49:09 +0100 Subject: [PATCH 2/2] Update examples/operator templates and values --- examples/operator/templates/deployment.yaml | 7 +++++++ examples/operator/values.yaml | 5 +++++ 2 files changed, 12 insertions(+) diff --git a/examples/operator/templates/deployment.yaml b/examples/operator/templates/deployment.yaml index a32a57b..e74a7dc 100644 --- a/examples/operator/templates/deployment.yaml +++ b/examples/operator/templates/deployment.yaml @@ -7,6 +7,13 @@ metadata: {{- include "operator.labels" . | nindent 4 }} spec: replicas: {{ .Values.controllerManager.replicas }} + strategy: + rollingUpdate: + maxSurge: {{ .Values.controllerManager.strategy.rollingUpdate.maxSurge | quote + }} + maxUnavailable: {{ .Values.controllerManager.strategy.rollingUpdate.maxUnavailable + | quote }} + type: {{ .Values.controllerManager.strategy.type | quote }} selector: matchLabels: control-plane: controller-manager diff --git a/examples/operator/values.yaml b/examples/operator/values.yaml index 399c6e5..a0c2c88 100644 --- a/examples/operator/values.yaml +++ b/examples/operator/values.yaml @@ -49,6 +49,11 @@ controllerManager: serviceAccount: annotations: k8s.acme.org/some-meta-data: ACME Inc. + strategy: + rollingUpdate: + maxSurge: 25% + maxUnavailable: 25% + type: RollingUpdate kubernetesClusterDomain: cluster.local managerConfig: controllerManagerConfigYaml: |-