Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
czeslavo committed Oct 23, 2024
1 parent 26f8a4f commit 1a8a317
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 14 deletions.
28 changes: 15 additions & 13 deletions controller/konnect/ops/objects_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,31 +47,33 @@ type ObjectWithMetadata interface {
// It returns a slice of unique, sorted strings for deterministic output.
func GenerateTagsForObject(obj ObjectWithMetadata, additionalTags ...string) []string {
const (
// The maximum length of a tag in Konnect.
maxAllowedTagLength = 128
maxAllowedTagCount = 20
// The maximum number of tags that can be attached to a Konnect entity.
maxAllowedTagsCount = 20
)

// truncate the tags from annotations.
// 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))
}

// tags from k8s metadata must be kept.
k8sMetaTags := generateKubernetesMetadataTags(obj)

// If numbers "custom" tags from spec and annotations exceeds allowed count of tags, truncate them.
customTagCountLimit := maxAllowedTagCount - len(k8sMetaTags)
customTags := lo.Uniq(slices.Concat(annotationTags, additionalTags))
// TODO: Since the order of tags is not deterministic, when truncate happens, the preserved tags are also non deterministic.
// Should we provide some order like alphabetical order?
if len(customTags) > customTagCountLimit {
customTags = customTags[:customTagCountLimit]
// 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]
}

res := lo.Uniq(slices.Concat(k8sMetaTags, customTags))
sort.Strings(res)
return res
sort.Strings(allTags)
return allTags
}

// generateKubernetesMetadataTags generates a list of tags from a Kubernetes object's metadata. The tags are formatted as
Expand Down
65 changes: 65 additions & 0 deletions controller/konnect/ops/objects_metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,71 @@ func TestGenerateTagsForObject(t *testing.T) {
"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
2 changes: 1 addition & 1 deletion controller/konnect/ops/ops_kongpluginbinding.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

sdkkonnectcomp "github.com/Kong/sdk-konnect-go/models/components"
sdkkonnectops "github.com/Kong/sdk-konnect-go/models/operations"
"github.com/kong/kubernetes-configuration/pkg/metadata"
"github.com/samber/lo"
"sigs.k8s.io/controller-runtime/pkg/client"

Expand All @@ -16,7 +17,6 @@ import (
configurationv1 "github.com/kong/kubernetes-configuration/api/configuration/v1"
configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1"
configurationv1beta1 "github.com/kong/kubernetes-configuration/api/configuration/v1beta1"
"github.com/kong/kubernetes-configuration/pkg/metadata"
)

// -----------------------------------------------------------------------------
Expand Down

0 comments on commit 1a8a317

Please sign in to comment.