Skip to content

Commit

Permalink
feat: add deployment revisionHistoryLimit
Browse files Browse the repository at this point in the history
  • Loading branch information
PG2000 authored and arttor committed Oct 30, 2023
1 parent 8a688a5 commit 0b64abb
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 19 deletions.
1 change: 1 addition & 0 deletions examples/app/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ metadata:
{{- include "app.labels" . | nindent 4 }}
spec:
replicas: {{ .Values.myapp.replicas }}
revisionHistoryLimit: {{ .Values.myapp.revisionHistoryLimit }}
selector:
matchLabels:
app: myapp
Expand Down
1 change: 1 addition & 0 deletions examples/app/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ myapp:
repository: gcr.io/kubebuilder/kube-rbac-proxy
tag: v0.8.0
replicas: 3
revisionHistoryLimit: 5
myappPdb:
minAvailable: 2
myappService:
Expand Down
64 changes: 46 additions & 18 deletions pkg/processor/deployment/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ var deploymentTempl, _ = template.New("deployment").Parse(
spec:
{{- if .Replicas }}
{{ .Replicas }}
{{- end }}
{{- if .RevisionHistoryLimit }}
{{ .RevisionHistoryLimit }}
{{- end }}
selector:
{{ .Selector }}
Expand Down Expand Up @@ -57,6 +60,7 @@ func (d deployment) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstr
return false, nil, nil
}
depl := appsv1.Deployment{}

err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &depl)
if err != nil {
return true, nil, fmt.Errorf("%w: unable to cast to deployment", err)
Expand All @@ -74,6 +78,11 @@ func (d deployment) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstr
return true, nil, err
}

revisionHistoryLimit, err := processRevisionHistoryLimit(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 @@ -125,19 +134,21 @@ func (d deployment) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstr
return true, &result{
values: values,
data: struct {
Meta string
Replicas string
Selector string
PodLabels string
PodAnnotations string
Spec string
Meta string
Replicas string
RevisionHistoryLimit string
Selector string
PodLabels string
PodAnnotations string
Spec string
}{
Meta: meta,
Replicas: replicas,
Selector: selector,
PodLabels: podLabels,
PodAnnotations: podAnnotations,
Spec: spec,
Meta: meta,
Replicas: replicas,
RevisionHistoryLimit: revisionHistoryLimit,
Selector: selector,
PodLabels: podLabels,
PodAnnotations: podAnnotations,
Spec: spec,
},
}, nil
}
Expand All @@ -158,14 +169,31 @@ func processReplicas(name string, deployment *appsv1.Deployment, values *helmify
return replicas, nil
}

func processRevisionHistoryLimit(name string, deployment *appsv1.Deployment, values *helmify.Values) (string, error) {
if deployment.Spec.RevisionHistoryLimit == nil {
return "", nil
}
revisionHistoryLimitTpl, err := values.Add(int64(*deployment.Spec.RevisionHistoryLimit), name, "revisionHistoryLimit")
if err != nil {
return "", err
}
revisionHistoryLimit, err := yamlformat.Marshal(map[string]interface{}{"revisionHistoryLimit": revisionHistoryLimitTpl}, 2)
if err != nil {
return "", err
}
revisionHistoryLimit = strings.ReplaceAll(revisionHistoryLimit, "'", "")
return revisionHistoryLimit, nil
}

type result struct {
data struct {
Meta string
Replicas string
Selector string
PodLabels string
PodAnnotations string
Spec string
Meta string
Replicas string
RevisionHistoryLimit string
Selector string
PodLabels string
PodAnnotations string
Spec string
}
values helmify.Values
}
Expand Down
1 change: 1 addition & 0 deletions pkg/processor/deployment/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ metadata:
name: my-operator-controller-manager
namespace: my-operator-system
spec:
revisionHistoryLimit: 5
replicas: 1
selector:
matchLabels:
Expand Down
3 changes: 2 additions & 1 deletion test_data/k8s-operator-ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ metadata:
creationTimestamp: null
name: cephvolumes.test.example.com
labels:
example-label: my-app
example-label: my-app
spec:
group: test.example.com
names:
Expand Down Expand Up @@ -570,6 +570,7 @@ metadata:
namespace: my-operator-system
spec:
replicas: 1
revisionHistoryLimit: 5
selector:
matchLabels:
control-plane: controller-manager
Expand Down
1 change: 1 addition & 0 deletions test_data/sample-app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ metadata:
namespace: my-ns
spec:
replicas: 3
revisionHistoryLimit: 5
selector:
matchLabels:
app: myapp
Expand Down

0 comments on commit 0b64abb

Please sign in to comment.