Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for deployment strategy attribute #162

Merged
merged 2 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions examples/operator/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions examples/operator/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |-
Expand Down
68 changes: 68 additions & 0 deletions pkg/processor/deployment/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -33,6 +34,9 @@ spec:
{{- end }}
{{- if .RevisionHistoryLimit }}
{{ .RevisionHistoryLimit }}
{{- end }}
{{- if .Strategy }}
{{ .Strategy }}
{{- end }}
selector:
{{ .Selector }}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions pkg/processor/deployment/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ metadata:
spec:
revisionHistoryLimit: 5
replicas: 1
strategy:
type: Recreate
selector:
matchLabels:
control-plane: controller-manager
Expand Down
5 changes: 5 additions & 0 deletions test_data/k8s-operator-kustomize.output
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading