Skip to content

Commit 480c99b

Browse files
committed
add support for adding annotations to kubernetes transform
Signed-off-by: Alex Price <[email protected]>
1 parent 7cb3c1f commit 480c99b

File tree

4 files changed

+99
-10
lines changed

4 files changed

+99
-10
lines changed

docs/api.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,8 @@ Following is the supported API format for network transformations:
265265
macField: entry MAC input field
266266
output: entry output field
267267
assignee: value needs to assign to output field
268-
labels_prefix: labels prefix to use to copy input lables, if empty labels will not be copied
268+
labels_prefix: labels prefix to use to copy input labels, if empty labels will not be copied
269+
annotations_prefix: annotations prefix to use to copy input annotations, if empty annotations will not be copied
269270
add_zone: if true the rule will add the zone
270271
add_subnet: Add subnet rule configuration
271272
input: entry input field

pkg/api/transform_network.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,16 @@ type K8sReference struct {
9898
}
9999

100100
type K8sRule struct {
101-
IPField string `yaml:"ipField,omitempty" json:"ipField,omitempty" doc:"entry IP input field"`
102-
InterfacesField string `yaml:"interfacesField,omitempty" json:"interfacesField,omitempty" doc:"entry Interfaces input field"`
103-
UDNsField string `yaml:"udnsField,omitempty" json:"udnsField,omitempty" doc:"entry UDNs input field"`
104-
MACField string `yaml:"macField,omitempty" json:"macField,omitempty" doc:"entry MAC input field"`
105-
Output string `yaml:"output,omitempty" json:"output,omitempty" doc:"entry output field"`
106-
Assignee string `yaml:"assignee,omitempty" json:"assignee,omitempty" doc:"value needs to assign to output field"`
107-
LabelsPrefix string `yaml:"labels_prefix,omitempty" json:"labels_prefix,omitempty" doc:"labels prefix to use to copy input lables, if empty labels will not be copied"`
108-
AddZone bool `yaml:"add_zone,omitempty" json:"add_zone,omitempty" doc:"if true the rule will add the zone"`
109-
OutputKeys K8SOutputKeys `yaml:"-" json:"-"`
101+
IPField string `yaml:"ipField,omitempty" json:"ipField,omitempty" doc:"entry IP input field"`
102+
InterfacesField string `yaml:"interfacesField,omitempty" json:"interfacesField,omitempty" doc:"entry Interfaces input field"`
103+
UDNsField string `yaml:"udnsField,omitempty" json:"udnsField,omitempty" doc:"entry UDNs input field"`
104+
MACField string `yaml:"macField,omitempty" json:"macField,omitempty" doc:"entry MAC input field"`
105+
Output string `yaml:"output,omitempty" json:"output,omitempty" doc:"entry output field"`
106+
Assignee string `yaml:"assignee,omitempty" json:"assignee,omitempty" doc:"value needs to assign to output field"`
107+
LabelsPrefix string `yaml:"labels_prefix,omitempty" json:"labels_prefix,omitempty" doc:"labels prefix to use to copy input labels, if empty labels will not be copied"`
108+
AnnotationsPrefix string `yaml:"annotations_prefix,omitempty" json:"annotations_prefix,omitempty" doc:"annotations prefix to use to copy input annotations, if empty annotations will not be copied"`
109+
AddZone bool `yaml:"add_zone,omitempty" json:"add_zone,omitempty" doc:"if true the rule will add the zone"`
110+
OutputKeys K8SOutputKeys `yaml:"-" json:"-"`
110111
}
111112

112113
type K8SOutputKeys struct {

pkg/pipeline/transform/kubernetes/enrich.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ func Enrich(outputEntry config.GenericMap, rule *api.K8sRule) {
5757
outputEntry[rule.LabelsPrefix+"_"+labelKey] = labelValue
5858
}
5959
}
60+
if rule.AnnotationsPrefix != "" {
61+
for annotationKey, annotationValue := range kubeInfo.Annotations {
62+
outputEntry[rule.AnnotationsPrefix+"_"+annotationKey] = annotationValue
63+
}
64+
}
6065
if kubeInfo.HostIP != "" {
6166
outputEntry[rule.OutputKeys.HostIP] = kubeInfo.HostIP
6267
if kubeInfo.HostName != "" {

pkg/pipeline/transform/kubernetes/enrich_test.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,3 +581,85 @@ func TestEnrichUsingUDN(t *testing.T) {
581581
"DstK8s_NetworkName": "ns-2/primary-udn",
582582
}, entry)
583583
}
584+
585+
func TestEnrich_LabelsAndAnnotationsPrefixes(t *testing.T) {
586+
testData := map[string]*model.ResourceMetaData{
587+
"10.0.0.10": {
588+
ObjectMeta: v1.ObjectMeta{
589+
Name: "test-pod",
590+
Namespace: "test-ns",
591+
Labels: map[string]string{"app": "web", "tier": "backend"},
592+
Annotations: map[string]string{
593+
"owner": "team-a",
594+
"prometheus.io/scrape": "true",
595+
},
596+
},
597+
Kind: "Pod",
598+
},
599+
}
600+
setupStubs(testData, nil, nodes)
601+
602+
tests := []struct {
603+
name string
604+
labelsPrefix string
605+
annotationsPrefix string
606+
expectLabels map[string]string
607+
expectAnnotations map[string]string
608+
notExpect []string
609+
}{
610+
{
611+
name: "both prefixes",
612+
labelsPrefix: "K8s_Labels",
613+
annotationsPrefix: "K8s_Annotations",
614+
expectLabels: map[string]string{"K8s_Labels_app": "web", "K8s_Labels_tier": "backend"},
615+
expectAnnotations: map[string]string{"K8s_Annotations_owner": "team-a", "K8s_Annotations_prometheus.io/scrape": "true"},
616+
},
617+
{
618+
name: "labels only",
619+
labelsPrefix: "K8s_Labels",
620+
expectLabels: map[string]string{"K8s_Labels_app": "web"},
621+
notExpect: []string{"K8s_Annotations_owner"},
622+
},
623+
{
624+
name: "annotations only",
625+
annotationsPrefix: "K8s_Annotations",
626+
expectAnnotations: map[string]string{"K8s_Annotations_owner": "team-a"},
627+
notExpect: []string{"K8s_Labels_app"},
628+
},
629+
{
630+
name: "no prefixes",
631+
notExpect: []string{"K8s_Labels_app", "K8s_Annotations_owner"},
632+
},
633+
}
634+
635+
for _, tt := range tests {
636+
t.Run(tt.name, func(t *testing.T) {
637+
rule := api.TransformNetwork{
638+
Rules: api.NetworkTransformRules{{
639+
Type: api.NetworkAddKubernetes,
640+
Kubernetes: &api.K8sRule{
641+
IPField: "SrcAddr",
642+
Output: "K8s",
643+
LabelsPrefix: tt.labelsPrefix,
644+
AnnotationsPrefix: tt.annotationsPrefix,
645+
},
646+
}},
647+
}
648+
rule.Preprocess()
649+
650+
entry := config.GenericMap{"SrcAddr": "10.0.0.10"}
651+
Enrich(entry, rule.Rules[0].Kubernetes)
652+
653+
assert.Equal(t, "test-pod", entry["K8s_Name"])
654+
for k, v := range tt.expectLabels {
655+
assert.Equal(t, v, entry[k])
656+
}
657+
for k, v := range tt.expectAnnotations {
658+
assert.Equal(t, v, entry[k])
659+
}
660+
for _, k := range tt.notExpect {
661+
assert.NotContains(t, entry, k)
662+
}
663+
})
664+
}
665+
}

0 commit comments

Comments
 (0)