-
Notifications
You must be signed in to change notification settings - Fork 71
feat: create new ttr for tiertemplate updates #1130
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
Merged
mfrancisc
merged 39 commits into
codeready-toolchain:master
from
mfrancisc:difftiertemplate
Feb 10, 2025
Merged
Changes from all commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
8d24435
remove spacebinding request migration controller
mfrancisc a25fb02
Merge branch 'master' of github.com:mfrancisc/host-operator
mfrancisc fd840c6
Merge branch 'master' of github.com:mfrancisc/host-operator
mfrancisc 0221d40
Merge branch 'master' of github.com:mfrancisc/host-operator
mfrancisc 0f4717d
Merge branch 'master' of github.com:mfrancisc/host-operator
mfrancisc 0977ae6
Merge branch 'master' of github.com:mfrancisc/host-operator
mfrancisc 7aa35c5
Merge branch 'master' of github.com:mfrancisc/host-operator
mfrancisc d09f3b8
Merge branch 'master' of github.com:mfrancisc/host-operator
mfrancisc 79c20ca
Merge branch 'master' of github.com:mfrancisc/host-operator
mfrancisc b21f39f
Merge branch 'master' of github.com:mfrancisc/host-operator
mfrancisc b0aea00
Merge branch 'master' of github.com:mfrancisc/host-operator
mfrancisc e9e35bc
Merge branch 'master' of github.com:mfrancisc/host-operator
mfrancisc 969562e
Merge branch 'master' of github.com:mfrancisc/host-operator
344bc04
Merge branch 'master' of github.com:mfrancisc/host-operator
ea71877
Merge branch 'master' of github.com:mfrancisc/host-operator
ef294f7
Merge branch 'master' of github.com:mfrancisc/host-operator
1f8d51d
Merge branch 'master' of github.com:mfrancisc/host-operator
b27cde7
Merge branch 'master' of github.com:mfrancisc/host-operator
bbb2689
Merge branch 'master' of github.com:mfrancisc/host-operator
ec51de9
Merge branch 'master' of github.com:mfrancisc/host-operator
cc3a260
Merge branch 'master' of github.com:mfrancisc/host-operator
14b1b26
Merge branch 'master' of github.com:mfrancisc/host-operator
28b6608
Merge branch 'master' of github.com:mfrancisc/host-operator
d844c83
Merge branch 'master' of github.com:mfrancisc/host-operator
a08f667
Merge branch 'master' of github.com:mfrancisc/host-operator
553b7fe
Merge branch 'master' of github.com:mfrancisc/host-operator
ff377e2
Create new TTR when tiertemplate changes
7df4dd2
Merge branch 'master' into difftiertemplate
mfrancisc e14adf7
Update controllers/nstemplatetier/nstemplatetier_controller.go
mfrancisc 7a94bf4
simplify code
8512d2a
Merge branch 'master' into difftiertemplate
mfrancisc d17e8d2
Merge branch 'master' into difftiertemplate
mfrancisc e8b1d8e
watch for tiertemplates
349c9c0
Merge branch 'master' into difftiertemplate
MatousJobanek f7b8c06
Update controllers/nstemplatetier/mapper.go
mfrancisc dc6be5b
Update controllers/nstemplatetier/mapper_test.go
mfrancisc 87ea29c
remove log config
bd5eb15
Merge branch 'master' into difftiertemplate
mfrancisc 18fcac6
Update controllers/nstemplatetier/mapper.go
mfrancisc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package nstemplatetier | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| toolchainv1alpha1 "github.com/codeready-toolchain/api/api/v1alpha1" | ||
|
|
||
| "k8s.io/apimachinery/pkg/types" | ||
| ctrl "sigs.k8s.io/controller-runtime" | ||
| runtimeclient "sigs.k8s.io/controller-runtime/pkg/client" | ||
| "sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
| ) | ||
|
|
||
| var mapperLog = ctrl.Log.WithName("MapTierTemplateToNSTemplateTier") | ||
|
|
||
| // MapTierTemplateToNSTemplateTier maps the TierTemplate to all the NSTemplateTiers that are referencing it. | ||
| func MapTierTemplateToNSTemplateTier(cl runtimeclient.Client) func(ctx context.Context, object runtimeclient.Object) []reconcile.Request { | ||
| return func(ctx context.Context, obj runtimeclient.Object) []reconcile.Request { | ||
| logger := mapperLog.WithValues("object-name", obj.GetName(), "object-kind", obj.GetObjectKind()) | ||
| nsTmplTierList := &toolchainv1alpha1.NSTemplateTierList{} | ||
| err := cl.List(context.TODO(), nsTmplTierList, | ||
| runtimeclient.InNamespace(obj.GetNamespace())) | ||
| if err != nil { | ||
| logger.Error(err, "unable to list NSTemplateTier") | ||
| return []reconcile.Request{} | ||
| } | ||
| if len(nsTmplTierList.Items) == 0 { | ||
| logger.Info("no NSTemplateTier found for an object") | ||
| return []reconcile.Request{} | ||
| } | ||
|
|
||
| req := make([]reconcile.Request, 0, len(nsTmplTierList.Items)) | ||
| for _, item := range nsTmplTierList.Items { | ||
| if len(item.Status.Revisions) == 0 { | ||
| // this tier doesn't use the template | ||
| continue | ||
| } | ||
| if _, inUse := item.Status.Revisions[obj.GetName()]; inUse { | ||
| // the tier uses this template | ||
| req = append(req, reconcile.Request{ | ||
| NamespacedName: types.NamespacedName{ | ||
| Namespace: item.Namespace, | ||
| Name: item.Name, | ||
| }, | ||
| }) | ||
| } | ||
| } | ||
| return req | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| package nstemplatetier_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| toolchainv1alpha1 "github.com/codeready-toolchain/api/api/v1alpha1" | ||
| "github.com/codeready-toolchain/host-operator/controllers/nstemplatetier" | ||
| tiertest "github.com/codeready-toolchain/host-operator/test/nstemplatetier" | ||
| "github.com/codeready-toolchain/toolchain-common/pkg/test" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| "k8s.io/apimachinery/pkg/types" | ||
| "sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
| ) | ||
|
|
||
| func TestMapTierTemplateToNSTemplateTier(t *testing.T) { | ||
| t.Run("should return the only NSTemplateTier referencing the TierTemplate", func(t *testing.T) { | ||
| // given | ||
| base1nsTier := tiertest.Base1nsTier(t, tiertest.CurrentBase1nsTemplates) | ||
| prepareTierWithRevisions(base1nsTier) | ||
| // and we have a tier without the revisions | ||
| otherTier := tiertest.AppStudioEnvTier(t, tiertest.AppStudioEnvTemplates) | ||
| cl := test.NewFakeClient(t, base1nsTier, otherTier) | ||
|
|
||
| // when | ||
| // for the simplicity of the test, we only try to map one tiertemplate | ||
| clusterResourceTierTemplate := createTierTemplate(t, "clusterresources", nil, base1nsTier.Name) | ||
| requests := nstemplatetier.MapTierTemplateToNSTemplateTier(cl)(context.TODO(), clusterResourceTierTemplate) | ||
|
|
||
| // then | ||
| require.Len(t, requests, 1) | ||
| assert.Contains(t, requests, newRequest(base1nsTier.Name)) | ||
| }) | ||
|
|
||
| t.Run("should return two NSTemplateTier when both are referencing the same TierTemplate", func(t *testing.T) { | ||
| // given | ||
| base1nsTier := tiertest.Base1nsTier(t, tiertest.CurrentBase1nsTemplates) | ||
| prepareTierWithRevisions(base1nsTier) | ||
| // we update the other tier to use the same revisions | ||
| otherTier := tiertest.AppStudioEnvTier(t, tiertest.AppStudioEnvTemplates) | ||
| prepareTierWithRevisions(otherTier) | ||
| cl := test.NewFakeClient(t, base1nsTier, otherTier) | ||
|
|
||
| // when | ||
| // for the simplicity of the test we only try to map one tiertemplate | ||
| clusterResourceTierTemplate := createTierTemplate(t, "clusterresources", nil, base1nsTier.Name) | ||
| requests := nstemplatetier.MapTierTemplateToNSTemplateTier(cl)(context.TODO(), clusterResourceTierTemplate) | ||
|
|
||
| // then | ||
| require.Len(t, requests, 2) | ||
| assert.Contains(t, requests, newRequest(base1nsTier.Name)) | ||
| assert.Contains(t, requests, newRequest(otherTier.Name)) | ||
| }) | ||
|
|
||
| t.Run("should return no NSTemplateTier when they don't use the TierTemplate", func(t *testing.T) { | ||
| // given | ||
| // both tiers are without revisions | ||
| base1nsTier := tiertest.Base1nsTier(t, tiertest.CurrentBase1nsTemplates) | ||
| otherTier := tiertest.AppStudioEnvTier(t, tiertest.AppStudioEnvTemplates) | ||
| prepareTierWithRevisions(otherTier) | ||
| delete(otherTier.Status.Revisions, "base1ns-clusterresources-123456new") | ||
| cl := test.NewFakeClient(t, base1nsTier, otherTier) | ||
|
|
||
| // when | ||
| // for the simplicity of the test, we only try to map one tiertemplate | ||
| clusterResourceTierTemplate := createTierTemplate(t, "clusterresources", nil, base1nsTier.Name) | ||
| requests := nstemplatetier.MapTierTemplateToNSTemplateTier(cl)(context.TODO(), clusterResourceTierTemplate) | ||
|
|
||
| // then | ||
| require.Empty(t, requests) | ||
| }) | ||
| } | ||
|
|
||
| func prepareTierWithRevisions(tier *toolchainv1alpha1.NSTemplateTier) { | ||
| initialRevisions := map[string]string{ | ||
| "base1ns-admin-123456new": "base1ns-admin-123456new-abcd", | ||
| "base1ns-clusterresources-123456new": "base1ns-clusterresources-123456new-abcd", | ||
| "base1ns-code-123456new": "base1ns-code-123456new-abcd", | ||
| "base1ns-dev-123456new": "base1ns-dev-123456new-abcd", | ||
| "base1ns-edit-123456new": "`base1ns-edit-123456new-abcd", | ||
| "base1ns-stage-123456new": "base1ns-stage-123456new-abcd", | ||
| "base1ns-viewer-123456new": "base1ns-viewer-123456new-abcd", | ||
| } | ||
| tier.Status.Revisions = initialRevisions | ||
| } | ||
|
|
||
| func newRequest(name string) reconcile.Request { | ||
| return reconcile.Request{ | ||
| NamespacedName: types.NamespacedName{ | ||
| Namespace: test.HostOperatorNs, | ||
| Name: name, | ||
| }, | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.