From d7430e4a889af569ee84632c5cef25d4c808e83c Mon Sep 17 00:00:00 2001 From: David Eads Date: Thu, 3 Oct 2024 16:50:52 -0400 Subject: [PATCH] refactor dynamic client creation and add testing ones --- .../dynamic_operator_client.go | 27 +++++--------- .../dynamic_staticpod_operator_client.go | 30 ++++------------ .../test_operator_client.go | 35 +++++++++++++++++++ 3 files changed, 50 insertions(+), 42 deletions(-) create mode 100644 pkg/operator/genericoperatorclient/test_operator_client.go diff --git a/pkg/operator/genericoperatorclient/dynamic_operator_client.go b/pkg/operator/genericoperatorclient/dynamic_operator_client.go index 477224b05b..c71d99ee08 100644 --- a/pkg/operator/genericoperatorclient/dynamic_operator_client.go +++ b/pkg/operator/genericoperatorclient/dynamic_operator_client.go @@ -32,11 +32,11 @@ type StaticPodOperatorStatusExtractorFunc func(obj *unstructured.Unstructured, f type OperatorSpecExtractorFunc func(obj *unstructured.Unstructured, fieldManager string) (*applyoperatorv1.OperatorSpecApplyConfiguration, error) type OperatorStatusExtractorFunc func(obj *unstructured.Unstructured, fieldManager string) (*applyoperatorv1.OperatorStatusApplyConfiguration, error) -func newClusterScopedOperatorClient(config *rest.Config, gvr schema.GroupVersionResource, gvk schema.GroupVersionKind, extractApplySpec StaticPodOperatorSpecExtractorFunc, extractApplyStatus StaticPodOperatorStatusExtractorFunc) (*dynamicOperatorClient, dynamicinformer.DynamicSharedInformerFactory, error) { - dynamicClient, err := dynamic.NewForConfig(config) - if err != nil { - return nil, nil, err +func newClusterScopedOperatorClient(dynamicClient dynamic.Interface, gvr schema.GroupVersionResource, gvk schema.GroupVersionKind, instanceName string, extractApplySpec StaticPodOperatorSpecExtractorFunc, extractApplyStatus StaticPodOperatorStatusExtractorFunc) (*dynamicOperatorClient, dynamicinformer.DynamicSharedInformerFactory, error) { + if len(instanceName) < 1 { + return nil, nil, fmt.Errorf("config name cannot be empty") } + client := dynamicClient.Resource(gvr) informers := dynamicinformer.NewDynamicSharedInformerFactory(dynamicClient, 12*time.Hour) @@ -46,6 +46,7 @@ func newClusterScopedOperatorClient(config *rest.Config, gvr schema.GroupVersion gvk: gvk, informer: informer, client: client, + configName: instanceName, extractApplySpec: extractApplySpec, extractApplyStatus: extractApplyStatus, }, informers, nil @@ -82,28 +83,18 @@ func convertOperatorStatusToStaticPodOperatorStatus(extractApplyStatus OperatorS } func NewClusterScopedOperatorClient(config *rest.Config, gvr schema.GroupVersionResource, gvk schema.GroupVersionKind, extractApplySpec OperatorSpecExtractorFunc, extractApplyStatus OperatorStatusExtractorFunc) (v1helpers.OperatorClientWithFinalizers, dynamicinformer.DynamicSharedInformerFactory, error) { - d, informers, err := newClusterScopedOperatorClient(config, gvr, gvk, - convertOperatorSpecToStaticPodOperatorSpec(extractApplySpec), convertOperatorStatusToStaticPodOperatorStatus(extractApplyStatus)) - if err != nil { - return nil, nil, err - } - d.configName = defaultConfigName - return d, informers, nil + return NewClusterScopedOperatorClientWithConfigName(config, gvr, gvk, defaultConfigName, extractApplySpec, extractApplyStatus) } func NewClusterScopedOperatorClientWithConfigName(config *rest.Config, gvr schema.GroupVersionResource, gvk schema.GroupVersionKind, configName string, extractApplySpec OperatorSpecExtractorFunc, extractApplyStatus OperatorStatusExtractorFunc) (v1helpers.OperatorClientWithFinalizers, dynamicinformer.DynamicSharedInformerFactory, error) { - if len(configName) < 1 { - return nil, nil, fmt.Errorf("config name cannot be empty") - } - d, informers, err := newClusterScopedOperatorClient(config, gvr, gvk, - convertOperatorSpecToStaticPodOperatorSpec(extractApplySpec), convertOperatorStatusToStaticPodOperatorStatus(extractApplyStatus)) + dynamicClient, err := dynamic.NewForConfig(config) if err != nil { return nil, nil, err } - d.configName = configName - return d, informers, nil + return newClusterScopedOperatorClient(dynamicClient, gvr, gvk, configName, + convertOperatorSpecToStaticPodOperatorSpec(extractApplySpec), convertOperatorStatusToStaticPodOperatorStatus(extractApplyStatus)) } type dynamicOperatorClient struct { diff --git a/pkg/operator/genericoperatorclient/dynamic_staticpod_operator_client.go b/pkg/operator/genericoperatorclient/dynamic_staticpod_operator_client.go index 77b7824169..3c3680f62e 100644 --- a/pkg/operator/genericoperatorclient/dynamic_staticpod_operator_client.go +++ b/pkg/operator/genericoperatorclient/dynamic_staticpod_operator_client.go @@ -2,8 +2,6 @@ package genericoperatorclient import ( "context" - "time" - applyoperatorv1 "github.com/openshift/client-go/operator/applyconfigurations/operator/v1" "github.com/imdario/mergo" @@ -25,28 +23,12 @@ func NewStaticPodOperatorClient(config *rest.Config, gvr schema.GroupVersionReso if err != nil { return nil, nil, err } - client := dynamicClient.Resource(gvr) - - informers := dynamicinformer.NewDynamicSharedInformerFactory(dynamicClient, 12*time.Hour) - informer := informers.ForResource(gvr) - - return &dynamicStaticPodOperatorClient{ - dynamicOperatorClient: dynamicOperatorClient{ - gvk: gvk, - configName: defaultConfigName, - informer: informer, - client: client, - extractApplySpec: extractApplySpec, - extractApplyStatus: extractApplyStatus, - }, - }, informers, nil -} -type dynamicStaticPodOperatorClient struct { - dynamicOperatorClient + return newClusterScopedOperatorClient(dynamicClient, gvr, gvk, defaultConfigName, + extractApplySpec, extractApplyStatus) } -func (c dynamicStaticPodOperatorClient) GetStaticPodOperatorState() (*operatorv1.StaticPodOperatorSpec, *operatorv1.StaticPodOperatorStatus, string, error) { +func (c dynamicOperatorClient) GetStaticPodOperatorState() (*operatorv1.StaticPodOperatorSpec, *operatorv1.StaticPodOperatorStatus, string, error) { uncastInstance, err := c.informer.Lister().Get("cluster") if err != nil { return nil, nil, "", err @@ -69,7 +51,7 @@ func getStaticPodOperatorStateFromInstance(instance *unstructured.Unstructured) return spec, status, instance.GetResourceVersion(), nil } -func (c dynamicStaticPodOperatorClient) GetStaticPodOperatorStateWithQuorum(ctx context.Context) (*operatorv1.StaticPodOperatorSpec, *operatorv1.StaticPodOperatorStatus, string, error) { +func (c dynamicOperatorClient) GetStaticPodOperatorStateWithQuorum(ctx context.Context) (*operatorv1.StaticPodOperatorSpec, *operatorv1.StaticPodOperatorStatus, string, error) { instance, err := c.client.Get(ctx, "cluster", metav1.GetOptions{}) if err != nil { return nil, nil, "", err @@ -78,7 +60,7 @@ func (c dynamicStaticPodOperatorClient) GetStaticPodOperatorStateWithQuorum(ctx return getStaticPodOperatorStateFromInstance(instance) } -func (c dynamicStaticPodOperatorClient) UpdateStaticPodOperatorSpec(ctx context.Context, resourceVersion string, spec *operatorv1.StaticPodOperatorSpec) (*operatorv1.StaticPodOperatorSpec, string, error) { +func (c dynamicOperatorClient) UpdateStaticPodOperatorSpec(ctx context.Context, resourceVersion string, spec *operatorv1.StaticPodOperatorSpec) (*operatorv1.StaticPodOperatorSpec, string, error) { uncastOriginal, err := c.informer.Lister().Get("cluster") if err != nil { return nil, "", err @@ -103,7 +85,7 @@ func (c dynamicStaticPodOperatorClient) UpdateStaticPodOperatorSpec(ctx context. return retSpec, ret.GetResourceVersion(), nil } -func (c dynamicStaticPodOperatorClient) UpdateStaticPodOperatorStatus(ctx context.Context, resourceVersion string, status *operatorv1.StaticPodOperatorStatus) (*operatorv1.StaticPodOperatorStatus, error) { +func (c dynamicOperatorClient) UpdateStaticPodOperatorStatus(ctx context.Context, resourceVersion string, status *operatorv1.StaticPodOperatorStatus) (*operatorv1.StaticPodOperatorStatus, error) { uncastOriginal, err := c.informer.Lister().Get("cluster") if err != nil { return nil, err diff --git a/pkg/operator/genericoperatorclient/test_operator_client.go b/pkg/operator/genericoperatorclient/test_operator_client.go new file mode 100644 index 0000000000..500e53ba4f --- /dev/null +++ b/pkg/operator/genericoperatorclient/test_operator_client.go @@ -0,0 +1,35 @@ +package genericoperatorclient + +import ( + "github.com/openshift/library-go/pkg/operator/v1helpers" + "k8s.io/apimachinery/pkg/runtime/schema" + "k8s.io/client-go/dynamic" + "k8s.io/client-go/dynamic/dynamicinformer" + "k8s.io/client-go/rest" + "net/http" +) + +func NewOperatorClientWithClient(httpClient *http.Client, gvr schema.GroupVersionResource, gvk schema.GroupVersionKind, extractApplySpec OperatorSpecExtractorFunc, extractApplyStatus OperatorStatusExtractorFunc) (v1helpers.OperatorClientWithFinalizers, dynamicinformer.DynamicSharedInformerFactory, error) { + return NewOperatorClientWithConfigNameWithClient(httpClient, gvr, gvk, defaultConfigName, extractApplySpec, extractApplyStatus) + +} + +func NewOperatorClientWithConfigNameWithClient(httpClient *http.Client, gvr schema.GroupVersionResource, gvk schema.GroupVersionKind, configName string, extractApplySpec OperatorSpecExtractorFunc, extractApplyStatus OperatorStatusExtractorFunc) (v1helpers.OperatorClientWithFinalizers, dynamicinformer.DynamicSharedInformerFactory, error) { + dynamicClient, err := dynamic.NewForConfigAndClient(&rest.Config{}, httpClient) + if err != nil { + return nil, nil, err + } + + return newClusterScopedOperatorClient(dynamicClient, gvr, gvk, configName, + convertOperatorSpecToStaticPodOperatorSpec(extractApplySpec), convertOperatorStatusToStaticPodOperatorStatus(extractApplyStatus)) +} + +func NewStaticPodOperatorClientWithConfigNameWithClient(httpClient *http.Client, gvr schema.GroupVersionResource, gvk schema.GroupVersionKind, configName string, extractApplySpec OperatorSpecExtractorFunc, extractApplyStatus OperatorStatusExtractorFunc) (v1helpers.StaticPodOperatorClient, dynamicinformer.DynamicSharedInformerFactory, error) { + dynamicClient, err := dynamic.NewForConfigAndClient(&rest.Config{}, httpClient) + if err != nil { + return nil, nil, err + } + + return newClusterScopedOperatorClient(dynamicClient, gvr, gvk, configName, + convertOperatorSpecToStaticPodOperatorSpec(extractApplySpec), convertOperatorStatusToStaticPodOperatorStatus(extractApplyStatus)) +}