From 7c0cd217658676ba785b141d2d7f9779fba67433 Mon Sep 17 00:00:00 2001 From: dvirgilad Date: Thu, 18 Jul 2024 11:52:37 +0000 Subject: [PATCH] feat: copy all annotations with dana api group from capp to capprevision --- .../adapters/capprevision_adapter.go | 25 ++++++++++++++++--- test/e2e_tests/capp_revision_e2e_test.go | 24 ++++++++++++++++++ test/e2e_tests/utils/capp_revision_adapter.go | 8 ++++-- 3 files changed, 51 insertions(+), 6 deletions(-) diff --git a/internal/kinds/capprevision/adapters/capprevision_adapter.go b/internal/kinds/capprevision/adapters/capprevision_adapter.go index 94ccef34..2354de66 100644 --- a/internal/kinds/capprevision/adapters/capprevision_adapter.go +++ b/internal/kinds/capprevision/adapters/capprevision_adapter.go @@ -5,6 +5,7 @@ import ( "fmt" "math/rand" "strconv" + "strings" "time" cappv1alpha1 "github.com/dana-team/container-app-operator/api/v1alpha1" @@ -17,8 +18,12 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" ) +var ( + domain = cappv1alpha1.GroupVersion.Group + cappNameLabelKey = domain + "/cappName" +) + const ( - CappNameLabelKey = "rcs.dana.io/cappName" ClientListLimit = 100 charSet = "abcdefghijklmnopqrstuvwxyz0123456789" RandomStringLength = 5 @@ -45,11 +50,22 @@ func getSubstringUntilIndex(s string, index int) string { return s[:index] } +// copyAnnotations returns a map of annotations from a Capp that contain the Domain string. +func copyAnnotations(capp cappv1alpha1.Capp) map[string]string { + annotations := make(map[string]string) + for key, value := range capp.ObjectMeta.Annotations { + if strings.Contains(key, domain) { + annotations[key] = value + } + } + return annotations +} + // GetCappRevisions retrieves a list of CappRevision resources filtered by labels matching a specific Capp, returning the list and any error encountered. func GetCappRevisions(ctx context.Context, r client.Client, capp cappv1alpha1.Capp) ([]cappv1alpha1.CappRevision, error) { cappRevisions := cappv1alpha1.CappRevisionList{} - requirement, err := labels.NewRequirement(CappNameLabelKey, selection.Equals, []string{capp.Name}) + requirement, err := labels.NewRequirement(cappNameLabelKey, selection.Equals, []string{capp.Name}) if err != nil { return cappRevisions.Items, err } @@ -72,8 +88,9 @@ func CreateCappRevision(ctx context.Context, k8sClient client.Client, logger log ObjectMeta: metav1.ObjectMeta{ Name: fmt.Sprintf("%s-%s-v%s", getSubstringUntilIndex(capp.Name, IndexCut), generateRandomString(RandomStringLength), strconv.Itoa(revisionNumber)), - Namespace: capp.Namespace, - Labels: map[string]string{CappNameLabelKey: capp.Name}, + Namespace: capp.Namespace, + Labels: map[string]string{cappNameLabelKey: capp.Name}, + Annotations: copyAnnotations(capp), }, Spec: cappv1alpha1.CappRevisionSpec{ CappTemplate: cappv1alpha1.CappTemplate{ diff --git a/test/e2e_tests/capp_revision_e2e_test.go b/test/e2e_tests/capp_revision_e2e_test.go index 330d1ced..bd0b8de8 100644 --- a/test/e2e_tests/capp_revision_e2e_test.go +++ b/test/e2e_tests/capp_revision_e2e_test.go @@ -12,6 +12,11 @@ import ( . "github.com/onsi/gomega" ) +var ( + testAnnotationKey = utilst.Domain + "/test" + testAnnotationValue = "test" +) + const ( moreThanRevisionsToKeep = 12 revisionsToKeep = 10 @@ -74,4 +79,23 @@ var _ = Describe("Validate CappRevision creation", func() { fmt.Sprintf("Should limit to %s CappRevision", strconv.Itoa(revisionsToKeep))) }) + + It(fmt.Sprintf("Should copy annotations containing %s to CappRevision", utilst.Domain), func() { + baseCapp := mock.CreateBaseCapp() + baseCapp.Annotations = map[string]string{testAnnotationKey: testAnnotationValue} + By("Creating Capp") + desiredCapp := utilst.CreateCapp(k8sClient, baseCapp) + desiredCapp = utilst.GetCapp(k8sClient, desiredCapp.Name, desiredCapp.Namespace) + + Eventually(func() string { + cappRevisions, _ := utilst.GetCappRevisions(context.Background(), k8sClient, *desiredCapp) + val, ok := cappRevisions[0].Annotations[testAnnotationKey] + if ok { + return val + } + return "" + }, testconsts.Timeout, testconsts.Interval).Should(Equal(testAnnotationValue), + "Should copy annotations to CappRevision") + + }) }) diff --git a/test/e2e_tests/utils/capp_revision_adapter.go b/test/e2e_tests/utils/capp_revision_adapter.go index 7c386678..fd3a37e1 100644 --- a/test/e2e_tests/utils/capp_revision_adapter.go +++ b/test/e2e_tests/utils/capp_revision_adapter.go @@ -9,9 +9,13 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" ) +var ( + Domain = cappv1alpha1.GroupVersion.Group + cappNameLabelKey = Domain + "/cappName" +) + const ( - cappNameLabelKey = "rcs.dana.io/cappName" - clientListLimit = 100 + clientListLimit = 100 ) // GetCappRevisions retrieves a list of CappRevision resources filtered by labels matching a specific Capp, returning the list and any error encountered.