Skip to content

Commit

Permalink
refactor dynamic client creation and add testing ones
Browse files Browse the repository at this point in the history
  • Loading branch information
deads2k committed Oct 4, 2024
1 parent 756adf2 commit d7430e4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 42 deletions.
27 changes: 9 additions & 18 deletions pkg/operator/genericoperatorclient/dynamic_operator_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package genericoperatorclient

import (
"context"
"time"

applyoperatorv1 "github.com/openshift/client-go/operator/applyconfigurations/operator/v1"

"github.com/imdario/mergo"
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
35 changes: 35 additions & 0 deletions pkg/operator/genericoperatorclient/test_operator_client.go
Original file line number Diff line number Diff line change
@@ -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))
}

0 comments on commit d7430e4

Please sign in to comment.