Skip to content

Commit

Permalink
fix: truncate long tags in annotation (#779)
Browse files Browse the repository at this point in the history
* fix: truncate long tags in annotation

* add tests

---------

Co-authored-by: Grzegorz Burzyński <[email protected]>
  • Loading branch information
randmonkey and czeslavo authored Oct 23, 2024
1 parent 0fc29d2 commit fad649a
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 6 deletions.
33 changes: 27 additions & 6 deletions controller/konnect/ops/objects_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,34 @@ type ObjectWithMetadata interface {
// An optional set of tags can be passed to be included in the generated tags (e.g. tags from the spec).
// It returns a slice of unique, sorted strings for deterministic output.
func GenerateTagsForObject(obj ObjectWithMetadata, additionalTags ...string) []string {
var (
annotationTags = metadata.ExtractTags(obj)
k8sMetaTags = generateKubernetesMetadataTags(obj)
res = lo.Uniq(slices.Concat(annotationTags, k8sMetaTags, additionalTags))
const (
// The maximum length of a tag in Konnect.
maxAllowedTagLength = 128
// The maximum number of tags that can be attached to a Konnect entity.
maxAllowedTagsCount = 20
)
sort.Strings(res)
return res

// Truncate the tags from annotations as we do not validate their length in CEL validations rules.
var annotationTags []string
for _, tag := range metadata.ExtractTags(obj) {
annotationTags = append(annotationTags, truncate(tag, maxAllowedTagLength))
}

k8sMetaTags := generateKubernetesMetadataTags(obj)

// We concatenate the tags in this order to ensure that the k8sMetaTags and additionalTags (from spec) are never
// truncated below. CEL rules ensure that the total length of k8sMetaTags and additionalTags never exceeds
// the maximum allowed tags count. That means we will only discard tags from annotations.
allTags := lo.Uniq(slices.Concat(k8sMetaTags, additionalTags, annotationTags))

// If the total number of tags exceeds the maximum allowed tags counts, we limit the number of tags to the maximum
// allowed tags count, discarding the tags from annotations.
if len(allTags) > maxAllowedTagsCount {
allTags = allTags[:maxAllowedTagsCount]
}

sort.Strings(allTags)
return allTags
}

// generateKubernetesMetadataTags generates a list of tags from a Kubernetes object's metadata. The tags are formatted as
Expand Down
87 changes: 87 additions & 0 deletions controller/konnect/ops/objects_metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,93 @@ func TestGenerateTagsForObject(t *testing.T) {
"k8s-version:v1",
},
},
{
name: "too long tags in annotations are truncated",
obj: func() testObjectKind {
obj := namespacedObject()
obj.ObjectMeta.Annotations = map[string]string{
"konghq.com/tags": "tag1,tag2,long-long-long-long-long-long-long-long-long-long-long-long-long-long-long-long-long-long-long-long-long-tag-that-would-end-here-and-no-more-things-should-be-preserved",
}
return obj
}(),
expectedTags: []string{
"k8s-generation:2",
"k8s-group:test.objects.io",
"k8s-kind:TestObjectKind",
"k8s-name:test-object",
"k8s-namespace:test-namespace",
"k8s-uid:test-uid",
"k8s-version:v1",
"long-long-long-long-long-long-long-long-long-long-long-long-long-long-long-long-long-long-long-long-long-tag-that-would-end-here",
"tag1",
"tag2",
},
},
{
name: "when too many tags in total, last from annotations are discarded",
obj: func() testObjectKind {
obj := namespacedObject()
obj.ObjectMeta.Annotations = map[string]string{
"konghq.com/tags": "a,b,c,d,e,f,g,h,i,j,k,l,m,iwillbediscarded",
}
return obj
}(),
expectedTags: []string{
"a",
"b",
"c",
"d",
"e",
"f",
"g",
"h",
"i",
"j",
"k",
"k8s-generation:2",
"k8s-group:test.objects.io",
"k8s-kind:TestObjectKind",
"k8s-name:test-object",
"k8s-namespace:test-namespace",
"k8s-uid:test-uid",
"k8s-version:v1",
"l",
"m",
},
},
{
name: "when too many tags in total and additional tags are passed, last from annotations are discarded",
obj: func() testObjectKind {
obj := namespacedObject()
obj.ObjectMeta.Annotations = map[string]string{
"konghq.com/tags": "a,c,e,gwillbediscarded,iwillbediscarded,kwillbediscarded,mwillbediscarded",
}
return obj
}(),
additionalTags: []string{"b", "d", "f", "h", "j", "l", "n", "o", "p", "r"},
expectedTags: []string{
"a",
"b",
"c",
"d",
"e",
"f",
"h",
"j",
"k8s-generation:2",
"k8s-group:test.objects.io",
"k8s-kind:TestObjectKind",
"k8s-name:test-object",
"k8s-namespace:test-namespace",
"k8s-uid:test-uid",
"k8s-version:v1",
"l",
"n",
"o",
"p",
"r",
},
},
}

for _, tc := range testCases {
Expand Down

0 comments on commit fad649a

Please sign in to comment.