Skip to content

Commit 3e87d86

Browse files
bigkevmcdarttor
authored andcommitted
Add support for extracting args from containers.
This updates the PodSpec processor to extract args from containers as options. If a Container doesn't have an args field it will be passed through untouched.
1 parent 9e709ee commit 3e87d86

File tree

7 files changed

+193
-20
lines changed

7 files changed

+193
-20
lines changed

examples/app/templates/deployment.yaml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ spec:
1818
{{- include "app.selectorLabels" . | nindent 8 }}
1919
spec:
2020
containers:
21-
- args:
22-
- --health-probe-bind-address=:8081
23-
- --metrics-bind-address=127.0.0.1:8080
24-
- --leader-elect
21+
- args: {{- toYaml .Values.myapp.app.args | nindent 8 }}
2522
command:
2623
- /manager
2724
env:
@@ -65,9 +62,7 @@ spec:
6562
name: props
6663
- mountPath: /usr/share/nginx/html
6764
name: sample-pv-storage
68-
- args:
69-
- --secure-listen-address=0.0.0.0:8443
70-
- --v=10
65+
- args: {{- toYaml .Values.myapp.proxySidecar.args | nindent 8 }}
7166
env:
7267
- name: KUBERNETES_CLUSTER_DOMAIN
7368
value: {{ quote .Values.kubernetesClusterDomain }}

examples/app/values.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ mySecretVars:
5353
var2: ""
5454
myapp:
5555
app:
56+
args:
57+
- --health-probe-bind-address=:8081
58+
- --metrics-bind-address=127.0.0.1:8080
59+
- --leader-elect
5660
containerSecurityContext:
5761
allowPrivilegeEscalation: false
5862
image:
@@ -69,6 +73,9 @@ myapp:
6973
region: east
7074
type: user-node
7175
proxySidecar:
76+
args:
77+
- --secure-listen-address=0.0.0.0:8443
78+
- --v=10
7279
image:
7380
repository: gcr.io/kubebuilder/kube-rbac-proxy
7481
tag: v0.8.0

examples/operator/templates/deployment.yaml

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,7 @@ spec:
2525
{{- include "operator.selectorLabels" . | nindent 8 }}
2626
spec:
2727
containers:
28-
- args:
29-
- --secure-listen-address=0.0.0.0:8443
30-
- --upstream=http://127.0.0.1:8080/
31-
- --logtostderr=true
32-
- --v=10
28+
- args: {{- toYaml .Values.controllerManager.kubeRbacProxy.args | nindent 8 }}
3329
env:
3430
- name: KUBERNETES_CLUSTER_DOMAIN
3531
value: {{ quote .Values.kubernetesClusterDomain }}
@@ -40,10 +36,7 @@ spec:
4036
- containerPort: 8443
4137
name: https
4238
resources: {}
43-
- args:
44-
- --health-probe-bind-address=:8081
45-
- --metrics-bind-address=127.0.0.1:8080
46-
- --leader-elect
39+
- args: {{- toYaml .Values.controllerManager.manager.args | nindent 8 }}
4740
command:
4841
- /manager
4942
env:

examples/operator/values.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,19 @@ configmapVars:
22
var4: value for var4
33
controllerManager:
44
kubeRbacProxy:
5+
args:
6+
- --secure-listen-address=0.0.0.0:8443
7+
- --upstream=http://127.0.0.1:8080/
8+
- --logtostderr=true
9+
- --v=10
510
image:
611
repository: gcr.io/kubebuilder/kube-rbac-proxy
712
tag: v0.8.0
813
manager:
14+
args:
15+
- --health-probe-bind-address=:8081
16+
- --metrics-bind-address=127.0.0.1:8080
17+
- --leader-elect
918
containerSecurityContext:
1019
allowPrivilegeEscalation: false
1120
capabilities:

pkg/processor/deployment/deployment.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package deployment
22

33
import (
44
"fmt"
5-
"github.com/arttor/helmify/pkg/processor/pod"
65
"io"
76
"strings"
87
"text/template"
98

9+
"github.com/arttor/helmify/pkg/processor/pod"
10+
1011
"github.com/arttor/helmify/pkg/helmify"
1112
"github.com/arttor/helmify/pkg/processor"
1213
yamlformat "github.com/arttor/helmify/pkg/yaml"

pkg/processor/pod/pod.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,30 @@ func ProcessSpec(objName string, appMeta helmify.AppMetadata, spec corev1.PodSpe
4848
if err != nil {
4949
return nil, nil, err
5050
}
51-
if !exists || len(res) == 0 {
52-
continue
51+
if exists && len(res) > 0 {
52+
err = unstructured.SetNestedField(containers[i].(map[string]interface{}), fmt.Sprintf(`{{- toYaml .Values.%s.%s.resources | nindent 10 }}`, objName, containerName), "resources")
53+
if err != nil {
54+
return nil, nil, err
55+
}
5356
}
54-
err = unstructured.SetNestedField(containers[i].(map[string]interface{}), fmt.Sprintf(`{{- toYaml .Values.%s.%s.resources | nindent 10 }}`, objName, containerName), "resources")
57+
58+
args, exists, err := unstructured.NestedStringSlice(containers[i].(map[string]interface{}), "args")
5559
if err != nil {
5660
return nil, nil, err
5761
}
62+
if exists && len(args) > 0 {
63+
err = unstructured.SetNestedField(containers[i].(map[string]interface{}), fmt.Sprintf(`{{- toYaml .Values.%[1]s.%[2]s.args | nindent 8 }}`, objName, containerName), "args")
64+
if err != nil {
65+
return nil, nil, err
66+
}
67+
68+
err = unstructured.SetNestedStringSlice(values, args, objName, containerName, "args")
69+
if err != nil {
70+
return nil, nil, fmt.Errorf("%w: unable to set deployment value field", err)
71+
}
72+
}
5873
}
74+
5975
err = unstructured.SetNestedSlice(specMap, containers, "containers")
6076
if err != nil {
6177
return nil, nil, err

pkg/processor/pod/pod_test.go

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
package pod
2+
3+
import (
4+
"testing"
5+
6+
"github.com/arttor/helmify/pkg/helmify"
7+
"github.com/arttor/helmify/pkg/metadata"
8+
appsv1 "k8s.io/api/apps/v1"
9+
"k8s.io/apimachinery/pkg/runtime"
10+
11+
"github.com/arttor/helmify/internal"
12+
"github.com/stretchr/testify/assert"
13+
)
14+
15+
const (
16+
strDeployment = `
17+
apiVersion: apps/v1
18+
kind: Deployment
19+
metadata:
20+
name: nginx-deployment
21+
labels:
22+
app: nginx
23+
spec:
24+
replicas: 3
25+
selector:
26+
matchLabels:
27+
app: nginx
28+
template:
29+
metadata:
30+
labels:
31+
app: nginx
32+
spec:
33+
containers:
34+
- name: nginx
35+
image: nginx:1.14.2
36+
args:
37+
- --test
38+
- --arg
39+
ports:
40+
- containerPort: 80
41+
`
42+
43+
strDeploymentWithNoArgs = `
44+
apiVersion: apps/v1
45+
kind: Deployment
46+
metadata:
47+
name: nginx-deployment
48+
labels:
49+
app: nginx
50+
spec:
51+
replicas: 3
52+
selector:
53+
matchLabels:
54+
app: nginx
55+
template:
56+
metadata:
57+
labels:
58+
app: nginx
59+
spec:
60+
containers:
61+
- name: nginx
62+
image: nginx:1.14.2
63+
ports:
64+
- containerPort: 80
65+
`
66+
)
67+
68+
func Test_pod_Process(t *testing.T) {
69+
t.Run("deployment with args", func(t *testing.T) {
70+
var deploy appsv1.Deployment
71+
obj := internal.GenerateObj(strDeployment)
72+
err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &deploy)
73+
specMap, tmpl, err := ProcessSpec("nginx", &metadata.Service{}, deploy.Spec.Template.Spec)
74+
assert.NoError(t, err)
75+
76+
assert.Equal(t, map[string]interface{}{
77+
"containers": []interface{}{
78+
map[string]interface{}{
79+
"args": "{{- toYaml .Values.nginx.nginx.args | nindent 8 }}",
80+
"env": []interface{}{
81+
map[string]interface{}{
82+
"name": "KUBERNETES_CLUSTER_DOMAIN",
83+
"value": "{{ quote .Values.kubernetesClusterDomain }}",
84+
},
85+
},
86+
"image": "{{ .Values.nginx.nginx.image.repository }}:{{ .Values.nginx.nginx.image.tag | default .Chart.AppVersion }}",
87+
"name": "nginx", "ports": []interface{}{
88+
map[string]interface{}{
89+
"containerPort": int64(80),
90+
},
91+
},
92+
"resources": map[string]interface{}{},
93+
},
94+
},
95+
}, specMap)
96+
97+
assert.Equal(t, helmify.Values{
98+
"nginx": map[string]interface{}{
99+
"nginx": map[string]interface{}{
100+
"image": map[string]interface{}{
101+
"repository": "nginx",
102+
"tag": "1.14.2",
103+
},
104+
"args": []interface{}{
105+
"--test",
106+
"--arg",
107+
},
108+
},
109+
},
110+
}, tmpl)
111+
})
112+
113+
t.Run("deployment with no args", func(t *testing.T) {
114+
var deploy appsv1.Deployment
115+
obj := internal.GenerateObj(strDeploymentWithNoArgs)
116+
err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &deploy)
117+
specMap, tmpl, err := ProcessSpec("nginx", &metadata.Service{}, deploy.Spec.Template.Spec)
118+
assert.NoError(t, err)
119+
120+
assert.Equal(t, map[string]interface{}{
121+
"containers": []interface{}{
122+
map[string]interface{}{
123+
"env": []interface{}{
124+
map[string]interface{}{
125+
"name": "KUBERNETES_CLUSTER_DOMAIN",
126+
"value": "{{ quote .Values.kubernetesClusterDomain }}",
127+
},
128+
},
129+
"image": "{{ .Values.nginx.nginx.image.repository }}:{{ .Values.nginx.nginx.image.tag | default .Chart.AppVersion }}",
130+
"name": "nginx", "ports": []interface{}{
131+
map[string]interface{}{
132+
"containerPort": int64(80),
133+
},
134+
},
135+
"resources": map[string]interface{}{},
136+
},
137+
},
138+
}, specMap)
139+
140+
assert.Equal(t, helmify.Values{
141+
"nginx": map[string]interface{}{
142+
"nginx": map[string]interface{}{
143+
"image": map[string]interface{}{
144+
"repository": "nginx",
145+
"tag": "1.14.2",
146+
},
147+
},
148+
},
149+
}, tmpl)
150+
})
151+
152+
}

0 commit comments

Comments
 (0)