Skip to content

Commit 5384935

Browse files
committed
WIP: drop dynamic client from reconcile object
Refactor acm code, add tests, update vendor code
1 parent b4368fc commit 5384935

File tree

5 files changed

+63
-618
lines changed

5 files changed

+63
-618
lines changed

internal/controller/acm.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,19 @@ import (
2323
"log"
2424

2525
corev1 "k8s.io/api/core/v1"
26-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2727
"k8s.io/apimachinery/pkg/labels"
2828
"k8s.io/apimachinery/pkg/runtime/schema"
2929
"sigs.k8s.io/controller-runtime/pkg/client"
3030
)
3131

32-
func haveACMHub(r *PatternReconciler) bool {
33-
gvrMCH := schema.GroupVersionResource{Group: "operator.open-cluster-management.io", Version: "v1", Resource: "multiclusterhubs"}
32+
var mchGVK = schema.GroupVersionKind{
33+
Group: "operator.open-cluster-management.io",
34+
Kind: "multiclusterhubs",
35+
Version: "v1",
36+
}
3437

38+
func haveACMHub(r *PatternReconciler) bool {
3539
labelSelector, err := labels.Parse(fmt.Sprintf("%v = %v", "ocm-configmap-type", "image-manifest"))
3640

3741
if err != nil {
@@ -54,7 +58,11 @@ func haveACMHub(r *PatternReconciler) bool {
5458
}
5559
ns := cms.Items[0].Namespace
5660

57-
umch, err := r.dynamicClient.Resource(gvrMCH).Namespace(ns).List(context.TODO(), metav1.ListOptions{})
61+
umch := &unstructured.UnstructuredList{}
62+
umch.SetGroupVersionKind(mchGVK)
63+
64+
err = r.List(context.Background(), umch, &client.ListOptions{Namespace: ns})
65+
5866
if err != nil {
5967
log.Printf("Error obtaining hub: %s\n", err)
6068
return false

internal/controller/acm_test.go

Lines changed: 51 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -4,75 +4,74 @@ import (
44
"context"
55
"fmt"
66

7-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
8-
"k8s.io/apimachinery/pkg/runtime"
9-
"k8s.io/apimachinery/pkg/runtime/schema"
10-
dynamicfake "k8s.io/client-go/dynamic/fake"
11-
"k8s.io/client-go/testing"
127
"sigs.k8s.io/controller-runtime/pkg/client"
138
"sigs.k8s.io/controller-runtime/pkg/client/fake"
149
"sigs.k8s.io/controller-runtime/pkg/client/interceptor"
1510

1611
v1 "k8s.io/api/core/v1"
12+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1713
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1814

1915
. "github.com/onsi/ginkgo/v2"
2016
. "github.com/onsi/gomega"
2117
)
2218

2319
var _ = Describe("HaveACMHub", func() {
20+
2421
var (
2522
patternReconciler *PatternReconciler
26-
dynamicClient *dynamicfake.FakeDynamicClient
27-
gvrMCH schema.GroupVersionResource
2823
fakeClient client.Client
24+
configMap *v1.ConfigMap
25+
hub *unstructured.Unstructured
2926
)
30-
3127
BeforeEach(func() {
32-
gvrMCH = schema.GroupVersionResource{Group: "operator.open-cluster-management.io", Version: "v1", Resource: "multiclusterhubs"}
28+
configMap = &v1.ConfigMap{
29+
ObjectMeta: metav1.ObjectMeta{
30+
Name: "test-configmap",
31+
Namespace: "default",
32+
Labels: map[string]string{
33+
"ocm-configmap-type": "image-manifest",
34+
},
35+
},
36+
}
37+
hub = &unstructured.Unstructured{
38+
Object: map[string]any{
39+
"apiVersion": "operator.open-cluster-management.io/v1",
40+
"kind": "MultiClusterHub",
41+
"metadata": map[string]any{
42+
"name": "test-hub",
43+
"namespace": "default",
44+
},
45+
},
46+
}
47+
hub.SetGroupVersionKind(mchGVK)
48+
})
3349

34-
dynamicClient = dynamicfake.NewSimpleDynamicClientWithCustomListKinds(runtime.NewScheme(), map[schema.GroupVersionResource]string{
35-
gvrMCH: "MultiClusterHubList",
36-
})
50+
Context("when the ACM Hub exists in same ns as configmap", func() {
51+
It("should return true", func() {
52+
fakeClient = fake.NewClientBuilder().WithScheme(testEnv.Scheme).
53+
WithRuntimeObjects(configMap, hub).Build()
54+
patternReconciler = &PatternReconciler{
55+
Client: fakeClient,
56+
}
3757

58+
result := haveACMHub(patternReconciler)
59+
Expect(result).To(BeTrue())
60+
})
3861
})
3962

40-
Context("when the ACM Hub exists", func() {
41-
BeforeEach(func() {
42-
configMap := &v1.ConfigMap{
43-
ObjectMeta: metav1.ObjectMeta{
44-
Name: "test-configmap",
45-
Namespace: "default",
46-
Labels: map[string]string{
47-
"ocm-configmap-type": "image-manifest",
48-
},
49-
},
50-
}
63+
Context("when the ACM Hub exists different ns as configmap", func() {
64+
It("should return false", func() {
65+
hub.SetNamespace("different")
5166

5267
fakeClient = fake.NewClientBuilder().WithScheme(testEnv.Scheme).
53-
WithRuntimeObjects(configMap).Build()
68+
WithRuntimeObjects(configMap, hub).Build()
5469
patternReconciler = &PatternReconciler{
55-
Client: fakeClient,
56-
dynamicClient: dynamicClient,
70+
Client: fakeClient,
5771
}
5872

59-
hub := &unstructured.Unstructured{
60-
Object: map[string]any{
61-
"apiVersion": "operator.open-cluster-management.io/v1",
62-
"kind": "MultiClusterHub",
63-
"metadata": map[string]any{
64-
"name": "test-hub",
65-
"namespace": "default",
66-
},
67-
},
68-
}
69-
_, err := dynamicClient.Resource(gvrMCH).Namespace("default").Create(context.Background(), hub, metav1.CreateOptions{})
70-
Expect(err).ToNot(HaveOccurred())
71-
})
72-
73-
It("should return true", func() {
7473
result := haveACMHub(patternReconciler)
75-
Expect(result).To(BeTrue())
74+
Expect(result).To(BeFalse())
7675
})
7776
})
7877

@@ -81,8 +80,7 @@ var _ = Describe("HaveACMHub", func() {
8180
fakeClient = fake.NewClientBuilder().WithScheme(testEnv.Scheme).
8281
WithRuntimeObjects().Build()
8382
patternReconciler = &PatternReconciler{
84-
Client: fakeClient,
85-
dynamicClient: dynamicClient,
83+
Client: fakeClient,
8684
}
8785

8886
result := haveACMHub(patternReconciler)
@@ -91,47 +89,32 @@ var _ = Describe("HaveACMHub", func() {
9189
})
9290

9391
Context("when there is an error listing ConfigMaps", func() {
94-
BeforeEach(func() {
92+
It("should return false and log the error", func() {
9593
fakeClient = fake.NewClientBuilder().WithInterceptorFuncs(
9694
interceptor.Funcs{List: func(ctx context.Context, client client.WithWatch, obj client.ObjectList, opts ...client.ListOption) error {
9795
return fmt.Errorf("list error")
9896
}}).WithScheme(testEnv.Scheme).Build()
97+
9998
patternReconciler = &PatternReconciler{
100-
Client: fakeClient,
101-
dynamicClient: dynamicClient,
99+
Client: fakeClient,
102100
}
103-
})
104101

105-
It("should return false and log the error", func() {
106102
result := haveACMHub(patternReconciler)
107103
Expect(result).To(BeFalse())
108104
})
109105
})
110106

111107
Context("when there is an error listing the MultiClusterHubs", func() {
112-
BeforeEach(func() {
113-
configMap := &v1.ConfigMap{
114-
ObjectMeta: metav1.ObjectMeta{
115-
Name: "test-configmap",
116-
Namespace: "default",
117-
Labels: map[string]string{
118-
"ocm-configmap-type": "image-manifest",
119-
},
120-
},
121-
}
122-
fakeClient = fake.NewClientBuilder().WithScheme(testEnv.Scheme).
123-
WithRuntimeObjects(configMap).Build()
108+
It("should return false and log the error", func() {
109+
fakeClient = fake.NewClientBuilder().WithInterceptorFuncs(
110+
interceptor.Funcs{List: func(ctx context.Context, client client.WithWatch, list client.ObjectList, opts ...client.ListOption) error {
111+
return fmt.Errorf("list error")
112+
}}).WithScheme(testEnv.Scheme).WithRuntimeObjects(configMap).Build()
124113

125-
dynamicClient.PrependReactor("list", "multiclusterhubs", func(testing.Action) (handled bool, ret runtime.Object, err error) {
126-
return true, nil, fmt.Errorf("multiclusterhub error")
127-
})
128114
patternReconciler = &PatternReconciler{
129-
Client: fakeClient,
130-
dynamicClient: dynamicClient,
115+
Client: fakeClient,
131116
}
132-
})
133117

134-
It("should return false and log the error", func() {
135118
result := haveACMHub(patternReconciler)
136119
Expect(result).To(BeFalse())
137120
})

internal/controller/pattern_controller.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import (
3232
"k8s.io/apimachinery/pkg/labels"
3333
"k8s.io/apimachinery/pkg/runtime"
3434
"k8s.io/apimachinery/pkg/types"
35-
"k8s.io/client-go/dynamic"
3635
"k8s.io/client-go/rest"
3736
ctrl "sigs.k8s.io/controller-runtime"
3837
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -60,7 +59,6 @@ type PatternReconciler struct {
6059
logger logr.Logger
6160

6261
config *rest.Config
63-
dynamicClient dynamic.Interface
6462
routeClient routeclient.Interface
6563
operatorClient operatorclient.OperatorV1Interface
6664
driftWatcher driftWatcher
@@ -596,10 +594,6 @@ func (r *PatternReconciler) SetupWithManager(mgr ctrl.Manager) error {
596594
var err error
597595
r.config = mgr.GetConfig()
598596

599-
if r.dynamicClient, err = dynamic.NewForConfig(r.config); err != nil {
600-
return err
601-
}
602-
603597
if r.operatorClient, err = operatorclient.NewForConfig(r.config); err != nil {
604598
return err
605599
}

0 commit comments

Comments
 (0)