diff --git a/pkg/k8s/client/client.go b/pkg/k8s/client/client.go index 1325b685e..dd397524d 100644 --- a/pkg/k8s/client/client.go +++ b/pkg/k8s/client/client.go @@ -17,8 +17,10 @@ package client import ( "path/filepath" + apix "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" "k8s.io/client-go/dynamic" "k8s.io/client-go/kubernetes" + "k8s.io/client-go/tools/clientcmd" "opendev.org/airship/airshipctl/pkg/environment" "opendev.org/airship/airshipctl/pkg/k8s/kubectl" @@ -34,6 +36,7 @@ import ( type Interface interface { ClientSet() kubernetes.Interface DynamicClient() dynamic.Interface + ApiextensionsClientSet() apix.Interface Kubectl() kubectl.Interface } @@ -42,6 +45,7 @@ type Interface interface { type Client struct { clientSet kubernetes.Interface dynamicClient dynamic.Interface + apixClient apix.Interface kubectl kubectl.Interface } @@ -70,6 +74,17 @@ func NewClient(settings *environment.AirshipCTLSettings) (Interface, error) { return nil, err } + // kubectl factories can't create CRD clients... + config, err := clientcmd.BuildConfigFromFlags("", settings.KubeConfigPath) + if err != nil { + return nil, err + } + + client.apixClient, err = apix.NewForConfig(config) + if err != nil { + return nil, err + } + return client, nil } @@ -93,6 +108,16 @@ func (c *Client) SetDynamicClient(dynamicClient dynamic.Interface) { c.dynamicClient = dynamicClient } +// ApiextensionsV1 getter for ApiextensionsV1 interface +func (c *Client) ApiextensionsClientSet() apix.Interface { + return c.apixClient +} + +// SetApiextensionsV1 setter for ApiextensionsV1 interface +func (c *Client) SetApiextensionsClientSet(apixClient apix.Interface) { + c.apixClient = apixClient +} + // Kubectl getter for Kubectl interface func (c *Client) Kubectl() kubectl.Interface { return c.kubectl diff --git a/pkg/k8s/client/fake/fake.go b/pkg/k8s/client/fake/fake.go index 760e56f12..92d477b13 100644 --- a/pkg/k8s/client/fake/fake.go +++ b/pkg/k8s/client/fake/fake.go @@ -15,6 +15,7 @@ package fake import ( + apix "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" "k8s.io/client-go/dynamic" "k8s.io/client-go/kubernetes" @@ -27,9 +28,10 @@ import ( // per test. Examples of implementations can be found with each interface // method. type Client struct { - MockClientSet func() kubernetes.Interface - MockDynamicClient func() dynamic.Interface - MockKubectl func() kubectl.Interface + MockClientSet func() kubernetes.Interface + MockDynamicClient func() dynamic.Interface + MockApiextensionsClientSet func() apix.Interface + MockKubectl func() kubectl.Interface } var _ client.Interface = &Client{} @@ -66,6 +68,22 @@ func (c Client) DynamicClient() dynamic.Interface { return c.MockDynamicClient() } +// ApiextensionsClientSet is used to get a mocked implementation of an +// Apiextensions clientset. To initialize the mocked client to be returned, +// the MockApiextensionsClientSet method must be implemented, ideally returning a +// k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/fake.ClientSet. +// +// Example: +// +// testClient := fake.Client { +// MockApiextensionsClientSet: func() apix.Interface { +// return apix_fake.NewSimpleClientset() +// }, +// } +func (c Client) ApiextensionsClientSet() apix.Interface { + return c.MockApiextensionsClientSet() +} + // Kubectl is used to get a mocked implementation of a Kubectl client. // To initialize the mocked client to be returned, the MockKubectl method // must be implemented.