Skip to content

Commit 3e732b0

Browse files
ag2308arttor
authored andcommitted
added deployment strategy attribute support
1 parent 98a79ff commit 3e732b0

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

pkg/processor/deployment/deployment.go

+68
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1818
"k8s.io/apimachinery/pkg/runtime"
1919
"k8s.io/apimachinery/pkg/runtime/schema"
20+
"k8s.io/apimachinery/pkg/util/intstr"
2021
)
2122

2223
var deploymentGVC = schema.GroupVersionKind{
@@ -33,6 +34,9 @@ spec:
3334
{{- end }}
3435
{{- if .RevisionHistoryLimit }}
3536
{{ .RevisionHistoryLimit }}
37+
{{- end }}
38+
{{- if .Strategy }}
39+
{{ .Strategy }}
3640
{{- end }}
3741
selector:
3842
{{ .Selector }}
@@ -84,6 +88,11 @@ func (d deployment) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstr
8488
return true, nil, err
8589
}
8690

91+
strategy, err := processStrategy(name, &depl, &values)
92+
if err != nil {
93+
return true, nil, err
94+
}
95+
8796
matchLabels, err := yamlformat.Marshal(map[string]interface{}{"matchLabels": depl.Spec.Selector.MatchLabels}, 0)
8897
if err != nil {
8998
return true, nil, err
@@ -141,6 +150,7 @@ func (d deployment) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstr
141150
Meta string
142151
Replicas string
143152
RevisionHistoryLimit string
153+
Strategy string
144154
Selector string
145155
PodLabels string
146156
PodAnnotations string
@@ -149,6 +159,7 @@ func (d deployment) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstr
149159
Meta: meta,
150160
Replicas: replicas,
151161
RevisionHistoryLimit: revisionHistoryLimit,
162+
Strategy: strategy,
152163
Selector: selector,
153164
PodLabels: podLabels,
154165
PodAnnotations: podAnnotations,
@@ -218,11 +229,68 @@ func processRevisionHistoryLimit(name string, deployment *appsv1.Deployment, val
218229
return revisionHistoryLimit, nil
219230
}
220231

232+
func processStrategy(name string, deployment *appsv1.Deployment, values *helmify.Values) (string, error) {
233+
if deployment.Spec.Strategy.Type == "" {
234+
return "", nil
235+
}
236+
allowedStrategyTypes := map[appsv1.DeploymentStrategyType]bool{
237+
appsv1.RecreateDeploymentStrategyType: true,
238+
appsv1.RollingUpdateDeploymentStrategyType: true,
239+
}
240+
if !allowedStrategyTypes[deployment.Spec.Strategy.Type] {
241+
return "", fmt.Errorf("invalid deployment strategy type: %s", deployment.Spec.Strategy.Type)
242+
}
243+
strategyTypeTpl, err := values.Add(string(deployment.Spec.Strategy.Type), name, "strategy", "type")
244+
if err != nil {
245+
return "", err
246+
}
247+
strategyMap := map[string]interface{}{
248+
"type": strategyTypeTpl,
249+
}
250+
if deployment.Spec.Strategy.Type == appsv1.RollingUpdateDeploymentStrategyType {
251+
if rollingUpdate := deployment.Spec.Strategy.RollingUpdate; rollingUpdate != nil {
252+
rollingUpdateMap := map[string]interface{}{}
253+
setRollingUpdateField := func(value *intstr.IntOrString, fieldName string) error {
254+
var tpl string
255+
var err error
256+
if value.Type == intstr.Int {
257+
tpl, err = values.Add(value.IntValue(), name, "strategy", "rollingUpdate", fieldName)
258+
} else {
259+
tpl, err = values.Add(value.String(), name, "strategy", "rollingUpdate", fieldName)
260+
}
261+
if err != nil {
262+
return err
263+
}
264+
rollingUpdateMap[fieldName] = tpl
265+
return nil
266+
}
267+
if rollingUpdate.MaxSurge != nil {
268+
if err := setRollingUpdateField(rollingUpdate.MaxSurge, "maxSurge"); err != nil {
269+
return "", err
270+
}
271+
}
272+
if rollingUpdate.MaxUnavailable != nil {
273+
if err := setRollingUpdateField(rollingUpdate.MaxUnavailable, "maxUnavailable"); err != nil {
274+
return "", err
275+
}
276+
}
277+
strategyMap["rollingUpdate"] = rollingUpdateMap
278+
}
279+
}
280+
strategy, err := yamlformat.Marshal(map[string]interface{}{"strategy": strategyMap}, 2)
281+
if err != nil {
282+
return "", err
283+
}
284+
strategy = strings.ReplaceAll(strategy, "'", "")
285+
return strategy, nil
286+
}
287+
221288
type result struct {
222289
data struct {
223290
Meta string
224291
Replicas string
225292
RevisionHistoryLimit string
293+
Strategy string
226294
Selector string
227295
PodLabels string
228296
PodAnnotations string

pkg/processor/deployment/deployment_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ metadata:
2020
spec:
2121
revisionHistoryLimit: 5
2222
replicas: 1
23+
strategy:
24+
type: Recreate
2325
selector:
2426
matchLabels:
2527
control-plane: controller-manager

test_data/k8s-operator-kustomize.output

+5
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,11 @@ metadata:
580580
namespace: my-operator-system
581581
spec:
582582
replicas: 1
583+
strategy:
584+
type: RollingUpdate
585+
rollingUpdate:
586+
maxSurge: 25%
587+
maxUnavailable: 25%
583588
selector:
584589
matchLabels:
585590
control-plane: controller-manager

0 commit comments

Comments
 (0)