Skip to content

Commit 4d9e730

Browse files
authored
Deploy the bundled NSTemplateTiers using SSA (#1185)
1 parent 2b8cdd5 commit 4d9e730

File tree

4 files changed

+13
-39
lines changed

4 files changed

+13
-39
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/codeready-toolchain/host-operator
33
require (
44
cloud.google.com/go/recaptchaenterprise/v2 v2.13.0
55
github.com/codeready-toolchain/api v0.0.0-20250605152105-383ffe6cac27
6-
github.com/codeready-toolchain/toolchain-common v0.0.0-20250506093954-2b65ad3a2e12
6+
github.com/codeready-toolchain/toolchain-common v0.0.0-20250708104334-c3ec13e2e8da
77
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
88
github.com/go-logr/logr v1.4.2
99
github.com/gofrs/uuid v4.4.0+incompatible

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ github.com/cloudflare/circl v1.6.1/go.mod h1:uddAzsPgqdMAYatqJ0lsjX1oECcQLIlRpzZ
4040
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
4141
github.com/codeready-toolchain/api v0.0.0-20250605152105-383ffe6cac27 h1:g1ivSPPHTC96RHp8S/gRmqODWgoHyivq+/d5kSI0pEs=
4242
github.com/codeready-toolchain/api v0.0.0-20250605152105-383ffe6cac27/go.mod h1:20258od6i5+jP406Z76YaI2ow/vc7URwsDU2bokpkRE=
43-
github.com/codeready-toolchain/toolchain-common v0.0.0-20250506093954-2b65ad3a2e12 h1:w54sojJJ8PsHZzK1mC+/EUBrQ9F2sC/k7JUVc8LSqK4=
44-
github.com/codeready-toolchain/toolchain-common v0.0.0-20250506093954-2b65ad3a2e12/go.mod h1:TrMvD0sP69wI6Rouzfs7OsOUSj4CGn/ZiIdiDBAFQjk=
43+
github.com/codeready-toolchain/toolchain-common v0.0.0-20250708104334-c3ec13e2e8da h1:5V3vJKbhDViUwC5M3jN57Us4lFls/wbtV9xmSsXjONQ=
44+
github.com/codeready-toolchain/toolchain-common v0.0.0-20250708104334-c3ec13e2e8da/go.mod h1:mjwK6D+gH299P2CEUgliLEwJs4K+Cx++Bns/8rYkxUU=
4545
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
4646
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4747
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

pkg/templates/nstemplatetiers/nstemplatetier_generator.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
commonclient "github.com/codeready-toolchain/toolchain-common/pkg/client"
1717
"github.com/codeready-toolchain/toolchain-common/pkg/template/nstemplatetiers"
1818
"gopkg.in/yaml.v2"
19-
apierrors "k8s.io/apimachinery/pkg/api/errors"
2019
"k8s.io/apimachinery/pkg/runtime"
2120
runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"
2221
)
@@ -39,19 +38,13 @@ func SyncResources(ctx context.Context, s *runtime.Scheme, client runtimeclient.
3938
var bundledTierKeys []runtimeclient.ObjectKey
4039

4140
// initialize tier generator, loads templates from assets
42-
err = nstemplatetiers.GenerateTiers(s, func(toEnsure runtimeclient.Object, canUpdate bool, _ string) (bool, error) {
41+
err = nstemplatetiers.GenerateTiers(s, func(toEnsure runtimeclient.Object, _ string) error {
4342
commonclient.MergeAnnotations(toEnsure, bundledAnnotation)
4443

4544
bundledTierKeys = append(bundledTierKeys, runtimeclient.ObjectKeyFromObject(toEnsure))
4645

47-
if !canUpdate {
48-
if err := client.Create(ctx, toEnsure); err != nil && !apierrors.IsAlreadyExists(err) {
49-
return false, err
50-
}
51-
return true, nil
52-
}
53-
applyCl := commonclient.NewApplyClient(client)
54-
return applyCl.ApplyObject(ctx, toEnsure, commonclient.ForceUpdate(true))
46+
applyCl := commonclient.NewSSAApplyClient(client, constants.HostOperatorFieldManager)
47+
return applyCl.ApplyObject(ctx, toEnsure)
5548
}, namespace, metadata, files)
5649
if err != nil {
5750
return err

pkg/templates/nstemplatetiers/nstemplatetier_generator_test.go

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -112,53 +112,34 @@ func TestSyncResourcesWitProdAssets(t *testing.T) {
112112
t.Run("failures", func(t *testing.T) {
113113
namespace := "host-operator" + uuid.Must(uuid.NewV4()).String()[:7]
114114
t.Run("nstemplatetiers", func(t *testing.T) {
115-
t.Run("failed to create nstemplatetiers", func(t *testing.T) {
115+
t.Run("failed to patch nstemplatetiers", func(t *testing.T) {
116116
// given
117117
clt := commontest.NewFakeClient(t)
118-
clt.MockCreate = func(ctx context.Context, obj runtimeclient.Object, opts ...runtimeclient.CreateOption) error {
118+
clt.MockPatch = func(ctx context.Context, obj runtimeclient.Object, patch runtimeclient.Patch, opts ...runtimeclient.PatchOption) error {
119119
if obj.GetObjectKind().GroupVersionKind().Kind == "NSTemplateTier" && obj.GetName() == "base" {
120120
// simulate a client/server error
121121
return errors.Errorf("an error")
122122
}
123-
return clt.Client.Create(ctx, obj, opts...)
123+
return commontest.Patch(ctx, clt, obj, patch, opts...)
124124
}
125125
// when
126126
err := nstemplatetiers.SyncResources(context.TODO(), s, clt, namespace)
127127
// then
128128
require.Error(t, err)
129-
assert.Regexp(t, "unable to create NSTemplateTiers: unable to create or update the 'base' NSTemplateTier: unable to create resource of kind: NSTemplateTier, version: v1alpha1: an error", err.Error())
130-
})
131-
132-
t.Run("failed to update nstemplatetiers", func(t *testing.T) {
133-
// given
134-
// initialize the client with an existing `advanced` NSTemplatetier
135-
clt := commontest.NewFakeClient(t, tiertest.TierInNamespace(t, "advanced", namespace, toolchainv1alpha1.NSTemplateTierSpec{}))
136-
clt.MockUpdate = func(ctx context.Context, obj runtimeclient.Object, opts ...runtimeclient.UpdateOption) error {
137-
if obj.GetObjectKind().GroupVersionKind().Kind == "NSTemplateTier" && obj.GetName() == "advanced" {
138-
// simulate a client/server error
139-
return errors.Errorf("an error")
140-
}
141-
return clt.Client.Update(ctx, obj, opts...)
142-
}
143-
144-
// when
145-
err := nstemplatetiers.SyncResources(context.TODO(), s, clt, namespace)
146-
// then
147-
require.Error(t, err)
148-
assert.Contains(t, err.Error(), "unable to create NSTemplateTiers: unable to create or update the 'advanced' NSTemplateTier: unable to create resource of kind: NSTemplateTier, version: v1alpha1: unable to update the resource")
129+
assert.Regexp(t, "unable to create NSTemplateTiers: unable to create or update the 'base' NSTemplateTier: unable to patch 'toolchain.dev.openshift.com/v1alpha1, Kind=NSTemplateTier' called 'base' in namespace '[a-zA-Z0-9-]+': an error", err.Error())
149130
})
150131
})
151132

152133
t.Run("tiertemplates", func(t *testing.T) {
153-
t.Run("failed to create nstemplatetiers", func(t *testing.T) {
134+
t.Run("failed to create tiertemplate", func(t *testing.T) {
154135
// given
155136
clt := commontest.NewFakeClient(t)
156-
clt.MockCreate = func(ctx context.Context, obj runtimeclient.Object, opts ...runtimeclient.CreateOption) error {
137+
clt.MockPatch = func(ctx context.Context, obj runtimeclient.Object, patch runtimeclient.Patch, opts ...runtimeclient.PatchOption) error {
157138
if strings.HasPrefix(obj.GetName(), "base1ns-dev-") {
158139
// simulate a client/server error
159140
return errors.Errorf("an error")
160141
}
161-
return clt.Client.Create(ctx, obj, opts...)
142+
return commontest.Patch(ctx, clt, obj, patch, opts...)
162143
}
163144
// when
164145
err := nstemplatetiers.SyncResources(context.TODO(), s, clt, namespace)

0 commit comments

Comments
 (0)