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 28 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
28 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
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
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/fatih/color v1.15.0
github.com/ghodss/yaml v1.0.0
github.com/gofrs/uuid v3.3.0+incompatible
github.com/google/go-cmp v0.5.9
github.com/google/go-cmp v0.6.0
github.com/gorilla/websocket v1.4.2
github.com/gosuri/uiprogress v0.0.1
github.com/gosuri/uitable v0.0.4
Expand All @@ -31,7 +31,10 @@ require (
sigs.k8s.io/controller-runtime v0.15.0
)

require github.com/google/uuid v1.6.0
require (
github.com/google/uuid v1.6.0
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f
)

require (
github.com/BurntSushi/toml v1.2.1 // indirect
Expand Down
7 changes: 5 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,9 @@ github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-github/v52 v52.0.0 h1:uyGWOY+jMQ8GVGSX8dkSwCzlehU3WfdxQ7GweO/JP7M=
github.com/google/go-github/v52 v52.0.0/go.mod h1:WJV6VEEUPuMo5pXqqa2ZCZEdbQqua4zAk2MZTIo+m+4=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
Expand Down Expand Up @@ -746,6 +747,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f h1:XdNn9LlyWAhLVp6P/i8QYBW+hlyhrhei9uErw2B5GJo=
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f/go.mod h1:D5SMRVC3C2/4+F/DB1wZsLRnSNimn2Sp/NPsCrsv8ak=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
Expand Down Expand Up @@ -1041,7 +1044,7 @@ golang.org/x/tools v0.1.2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/tools v0.9.1 h1:8WMNJAz3zrtPmnYC7ISf5dEn3MT0gY7jBJfw27yrrLo=
golang.org/x/tools v0.27.0 h1:qEKojBykQkQ4EynWy4S8Weg69NumxKdn40Fce3uc/8o=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
59 changes: 59 additions & 0 deletions test/e2e/parallel/nstemplatetier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

v1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"

"github.com/gofrs/uuid"
Expand All @@ -20,6 +21,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 +380,63 @@ func TestFeatureToggles(t *testing.T) {
})
}

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

// given
awaitilities := WaitForDeployments(t)
hostAwait := awaitilities.Host()
// we create new NSTemplateTiers (derived from `base`)
baseTier, err := hostAwait.WaitForNSTemplateTier(t, "base1ns")
require.NoError(t, err)
// for the tiertemplaterevisions to be created the tiertemplates need to have template objects populated
// we add the RawExtension objects to the TemplateObjects field
ns := unstructured.Unstructured{
Object: map[string]interface{}{
"kind": "Namespace",
"metadata": map[string]interface{}{
"name": "{{.SPACE_NAME}}",
},
},
}
rawTemplateObjects := []runtime.RawExtension{{Object: &ns}}
updateTierTemplateObjects := func(template *toolchainv1alpha1.TierTemplate) error {
template.Spec.TemplateObjects = rawTemplateObjects
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, tiers.WithParameter("SPACE_NAME", "janedoe"))

// when
// we 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
_, err = hostAwait.WaitForNSTemplateTierAndCheckTemplates(t, "ttr", wait.HasStatusTierTemplateRevisions([]string{
fmt.Sprintf("ttrfrom%s", baseTier.Spec.Namespaces[0].TemplateRef), // check that the revision field is set using the expected tierTemplate refs as keys
fmt.Sprintf("ttrfrom%s", baseTier.Spec.SpaceRoles["admin"].TemplateRef), // we can safely use the template refs from base tier since the custom tier was created from base one.
fmt.Sprintf("ttrfrom%s", baseTier.Spec.ClusterResources.TemplateRef),
}))
require.NoError(t, err)

// then
// 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)
// we check that the TTR content has the parameters replaced with values from the NSTemplateTier
for _, obj := range objs.Items {
assert.Contains(t, string(obj.Spec.TemplateObjects[0].Raw), "janedoe") // the object should have the namespace name replaced
assert.NotContains(t, string(obj.Spec.TemplateObjects[0].Raw), ".SPACE_NAME") // the object should NOT contain the variable anymore
}
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
23 changes: 19 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 All @@ -81,6 +81,21 @@ func WithSpaceRoles(t *testing.T, otherTier *toolchainv1alpha1.NSTemplateTier) C
}
}

func WithParameter(name, value string) CustomNSTemplateTierModifier {
return func(hostAwait *HostAwaitility, tier *CustomNSTemplateTier) error {
if tier.Spec.Parameters == nil {
tier.Spec.Parameters = []toolchainv1alpha1.Parameter{}
}
tier.Spec.Parameters = append(tier.Spec.Parameters,
toolchainv1alpha1.Parameter{
Name: name,
Value: value,
},
)
return nil
}
}

// CreateCustomNSTemplateTier creates a custom tier.
// If no modifiers provided then the new tier will use copies of the baseTier cluster, namespace and space roles templates
// without any modifications.
Expand Down
131 changes: 129 additions & 2 deletions testsupport/wait/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -1023,18 +1023,61 @@ 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 {
ttrName, found := tier.Status.Revisions[tierTemplateNamespaces.GetName()]
if !found {
return nil, fmt.Errorf("missing revision for TierTemplate %s in NSTemplateTier '%s'", tierTemplateNamespaces.GetName(), tier.Name)
}
if _, err := a.WaitForTierTemplateRevision(t, ttrName); 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 {
ttrName, found := tier.Status.Revisions[tierTemplateClusterResources.GetName()]
if !found {
return nil, fmt.Errorf("missing revision for TierTemplate %s in NSTemplateTier '%s'", tierTemplateClusterResources.GetName(), tier.Name)
}
if _, err := a.WaitForTierTemplateRevision(t, ttrName); 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 {
ttrName, found := tier.Status.Revisions[tierTemplateSpaceRoles.GetName()]
if !found {
return nil, fmt.Errorf("missing revision for TierTemplate %s in NSTemplateTier '%s'", tierTemplateSpaceRoles.GetName(), tier.Name)
}
if _, err := a.WaitForTierTemplateRevision(t, ttrName); err != nil {
return nil, err
}
}
}

return tier, err
}

Expand All @@ -1061,6 +1104,70 @@ 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, ttrName string, criteria ...TierTemplateRevisionWaitCriterion) (*toolchainv1alpha1.TierTemplateRevision, error) { // nolint:unparam
ttr := &toolchainv1alpha1.TierTemplateRevision{}
t.Logf("waiting until TierTemplateRevision with name '%s' exists in namespace '%s'...", ttrName, a.Namespace)
err := wait.PollUntilContextTimeout(context.TODO(), a.RetryInterval, a.Timeout, true, func(ctx context.Context) (done bool, err error) {
ttr := &toolchainv1alpha1.TierTemplateRevision{}
mfrancisc marked this conversation as resolved.
Show resolved Hide resolved
err = a.Client.Get(ctx, types.NamespacedName{
Namespace: a.Namespace,
Name: ttrName,
}, ttr)
// no match found, print the diffs
if err != nil {
return false, err
}
return matchTierTemplateRevisionWaitCriterion(ttr, criteria...), nil
})
// log message if an error occurred
if err != nil {
a.printTierTemplateRevisionWaitCriterionDiffs(t, ttr, criteria...)
}
return ttr, err
}

// NSTemplateTierWaitCriterion a struct to compare with an expected NSTemplateTier
type NSTemplateTierWaitCriterion struct {
Match func(*toolchainv1alpha1.NSTemplateTier) bool
Expand Down Expand Up @@ -1131,6 +1238,26 @@ func UntilNSTemplateTierStatusUpdates(expected int) NSTemplateTierWaitCriterion
}
}

// HasStatusTierTemplateRevisions verifies revisions for the given TierTemplates are set in the `NSTemplateTier.Status.Revisions`
func HasStatusTierTemplateRevisions(revisions []string) NSTemplateTierWaitCriterion {
return NSTemplateTierWaitCriterion{
Match: func(actual *toolchainv1alpha1.NSTemplateTier) bool {
if len(actual.Status.Revisions) != len(revisions) {
return false
}
for _, tierTemplateRef := range revisions {
if _, found := actual.Status.Revisions[tierTemplateRef]; !found {
return false
}
}
return true
},
Diff: func(actual *toolchainv1alpha1.NSTemplateTier) string {
return fmt.Sprintf("expected revision keys %v not found in: %v", revisions, actual.Status.Revisions)
},
}
}

// HasNoTemplateRefWithSuffix checks that ALL namespaces' `TemplateRef` doesn't have the suffix
func HasNoTemplateRefWithSuffix(suffix string) NSTemplateTierSpecMatcher {
return NSTemplateTierSpecMatcher{
Expand Down
Loading