Skip to content

Commit 848c379

Browse files
committed
make labels generic
1 parent 6df0ca0 commit 848c379

File tree

7 files changed

+119
-11
lines changed

7 files changed

+119
-11
lines changed

kube/config/templates/5xx-rate.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@ spec:
4545
{{if .Sensitivity}}
4646
sensitivity: {{.Sensitivity}}
4747
{{end}}
48-
{{if .Priority}}
49-
priority: {{.Priority}}
48+
{{range $key, $value := .CustomLabels}}
49+
{{$key}}: {{$value}}
5050
{{end}}

kube/config/templates/replicas-availability-deployment.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@ spec:
3737
{{if .Sensitivity}}
3838
sensitivity: {{.Sensitivity}}
3939
{{end}}
40-
{{if .Priority}}
41-
priority: {{.Priority}}
40+
{{range $key, $value := .CustomLabels}}
41+
{{$key}}: {{$value}}
4242
{{end}}

pkg/templates/deployment.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type templateParameterDeployment struct {
3131
Criticality string
3232
Sensitivity string
3333
Deployment *apps.Deployment
34-
Priority string
34+
CustomLabels map[string]string
3535
}
3636

3737
// CreateFromDeployment
@@ -69,13 +69,17 @@ func (a *PrometheusRuleTemplateManager) CreateFromDeployment(deployment *apps.De
6969

7070
prometheusRules := map[string]*monitoringv1.PrometheusRule{}
7171
annotations := params.Deployment.GetAnnotations()
72-
7372
for k, v := range annotations {
7473
if !strings.HasPrefix(k, heimPrefix) {
7574
continue
7675
}
7776

78-
params.Priority = deployment.GetAnnotations()[priorityAnnotation]
77+
// Skip label-* annotations as they are not templates
78+
if strings.HasPrefix(k, labelPrefix) {
79+
continue
80+
}
81+
82+
params.CustomLabels = extractCustomLabels(annotations)
7983

8084
templateName := strings.TrimLeft(k, fmt.Sprintf("%s/", heimPrefix))
8185
logger.Infow("template selected", "template", templateName)

pkg/templates/deployment_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,31 @@ var (
3434
Selector: &metav1.LabelSelector{},
3535
},
3636
}
37+
38+
testDeploymentMultiLabel = &appsv1.Deployment{
39+
ObjectMeta: metav1.ObjectMeta{
40+
Name: "testAppMulti",
41+
Namespace: "testNamespace",
42+
Labels: map[string]string{
43+
"app": "testAppMulti",
44+
},
45+
Annotations: map[string]string{
46+
ownerAnnotation: "testDeploymentOwner",
47+
environmentAnnotation: "testing",
48+
criticalityAnnotation: "low",
49+
sensitivityAnnotation: "public",
50+
"com.uswitch.heimdall/label-priority": "p3",
51+
"com.uswitch.heimdall/label-channel": "testing",
52+
"com.uswitch.heimdall/label-team": "platform",
53+
"com.uswitch.heimdall/replicas-availability-deployment": "1",
54+
},
55+
OwnerReferences: []metav1.OwnerReference{},
56+
},
57+
Spec: appsv1.DeploymentSpec{
58+
Replicas: new(int32),
59+
Selector: &metav1.LabelSelector{},
60+
},
61+
}
3762
)
3863

3964
func TestDeploymentAnnotations(t *testing.T) {
@@ -57,3 +82,18 @@ kube_deployment_spec_replicas{namespace="testNamespace", deployment="testApp"} <
5782
assert.Equal(t, promrules[0].Spec.Groups[0].Rules[0].Labels["sensitivity"], "public")
5883
assert.Equal(t, promrules[0].Spec.Groups[0].Rules[0].Labels["priority"], "p1")
5984
}
85+
86+
func TestDeploymentMultipleCustomLabels(t *testing.T) {
87+
log.Setup(log.DEBUG_LEVEL)
88+
89+
client := fake.NewSimpleClientset()
90+
91+
template, err := NewPrometheusRuleTemplateManager("../../kube/config/templates", client)
92+
93+
promrules, err := template.CreateFromDeployment(testDeploymentMultiLabel, "testNamespace")
94+
assert.Assert(t, is.Nil(err))
95+
assert.Assert(t, is.Len(promrules, 1))
96+
assert.Equal(t, promrules[0].Spec.Groups[0].Rules[0].Labels["priority"], "p3")
97+
assert.Equal(t, promrules[0].Spec.Groups[0].Rules[0].Labels["channel"], "testing")
98+
assert.Equal(t, promrules[0].Spec.Groups[0].Rules[0].Labels["team"], "platform")
99+
}

pkg/templates/ingress.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type templateParameterIngress struct {
2929
Criticality string
3030
Sensitivity string
3131
BackendService string
32-
Priority string
32+
CustomLabels map[string]string
3333
}
3434

3535
// CreateFromIngress
@@ -57,7 +57,12 @@ func (a *PrometheusRuleTemplateManager) CreateFromIngress(ingress *networkingv1.
5757
continue
5858
}
5959

60-
params.Priority = ingress.GetAnnotations()[priorityAnnotation]
60+
// Skip label-* annotations as they are not templates
61+
if strings.HasPrefix(k, labelPrefix) {
62+
continue
63+
}
64+
65+
params.CustomLabels = extractCustomLabels(annotations)
6166

6267
templateName := strings.TrimLeft(k, fmt.Sprintf("%s/", heimPrefix))
6368
template, ok := a.templates[templateName]
@@ -125,7 +130,6 @@ func (a *PrometheusRuleTemplateManager) resolveIngressOwner(params *templatePara
125130
params.Environment = deployment.GetAnnotations()[environmentAnnotation]
126131
params.Criticality = deployment.GetAnnotations()[criticalityAnnotation]
127132
params.Sensitivity = deployment.GetAnnotations()[sensitivityAnnotation]
128-
params.Priority = deployment.GetAnnotations()[priorityAnnotation]
129133

130134
return params, nil
131135
}

pkg/templates/ingress_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,33 @@ var (
120120
},
121121
},
122122
}
123+
124+
testIngressMultiLabel = &networkingv1.Ingress{
125+
ObjectMeta: metav1.ObjectMeta{
126+
Name: "testMultiLabel",
127+
Namespace: "testNamespace",
128+
Annotations: map[string]string{
129+
"com.uswitch.heimdall/5xx-rate": "0.001",
130+
ownerAnnotation: "testIngressOwner",
131+
environmentAnnotation: "testing",
132+
criticalityAnnotation: "low",
133+
sensitivityAnnotation: "public",
134+
"com.uswitch.heimdall/label-priority": "p2",
135+
"com.uswitch.heimdall/label-channel": "testing",
136+
"com.uswitch.heimdall/label-region": "eu-west-1",
137+
},
138+
},
139+
Spec: networkingv1.IngressSpec{
140+
DefaultBackend: &networkingv1.IngressBackend{
141+
Service: &networkingv1.IngressServiceBackend{
142+
Name: "testService",
143+
Port: networkingv1.ServiceBackendPort{
144+
Number: 80,
145+
},
146+
},
147+
},
148+
},
149+
}
123150
)
124151

125152
func TestIngressAnnotationsDefaultBackend(t *testing.T) {
@@ -191,3 +218,19 @@ func TestNamesMatch(t *testing.T) {
191218
service = checkNamesMatch(services)
192219
assert.Equal(t, "", service)
193220
}
221+
222+
func TestIngressMultipleCustomLabels(t *testing.T) {
223+
log.Setup(log.DEBUG_LEVEL)
224+
225+
client := fake.NewSimpleClientset(testService, testDeployment, testReplicaset, testPod)
226+
227+
template, err := NewPrometheusRuleTemplateManager("../../kube/config/templates", client)
228+
229+
promrules, err := template.CreateFromIngress(testIngressMultiLabel)
230+
assert.Assert(t, is.Nil(err))
231+
assert.Assert(t, is.Len(promrules, 1))
232+
assert.Equal(t, promrules[0].Spec.Groups[0].Rules[0].Labels["priority"], "p2")
233+
assert.Equal(t, promrules[0].Spec.Groups[0].Rules[0].Labels["channel"], "testing")
234+
assert.Equal(t, promrules[0].Spec.Groups[0].Rules[0].Labels["region"], "eu-west-1")
235+
}
236+

pkg/templates/promrules.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ import (
1414
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
1515
)
1616

17-
var heimPrefix = "com.uswitch.heimdall"
17+
var (
18+
heimPrefix = "com.uswitch.heimdall"
19+
labelPrefix = "com.uswitch.heimdall/label-"
20+
)
1821

1922
const (
2023
ownerAnnotation = "service.rvu.co.uk/owner"
@@ -80,3 +83,17 @@ func collectPrometheusRules(prometheusRules map[string]*monitoringv1.PrometheusR
8083

8184
return ret
8285
}
86+
87+
// com.uswitch.heimdall/label-priority: p3 -> map["priority"] = "p3"
88+
func extractCustomLabels(annotations map[string]string) map[string]string {
89+
customLabels := make(map[string]string)
90+
91+
for k, v := range annotations {
92+
if strings.HasPrefix(k, labelPrefix) {
93+
labelName := strings.TrimPrefix(k, labelPrefix)
94+
customLabels[labelName] = v
95+
}
96+
}
97+
98+
return customLabels
99+
}

0 commit comments

Comments
 (0)