Skip to content

Commit d7430e4

Browse files
committed
refactor dynamic client creation and add testing ones
1 parent 756adf2 commit d7430e4

File tree

3 files changed

+50
-42
lines changed

3 files changed

+50
-42
lines changed

pkg/operator/genericoperatorclient/dynamic_operator_client.go

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ type StaticPodOperatorStatusExtractorFunc func(obj *unstructured.Unstructured, f
3232
type OperatorSpecExtractorFunc func(obj *unstructured.Unstructured, fieldManager string) (*applyoperatorv1.OperatorSpecApplyConfiguration, error)
3333
type OperatorStatusExtractorFunc func(obj *unstructured.Unstructured, fieldManager string) (*applyoperatorv1.OperatorStatusApplyConfiguration, error)
3434

35-
func newClusterScopedOperatorClient(config *rest.Config, gvr schema.GroupVersionResource, gvk schema.GroupVersionKind, extractApplySpec StaticPodOperatorSpecExtractorFunc, extractApplyStatus StaticPodOperatorStatusExtractorFunc) (*dynamicOperatorClient, dynamicinformer.DynamicSharedInformerFactory, error) {
36-
dynamicClient, err := dynamic.NewForConfig(config)
37-
if err != nil {
38-
return nil, nil, err
35+
func newClusterScopedOperatorClient(dynamicClient dynamic.Interface, gvr schema.GroupVersionResource, gvk schema.GroupVersionKind, instanceName string, extractApplySpec StaticPodOperatorSpecExtractorFunc, extractApplyStatus StaticPodOperatorStatusExtractorFunc) (*dynamicOperatorClient, dynamicinformer.DynamicSharedInformerFactory, error) {
36+
if len(instanceName) < 1 {
37+
return nil, nil, fmt.Errorf("config name cannot be empty")
3938
}
39+
4040
client := dynamicClient.Resource(gvr)
4141

4242
informers := dynamicinformer.NewDynamicSharedInformerFactory(dynamicClient, 12*time.Hour)
@@ -46,6 +46,7 @@ func newClusterScopedOperatorClient(config *rest.Config, gvr schema.GroupVersion
4646
gvk: gvk,
4747
informer: informer,
4848
client: client,
49+
configName: instanceName,
4950
extractApplySpec: extractApplySpec,
5051
extractApplyStatus: extractApplyStatus,
5152
}, informers, nil
@@ -82,28 +83,18 @@ func convertOperatorStatusToStaticPodOperatorStatus(extractApplyStatus OperatorS
8283
}
8384

8485
func NewClusterScopedOperatorClient(config *rest.Config, gvr schema.GroupVersionResource, gvk schema.GroupVersionKind, extractApplySpec OperatorSpecExtractorFunc, extractApplyStatus OperatorStatusExtractorFunc) (v1helpers.OperatorClientWithFinalizers, dynamicinformer.DynamicSharedInformerFactory, error) {
85-
d, informers, err := newClusterScopedOperatorClient(config, gvr, gvk,
86-
convertOperatorSpecToStaticPodOperatorSpec(extractApplySpec), convertOperatorStatusToStaticPodOperatorStatus(extractApplyStatus))
87-
if err != nil {
88-
return nil, nil, err
89-
}
90-
d.configName = defaultConfigName
91-
return d, informers, nil
86+
return NewClusterScopedOperatorClientWithConfigName(config, gvr, gvk, defaultConfigName, extractApplySpec, extractApplyStatus)
9287

9388
}
9489

9590
func NewClusterScopedOperatorClientWithConfigName(config *rest.Config, gvr schema.GroupVersionResource, gvk schema.GroupVersionKind, configName string, extractApplySpec OperatorSpecExtractorFunc, extractApplyStatus OperatorStatusExtractorFunc) (v1helpers.OperatorClientWithFinalizers, dynamicinformer.DynamicSharedInformerFactory, error) {
96-
if len(configName) < 1 {
97-
return nil, nil, fmt.Errorf("config name cannot be empty")
98-
}
99-
d, informers, err := newClusterScopedOperatorClient(config, gvr, gvk,
100-
convertOperatorSpecToStaticPodOperatorSpec(extractApplySpec), convertOperatorStatusToStaticPodOperatorStatus(extractApplyStatus))
91+
dynamicClient, err := dynamic.NewForConfig(config)
10192
if err != nil {
10293
return nil, nil, err
10394
}
104-
d.configName = configName
105-
return d, informers, nil
10695

96+
return newClusterScopedOperatorClient(dynamicClient, gvr, gvk, configName,
97+
convertOperatorSpecToStaticPodOperatorSpec(extractApplySpec), convertOperatorStatusToStaticPodOperatorStatus(extractApplyStatus))
10798
}
10899

109100
type dynamicOperatorClient struct {

pkg/operator/genericoperatorclient/dynamic_staticpod_operator_client.go

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package genericoperatorclient
22

33
import (
44
"context"
5-
"time"
6-
75
applyoperatorv1 "github.com/openshift/client-go/operator/applyconfigurations/operator/v1"
86

97
"github.com/imdario/mergo"
@@ -25,28 +23,12 @@ func NewStaticPodOperatorClient(config *rest.Config, gvr schema.GroupVersionReso
2523
if err != nil {
2624
return nil, nil, err
2725
}
28-
client := dynamicClient.Resource(gvr)
29-
30-
informers := dynamicinformer.NewDynamicSharedInformerFactory(dynamicClient, 12*time.Hour)
31-
informer := informers.ForResource(gvr)
32-
33-
return &dynamicStaticPodOperatorClient{
34-
dynamicOperatorClient: dynamicOperatorClient{
35-
gvk: gvk,
36-
configName: defaultConfigName,
37-
informer: informer,
38-
client: client,
39-
extractApplySpec: extractApplySpec,
40-
extractApplyStatus: extractApplyStatus,
41-
},
42-
}, informers, nil
43-
}
4426

45-
type dynamicStaticPodOperatorClient struct {
46-
dynamicOperatorClient
27+
return newClusterScopedOperatorClient(dynamicClient, gvr, gvk, defaultConfigName,
28+
extractApplySpec, extractApplyStatus)
4729
}
4830

49-
func (c dynamicStaticPodOperatorClient) GetStaticPodOperatorState() (*operatorv1.StaticPodOperatorSpec, *operatorv1.StaticPodOperatorStatus, string, error) {
31+
func (c dynamicOperatorClient) GetStaticPodOperatorState() (*operatorv1.StaticPodOperatorSpec, *operatorv1.StaticPodOperatorStatus, string, error) {
5032
uncastInstance, err := c.informer.Lister().Get("cluster")
5133
if err != nil {
5234
return nil, nil, "", err
@@ -69,7 +51,7 @@ func getStaticPodOperatorStateFromInstance(instance *unstructured.Unstructured)
6951
return spec, status, instance.GetResourceVersion(), nil
7052
}
7153

72-
func (c dynamicStaticPodOperatorClient) GetStaticPodOperatorStateWithQuorum(ctx context.Context) (*operatorv1.StaticPodOperatorSpec, *operatorv1.StaticPodOperatorStatus, string, error) {
54+
func (c dynamicOperatorClient) GetStaticPodOperatorStateWithQuorum(ctx context.Context) (*operatorv1.StaticPodOperatorSpec, *operatorv1.StaticPodOperatorStatus, string, error) {
7355
instance, err := c.client.Get(ctx, "cluster", metav1.GetOptions{})
7456
if err != nil {
7557
return nil, nil, "", err
@@ -78,7 +60,7 @@ func (c dynamicStaticPodOperatorClient) GetStaticPodOperatorStateWithQuorum(ctx
7860
return getStaticPodOperatorStateFromInstance(instance)
7961
}
8062

81-
func (c dynamicStaticPodOperatorClient) UpdateStaticPodOperatorSpec(ctx context.Context, resourceVersion string, spec *operatorv1.StaticPodOperatorSpec) (*operatorv1.StaticPodOperatorSpec, string, error) {
63+
func (c dynamicOperatorClient) UpdateStaticPodOperatorSpec(ctx context.Context, resourceVersion string, spec *operatorv1.StaticPodOperatorSpec) (*operatorv1.StaticPodOperatorSpec, string, error) {
8264
uncastOriginal, err := c.informer.Lister().Get("cluster")
8365
if err != nil {
8466
return nil, "", err
@@ -103,7 +85,7 @@ func (c dynamicStaticPodOperatorClient) UpdateStaticPodOperatorSpec(ctx context.
10385
return retSpec, ret.GetResourceVersion(), nil
10486
}
10587

106-
func (c dynamicStaticPodOperatorClient) UpdateStaticPodOperatorStatus(ctx context.Context, resourceVersion string, status *operatorv1.StaticPodOperatorStatus) (*operatorv1.StaticPodOperatorStatus, error) {
88+
func (c dynamicOperatorClient) UpdateStaticPodOperatorStatus(ctx context.Context, resourceVersion string, status *operatorv1.StaticPodOperatorStatus) (*operatorv1.StaticPodOperatorStatus, error) {
10789
uncastOriginal, err := c.informer.Lister().Get("cluster")
10890
if err != nil {
10991
return nil, err
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package genericoperatorclient
2+
3+
import (
4+
"github.com/openshift/library-go/pkg/operator/v1helpers"
5+
"k8s.io/apimachinery/pkg/runtime/schema"
6+
"k8s.io/client-go/dynamic"
7+
"k8s.io/client-go/dynamic/dynamicinformer"
8+
"k8s.io/client-go/rest"
9+
"net/http"
10+
)
11+
12+
func NewOperatorClientWithClient(httpClient *http.Client, gvr schema.GroupVersionResource, gvk schema.GroupVersionKind, extractApplySpec OperatorSpecExtractorFunc, extractApplyStatus OperatorStatusExtractorFunc) (v1helpers.OperatorClientWithFinalizers, dynamicinformer.DynamicSharedInformerFactory, error) {
13+
return NewOperatorClientWithConfigNameWithClient(httpClient, gvr, gvk, defaultConfigName, extractApplySpec, extractApplyStatus)
14+
15+
}
16+
17+
func NewOperatorClientWithConfigNameWithClient(httpClient *http.Client, gvr schema.GroupVersionResource, gvk schema.GroupVersionKind, configName string, extractApplySpec OperatorSpecExtractorFunc, extractApplyStatus OperatorStatusExtractorFunc) (v1helpers.OperatorClientWithFinalizers, dynamicinformer.DynamicSharedInformerFactory, error) {
18+
dynamicClient, err := dynamic.NewForConfigAndClient(&rest.Config{}, httpClient)
19+
if err != nil {
20+
return nil, nil, err
21+
}
22+
23+
return newClusterScopedOperatorClient(dynamicClient, gvr, gvk, configName,
24+
convertOperatorSpecToStaticPodOperatorSpec(extractApplySpec), convertOperatorStatusToStaticPodOperatorStatus(extractApplyStatus))
25+
}
26+
27+
func NewStaticPodOperatorClientWithConfigNameWithClient(httpClient *http.Client, gvr schema.GroupVersionResource, gvk schema.GroupVersionKind, configName string, extractApplySpec OperatorSpecExtractorFunc, extractApplyStatus OperatorStatusExtractorFunc) (v1helpers.StaticPodOperatorClient, dynamicinformer.DynamicSharedInformerFactory, error) {
28+
dynamicClient, err := dynamic.NewForConfigAndClient(&rest.Config{}, httpClient)
29+
if err != nil {
30+
return nil, nil, err
31+
}
32+
33+
return newClusterScopedOperatorClient(dynamicClient, gvr, gvk, configName,
34+
convertOperatorSpecToStaticPodOperatorSpec(extractApplySpec), convertOperatorStatusToStaticPodOperatorStatus(extractApplyStatus))
35+
}

0 commit comments

Comments
 (0)