Skip to content

Commit 0359c7f

Browse files
committed
WIP: drop olm client from reconcile object
1 parent ce22d3c commit 0359c7f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+72
-2561
lines changed

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ require (
1515
github.com/openshift/api v3.9.1-0.20190916204813-cdbe64fb0c91+incompatible
1616
github.com/openshift/client-go v0.0.0-20240115204758-e6bf7d631d5e // release-4.16
1717
github.com/operator-framework/api v0.33.0
18-
github.com/operator-framework/operator-lifecycle-manager v0.33.0
1918
github.com/segmentio/analytics-go/v3 v3.3.0
2019
go.uber.org/mock v0.6.0
2120
golang.org/x/crypto v0.41.0

internal/controller/pattern_controller.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,8 @@ import (
4343
argoapi "github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1"
4444
v1 "github.com/openshift/api/config/v1"
4545
routeclient "github.com/openshift/client-go/route/clientset/versioned"
46-
olmclient "github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/clientset/versioned"
4746
"k8s.io/client-go/kubernetes"
4847

49-
// olmapi "github.com/operator-framework/api/pkg/operators/v1alpha1"
50-
5148
api "github.com/hybrid-cloud-patterns/patterns-operator/api/v1alpha1"
5249
operatorclient "github.com/openshift/client-go/operator/clientset/versioned/typed/operator/v1"
5350
)
@@ -63,7 +60,6 @@ type PatternReconciler struct {
6360
logger logr.Logger
6461

6562
config *rest.Config
66-
olmClient olmclient.Interface
6763
fullClient kubernetes.Interface
6864
dynamicClient dynamic.Interface
6965
routeClient routeclient.Interface
@@ -190,14 +186,14 @@ func (r *PatternReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
190186
targetSub, _ := newSubscriptionFromConfigMap(r.fullClient)
191187
_ = controllerutil.SetOwnerReference(qualifiedInstance, targetSub, r.Scheme)
192188

193-
sub, _ := getSubscription(r.olmClient, targetSub.Name)
189+
sub, _ := getSubscription(r.Client, targetSub.Name)
194190
if sub == nil {
195-
err = createSubscription(r.olmClient, targetSub)
191+
err = createSubscription(r.Client, targetSub)
196192
return r.actionPerformed(qualifiedInstance, "create gitops subscription", err)
197193
} else if ownedBySame(targetSub, sub) {
198194
// Check version/channel etc
199195
// Dangerous if multiple patterns do not agree, or automatic upgrades are in place...
200-
changed, errSub := updateSubscription(r.olmClient, targetSub, sub)
196+
changed, errSub := updateSubscription(r.Client, targetSub, sub)
201197
if changed {
202198
return r.actionPerformed(qualifiedInstance, "update gitops subscription", errSub)
203199
}
@@ -601,10 +597,6 @@ func (r *PatternReconciler) SetupWithManager(mgr ctrl.Manager) error {
601597
var err error
602598
r.config = mgr.GetConfig()
603599

604-
if r.olmClient, err = olmclient.NewForConfig(r.config); err != nil {
605-
return err
606-
}
607-
608600
if r.fullClient, err = kubernetes.NewForConfig(r.config); err != nil {
609601
return err
610602
}

internal/controller/pattern_controller_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
v1 "github.com/openshift/api/config/v1"
3030
operatorv1 "github.com/openshift/api/operator/v1"
3131
operatorclient "github.com/openshift/client-go/operator/clientset/versioned/fake"
32-
olmclient "github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/clientset/versioned/fake"
3332
gomock "go.uber.org/mock/gomock"
3433

3534
kubeclient "k8s.io/client-go/kubernetes/fake"
@@ -230,7 +229,6 @@ func newFakeReconciler(initObjects ...runtime.Object) *PatternReconciler {
230229
return &PatternReconciler{
231230
Scheme: scheme.Scheme,
232231
Client: fakeClient,
233-
olmClient: olmclient.NewSimpleClientset(),
234232
driftWatcher: watcher,
235233
fullClient: kubeclient.NewSimpleClientset(),
236234
operatorClient: operatorclient.NewSimpleClientset(osControlManager).OperatorV1(),

internal/controller/subscription.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ import (
2323
"reflect"
2424

2525
operatorv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
26-
olmclient "github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/clientset/versioned"
26+
"sigs.k8s.io/controller-runtime/pkg/client"
27+
2728
corev1 "k8s.io/api/core/v1"
2829
apierrors "k8s.io/apimachinery/pkg/api/errors"
2930
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
31+
"k8s.io/apimachinery/pkg/types"
3032
"k8s.io/client-go/kubernetes"
3133
)
3234

@@ -80,20 +82,20 @@ func newSubscriptionFromConfigMap(r kubernetes.Interface) (*operatorv1alpha1.Sub
8082
return newSubscription, nil
8183
}
8284

83-
func getSubscription(client olmclient.Interface, name string) (*operatorv1alpha1.Subscription, error) {
84-
sub, err := client.OperatorsV1alpha1().Subscriptions(SubscriptionNamespace).Get(context.Background(), name, metav1.GetOptions{})
85+
func getSubscription(cl client.Client, name string) (*operatorv1alpha1.Subscription, error) {
86+
sub := &operatorv1alpha1.Subscription{}
87+
err := cl.Get(context.Background(), types.NamespacedName{Name: name, Namespace: SubscriptionNamespace}, sub)
8588
if err != nil {
8689
return nil, err
8790
}
8891
return sub, nil
8992
}
9093

91-
func createSubscription(client olmclient.Interface, sub *operatorv1alpha1.Subscription) error {
92-
_, err := client.OperatorsV1alpha1().Subscriptions(SubscriptionNamespace).Create(context.Background(), sub, metav1.CreateOptions{})
93-
return err
94+
func createSubscription(cl client.Client, sub *operatorv1alpha1.Subscription) error {
95+
return cl.Create(context.Background(), sub)
9496
}
9597

96-
func updateSubscription(client olmclient.Interface, target, current *operatorv1alpha1.Subscription) (bool, error) {
98+
func updateSubscription(cl client.Client, target, current *operatorv1alpha1.Subscription) (bool, error) {
9799
changed := false
98100
if current == nil || current.Spec == nil {
99101
return false, fmt.Errorf("current subscription was nil")
@@ -133,7 +135,7 @@ func updateSubscription(client olmclient.Interface, target, current *operatorv1a
133135

134136
if changed {
135137
target.Spec.DeepCopyInto(current.Spec)
136-
_, err := client.OperatorsV1alpha1().Subscriptions(SubscriptionNamespace).Update(context.Background(), current, metav1.UpdateOptions{})
138+
err := cl.Update(context.Background(), current)
137139
return changed, err
138140
}
139141

internal/controller/subscription_test.go

Lines changed: 55 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ import (
77
. "github.com/onsi/ginkgo/v2"
88
. "github.com/onsi/gomega"
99
operatorv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
10-
olmclient "github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/clientset/versioned/fake"
1110
corev1 "k8s.io/api/core/v1"
1211
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
13-
"k8s.io/apimachinery/pkg/runtime"
12+
"k8s.io/apimachinery/pkg/types"
1413
kubeclient "k8s.io/client-go/kubernetes/fake"
15-
"k8s.io/client-go/testing"
14+
"sigs.k8s.io/controller-runtime/pkg/client"
15+
"sigs.k8s.io/controller-runtime/pkg/client/fake"
16+
"sigs.k8s.io/controller-runtime/pkg/client/interceptor"
1617
)
1718

1819
var defaultTestSubscription = operatorv1alpha1.Subscription{
@@ -53,26 +54,23 @@ var defaultTestSubConfigMap = corev1.ConfigMap{
5354
}
5455

5556
var _ = Describe("Subscription Functions", func() {
56-
Context("getSubscription", func() {
57-
var testSubscription *operatorv1alpha1.Subscription
58-
var fakeOlmClientSet *olmclient.Clientset
59-
60-
BeforeEach(func() {
61-
testSubscription = defaultTestSubscription.DeepCopy()
62-
fakeOlmClientSet = olmclient.NewSimpleClientset()
63-
})
57+
var fakeClient client.Client
6458

59+
Context("getSubscription", func() {
6560
It("should error out with a non existing a Subscription", func() {
66-
err := createSubscription(fakeOlmClientSet, testSubscription)
67-
Expect(err).ToNot(HaveOccurred())
68-
_, err = getSubscription(fakeOlmClientSet, "foo")
61+
fakeClient = fake.NewClientBuilder().WithScheme(testEnv.Scheme).
62+
WithRuntimeObjects().Build()
63+
_, err := getSubscription(fakeClient, "foo")
6964
Expect(err).To(HaveOccurred())
7065
})
7166

7267
It("should return a proper Subscription", func() {
73-
err := createSubscription(fakeOlmClientSet, testSubscription)
68+
fakeClient = fake.NewClientBuilder().WithScheme(testEnv.Scheme).Build()
69+
70+
err := createSubscription(fakeClient, &defaultTestSubscription)
7471
Expect(err).ToNot(HaveOccurred())
75-
sub, err := getSubscription(fakeOlmClientSet, "foosubscription")
72+
73+
sub, err := getSubscription(fakeClient, "foosubscription")
7674
Expect(err).ToNot(HaveOccurred())
7775
Expect(sub.Spec.Channel).To(Equal("foochannel"))
7876
Expect(sub.Spec.CatalogSource).To(Equal("foosource"))
@@ -123,14 +121,14 @@ var _ = Describe("Subscription Functions", func() {
123121

124122
var _ = Describe("UpdateSubscription", func() {
125123
var (
126-
client *olmclient.Clientset
124+
fakeClient client.Client
127125
target *operatorv1alpha1.Subscription
128126
current *operatorv1alpha1.Subscription
129127
subscriptionNs string
130128
)
131129

132130
BeforeEach(func() {
133-
client = olmclient.NewSimpleClientset()
131+
134132
subscriptionNs = "openshift-operators"
135133

136134
current = &operatorv1alpha1.Subscription{
@@ -148,11 +146,15 @@ var _ = Describe("UpdateSubscription", func() {
148146
},
149147
}
150148
target = current.DeepCopy()
149+
150+
fakeClient = fake.NewClientBuilder().WithScheme(testEnv.Scheme).
151+
WithRuntimeObjects(current).Build()
152+
151153
})
152154

153155
Context("when current subscription is nil", func() {
154156
It("should return an error", func() {
155-
changed, err := updateSubscription(client, target, nil)
157+
changed, err := updateSubscription(fakeClient, target, nil)
156158
Expect(changed).To(BeFalse())
157159
Expect(err).To(HaveOccurred())
158160
Expect(err.Error()).To(ContainSubstring("current subscription was nil"))
@@ -161,7 +163,7 @@ var _ = Describe("UpdateSubscription", func() {
161163

162164
Context("when target subscription is nil", func() {
163165
It("should return an error", func() {
164-
changed, err := updateSubscription(client, nil, current)
166+
changed, err := updateSubscription(fakeClient, nil, current)
165167
Expect(changed).To(BeFalse())
166168
Expect(err).To(HaveOccurred())
167169
Expect(err.Error()).To(ContainSubstring("target subscription was nil"))
@@ -170,79 +172,85 @@ var _ = Describe("UpdateSubscription", func() {
170172

171173
Context("when the subscription specs are the same", func() {
172174
It("should return false and no error", func() {
173-
changed, err := updateSubscription(client, target, current)
175+
changed, err := updateSubscription(fakeClient, target, current)
174176
Expect(changed).To(BeFalse())
175177
Expect(err).ToNot(HaveOccurred())
176178
})
177179
})
178180

179181
Context("when the subscription specs are different", func() {
180-
BeforeEach(func() {
181-
_, _ = client.OperatorsV1alpha1().Subscriptions(subscriptionNs).Create(context.Background(), current, metav1.CreateOptions{})
182-
})
183182

184183
It("channel difference should return true and update the current subscription", func() {
185184
target.Spec.Channel = "beta"
186-
changed, err := updateSubscription(client, target, current)
185+
changed, err := updateSubscription(fakeClient, target, current)
187186
Expect(changed).To(BeTrue())
188187
Expect(err).ToNot(HaveOccurred())
189188

190-
updated, err := client.OperatorsV1alpha1().Subscriptions(subscriptionNs).Get(context.Background(), current.Name, metav1.GetOptions{})
189+
updated := operatorv1alpha1.Subscription{}
190+
err = fakeClient.Get(context.Background(), types.NamespacedName{Name: current.Name, Namespace: current.Namespace}, &updated)
191191
Expect(err).ToNot(HaveOccurred())
192192
Expect(updated.Spec.Channel).To(Equal("beta"))
193193
})
194194

195195
It("catalgsource difference should return true and update the current subscription", func() {
196196
target.Spec.CatalogSource = "somesource"
197-
changed, err := updateSubscription(client, target, current)
197+
changed, err := updateSubscription(fakeClient, target, current)
198198
Expect(changed).To(BeTrue())
199199
Expect(err).ToNot(HaveOccurred())
200200

201-
updated, err := client.OperatorsV1alpha1().Subscriptions(subscriptionNs).Get(context.Background(), current.Name, metav1.GetOptions{})
201+
updated := operatorv1alpha1.Subscription{}
202+
err = fakeClient.Get(context.Background(), types.NamespacedName{Name: current.Name, Namespace: current.Namespace}, &updated)
202203
Expect(err).ToNot(HaveOccurred())
203204
Expect(updated.Spec.CatalogSource).To(Equal("somesource"))
204205
})
205206

206207
It("catalogsourcenamespace difference should return true and update the current subscription", func() {
207208
target.Spec.CatalogSourceNamespace = "another"
208-
changed, err := updateSubscription(client, target, current)
209+
changed, err := updateSubscription(fakeClient, target, current)
209210
Expect(changed).To(BeTrue())
210211
Expect(err).ToNot(HaveOccurred())
211212

212-
updated, err := client.OperatorsV1alpha1().Subscriptions(subscriptionNs).Get(context.Background(), current.Name, metav1.GetOptions{})
213+
updated := operatorv1alpha1.Subscription{}
214+
err = fakeClient.Get(context.Background(), types.NamespacedName{Name: current.Name, Namespace: current.Namespace}, &updated)
213215
Expect(err).ToNot(HaveOccurred())
214216
Expect(updated.Spec.CatalogSourceNamespace).To(Equal("another"))
215217
})
216218

217219
It("package difference should return true and update the current subscription", func() {
218220
target.Spec.Package = "notdefault"
219-
changed, err := updateSubscription(client, target, current)
221+
changed, err := updateSubscription(fakeClient, target, current)
220222
Expect(changed).To(BeTrue())
221223
Expect(err).ToNot(HaveOccurred())
222224

223-
updated, err := client.OperatorsV1alpha1().Subscriptions(subscriptionNs).Get(context.Background(), current.Name, metav1.GetOptions{})
225+
updated := operatorv1alpha1.Subscription{}
226+
err = fakeClient.Get(context.Background(), types.NamespacedName{Name: current.Name, Namespace: current.Namespace}, &updated)
227+
224228
Expect(err).ToNot(HaveOccurred())
225229
Expect(updated.Spec.Package).To(Equal("notdefault"))
226230
})
227231

228232
It("InstallPlanApproval difference should return true and update the current subscription", func() {
229233
target.Spec.InstallPlanApproval = operatorv1alpha1.ApprovalManual
230-
changed, err := updateSubscription(client, target, current)
234+
changed, err := updateSubscription(fakeClient, target, current)
231235
Expect(changed).To(BeTrue())
232236
Expect(err).ToNot(HaveOccurred())
233237

234-
updated, err := client.OperatorsV1alpha1().Subscriptions(subscriptionNs).Get(context.Background(), current.Name, metav1.GetOptions{})
238+
updated := operatorv1alpha1.Subscription{}
239+
err = fakeClient.Get(context.Background(), types.NamespacedName{Name: current.Name, Namespace: current.Namespace}, &updated)
240+
235241
Expect(err).ToNot(HaveOccurred())
236242
Expect(updated.Spec.InstallPlanApproval).To(Equal(operatorv1alpha1.ApprovalManual))
237243
})
238244

239245
It("StartingCSV difference should return true and update the current subscription", func() {
240246
target.Spec.StartingCSV = "v1.1.0"
241-
changed, err := updateSubscription(client, target, current)
247+
changed, err := updateSubscription(fakeClient, target, current)
242248
Expect(changed).To(BeTrue())
243249
Expect(err).ToNot(HaveOccurred())
244250

245-
updated, err := client.OperatorsV1alpha1().Subscriptions(subscriptionNs).Get(context.Background(), current.Name, metav1.GetOptions{})
251+
updated := operatorv1alpha1.Subscription{}
252+
err = fakeClient.Get(context.Background(), types.NamespacedName{Name: current.Name, Namespace: current.Namespace}, &updated)
253+
246254
Expect(err).ToNot(HaveOccurred())
247255
Expect(updated.Spec.StartingCSV).To(Equal("v1.1.0"))
248256
})
@@ -257,26 +265,28 @@ var _ = Describe("UpdateSubscription", func() {
257265
},
258266
}
259267
target.Spec.Config = tmp
260-
changed, err := updateSubscription(client, target, current)
268+
changed, err := updateSubscription(fakeClient, target, current)
261269
Expect(changed).To(BeTrue())
262270
Expect(err).ToNot(HaveOccurred())
263271

264-
updated, err := client.OperatorsV1alpha1().Subscriptions(subscriptionNs).Get(context.Background(), current.Name, metav1.GetOptions{})
272+
updated := operatorv1alpha1.Subscription{}
273+
err = fakeClient.Get(context.Background(), types.NamespacedName{Name: current.Name, Namespace: current.Namespace}, &updated)
274+
265275
Expect(err).ToNot(HaveOccurred())
266276
Expect(updated.Spec.Config.Env[0].Name).To(Equal("foo"))
267277
})
268278
})
269279

270280
Context("when there is an error updating the subscription", func() {
271-
BeforeEach(func() {
272-
client.PrependReactor("update", "subscriptions", func(testing.Action) (handled bool, ret runtime.Object, err error) {
273-
return true, nil, fmt.Errorf("update error")
274-
})
275-
target.Spec.Channel = "beta"
276-
})
277281

278282
It("should return true and an error", func() {
279-
changed, err := updateSubscription(client, target, current)
283+
fakeClient = fake.NewClientBuilder().WithInterceptorFuncs(
284+
interceptor.Funcs{Update: func(ctx context.Context, client client.WithWatch, obj client.Object, opts ...client.UpdateOption) error {
285+
return fmt.Errorf("update error")
286+
}}).WithScheme(testEnv.Scheme).Build()
287+
target.Spec.Channel = "beta"
288+
289+
changed, err := updateSubscription(fakeClient, target, current)
280290
Expect(changed).To(BeTrue())
281291
Expect(err).To(HaveOccurred())
282292
Expect(err.Error()).To(ContainSubstring("update error"))

internal/controller/suite_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ import (
2323

2424
argoapi "github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1"
2525
gitopsv1alpha1 "github.com/hybrid-cloud-patterns/patterns-operator/api/v1alpha1"
26+
2627
. "github.com/onsi/ginkgo/v2"
2728
. "github.com/onsi/gomega"
2829
apiv1 "github.com/openshift/api/config/v1"
2930
operatorv1 "github.com/openshift/api/operator/v1"
31+
operatorv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
3032

3133
"k8s.io/client-go/kubernetes/scheme"
3234
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -81,6 +83,8 @@ var _ = BeforeSuite(func() {
8183
Expect(err).NotTo(HaveOccurred())
8284
err = argoapi.AddToScheme(scheme.Scheme)
8385
Expect(err).NotTo(HaveOccurred())
86+
err = operatorv1alpha1.AddToScheme(scheme.Scheme)
87+
Expect(err).NotTo(HaveOccurred())
8488
//+kubebuilder:scaffold:scheme
8589

8690
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})

vendor/github.com/operator-framework/api/pkg/operators/v1/doc.go

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)