Skip to content

Commit

Permalink
Merge pull request dana-team#183 from dana-team/fix/updated-annotation
Browse files Browse the repository at this point in the history
feat: copy all annotations with dana domain from capp to capprevision
  • Loading branch information
dana-prow-ci[bot] authored Jul 18, 2024
2 parents 49d0bbe + 7c0cd21 commit 4bcde5a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
25 changes: 21 additions & 4 deletions internal/kinds/capprevision/adapters/capprevision_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"math/rand"
"strconv"
"strings"
"time"

cappv1alpha1 "github.com/dana-team/container-app-operator/api/v1alpha1"
Expand All @@ -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
Expand All @@ -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
}
Expand All @@ -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{
Expand Down
24 changes: 24 additions & 0 deletions test/e2e_tests/capp_revision_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import (
. "github.com/onsi/gomega"
)

var (
testAnnotationKey = utilst.Domain + "/test"
testAnnotationValue = "test"
)

const (
moreThanRevisionsToKeep = 12
revisionsToKeep = 10
Expand Down Expand Up @@ -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")

})
})
8 changes: 6 additions & 2 deletions test/e2e_tests/utils/capp_revision_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 4bcde5a

Please sign in to comment.