Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create tiertemplaterevision for each tiertemplate #1061

Open
wants to merge 40 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
fb8a690
wip add ttr tests
Nov 11, 2024
d66af28
test TierTemplate revisions are created when templateObjects are set
Nov 11, 2024
62a1a83
check for owner refernces
Nov 11, 2024
09fe90b
fix ownership check
Nov 12, 2024
edcf467
remove unused import
Nov 12, 2024
6b4deb8
Merge branch 'master' into createttr
mfrancisc Nov 12, 2024
b9e5e84
Merge branch 'master' into createttr
Nov 18, 2024
e91f57c
make linter happy
Nov 18, 2024
7e03339
Merge branch 'master' into createttr
MatousJobanek Nov 18, 2024
7d34843
add comments, check for ttr content, check revisions
Nov 20, 2024
b826d56
fix compilation
Nov 20, 2024
9cb5461
remove owner ref check
Nov 20, 2024
b50289c
avoid initializing twice
Nov 20, 2024
bdba663
remove second declaration
Nov 20, 2024
b4f334d
add ttr cleanup logic
Nov 21, 2024
dc38c17
fix linter
Nov 21, 2024
22f4e64
Merge branch 'master' into createttr
mfrancisc Nov 25, 2024
a0ec293
Merge branch 'master' into createttr
mfrancisc Nov 25, 2024
b1c59ab
Merge branch 'master' into createttr
mfrancisc Nov 26, 2024
a61de18
relax number of ttr check
Nov 26, 2024
7665e00
fix test
Nov 26, 2024
f73f65b
Merge branch 'master' into createttr
mfrancisc Nov 27, 2024
02797f9
handle parameters in TierTemplateRevision
Nov 28, 2024
f9069f6
Merge remote-tracking branch 'origin/createttr' into createttr
Nov 28, 2024
62ee614
fix test
Nov 30, 2024
1b44744
Merge branch 'master' into createttr
alexeykazakov Nov 30, 2024
aec5c1d
update api
Dec 2, 2024
d18c2a4
Merge remote-tracking branch 'origin/createttr' into createttr
Dec 2, 2024
8260a86
Merge branch 'master' into createttr
mfrancisc Dec 5, 2024
f818ea9
add comment about param assertion
Dec 5, 2024
3f36b81
merge
Dec 10, 2024
7dd6053
fix merge
Dec 12, 2024
f442e1b
Merge branch 'master' into createttr
mfrancisc Dec 19, 2024
1fc2e34
Merge branch 'master' into createttr
Jan 8, 2025
b24e0c0
address PR comments
Jan 8, 2025
ad40d0c
Merge remote-tracking branch 'origin/createttr' into createttr
Jan 8, 2025
be087d9
fix check
Jan 8, 2025
38bb9d6
fix assert check
Jan 8, 2025
6836296
cleanup unused functions
Jan 8, 2025
e006742
Merge branch 'master' into createttr
Jan 9, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions test/e2e/parallel/nstemplatetier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
. "github.com/codeready-toolchain/toolchain-e2e/testsupport/space"
"github.com/codeready-toolchain/toolchain-e2e/testsupport/tiers"
"github.com/codeready-toolchain/toolchain-e2e/testsupport/wait"
apiwait "k8s.io/apimachinery/pkg/util/wait"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -378,6 +379,41 @@ func TestFeatureToggles(t *testing.T) {
})
}

func TestTierTemplateRevision(t *testing.T) {
mfrancisc marked this conversation as resolved.
Show resolved Hide resolved
t.Parallel()

awaitilities := WaitForDeployments(t)
hostAwait := awaitilities.Host()

baseTier, err := hostAwait.WaitForNSTemplateTier(t, "base1ns")
require.NoError(t, err)

// create new NSTemplateTiers (derived from `base`)
// for the tiertemplaterevisions to be created the tiertemplates need to have template objects populated
updateTierTemplateObjects := func(template *toolchainv1alpha1.TierTemplate) error {
template.Spec.TemplateObjects = template.Spec.Template.Objects
return nil
}
namespaceResourcesWithTemplateObjects := tiers.WithNamespaceResources(t, baseTier, updateTierTemplateObjects)
clusterResourcesWithTemplateObjects := tiers.WithClusterResources(t, baseTier, updateTierTemplateObjects)
spaceRolesWithTemplateObjects := tiers.WithSpaceRoles(t, baseTier, updateTierTemplateObjects)
tiers.CreateCustomNSTemplateTier(t, hostAwait, "ttr", baseTier, namespaceResourcesWithTemplateObjects, clusterResourcesWithTemplateObjects, spaceRolesWithTemplateObjects)

// verify the counters in the status.history for 'tierUsingTierTemplateRevisions' tier
// and verify that TierTemplateRevision CRs were created, since all the tiertemplates now have templateObjects field populated
verifyStatus(t, hostAwait, "ttr", 1)
mfrancisc marked this conversation as resolved.
Show resolved Hide resolved
// check the expected total number of ttr matches
err = apiwait.PollUntilContextTimeout(context.TODO(), hostAwait.RetryInterval, hostAwait.Timeout, true, func(ctx context.Context) (done bool, err error) {
objs := &toolchainv1alpha1.TierTemplateRevisionList{}
if err := hostAwait.Client.List(ctx, objs, client.InNamespace(hostAwait.Namespace)); err != nil {
return false, err
}
require.Len(t, objs.Items, 3) // expect one TTR per each tiertemplate ( clusterresource, namespace and spacerole )
return true, nil
})
require.NoError(t, err)
}

func withClusterRoleBindings(t *testing.T, otherTier *toolchainv1alpha1.NSTemplateTier, feature string) tiers.CustomNSTemplateTierModifier {
clusterRB := getCRBforFeature(t, feature) // This is the ClusterRoleBinding for the desired feature
noiseCRB := getCRBforFeature(t, unknownFeature) // This is a noise CRB for unknown/disabled feature. To be used to check that this CRB is never created.
Expand Down
8 changes: 4 additions & 4 deletions testsupport/tiers/tier_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ func WithClusterResources(t *testing.T, otherTier *toolchainv1alpha1.NSTemplateT
}
}

func WithNamespaceResources(t *testing.T, otherTier *toolchainv1alpha1.NSTemplateTier) CustomNSTemplateTierModifier {
func WithNamespaceResources(t *testing.T, otherTier *toolchainv1alpha1.NSTemplateTier, modifiers ...TierTemplateModifier) CustomNSTemplateTierModifier {
return func(hostAwait *HostAwaitility, tier *CustomNSTemplateTier) error {
tier.NamespaceResourcesTier = otherTier
// configure the "wrapped" NSTemplateTier
tier.Spec.Namespaces = make([]toolchainv1alpha1.NSTemplateTierNamespace, len(otherTier.Spec.Namespaces))
for i, def := range otherTier.Spec.Namespaces {
tmplRef, err := duplicateTierTemplate(t, hostAwait, otherTier.Namespace, tier.Name, def.TemplateRef)
tmplRef, err := duplicateTierTemplate(t, hostAwait, otherTier.Namespace, tier.Name, def.TemplateRef, modifiers...)
if err != nil {
return err
}
Expand All @@ -63,13 +63,13 @@ func WithNamespaceResources(t *testing.T, otherTier *toolchainv1alpha1.NSTemplat
}
}

func WithSpaceRoles(t *testing.T, otherTier *toolchainv1alpha1.NSTemplateTier) CustomNSTemplateTierModifier {
func WithSpaceRoles(t *testing.T, otherTier *toolchainv1alpha1.NSTemplateTier, modifiers ...TierTemplateModifier) CustomNSTemplateTierModifier {
return func(hostAwait *HostAwaitility, tier *CustomNSTemplateTier) error {
tier.SpaceRolesTier = otherTier
// configure the "wrapped" NSTemplateTier
tier.Spec.SpaceRoles = make(map[string]toolchainv1alpha1.NSTemplateTierSpaceRole, len(otherTier.Spec.SpaceRoles))
for name, def := range otherTier.Spec.SpaceRoles {
tmplRef, err := duplicateTierTemplate(t, hostAwait, otherTier.Namespace, tier.Name, def.TemplateRef)
tmplRef, err := duplicateTierTemplate(t, hostAwait, otherTier.Namespace, tier.Name, def.TemplateRef, modifiers...)
if err != nil {
return err
}
Expand Down
145 changes: 143 additions & 2 deletions testsupport/wait/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -1023,18 +1023,67 @@ func (a *HostAwaitility) WaitForNSTemplateTierAndCheckTemplates(t *testing.T, na
if ns.TemplateRef == "" {
return nil, fmt.Errorf("missing 'templateRef' in namespace #%d in NSTemplateTier '%s'", i, tier.Name)
}
if _, err := a.WaitForTierTemplate(t, ns.TemplateRef); err != nil {
tierTemplateNamespaces, err := a.WaitForTierTemplate(t, ns.TemplateRef)
if err != nil {
return nil, err
}
// if the tier template supports Tier Template Revisions then let's check those
if tierTemplateNamespaces.Spec.TemplateObjects != nil {
if _, err := a.WaitForTierTemplateRevision(t, tierTemplateNamespaces.GetName(), UntilTierTemplateRevisionsHasOwnerReferences([]metav1.OwnerReference{
mfrancisc marked this conversation as resolved.
Show resolved Hide resolved
{
APIVersion: "toolchain.dev.openshift.com/v1alpha1", // for some reason, the apiversion and kind are empty on the CR
Kind: "TierTemplate",
Name: tierTemplateNamespaces.Name,
},
mfrancisc marked this conversation as resolved.
Show resolved Hide resolved
})); err != nil {
return nil, err
}
}
}
if tier.Spec.ClusterResources != nil {
if tier.Spec.ClusterResources.TemplateRef == "" {
return nil, fmt.Errorf("missing 'templateRef' for the cluster resources in NSTemplateTier '%s'", tier.Name)
}
if _, err := a.WaitForTierTemplate(t, tier.Spec.ClusterResources.TemplateRef); err != nil {
tierTemplateClusterResources, err := a.WaitForTierTemplate(t, tier.Spec.ClusterResources.TemplateRef)
if err != nil {
return nil, err
}
// if the tier template supports Tier Template Revisions then let's check those
if tierTemplateClusterResources.Spec.TemplateObjects != nil {
if _, err := a.WaitForTierTemplateRevision(t, tierTemplateClusterResources.GetName(), UntilTierTemplateRevisionsHasOwnerReferences([]metav1.OwnerReference{
{
APIVersion: "toolchain.dev.openshift.com/v1alpha1", // for some reason, the apiversion and kind are empty on the CR
Kind: "TierTemplate",
Name: tierTemplateClusterResources.Name,
},
})); err != nil {
return nil, err
}
}
}

for _, r := range tier.Spec.SpaceRoles {
if r.TemplateRef == "" {
return nil, fmt.Errorf("missing 'templateRef' in spaceRole %s in NSTemplateTier '%s'", r.TemplateRef, tier.Name)
}
tierTemplateSpaceRoles, err := a.WaitForTierTemplate(t, r.TemplateRef)
if err != nil {
return nil, err
}
// if the tier template supports Tier Template Revisions then let's check those
if tierTemplateSpaceRoles.Spec.TemplateObjects != nil {
if _, err := a.WaitForTierTemplateRevision(t, r.TemplateRef, UntilTierTemplateRevisionsHasOwnerReferences([]metav1.OwnerReference{
{
APIVersion: "toolchain.dev.openshift.com/v1alpha1", // for some reason, the apiversion and kind are empty on the CR
Kind: "TierTemplate",
Name: tierTemplateSpaceRoles.Name,
},
})); err != nil {
return nil, err
}
}
}

return tier, err
}

Expand All @@ -1061,6 +1110,98 @@ func (a *HostAwaitility) WaitForTierTemplate(t *testing.T, name string) (*toolch
return tierTemplate, err
}

// TierTemplateRevisionWaitCriterion a struct to compare with an expected TierTemplateRevision
type TierTemplateRevisionWaitCriterion struct {
Match func(*toolchainv1alpha1.TierTemplateRevision) bool
Diff func(*toolchainv1alpha1.TierTemplateRevision) string
}

func matchTierTemplateRevisionWaitCriterion(actual *toolchainv1alpha1.TierTemplateRevision, criteria ...TierTemplateRevisionWaitCriterion) bool {
for _, c := range criteria {
// if at least one criteria does not match, keep waiting
if !c.Match(actual) {
return false
}
}
return true
}

func (a *HostAwaitility) printTierTemplateRevisionWaitCriterionDiffs(t *testing.T, actual *toolchainv1alpha1.TierTemplateRevision, criteria ...TierTemplateRevisionWaitCriterion) {
buf := &strings.Builder{}
if actual == nil {
buf.WriteString("failed to find TierTemplateRevision\n")
} else {
buf.WriteString("failed to find TierTemplateRevision with matching criteria:\n")
buf.WriteString("actual:\n")
y, _ := StringifyObject(actual)
buf.Write(y)
buf.WriteString("\n----\n")
buf.WriteString("diffs:\n")
for _, c := range criteria {
if !c.Match(actual) {
buf.WriteString(c.Diff(actual))
buf.WriteString("\n")
}
}
}
// include also all TierTemplateRevisions in the host namespace, to help troubleshooting
a.listAndPrint(t, "TierTemplateRevisions", a.Namespace, &toolchainv1alpha1.TierTemplateRevisionList{})

t.Log(buf.String())
}

// WaitForTierTemplateRevision waits until a TierTemplateRevision with the given labels to exists
// Returns an error if the resource did not exist (or something wrong happened)
func (a *HostAwaitility) WaitForTierTemplateRevision(t *testing.T, templateRef string, criteria ...TierTemplateRevisionWaitCriterion) (*toolchainv1alpha1.TierTemplateRevision, error) { // nolint:unparam
ttr := &toolchainv1alpha1.TierTemplateRevision{}
t.Logf("waiting until TierTemplateRevision for templateRef '%s' exists in namespace '%s'...", templateRef, a.Namespace)
err := wait.PollUntilContextTimeout(context.TODO(), a.RetryInterval, a.Timeout, true, func(ctx context.Context) (done bool, err error) {
objs := &toolchainv1alpha1.TierTemplateRevisionList{}
err = a.Client.List(ctx, objs, client.InNamespace(a.Namespace), client.MatchingLabels(map[string]string{
toolchainv1alpha1.TemplateRefLabelKey: templateRef,
}))
// no match found, print the diffs
if err != nil {
return false, err
}
require.Len(t, objs.Items, 1)
ttr = &objs.Items[0]
mfrancisc marked this conversation as resolved.
Show resolved Hide resolved
return matchTierTemplateRevisionWaitCriterion(ttr, criteria...), nil
})
// log message if an error occurred
if err != nil {
a.printTierTemplateRevisionWaitCriterionDiffs(t, ttr, criteria...)
}
return ttr, err
}

// UntilTierTemplateRevisionsHasOwnerReferences verify that the TierTemplateRevision has the specified owner references
func UntilTierTemplateRevisionsHasOwnerReferences(references []metav1.OwnerReference) TierTemplateRevisionWaitCriterion {
return TierTemplateRevisionWaitCriterion{
Match: func(actual *toolchainv1alpha1.TierTemplateRevision) bool {
actualReferences := actual.GetOwnerReferences()
for _, actualRef := range actualReferences {
found := false
for _, expectedRef := range references {
if expectedRef.Name == actualRef.Name &&
expectedRef.APIVersion == actualRef.APIVersion &&
expectedRef.Kind == actualRef.Kind {
found = true
break
}
}
if !found {
return false
}
}
return len(references) == len(actualReferences)
},
Diff: func(actual *toolchainv1alpha1.TierTemplateRevision) string {
return fmt.Sprintf("unable to find expected owner references: %v", references)
},
}
}

// NSTemplateTierWaitCriterion a struct to compare with an expected NSTemplateTier
type NSTemplateTierWaitCriterion struct {
Match func(*toolchainv1alpha1.NSTemplateTier) bool
Expand Down
Loading