Skip to content

Commit

Permalink
added deployment strategy attribute support
Browse files Browse the repository at this point in the history
ag2308 authored and arttor committed Dec 18, 2024
1 parent 98a79ff commit 3e732b0
Showing 3 changed files with 75 additions and 0 deletions.
68 changes: 68 additions & 0 deletions pkg/processor/deployment/deployment.go
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions pkg/processor/deployment/deployment_test.go
Original file line number Diff line number Diff line change
@@ -20,6 +20,8 @@ metadata:
spec:
revisionHistoryLimit: 5
replicas: 1
strategy:
type: Recreate
selector:
matchLabels:
control-plane: controller-manager
5 changes: 5 additions & 0 deletions test_data/k8s-operator-kustomize.output
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 3e732b0

Please sign in to comment.