Skip to content

Commit 96bfddf

Browse files
committed
check that the bundled resources are deployed using SSA
1 parent 951ae7b commit 96bfddf

File tree

4 files changed

+16
-51
lines changed

4 files changed

+16
-51
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,5 @@ require (
133133
go 1.22.0
134134

135135
toolchain go1.22.12
136+
137+
replace github.com/codeready-toolchain/toolchain-common => github.com/metlos/toolchain-common v0.0.0-20250702122526-cf062f90f967

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ 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=
4543
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
4644
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4745
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -181,6 +179,8 @@ github.com/mailgun/mailgun-go/v4 v4.8.1 h1:1+MdKakJuXnW2JJDbyPdO1ngAANOyHyVPxQvF
181179
github.com/mailgun/mailgun-go/v4 v4.8.1/go.mod h1:FJlF9rI5cQT+mrwujtJjPMbIVy3Ebor9bKTVsJ0QU40=
182180
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
183181
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
182+
github.com/metlos/toolchain-common v0.0.0-20250702122526-cf062f90f967 h1:fIIjmLl4l1NlRPcC9joEitiB/5s19F4cfg1AnxukYEQ=
183+
github.com/metlos/toolchain-common v0.0.0-20250702122526-cf062f90f967/go.mod h1:mjwK6D+gH299P2CEUgliLEwJs4K+Cx++Bns/8rYkxUU=
184184
github.com/migueleliasweb/go-github-mock v0.0.18 h1:0lWt9MYmZQGnQE2rFtjlft/YtD6hzxuN6JJRFpujzEI=
185185
github.com/migueleliasweb/go-github-mock v0.0.18/go.mod h1:CcgXcbMoRnf3rRVHqGssuBquZDIcaplxL2W6G+xs7kM=
186186
github.com/mitchellh/copystructure v1.0.0 h1:Laisrj+bAB6b/yJwB5Bt3ITZhGJdqmxquMKeZ+mmkFQ=

pkg/templates/nstemplatetiers/nstemplatetier_generator.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import (
88
"strings"
99

1010
"github.com/codeready-toolchain/host-operator/deploy"
11+
"github.com/codeready-toolchain/host-operator/pkg/constants"
1112
"github.com/codeready-toolchain/host-operator/pkg/templates"
1213
commonclient "github.com/codeready-toolchain/toolchain-common/pkg/client"
1314
"github.com/codeready-toolchain/toolchain-common/pkg/template/nstemplatetiers"
1415
"github.com/pkg/errors"
1516
"gopkg.in/yaml.v2"
16-
apierrors "k8s.io/apimachinery/pkg/api/errors"
1717
"k8s.io/apimachinery/pkg/runtime"
1818
runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"
1919
)
@@ -23,22 +23,15 @@ const NsTemplateTierRootDir = "templates/nstemplatetiers"
2323
// CreateOrUpdateResources generates the NSTemplateTier resources from the cluster resource template and namespace templates,
2424
// then uses the manager's client to create or update the resources on the cluster.
2525
func CreateOrUpdateResources(ctx context.Context, s *runtime.Scheme, client runtimeclient.Client, namespace string) error {
26-
2726
metadata, files, err := LoadFiles(deploy.NSTemplateTiersFS, NsTemplateTierRootDir)
2827
if err != nil {
2928
return err
3029
}
3130

3231
// initialize tier generator, loads templates from assets
33-
return nstemplatetiers.GenerateTiers(s, func(toEnsure runtimeclient.Object, canUpdate bool, _ string) (bool, error) {
34-
if !canUpdate {
35-
if err := client.Create(ctx, toEnsure); err != nil && !apierrors.IsAlreadyExists(err) {
36-
return false, err
37-
}
38-
return true, nil
39-
}
40-
applyCl := commonclient.NewApplyClient(client)
41-
return applyCl.ApplyObject(ctx, toEnsure, commonclient.ForceUpdate(true))
32+
return nstemplatetiers.GenerateTiers(s, func(toEnsure runtimeclient.Object, _ string) error {
33+
applyCl := commonclient.NewSSAApplyClient(client, constants.HostOperatorFieldManager)
34+
return applyCl.ApplyObject(ctx, toEnsure)
4235
}, namespace, metadata, files)
4336
}
4437

pkg/templates/nstemplatetiers/nstemplatetier_generator_test.go

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ import (
1010
"github.com/codeready-toolchain/host-operator/deploy"
1111
"github.com/codeready-toolchain/host-operator/pkg/apis"
1212
"github.com/codeready-toolchain/host-operator/pkg/templates/nstemplatetiers"
13+
"github.com/codeready-toolchain/toolchain-common/pkg/test"
1314
commontest "github.com/codeready-toolchain/toolchain-common/pkg/test"
1415
"github.com/gofrs/uuid"
1516
"github.com/pkg/errors"
1617

1718
"github.com/stretchr/testify/assert"
1819
"github.com/stretchr/testify/require"
19-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2020
"k8s.io/client-go/kubernetes/scheme"
2121
runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"
2222
logf "sigs.k8s.io/controller-runtime/pkg/log"
@@ -51,7 +51,6 @@ func roles(_ string) []string {
5151
}
5252

5353
func TestCreateOrUpdateResourcesWitProdAssets(t *testing.T) {
54-
5554
s := scheme.Scheme
5655
err := apis.AddToScheme(s)
5756
require.NoError(t, err)
@@ -109,63 +108,36 @@ func TestCreateOrUpdateResourcesWitProdAssets(t *testing.T) {
109108
}
110109

111110
t.Run("failures", func(t *testing.T) {
112-
113111
namespace := "host-operator" + uuid.Must(uuid.NewV4()).String()[:7]
114112
t.Run("nstemplatetiers", func(t *testing.T) {
115-
116-
t.Run("failed to create nstemplatetiers", func(t *testing.T) {
113+
t.Run("failed to patch nstemplatetiers", func(t *testing.T) {
117114
// given
118115
clt := commontest.NewFakeClient(t)
119-
clt.MockCreate = func(ctx context.Context, obj runtimeclient.Object, opts ...runtimeclient.CreateOption) error {
116+
clt.MockPatch = func(ctx context.Context, obj runtimeclient.Object, patch runtimeclient.Patch, opts ...runtimeclient.PatchOption) error {
120117
if obj.GetObjectKind().GroupVersionKind().Kind == "NSTemplateTier" && obj.GetName() == "base" {
121118
// simulate a client/server error
122119
return errors.Errorf("an error")
123120
}
124-
return clt.Client.Create(ctx, obj, opts...)
125-
}
126-
// when
127-
err := nstemplatetiers.CreateOrUpdateResources(context.TODO(), s, clt, namespace)
128-
// then
129-
require.Error(t, err)
130-
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())
131-
})
132-
133-
t.Run("failed to update nstemplatetiers", func(t *testing.T) {
134-
// given
135-
// initialize the client with an existing `advanced` NSTemplatetier
136-
clt := commontest.NewFakeClient(t, &toolchainv1alpha1.NSTemplateTier{
137-
ObjectMeta: metav1.ObjectMeta{
138-
Namespace: namespace,
139-
Name: "advanced",
140-
},
141-
})
142-
clt.MockUpdate = func(ctx context.Context, obj runtimeclient.Object, opts ...runtimeclient.UpdateOption) error {
143-
if obj.GetObjectKind().GroupVersionKind().Kind == "NSTemplateTier" && obj.GetName() == "advanced" {
144-
// simulate a client/server error
145-
return errors.Errorf("an error")
146-
}
147-
return clt.Client.Update(ctx, obj, opts...)
121+
return test.Patch(ctx, clt, obj, patch, opts...)
148122
}
149-
150123
// when
151124
err := nstemplatetiers.CreateOrUpdateResources(context.TODO(), s, clt, namespace)
152125
// then
153126
require.Error(t, err)
154-
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")
127+
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())
155128
})
156129
})
157130

158131
t.Run("tiertemplates", func(t *testing.T) {
159-
160-
t.Run("failed to create nstemplatetiers", func(t *testing.T) {
132+
t.Run("failed to create tiertemplate", func(t *testing.T) {
161133
// given
162134
clt := commontest.NewFakeClient(t)
163-
clt.MockCreate = func(ctx context.Context, obj runtimeclient.Object, opts ...runtimeclient.CreateOption) error {
135+
clt.MockPatch = func(ctx context.Context, obj runtimeclient.Object, patch runtimeclient.Patch, opts ...runtimeclient.PatchOption) error {
164136
if strings.HasPrefix(obj.GetName(), "base1ns-dev-") {
165137
// simulate a client/server error
166138
return errors.Errorf("an error")
167139
}
168-
return clt.Client.Create(ctx, obj, opts...)
140+
return test.Patch(ctx, clt, obj, patch, opts...)
169141
}
170142
// when
171143
err := nstemplatetiers.CreateOrUpdateResources(context.TODO(), s, clt, namespace)
@@ -174,7 +146,6 @@ func TestCreateOrUpdateResourcesWitProdAssets(t *testing.T) {
174146
assert.Regexp(t, fmt.Sprintf("unable to create TierTemplates: unable to create the 'base1ns-dev-\\w+-\\w+' TierTemplate in namespace '%s'", namespace), err.Error()) // we can't tell for sure which namespace will fail first, but the error should match the given regex
175147
})
176148
})
177-
178149
})
179150
t.Run("failed to load assets", func(t *testing.T) {
180151
// when
@@ -183,7 +154,6 @@ func TestCreateOrUpdateResourcesWitProdAssets(t *testing.T) {
183154
require.Error(t, err)
184155
assert.Equal(t, "unable to load templates: open /templates/nstemplatetiers/metadata.yaml: file does not exist", err.Error()) // error occurred while creating TierTemplate resources
185156
})
186-
187157
}
188158

189159
func verifyTierTemplate(t *testing.T, cl *commontest.FakeClient, namespace, tierName, typeName string) string {

0 commit comments

Comments
 (0)