Skip to content

Commit 7e565a3

Browse files
authored
Merge pull request #39 from uswitch/add-priority-labeling
Add labels from annotations
2 parents 6914761 + 45834c5 commit 7e565a3

File tree

7 files changed

+132
-1
lines changed

7 files changed

+132
-1
lines changed

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

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

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

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

pkg/templates/deployment.go

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

3637
// CreateFromDeployment
@@ -68,12 +69,18 @@ func (a *PrometheusRuleTemplateManager) CreateFromDeployment(deployment *apps.De
6869

6970
prometheusRules := map[string]*monitoringv1.PrometheusRule{}
7071
annotations := params.Deployment.GetAnnotations()
72+
params.CustomLabels = extractCustomLabels(annotations)
7173

7274
for k, v := range annotations {
7375
if !strings.HasPrefix(k, heimPrefix) {
7476
continue
7577
}
7678

79+
// Skip label-* annotations as they are not templates
80+
if strings.HasPrefix(k, labelPrefix) {
81+
continue
82+
}
83+
7784
templateName := strings.TrimLeft(k, fmt.Sprintf("%s/", heimPrefix))
7885
logger.Infow("template selected", "template", templateName)
7986
template, ok := a.templates[templateName]

pkg/templates/deployment_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,32 @@ var (
2424
environmentAnnotation: "testing",
2525
criticalityAnnotation: "low",
2626
sensitivityAnnotation: "public",
27+
priorityAnnotation: "p1",
28+
"com.uswitch.heimdall/replicas-availability-deployment": "1",
29+
},
30+
OwnerReferences: []metav1.OwnerReference{},
31+
},
32+
Spec: appsv1.DeploymentSpec{
33+
Replicas: new(int32),
34+
Selector: &metav1.LabelSelector{},
35+
},
36+
}
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",
2753
"com.uswitch.heimdall/replicas-availability-deployment": "1",
2854
},
2955
OwnerReferences: []metav1.OwnerReference{},
@@ -51,4 +77,23 @@ kube_deployment_spec_replicas{namespace="testNamespace", deployment="testApp"} <
5177
assert.Assert(t, is.Len(promrules, 1))
5278
assert.Equal(t, promrules[0].Spec.Groups[0].Rules[0].Expr.StrVal, expr)
5379
assert.Equal(t, promrules[0].Spec.Groups[0].Rules[0].Labels["owner"], "testDeploymentOwner")
80+
assert.Equal(t, promrules[0].Spec.Groups[0].Rules[0].Labels["environment"], "testing")
81+
assert.Equal(t, promrules[0].Spec.Groups[0].Rules[0].Labels["criticality"], "low")
82+
assert.Equal(t, promrules[0].Spec.Groups[0].Rules[0].Labels["sensitivity"], "public")
83+
assert.Equal(t, promrules[0].Spec.Groups[0].Rules[0].Labels["priority"], "p1")
84+
}
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")
5499
}

pkg/templates/ingress.go

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

3435
// CreateFromIngress
@@ -50,12 +51,18 @@ func (a *PrometheusRuleTemplateManager) CreateFromIngress(ingress *networkingv1.
5051

5152
prometheusRules := map[string]*monitoringv1.PrometheusRule{}
5253
annotations := ingress.GetAnnotations()
54+
params.CustomLabels = extractCustomLabels(annotations)
5355

5456
for k, v := range annotations {
5557
if !strings.HasPrefix(k, heimPrefix) {
5658
continue
5759
}
5860

61+
// Skip label-* annotations as they are not templates
62+
if strings.HasPrefix(k, labelPrefix) {
63+
continue
64+
}
65+
5966
templateName := strings.TrimLeft(k, fmt.Sprintf("%s/", heimPrefix))
6067
template, ok := a.templates[templateName]
6168
if !ok {

pkg/templates/ingress_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ var (
7474
environmentAnnotation: "testing",
7575
criticalityAnnotation: "low",
7676
sensitivityAnnotation: "public",
77+
priorityAnnotation: "p1",
7778
},
7879
},
7980
Spec: networkingv1.IngressSpec{
@@ -119,6 +120,33 @@ var (
119120
},
120121
},
121122
}
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+
}
122150
)
123151

124152
func TestIngressAnnotationsDefaultBackend(t *testing.T) {
@@ -147,6 +175,10 @@ func TestIngressAnnotationsDefaultBackend(t *testing.T) {
147175
assert.Assert(t, is.Len(promrules, 1))
148176
assert.Equal(t, promrules[0].Spec.Groups[0].Rules[0].Expr.StrVal, expr)
149177
assert.Equal(t, promrules[0].Spec.Groups[0].Rules[0].Labels["owner"], "testIngressOwner")
178+
assert.Equal(t, promrules[0].Spec.Groups[0].Rules[0].Labels["environment"], "testing")
179+
assert.Equal(t, promrules[0].Spec.Groups[0].Rules[0].Labels["criticality"], "low")
180+
assert.Equal(t, promrules[0].Spec.Groups[0].Rules[0].Labels["sensitivity"], "public")
181+
assert.Equal(t, promrules[0].Spec.Groups[0].Rules[0].Labels["priority"], "p1")
150182
}
151183

152184
func TestIngressAnnotationsRuleBackend(t *testing.T) {
@@ -186,3 +218,19 @@ func TestNamesMatch(t *testing.T) {
186218
service = checkNamesMatch(services)
187219
assert.Equal(t, "", service)
188220
}
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: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@ 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"
2124
environmentAnnotation = "service.rvu.co.uk/environment"
2225
criticalityAnnotation = "service.rvu.co.uk/criticality"
2326
sensitivityAnnotation = "service.rvu.co.uk/sensitivity"
27+
priorityAnnotation = "com.uswitch.heimdall/label-priority"
2428
)
2529

2630
// ClientSetI
@@ -79,3 +83,17 @@ func collectPrometheusRules(prometheusRules map[string]*monitoringv1.PrometheusR
7983

8084
return ret
8185
}
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)