Skip to content

Commit 488ecc5

Browse files
committed
add support for label and annotation value max lengths
Signed-off-by: Alex Price <[email protected]>
1 parent 9992ed3 commit 488ecc5

File tree

4 files changed

+366
-20
lines changed

4 files changed

+366
-20
lines changed

docs/api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,11 @@ Following is the supported API format for network transformations:
268268
labels_prefix: labels prefix to use to copy input labels, if empty labels will not be copied
269269
label_inclusions: labels to include, if empty all labels will be included. Only used if labels_prefix is specified
270270
label_exclusions: labels to exclude, if empty no labels will be excluded. Only used if labels_prefix is specified
271+
label_value_max_length: label value max length, if specified, will trim label values to this length
271272
annotations_prefix: annotations prefix to use to copy input annotations, if empty annotations will not be copied
272273
annotation_inclusions: annotations to include, if empty all annotations will be included. Only used if annotations_prefix is specified
273274
annotation_exclusions: annotations to exclude, if empty no annotations will be excluded. Only used if annotations_prefix is specified
275+
annotation_value_max_length: annotation value max length, if specified, will trim annotation values to this length
274276
add_zone: if true the rule will add the zone
275277
add_subnet: Add subnet rule configuration
276278
input: entry input field

pkg/api/transform_network.go

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -98,24 +98,26 @@ 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 labels, if empty labels will not be copied"`
108-
LabelInclusions []string `yaml:"label_inclusions,omitempty" json:"label_inclusions,omitempty" doc:"labels to include, if empty all labels will be included. Only used if labels_prefix is specified"`
109-
LabelExclusions []string `yaml:"label_exclusions,omitempty" json:"label_exclusions,omitempty" doc:"labels to exclude, if empty no labels will be excluded. Only used if labels_prefix is specified"`
110-
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"`
111-
AnnotationInclusions []string `yaml:"annotation_inclusions,omitempty" json:"annotation_inclusions,omitempty" doc:"annotations to include, if empty all annotations will be included. Only used if annotations_prefix is specified"`
112-
AnnotationExclusions []string `yaml:"annotation_exclusions,omitempty" json:"annotation_exclusions,omitempty" doc:"annotations to exclude, if empty no annotations will be excluded. Only used if annotations_prefix is specified"`
113-
AddZone bool `yaml:"add_zone,omitempty" json:"add_zone,omitempty" doc:"if true the rule will add the zone"`
114-
OutputKeys K8SOutputKeys `yaml:"-" json:"-"`
115-
LabelInclusionsMap map[string]struct{} `yaml:"-" json:"-"`
116-
LabelExclusionsMap map[string]struct{} `yaml:"-" json:"-"`
117-
AnnotationInclusionsMap map[string]struct{} `yaml:"-" json:"-"`
118-
AnnotationExclusionsMap map[string]struct{} `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+
LabelInclusions []string `yaml:"label_inclusions,omitempty" json:"label_inclusions,omitempty" doc:"labels to include, if empty all labels will be included. Only used if labels_prefix is specified"`
109+
LabelExclusions []string `yaml:"label_exclusions,omitempty" json:"label_exclusions,omitempty" doc:"labels to exclude, if empty no labels will be excluded. Only used if labels_prefix is specified"`
110+
LabelValueMaxLength *int `yaml:"label_value_max_length,omitempty" json:"label_value_max_length,omitempty" doc:"label value max length, if specified, will trim label values to this length"`
111+
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"`
112+
AnnotationInclusions []string `yaml:"annotation_inclusions,omitempty" json:"annotation_inclusions,omitempty" doc:"annotations to include, if empty all annotations will be included. Only used if annotations_prefix is specified"`
113+
AnnotationExclusions []string `yaml:"annotation_exclusions,omitempty" json:"annotation_exclusions,omitempty" doc:"annotations to exclude, if empty no annotations will be excluded. Only used if annotations_prefix is specified"`
114+
AnnotationValueMaxLength *int `yaml:"annotation_value_max_length,omitempty" json:"annotation_value_max_length,omitempty" doc:"annotation value max length, if specified, will trim annotation values to this length"`
115+
AddZone bool `yaml:"add_zone,omitempty" json:"add_zone,omitempty" doc:"if true the rule will add the zone"`
116+
OutputKeys K8SOutputKeys `yaml:"-" json:"-"`
117+
LabelInclusionsMap map[string]struct{} `yaml:"-" json:"-"`
118+
LabelExclusionsMap map[string]struct{} `yaml:"-" json:"-"`
119+
AnnotationInclusionsMap map[string]struct{} `yaml:"-" json:"-"`
120+
AnnotationExclusionsMap map[string]struct{} `yaml:"-" json:"-"`
119121
}
120122

121123
type K8SOutputKeys struct {

pkg/pipeline/transform/kubernetes/enrich.go

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ import (
1515
var ds *datasource.Datasource
1616
var infConfig informers.Config
1717

18+
const (
19+
truncateSuffix = "..."
20+
)
21+
1822
// For testing
1923
func MockInformers() {
2024
infConfig = informers.NewConfig(api.NetworkTransformKubeConfig{})
@@ -55,14 +59,22 @@ func Enrich(outputEntry config.GenericMap, rule *api.K8sRule) {
5559
if rule.LabelsPrefix != "" {
5660
for labelKey, labelValue := range kubeInfo.Labels {
5761
if shouldInclude(labelKey, rule.LabelInclusionsMap, rule.LabelExclusionsMap) {
58-
outputEntry[rule.LabelsPrefix+"_"+labelKey] = labelValue
62+
outputEntry[rule.LabelsPrefix+"_"+labelKey] = truncateWithSuffix(
63+
labelValue,
64+
rule.LabelValueMaxLength,
65+
truncateSuffix,
66+
)
5967
}
6068
}
6169
}
6270
if rule.AnnotationsPrefix != "" {
6371
for annotationKey, annotationValue := range kubeInfo.Annotations {
6472
if shouldInclude(annotationKey, rule.AnnotationInclusionsMap, rule.AnnotationExclusionsMap) {
65-
outputEntry[rule.AnnotationsPrefix+"_"+annotationKey] = annotationValue
73+
outputEntry[rule.AnnotationsPrefix+"_"+annotationKey] = truncateWithSuffix(
74+
annotationValue,
75+
rule.AnnotationValueMaxLength,
76+
truncateSuffix,
77+
)
6678
}
6779
}
6880
}
@@ -163,3 +175,24 @@ func shouldInclude(key string, inclusions, exclusions map[string]struct{}) bool
163175
}
164176
return true
165177
}
178+
179+
// truncateWithSuffix truncates s to max runes, including the suffix.
180+
func truncateWithSuffix(s string, max *int, suffix string) string {
181+
if max == nil {
182+
return s
183+
}
184+
m := *max
185+
if m <= 0 {
186+
return ""
187+
}
188+
r := []rune(s)
189+
sr := []rune(suffix)
190+
if len(r) <= m {
191+
return s
192+
}
193+
if m <= len(sr) {
194+
return string(sr[:m])
195+
}
196+
keep := m - len(sr)
197+
return string(r[:keep]) + suffix
198+
}

0 commit comments

Comments
 (0)