Skip to content

Commit 5672c82

Browse files
committed
added deployment strategy attribute support
1 parent 98a79ff commit 5672c82

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

pkg/processor/deployment/deployment.go

Lines changed: 79 additions & 0 deletions
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,79 @@ 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+
237+
allowedStrategyTypes := map[appsv1.DeploymentStrategyType]bool{
238+
appsv1.RecreateDeploymentStrategyType: true,
239+
appsv1.RollingUpdateDeploymentStrategyType: true,
240+
}
241+
242+
if !allowedStrategyTypes[deployment.Spec.Strategy.Type] {
243+
return "", fmt.Errorf("invalid deployment strategy type: %s", deployment.Spec.Strategy.Type)
244+
}
245+
246+
strategyTypeTpl, err := values.Add(string(deployment.Spec.Strategy.Type), name, "strategy", "type")
247+
if err != nil {
248+
return "", err
249+
}
250+
251+
strategyMap := map[string]interface{}{
252+
"type": strategyTypeTpl,
253+
}
254+
255+
if deployment.Spec.Strategy.Type == appsv1.RollingUpdateDeploymentStrategyType {
256+
if rollingUpdate := deployment.Spec.Strategy.RollingUpdate; rollingUpdate != nil {
257+
rollingUpdateMap := map[string]interface{}{}
258+
259+
addIntOrStringToRollingUpdate := func(value *intstr.IntOrString, fieldName string) error {
260+
var tpl string
261+
var err error
262+
if value.Type == intstr.Int {
263+
tpl, err = values.Add(value.IntValue(), name, "strategy", "rollingUpdate", fieldName)
264+
} else {
265+
tpl, err = values.Add(value.String(), name, "strategy", "rollingUpdate", fieldName)
266+
}
267+
if err != nil {
268+
return err
269+
}
270+
rollingUpdateMap[fieldName] = tpl
271+
return nil
272+
}
273+
274+
if rollingUpdate.MaxSurge != nil {
275+
if err := addIntOrStringToRollingUpdate(rollingUpdate.MaxSurge, "maxSurge"); err != nil {
276+
return "", err
277+
}
278+
}
279+
280+
if rollingUpdate.MaxUnavailable != nil {
281+
if err := addIntOrStringToRollingUpdate(rollingUpdate.MaxUnavailable, "maxUnavailable"); err != nil {
282+
return "", err
283+
}
284+
}
285+
286+
strategyMap["rollingUpdate"] = rollingUpdateMap
287+
}
288+
}
289+
290+
strategy, err := yamlformat.Marshal(map[string]interface{}{"strategy": strategyMap}, 2)
291+
if err != nil {
292+
return "", err
293+
}
294+
295+
strategy = strings.ReplaceAll(strategy, "'", "")
296+
return strategy, nil
297+
}
298+
221299
type result struct {
222300
data struct {
223301
Meta string
224302
Replicas string
225303
RevisionHistoryLimit string
304+
Strategy string
226305
Selector string
227306
PodLabels string
228307
PodAnnotations string

pkg/processor/deployment/deployment_test.go

Lines changed: 2 additions & 0 deletions
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

Lines changed: 5 additions & 0 deletions
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)