From 58e7427e4678587082b68929dc1998889daaaab4 Mon Sep 17 00:00:00 2001 From: Xudong Liu Date: Mon, 29 Jan 2024 21:33:46 +0800 Subject: [PATCH] change to use dynamic client Signed-off-by: Xudong Liu --- .../vmoperator/client/client.go | 64 +++++++++++++++++++ .../client/virtualmachine_client.go | 58 +++++++++++++++++ .../client/virtualmachineservice_client.go | 52 +++++++++++++++ 3 files changed, 174 insertions(+) create mode 100644 pkg/cloudprovider/vsphereparavirtual/vmoperator/client/client.go create mode 100644 pkg/cloudprovider/vsphereparavirtual/vmoperator/client/virtualmachine_client.go create mode 100644 pkg/cloudprovider/vsphereparavirtual/vmoperator/client/virtualmachineservice_client.go diff --git a/pkg/cloudprovider/vsphereparavirtual/vmoperator/client/client.go b/pkg/cloudprovider/vsphereparavirtual/vmoperator/client/client.go new file mode 100644 index 000000000..c1bdd436a --- /dev/null +++ b/pkg/cloudprovider/vsphereparavirtual/vmoperator/client/client.go @@ -0,0 +1,64 @@ +package client + +import ( + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/runtime/schema" + "k8s.io/client-go/dynamic" + "k8s.io/client-go/rest" + + vmopv1alpha1 "github.com/vmware-tanzu/vm-operator-api/api/v1alpha1" +) + +var ( + VirtualMachineServiceGVR = schema.GroupVersionResource{ + Group: "vmoperator.vmware.com", + Version: "v1alpha1", + Resource: "virtualmachineservices", + } + + VirtualMachineGVR = schema.GroupVersionResource{ + Group: "vmoperator.vmware.com", + Version: "v1alpha1", + Resource: "virtualmachines", + } +) + +type VmoperatorV1alpha1Interface interface { + Client() *dynamic.DynamicClient + VirtualMachines(namespace string) VirtualMachineInterface + VirtualMachineServices(namespace string) VirtualMachineServiceInterface +} + +type VmoperatorV1alpha1Client struct { + dynamicClient *dynamic.DynamicClient +} + +func (c *VmoperatorV1alpha1Client) VirtualMachines(namespace string) VirtualMachineInterface { + return newVirtualMachines(c, namespace) +} + +func (c *VmoperatorV1alpha1Client) VirtualMachineServices(namespace string) VirtualMachineServiceInterface { + return newVirtualMachineServices(c, namespace) +} + +func (c *VmoperatorV1alpha1Client) Client() *dynamic.DynamicClient { + if c == nil { + return nil + } + return c.dynamicClient +} + +func NewForConfig(c *rest.Config) (*VmoperatorV1alpha1Client, error) { + scheme := runtime.NewScheme() + _ = vmopv1alpha1.AddToScheme(scheme) + + dynamicClient, err := dynamic.NewForConfig(c) + if err != nil { + return nil, err + } + + client := &VmoperatorV1alpha1Client{ + dynamicClient: dynamicClient, + } + return client, nil +} diff --git a/pkg/cloudprovider/vsphereparavirtual/vmoperator/client/virtualmachine_client.go b/pkg/cloudprovider/vsphereparavirtual/vmoperator/client/virtualmachine_client.go new file mode 100644 index 000000000..e184a247a --- /dev/null +++ b/pkg/cloudprovider/vsphereparavirtual/vmoperator/client/virtualmachine_client.go @@ -0,0 +1,58 @@ +package client + +import ( + "context" + + vmopv1alpha1 "github.com/vmware-tanzu/vm-operator-api/api/v1alpha1" + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/client-go/dynamic" +) + +// VirtualMachineInterface has methods to work with VirtualMachineService resources. +type VirtualMachineInterface interface { + Create(ctx context.Context, virtualMachine *vmopv1alpha1.VirtualMachine, opts v1.CreateOptions) (*vmopv1alpha1.VirtualMachine, error) + Update(ctx context.Context, virtualMachine *vmopv1alpha1.VirtualMachine, opts v1.UpdateOptions) (*vmopv1alpha1.VirtualMachine, error) + Delete(ctx context.Context, name string, opts v1.DeleteOptions) error + Get(ctx context.Context, name string, opts v1.GetOptions) (*vmopv1alpha1.VirtualMachine, error) + List(ctx context.Context, opts v1.ListOptions) (*vmopv1alpha1.VirtualMachineList, error) +} + +// virtualMachines implements VirtualMachineInterface +type virtualMachines struct { + client *dynamic.DynamicClient + ns string +} + +func newVirtualMachines(c *VmoperatorV1alpha1Client, namespace string) *virtualMachines { + return &virtualMachines{ + client: c.Client(), + ns: namespace, + } +} + +func (v *virtualMachines) Create(ctx context.Context, virtualMachine *vmopv1alpha1.VirtualMachine, opts v1.CreateOptions) (*vmopv1alpha1.VirtualMachine, error) { + return nil, nil +} + +func (v *virtualMachines) Update(ctx context.Context, virtualMachine *vmopv1alpha1.VirtualMachine, opts v1.UpdateOptions) (*vmopv1alpha1.VirtualMachine, error) { + return nil, nil +} + +func (v *virtualMachines) Delete(ctx context.Context, name string, opts v1.DeleteOptions) error { + return nil +} + +func (v *virtualMachines) Get(ctx context.Context, name string, opts v1.GetOptions) (*vmopv1alpha1.VirtualMachine, error) { + virtualMachine := &vmopv1alpha1.VirtualMachine{} + if obj, err := v.client.Resource(VirtualMachineGVR).Namespace(v.ns).Get(ctx, name, opts); err != nil { + return nil, err + } else if err = runtime.DefaultUnstructuredConverter.FromUnstructured(obj.UnstructuredContent(), virtualMachine); err != nil { + return nil, err + } + return virtualMachine, nil +} + +func (v *virtualMachines) List(ctx context.Context, opts v1.ListOptions) (*vmopv1alpha1.VirtualMachineList, error) { + return nil, nil +} diff --git a/pkg/cloudprovider/vsphereparavirtual/vmoperator/client/virtualmachineservice_client.go b/pkg/cloudprovider/vsphereparavirtual/vmoperator/client/virtualmachineservice_client.go new file mode 100644 index 000000000..8bc5c0696 --- /dev/null +++ b/pkg/cloudprovider/vsphereparavirtual/vmoperator/client/virtualmachineservice_client.go @@ -0,0 +1,52 @@ +package client + +import ( + "context" + + vmopv1alpha1 "github.com/vmware-tanzu/vm-operator-api/api/v1alpha1" + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/dynamic" +) + +// VirtualMachineServiceInterface has methods to work with VirtualMachineService resources. +type VirtualMachineServiceInterface interface { + Create(ctx context.Context, virtualMachineService *vmopv1alpha1.VirtualMachineService, opts v1.CreateOptions) (*vmopv1alpha1.VirtualMachineService, error) + Update(ctx context.Context, virtualMachineService *vmopv1alpha1.VirtualMachineService, opts v1.UpdateOptions) (*vmopv1alpha1.VirtualMachineService, error) + Delete(ctx context.Context, name string, opts v1.DeleteOptions) error + Get(ctx context.Context, name string, opts v1.GetOptions) (*vmopv1alpha1.VirtualMachineService, error) + List(ctx context.Context, opts v1.ListOptions) (*vmopv1alpha1.VirtualMachineServiceList, error) +} + +// virtualMachineServices implements VirtualMachineServiceInterface +type virtualMachineServices struct { + client *dynamic.DynamicClient + ns string +} + +// newVirtualMachineServices returns a VirtualMachineServices +func newVirtualMachineServices(c *VmoperatorV1alpha1Client, namespace string) *virtualMachineServices { + return &virtualMachineServices{ + client: c.Client(), + ns: namespace, + } +} + +func (v *virtualMachineServices) Create(ctx context.Context, virtualMachineService *vmopv1alpha1.VirtualMachineService, opts v1.CreateOptions) (*vmopv1alpha1.VirtualMachineService, error) { + return nil, nil +} + +func (v *virtualMachineServices) Update(ctx context.Context, virtualMachineService *vmopv1alpha1.VirtualMachineService, opts v1.UpdateOptions) (*vmopv1alpha1.VirtualMachineService, error) { + return nil, nil +} + +func (v *virtualMachineServices) Delete(ctx context.Context, name string, opts v1.DeleteOptions) error { + return nil +} + +func (v *virtualMachineServices) Get(ctx context.Context, name string, opts v1.GetOptions) (*vmopv1alpha1.VirtualMachineService, error) { + return nil, nil +} + +func (v *virtualMachineServices) List(ctx context.Context, opts v1.ListOptions) (*vmopv1alpha1.VirtualMachineServiceList, error) { + return nil, nil +}