Skip to content

Commit 3769cf5

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

2 files changed

Lines changed: 12 additions & 49 deletions

File tree

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)