Skip to content

Commit d704639

Browse files
committed
use the SSA client to install the operators
1 parent 16d9570 commit d704639

3 files changed

Lines changed: 37 additions & 37 deletions

File tree

pkg/cmd/adm/install_operator.go

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/kubesaw/ksctl/pkg/configuration"
1212
clicontext "github.com/kubesaw/ksctl/pkg/context"
1313
"github.com/kubesaw/ksctl/pkg/ioutils"
14+
"github.com/kubesaw/ksctl/pkg/utils"
1415
olmv1 "github.com/operator-framework/api/pkg/operators/v1"
1516
olmv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
1617
v1 "k8s.io/api/core/v1"
@@ -43,7 +44,7 @@ func NewInstallOperatorCmd() *cobra.Command {
4344
return err
4445
}
4546

46-
cl := commonclient.NewApplyClient(kubeClient)
47+
cl := commonclient.NewSSAApplyClient(kubeClient, utils.KsctlFieldManager)
4748
ctx := clicontext.NewTerminalContext(term)
4849
return installOperator(ctx, commandArgs, args[0], cl)
4950
},
@@ -56,7 +57,7 @@ func NewInstallOperatorCmd() *cobra.Command {
5657
return cmd
5758
}
5859

59-
func installOperator(ctx *clicontext.TerminalContext, args installArgs, operator string, applyClient *commonclient.ApplyClient) error {
60+
func installOperator(ctx *clicontext.TerminalContext, args installArgs, operator string, applyClient *commonclient.SSAApplyClient) error {
6061
// validate cluster type
6162
if operator != string(configuration.Host) && operator != string(configuration.Member) {
6263
return fmt.Errorf("invalid operator type provided: %s. Valid ones are %s|%s", operator, configuration.Host, configuration.Member)
@@ -80,26 +81,26 @@ func installOperator(ctx *clicontext.TerminalContext, args installArgs, operator
8081
}
8182

8283
// check that we don't install both host and member in the same namespace
83-
if err := checkOneOperatorPerNamespace(ctx, applyClient, namespace, operator); err != nil {
84+
if err := checkOneOperatorPerNamespace(ctx, applyClient.Client, namespace, operator); err != nil {
8485
return err
8586
}
8687

8788
// install the catalog source
8889
namespacedName := types.NamespacedName{Name: operatorResourceName(operator), Namespace: namespace}
8990
catalogSource := newCatalogSource(namespacedName, operator)
9091
ctx.Println(fmt.Sprintf("Creating CatalogSource %s in namespace %s.", catalogSource.Name, catalogSource.Namespace))
91-
if _, err := applyClient.ApplyObject(ctx.Context, catalogSource, commonclient.SaveConfiguration(false)); err != nil {
92+
if err := applyClient.ApplyObject(ctx.Context, catalogSource); err != nil {
9293
return err
9394
}
9495
ctx.Println(fmt.Sprintf("CatalogSource %s created.", catalogSource.Name))
95-
if err := waitUntilCatalogSourceIsReady(ctx, applyClient, namespacedName, args.waitForReadyTimeout); err != nil {
96+
if err := waitUntilCatalogSourceIsReady(ctx, applyClient.Client, namespacedName, args.waitForReadyTimeout); err != nil {
9697
return err
9798
}
9899
ctx.Printlnf("CatalogSource %s is ready", namespacedName)
99100

100101
// check if operator group is already there
101102
ogs := olmv1.OperatorGroupList{}
102-
if err := applyClient.List(ctx, &ogs, runtimeclient.InNamespace(namespace)); err != nil {
103+
if err := applyClient.Client.List(ctx, &ogs, runtimeclient.InNamespace(namespace)); err != nil {
103104
return err
104105
}
105106
if len(ogs.Items) > 0 {
@@ -108,7 +109,7 @@ func installOperator(ctx *clicontext.TerminalContext, args installArgs, operator
108109
// install operator group
109110
operatorGroup := newOperatorGroup(namespacedName)
110111
ctx.Println(fmt.Sprintf("Creating new operator group %s in namespace %s.", operatorGroup.Name, operatorGroup.Namespace))
111-
if _, err := applyClient.ApplyObject(ctx, operatorGroup, commonclient.SaveConfiguration(false)); err != nil {
112+
if err := applyClient.ApplyObject(ctx, operatorGroup); err != nil {
112113
return err
113114
}
114115
ctx.Println(fmt.Sprintf("OperatorGroup %s created.", operatorGroup.Name))
@@ -118,11 +119,11 @@ func installOperator(ctx *clicontext.TerminalContext, args installArgs, operator
118119
operatorName := getOperatorName(operator)
119120
subscription := newSubscription(namespacedName, operatorName, namespacedName.Name)
120121
ctx.Println(fmt.Sprintf("Creating Subscription %s in namespace %s.", subscription.Name, subscription.Namespace))
121-
if _, err := applyClient.ApplyObject(ctx, subscription, commonclient.SaveConfiguration(false)); err != nil {
122+
if err := applyClient.ApplyObject(ctx, subscription); err != nil {
122123
return err
123124
}
124125
ctx.Println(fmt.Sprintf("Subcription %s created.", subscription.Name))
125-
if err := waitUntilInstallPlanIsComplete(ctx, applyClient, operatorName, namespace, args.waitForReadyTimeout); err != nil {
126+
if err := waitUntilInstallPlanIsComplete(ctx, applyClient.Client, operatorName, namespace, args.waitForReadyTimeout); err != nil {
126127
return err
127128
}
128129
ctx.Println(fmt.Sprintf("InstallPlan for the %s operator has been completed", operator))
@@ -135,20 +136,16 @@ func getOperatorName(operator string) string {
135136
return fmt.Sprintf("toolchain-%s-operator", operator)
136137
}
137138

138-
func createNamespaceIfNotFound(ctx *clicontext.TerminalContext, applyClient *commonclient.ApplyClient, namespace string) error {
139-
ns := &v1.Namespace{}
140-
if err := applyClient.Get(ctx.Context, types.NamespacedName{Name: namespace}, ns); err != nil {
141-
if errors.IsNotFound(err) {
142-
ctx.Println(fmt.Sprintf("Creating namespace %s.", namespace))
143-
ns.Name = namespace
144-
if errNs := applyClient.Create(ctx.Context, ns); errNs != nil {
145-
return errNs
146-
}
147-
} else {
148-
return err
149-
}
139+
func createNamespaceIfNotFound(ctx *clicontext.TerminalContext, applyClient *commonclient.SSAApplyClient, namespace string) error {
140+
ns := &v1.Namespace{
141+
ObjectMeta: metav1.ObjectMeta{
142+
Name: namespace,
143+
},
144+
}
145+
if err := applyClient.ApplyObject(ctx.Context, ns); err != nil {
146+
return err
150147
}
151-
ctx.Println(fmt.Sprintf("Namespace %s created.", namespace))
148+
ctx.Println(fmt.Sprintf("Ensured namespace %s.", namespace))
152149
return nil
153150
}
154151

@@ -209,12 +206,12 @@ func newSubscription(name types.NamespacedName, operatorName, catalogSourceName
209206
}
210207
}
211208

212-
func waitUntilCatalogSourceIsReady(ctx *clicontext.TerminalContext, applyClient *commonclient.ApplyClient, catalogSourceKey runtimeclient.ObjectKey, waitForReadyTimeout time.Duration) error {
209+
func waitUntilCatalogSourceIsReady(ctx *clicontext.TerminalContext, cl runtimeclient.Client, catalogSourceKey runtimeclient.ObjectKey, waitForReadyTimeout time.Duration) error {
213210
cs := &olmv1alpha1.CatalogSource{}
214211
if err := wait.PollUntilContextTimeout(context.TODO(), 2*time.Second, waitForReadyTimeout, true, func(ctx2 context.Context) (bool, error) {
215212
ctx.Printlnf("waiting for CatalogSource %s to become ready", catalogSourceKey)
216213
cs = &olmv1alpha1.CatalogSource{}
217-
if err := applyClient.Get(ctx, catalogSourceKey, cs); err != nil {
214+
if err := cl.Get(ctx, catalogSourceKey, cs); err != nil {
218215
return false, err
219216
}
220217

@@ -253,13 +250,13 @@ func waitUntilInstallPlanIsComplete(ctx *clicontext.TerminalContext, cl runtimec
253250

254251
// checkOneOperatorPerNamespace returns an error in case the namespace contains the other operator installed.
255252
// So for host namespace member operator should not be installed in there and vice-versa.
256-
func checkOneOperatorPerNamespace(ctx *clicontext.TerminalContext, applyClient *commonclient.ApplyClient, namespace, operator string) error {
253+
func checkOneOperatorPerNamespace(ctx *clicontext.TerminalContext, cl runtimeclient.Client, namespace, operator string) error {
257254
namespacedName := types.NamespacedName{
258255
Namespace: namespace,
259256
Name: configuration.ClusterType(operator).TheOtherType().String(),
260257
}
261258
subscription := olmv1alpha1.Subscription{}
262-
if err := applyClient.Get(ctx.Context, namespacedName, &subscription); err != nil {
259+
if err := cl.Get(ctx.Context, namespacedName, &subscription); err != nil {
263260
if errors.IsNotFound(err) {
264261
return nil
265262
}

pkg/cmd/adm/install_operator_test.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/kubesaw/ksctl/pkg/configuration"
1515
clicontext "github.com/kubesaw/ksctl/pkg/context"
1616
. "github.com/kubesaw/ksctl/pkg/test"
17+
"github.com/kubesaw/ksctl/pkg/utils"
1718
v1 "github.com/operator-framework/api/pkg/operators/v1"
1819
olmv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
1920
"github.com/stretchr/testify/assert"
@@ -64,7 +65,7 @@ func TestInstallOperator(t *testing.T) {
6465
ctx := clicontext.NewTerminalContext(term)
6566

6667
// when
67-
err := installOperator(ctx, args, operator, commonclient.NewApplyClient(fakeClient))
68+
err := installOperator(ctx, args, operator, commonclient.NewSSAApplyClient(fakeClient, utils.KsctlFieldManager))
6869

6970
// then
7071
require.NoError(t, err)
@@ -99,7 +100,7 @@ func TestInstallOperator(t *testing.T) {
99100
ctx := clicontext.NewTerminalContext(term)
100101

101102
// when
102-
err := installOperator(ctx, args, operator, commonclient.NewApplyClient(fakeClient))
103+
err := installOperator(ctx, args, operator, commonclient.NewSSAApplyClient(fakeClient, utils.KsctlFieldManager))
103104

104105
// then
105106
require.ErrorContains(t, err, "failed waiting for catalog source to be ready.")
@@ -119,7 +120,7 @@ func TestInstallOperator(t *testing.T) {
119120
ctx := clicontext.NewTerminalContext(term)
120121

121122
// when
122-
err := installOperator(ctx, args, operator, commonclient.NewApplyClient(fakeClient))
123+
err := installOperator(ctx, args, operator, commonclient.NewSSAApplyClient(fakeClient, utils.KsctlFieldManager))
123124

124125
// then
125126
require.ErrorContains(t, err, "failed waiting for install plan to be complete.")
@@ -139,7 +140,7 @@ func TestInstallOperator(t *testing.T) {
139140
// when
140141
err := installOperator(ctx, installArgs{namespace: namespace, waitForReadyTimeout: 1 * time.Second},
141142
operator,
142-
commonclient.NewApplyClient(fakeClient),
143+
commonclient.NewSSAApplyClient(fakeClient, utils.KsctlFieldManager),
143144
)
144145

145146
// then
@@ -157,7 +158,7 @@ func TestInstallOperator(t *testing.T) {
157158
ctx := clicontext.NewTerminalContext(term)
158159

159160
// when
160-
err := installOperator(ctx, args, operator, commonclient.NewApplyClient(fakeClient))
161+
err := installOperator(ctx, args, operator, commonclient.NewSSAApplyClient(fakeClient, utils.KsctlFieldManager))
161162

162163
// then
163164
require.NoError(t, err)
@@ -176,7 +177,7 @@ func TestInstallOperator(t *testing.T) {
176177
// when
177178
err := installOperator(ctx, installArgs{namespace: "", kubeConfig: kubeconfig, waitForReadyTimeout: timeout}, // we provide no namespace
178179
operator,
179-
commonclient.NewApplyClient(fakeClient),
180+
commonclient.NewSSAApplyClient(fakeClient, utils.KsctlFieldManager),
180181
)
181182
// then
182183
require.NoError(t, err)
@@ -194,7 +195,7 @@ func TestInstallOperator(t *testing.T) {
194195
// when
195196
err := installOperator(ctx, installArgs{},
196197
"INVALIDOPERATOR",
197-
commonclient.NewApplyClient(fakeClient),
198+
commonclient.NewSSAApplyClient(fakeClient, utils.KsctlFieldManager),
198199
)
199200

200201
// then
@@ -211,7 +212,7 @@ func TestInstallOperator(t *testing.T) {
211212
operator := "host"
212213
err := installOperator(ctx, installArgs{namespace: "toolchain-host-operator", waitForReadyTimeout: time.Second * 1},
213214
operator,
214-
commonclient.NewApplyClient(fakeClient),
215+
commonclient.NewSSAApplyClient(fakeClient, utils.KsctlFieldManager),
215216
)
216217

217218
// then
@@ -222,7 +223,7 @@ func TestInstallOperator(t *testing.T) {
222223
}
223224

224225
func fakeClientWithReadyCatalogSource(fakeClient *test.FakeClient) {
225-
fakeClient.MockCreate = func(ctx context.Context, obj runtimeclient.Object, opts ...runtimeclient.CreateOption) error {
226+
fakeClient.MockPatch = func(ctx context.Context, obj runtimeclient.Object, patch runtimeclient.Patch, opts ...runtimeclient.PatchOption) error {
226227
switch objT := obj.(type) {
227228
case *olmv1alpha1.CatalogSource:
228229
// let's set the status of the CS to be able to test the "happy path"
@@ -231,9 +232,9 @@ func fakeClientWithReadyCatalogSource(fakeClient *test.FakeClient) {
231232
LastObservedState: "READY",
232233
},
233234
}
234-
return fakeClient.Client.Create(ctx, objT)
235+
return test.Patch(ctx, fakeClient, objT, patch, opts...)
235236
default:
236-
return fakeClient.Client.Create(ctx, objT)
237+
return test.Patch(ctx, fakeClient, objT, patch, opts...)
237238
}
238239
}
239240
}

pkg/utils/util.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"strings"
77
)
88

9+
const KsctlFieldManager = "ksctl"
10+
911
const K8sLabelWithoutSuffixMaxLength = 62
1012

1113
// Contains checks if the given slice of strings contains the given string

0 commit comments

Comments
 (0)