Skip to content

Commit fd3acfe

Browse files
authored
feat: Add commonLabels property (#77)
1 parent a32ac26 commit fd3acfe

File tree

11 files changed

+164
-3
lines changed

11 files changed

+164
-3
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Here's a summary of the available configuration options:
5454
| imagePullSecrets | array | `[]` | Specifies docker registry secret names as an array |
5555
| nameOverride | string | `""` | Partially overrides the fullname template with a string (includes release name) |
5656
| fullnameOverride | string | `""` | Fully overrides the fullname template with a string |
57+
| commonLabels | object | `{}` | Specifies labels to be applied to all created resources |
5758
| serviceAccount.create | bool | `true` | Specifies whether a service account should be created |
5859
| serviceAccount.annotations | object | `{}` | Annotations to add to the service account |
5960
| serviceAccount.name | string | `""` | The name of the service account |
@@ -79,9 +80,9 @@ Here's a summary of the available configuration options:
7980
| nodeSelector | object | `{}` | Selector to target node placement for the relay pod |
8081
| tolerations | array | `[]` | Specify pod tolerations |
8182
| affinity | object | `{}` | Specify pod affinity |
82-
| pod.disruptionBudget.enabled | boolean | `false` | Enabled podDisruptionBudget |
83-
| pod.disruptionBudget.minAvailable | string | `""` | Minimum number of pods that are available after eviction as number or percentage |
84-
| pod.disruptionBudget.maxUnavailable | string | `""` | Maximum number of pods that are unavailable after eviction as number or percentage |
83+
| pod.disruptionBudget.enabled | boolean | `false` | Enabled podDisruptionBudget |
84+
| pod.disruptionBudget.minAvailable | string | `""` | Minimum number of pods that are available after eviction as number or percentage |
85+
| pod.disruptionBudget.maxUnavailable | string | `""` | Maximum number of pods that are unavailable after eviction as number or percentage |
8586
| pod.topologySpreadConstraints | array | `[]` | Specify the topology spread constrait definitions to apply to the relay deployment |
8687
| pod.priorityClassName | string | `""` | Specify a PriorityClass for the pod |
8788

templates/_helpers.tpl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ helm.sh/chart: {{ include "ld-relay.chart" . }}
4040
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
4141
{{- end }}
4242
app.kubernetes.io/managed-by: {{ .Release.Service }}
43+
{{- with .Values.commonLabels }}
44+
{{ toYaml . }}
45+
{{- end }}
4346
{{- end }}
4447

4548
{{/*

templates/deployment.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ spec:
2020
{{- end }}
2121
labels:
2222
{{- include "ld-relay.selectorLabels" . | nindent 8 }}
23+
{{- with .Values.commonLabels }}
24+
{{- toYaml . | nindent 8 }}
25+
{{- end }}
2326
{{- with .Values.pod.labels }}
2427
{{- toYaml . | nindent 8 }}
2528
{{- end }}

test/config_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package test
2+
3+
import (
4+
"github.com/gruntwork-io/terratest/modules/helm"
5+
"github.com/gruntwork-io/terratest/modules/k8s"
6+
corev1 "k8s.io/api/core/v1"
7+
)
8+
9+
func (s *TemplateTest) TestConfigCanSetCommonLabels() {
10+
options := &helm.Options{
11+
SetValues: map[string]string{
12+
"commonLabels.environment": "production",
13+
"commonLabels.team": "platform",
14+
},
15+
KubectlOptions: k8s.NewKubectlOptions("", "", s.Namespace),
16+
}
17+
18+
output := helm.RenderTemplate(s.T(), options, s.ChartPath, s.Release, []string{"templates/config.yaml"})
19+
var config corev1.ConfigMap
20+
helm.UnmarshalK8SYaml(s.T(), output, &config)
21+
22+
s.Require().Equal("production", config.Labels["environment"])
23+
s.Require().Equal("platform", config.Labels["team"])
24+
}

test/deployment_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,3 +451,20 @@ func (s *TemplateTest) TestNotSetTerminationGracePeriodSeconds() {
451451

452452
s.Require().Empty(deployment.Spec.Template.Spec.TerminationGracePeriodSeconds)
453453
}
454+
455+
func (s *TemplateTest) TestCanSetCommonLabels() {
456+
options := &helm.Options{
457+
SetValues: map[string]string{
458+
"commonLabels.environment": "production",
459+
"commonLabels.team": "platform",
460+
},
461+
KubectlOptions: k8s.NewKubectlOptions("", "", s.Namespace),
462+
}
463+
464+
output := helm.RenderTemplate(s.T(), options, s.ChartPath, s.Release, []string{"templates/deployment.yaml"})
465+
var deployment appsv1.Deployment
466+
helm.UnmarshalK8SYaml(s.T(), output, &deployment)
467+
468+
s.Require().Equal("production", deployment.Spec.Template.Labels["environment"])
469+
s.Require().Equal("platform", deployment.Spec.Template.Labels["team"])
470+
}

test/hpa_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package test
2+
3+
import (
4+
"github.com/gruntwork-io/terratest/modules/helm"
5+
"github.com/gruntwork-io/terratest/modules/k8s"
6+
autoscalingv2 "k8s.io/api/autoscaling/v2"
7+
)
8+
9+
func (s *TemplateTest) TestHPACanSetCommonLabels() {
10+
options := &helm.Options{
11+
SetValues: map[string]string{
12+
"autoscaling.enabled": "true",
13+
"commonLabels.environment": "production",
14+
"commonLabels.team": "platform",
15+
},
16+
KubectlOptions: k8s.NewKubectlOptions("", "", s.Namespace),
17+
}
18+
19+
output := helm.RenderTemplate(s.T(), options, s.ChartPath, s.Release, []string{"templates/hpa.yaml"})
20+
var hpa autoscalingv2.HorizontalPodAutoscaler
21+
helm.UnmarshalK8SYaml(s.T(), output, &hpa)
22+
23+
s.Require().Equal("production", hpa.Labels["environment"])
24+
s.Require().Equal("platform", hpa.Labels["team"])
25+
}

test/ingress_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package test
2+
3+
import (
4+
"github.com/gruntwork-io/terratest/modules/helm"
5+
"github.com/gruntwork-io/terratest/modules/k8s"
6+
networkingv1 "k8s.io/api/networking/v1"
7+
)
8+
9+
func (s *TemplateTest) TestIngressCanSetCommonLabels() {
10+
options := &helm.Options{
11+
SetValues: map[string]string{
12+
"ingress.enabled": "true",
13+
"commonLabels.environment": "production",
14+
"commonLabels.team": "platform",
15+
},
16+
KubectlOptions: k8s.NewKubectlOptions("", "", s.Namespace),
17+
}
18+
19+
output := helm.RenderTemplate(s.T(), options, s.ChartPath, s.Release, []string{"templates/ingress.yaml"})
20+
var ingress networkingv1.Ingress
21+
helm.UnmarshalK8SYaml(s.T(), output, &ingress)
22+
23+
s.Require().Equal("production", ingress.Labels["environment"])
24+
s.Require().Equal("platform", ingress.Labels["team"])
25+
}

test/poddisruptionbudget_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,21 @@ func (s *TemplateTest) TestBothPDBValuesWithDeprecatedProperty() {
199199
s.Require().Equal(2, pdb.Spec.MaxUnavailable.IntValue())
200200
s.Require().Nil(pdb.Spec.MinAvailable)
201201
}
202+
203+
func (s *TemplateTest) TestPDBCanSetCommonLabels() {
204+
options := &helm.Options{
205+
SetValues: map[string]string{
206+
"pod.disruptionBudget.enabled": "true",
207+
"commonLabels.environment": "production",
208+
"commonLabels.team": "platform",
209+
},
210+
KubectlOptions: k8s.NewKubectlOptions("", "", s.Namespace),
211+
}
212+
213+
output := helm.RenderTemplate(s.T(), options, s.ChartPath, s.Release, []string{"templates/poddisruptionbudget.yaml"})
214+
var pdb policyv1.PodDisruptionBudget
215+
helm.UnmarshalK8SYaml(s.T(), output, &pdb)
216+
217+
s.Require().Equal("production", pdb.Labels["environment"])
218+
s.Require().Equal("platform", pdb.Labels["team"])
219+
}

test/service_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,20 @@ func (s *TemplateTest) TestServiceSupportsAnnotations() {
5454

5555
s.Require().Equal(service.Annotations["service.beta.kubernetes.io/aws-load-balancer-internal"], "\"true\"")
5656
}
57+
58+
func (s *TemplateTest) TestServiceCanSetCommonLabels() {
59+
options := &helm.Options{
60+
SetValues: map[string]string{
61+
"commonLabels.environment": "production",
62+
"commonLabels.team": "platform",
63+
},
64+
KubectlOptions: k8s.NewKubectlOptions("", "", s.Namespace),
65+
}
66+
67+
output := helm.RenderTemplate(s.T(), options, s.ChartPath, s.Release, []string{"templates/service.yaml"})
68+
var service corev1.Service
69+
helm.UnmarshalK8SYaml(s.T(), output, &service)
70+
71+
s.Require().Equal("production", service.Labels["environment"])
72+
s.Require().Equal("platform", service.Labels["team"])
73+
}

test/serviceaccount_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package test
2+
3+
import (
4+
"github.com/gruntwork-io/terratest/modules/helm"
5+
"github.com/gruntwork-io/terratest/modules/k8s"
6+
corev1 "k8s.io/api/core/v1"
7+
)
8+
9+
func (s *TemplateTest) TestServiceAccountCanSetCommonLabels() {
10+
options := &helm.Options{
11+
SetValues: map[string]string{
12+
"serviceAccount.create": "true",
13+
"commonLabels.environment": "production",
14+
"commonLabels.team": "platform",
15+
},
16+
KubectlOptions: k8s.NewKubectlOptions("", "", s.Namespace),
17+
}
18+
19+
output := helm.RenderTemplate(s.T(), options, s.ChartPath, s.Release, []string{"templates/serviceaccount.yaml"})
20+
var serviceAccount corev1.ServiceAccount
21+
helm.UnmarshalK8SYaml(s.T(), output, &serviceAccount)
22+
23+
s.Require().Equal("production", serviceAccount.Labels["environment"])
24+
s.Require().Equal("platform", serviceAccount.Labels["team"])
25+
}

0 commit comments

Comments
 (0)