Skip to content

Commit 0b64abb

Browse files
PG2000arttor
authored andcommitted
feat: add deployment revisionHistoryLimit
1 parent 8a688a5 commit 0b64abb

File tree

6 files changed

+52
-19
lines changed

6 files changed

+52
-19
lines changed

examples/app/templates/deployment.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ metadata:
77
{{- include "app.labels" . | nindent 4 }}
88
spec:
99
replicas: {{ .Values.myapp.replicas }}
10+
revisionHistoryLimit: {{ .Values.myapp.revisionHistoryLimit }}
1011
selector:
1112
matchLabels:
1213
app: myapp

examples/app/values.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ myapp:
8484
repository: gcr.io/kubebuilder/kube-rbac-proxy
8585
tag: v0.8.0
8686
replicas: 3
87+
revisionHistoryLimit: 5
8788
myappPdb:
8889
minAvailable: 2
8990
myappService:

pkg/processor/deployment/deployment.go

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ var deploymentTempl, _ = template.New("deployment").Parse(
2929
spec:
3030
{{- if .Replicas }}
3131
{{ .Replicas }}
32+
{{- end }}
33+
{{- if .RevisionHistoryLimit }}
34+
{{ .RevisionHistoryLimit }}
3235
{{- end }}
3336
selector:
3437
{{ .Selector }}
@@ -57,6 +60,7 @@ func (d deployment) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstr
5760
return false, nil, nil
5861
}
5962
depl := appsv1.Deployment{}
63+
6064
err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &depl)
6165
if err != nil {
6266
return true, nil, fmt.Errorf("%w: unable to cast to deployment", err)
@@ -74,6 +78,11 @@ func (d deployment) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstr
7478
return true, nil, err
7579
}
7680

81+
revisionHistoryLimit, err := processRevisionHistoryLimit(name, &depl, &values)
82+
if err != nil {
83+
return true, nil, err
84+
}
85+
7786
matchLabels, err := yamlformat.Marshal(map[string]interface{}{"matchLabels": depl.Spec.Selector.MatchLabels}, 0)
7887
if err != nil {
7988
return true, nil, err
@@ -125,19 +134,21 @@ func (d deployment) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstr
125134
return true, &result{
126135
values: values,
127136
data: struct {
128-
Meta string
129-
Replicas string
130-
Selector string
131-
PodLabels string
132-
PodAnnotations string
133-
Spec string
137+
Meta string
138+
Replicas string
139+
RevisionHistoryLimit string
140+
Selector string
141+
PodLabels string
142+
PodAnnotations string
143+
Spec string
134144
}{
135-
Meta: meta,
136-
Replicas: replicas,
137-
Selector: selector,
138-
PodLabels: podLabels,
139-
PodAnnotations: podAnnotations,
140-
Spec: spec,
145+
Meta: meta,
146+
Replicas: replicas,
147+
RevisionHistoryLimit: revisionHistoryLimit,
148+
Selector: selector,
149+
PodLabels: podLabels,
150+
PodAnnotations: podAnnotations,
151+
Spec: spec,
141152
},
142153
}, nil
143154
}
@@ -158,14 +169,31 @@ func processReplicas(name string, deployment *appsv1.Deployment, values *helmify
158169
return replicas, nil
159170
}
160171

172+
func processRevisionHistoryLimit(name string, deployment *appsv1.Deployment, values *helmify.Values) (string, error) {
173+
if deployment.Spec.RevisionHistoryLimit == nil {
174+
return "", nil
175+
}
176+
revisionHistoryLimitTpl, err := values.Add(int64(*deployment.Spec.RevisionHistoryLimit), name, "revisionHistoryLimit")
177+
if err != nil {
178+
return "", err
179+
}
180+
revisionHistoryLimit, err := yamlformat.Marshal(map[string]interface{}{"revisionHistoryLimit": revisionHistoryLimitTpl}, 2)
181+
if err != nil {
182+
return "", err
183+
}
184+
revisionHistoryLimit = strings.ReplaceAll(revisionHistoryLimit, "'", "")
185+
return revisionHistoryLimit, nil
186+
}
187+
161188
type result struct {
162189
data struct {
163-
Meta string
164-
Replicas string
165-
Selector string
166-
PodLabels string
167-
PodAnnotations string
168-
Spec string
190+
Meta string
191+
Replicas string
192+
RevisionHistoryLimit string
193+
Selector string
194+
PodLabels string
195+
PodAnnotations string
196+
Spec string
169197
}
170198
values helmify.Values
171199
}

pkg/processor/deployment/deployment_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ metadata:
1818
name: my-operator-controller-manager
1919
namespace: my-operator-system
2020
spec:
21+
revisionHistoryLimit: 5
2122
replicas: 1
2223
selector:
2324
matchLabels:

test_data/k8s-operator-ci.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ metadata:
1414
creationTimestamp: null
1515
name: cephvolumes.test.example.com
1616
labels:
17-
example-label: my-app
17+
example-label: my-app
1818
spec:
1919
group: test.example.com
2020
names:
@@ -570,6 +570,7 @@ metadata:
570570
namespace: my-operator-system
571571
spec:
572572
replicas: 1
573+
revisionHistoryLimit: 5
573574
selector:
574575
matchLabels:
575576
control-plane: controller-manager

test_data/sample-app.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ metadata:
77
namespace: my-ns
88
spec:
99
replicas: 3
10+
revisionHistoryLimit: 5
1011
selector:
1112
matchLabels:
1213
app: myapp

0 commit comments

Comments
 (0)