Skip to content

Commit

Permalink
Adding Kubernetes Clientset to kudo.Client (#1528)
Browse files Browse the repository at this point in the history
Co-authored-by: Aleksey Dukhovniy <adukhovniy@mesosphere.io>
Signed-off-by: Ken Sipe <kensipe@gmail.com>
  • Loading branch information
kensipe and Aleksey Dukhovniy committed May 18, 2020
1 parent c22e53a commit 87b7966
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 38 deletions.
3 changes: 2 additions & 1 deletion pkg/kudoctl/cmd/get/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"gotest.tools/assert"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kubefake "k8s.io/client-go/kubernetes/fake"

"github.com/kudobuilder/kudo/pkg/apis/kudo/v1beta1"
"github.com/kudobuilder/kudo/pkg/client/clientset/versioned/fake"
Expand All @@ -32,7 +33,7 @@ func TestValidate(t *testing.T) {
}

func newTestClient() *kudo.Client {
return kudo.NewClientFromK8s(fake.NewSimpleClientset())
return kudo.NewClientFromK8s(fake.NewSimpleClientset(), kubefake.NewSimpleClientset())
}

func TestGetInstances(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion pkg/kudoctl/cmd/plan/plan_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/stretchr/testify/assert"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kubefake "k8s.io/client-go/kubernetes/fake"

"github.com/kudobuilder/kudo/pkg/apis/kudo/v1beta1"
"github.com/kudobuilder/kudo/pkg/client/clientset/versioned/fake"
Expand Down Expand Up @@ -125,7 +126,7 @@ func TestStatus(t *testing.T) {

for _, tt := range tests {
var buf bytes.Buffer
kc := kudo.NewClientFromK8s(fake.NewSimpleClientset())
kc := kudo.NewClientFromK8s(fake.NewSimpleClientset(), kubefake.NewSimpleClientset())
if tt.instance != nil {
_, err := kc.InstallInstanceObjToCluster(tt.instance, "default")
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/kudoctl/cmd/uninstall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kubefake "k8s.io/client-go/kubernetes/fake"

"github.com/kudobuilder/kudo/pkg/apis/kudo/v1beta1"
"github.com/kudobuilder/kudo/pkg/client/clientset/versioned/fake"
Expand All @@ -14,7 +15,7 @@ import (
)

func newTestClient() *kudo.Client {
return kudo.NewClientFromK8s(fake.NewSimpleClientset())
return kudo.NewClientFromK8s(fake.NewSimpleClientset(), kubefake.NewSimpleClientset())
}

func TestUninstall(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion pkg/kudoctl/util/kudo/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/version"
fakediscovery "k8s.io/client-go/discovery/fake"
kubefake "k8s.io/client-go/kubernetes/fake"

"github.com/kudobuilder/kudo/pkg/apis/kudo/v1beta1"
"github.com/kudobuilder/kudo/pkg/client/clientset/versioned/fake"
Expand Down Expand Up @@ -77,7 +78,7 @@ func Test_InstallPackage(t *testing.T) {

for _, tt := range tests {
client := fake.NewSimpleClientset()
kc := NewClientFromK8s(client)
kc := NewClientFromK8s(client, kubefake.NewSimpleClientset())

fakeDiscovery, ok := client.Discovery().(*fakediscovery.FakeDiscovery)
if !ok {
Expand Down
45 changes: 27 additions & 18 deletions pkg/kudoctl/util/kudo/kudo.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"k8s.io/apimachinery/pkg/util/uuid"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/discovery"
"k8s.io/client-go/kubernetes"

// Import Kubernetes authentication providers to support GKE, etc.
_ "k8s.io/client-go/plugin/pkg/client/auth"
Expand All @@ -31,9 +32,10 @@ import (
"github.com/kudobuilder/kudo/pkg/version"
)

// Client is a KUDO Client providing access to a clientset
// Client is a KUDO Client providing access to a kudo clientset and kubernetes clientsets
type Client struct {
clientset versioned.Interface
kudoClientset versioned.Interface
kubeClientset kubernetes.Interface
}

// NewClient creates new KUDO Client
Expand Down Expand Up @@ -66,27 +68,34 @@ func NewClient(kubeConfigPath string, requestTimeout int64, validateInstall bool
}
}

// create the clientset
// create the kudo clientset
kudoClientset, err := versioned.NewForConfig(config)
if err != nil {
return nil, err
}

// create the kubernetes clientset
kubeClientset, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}
return &Client{
clientset: kudoClientset,
kudoClientset: kudoClientset,
kubeClientset: kubeClientset,
}, nil
}

// NewClientFromK8s creates KUDO client from kubernetes client interface
func NewClientFromK8s(client versioned.Interface) *Client {
func NewClientFromK8s(kudo versioned.Interface, kube kubernetes.Interface) *Client {
result := Client{}
result.clientset = client
result.kudoClientset = kudo
result.kubeClientset = kube
return &result
}

// OperatorExistsInCluster checks if a given Operator object is installed on the current k8s cluster
func (c *Client) OperatorExistsInCluster(name, namespace string) bool {
operator, err := c.clientset.KudoV1beta1().Operators(namespace).Get(name, v1.GetOptions{})
operator, err := c.kudoClientset.KudoV1beta1().Operators(namespace).Get(name, v1.GetOptions{})
if err != nil {
clog.V(2).Printf("operator.kudo.dev/%s does not exist\n", name)
return false
Expand All @@ -110,7 +119,7 @@ func (c *Client) OperatorExistsInCluster(name, namespace string) bool {
// kudo.dev/operator: kafka
// This function also just returns true if the Instance matches a specific OperatorVersion of an Operator
func (c *Client) InstanceExistsInCluster(operatorName, namespace, version, instanceName string) (bool, error) {
instances, err := c.clientset.KudoV1beta1().Instances(namespace).List(v1.ListOptions{LabelSelector: fmt.Sprintf("%s=%s", label.OperatorLabel, operatorName)})
instances, err := c.kudoClientset.KudoV1beta1().Instances(namespace).List(v1.ListOptions{LabelSelector: fmt.Sprintf("%s=%s", label.OperatorLabel, operatorName)})
if err != nil {
return false, err
}
Expand Down Expand Up @@ -152,7 +161,7 @@ func setGVKFromScheme(object runtime.Object) error {
// GetInstance queries kubernetes api for instance of given name in given namespace
// returns error for error conditions. Instance not found is not considered an error and will result in 'nil, nil'
func (c *Client) GetInstance(name, namespace string) (*v1beta1.Instance, error) {
instance, err := c.clientset.KudoV1beta1().Instances(namespace).Get(name, v1.GetOptions{})
instance, err := c.kudoClientset.KudoV1beta1().Instances(namespace).Get(name, v1.GetOptions{})
if apierrors.IsNotFound(err) {
return nil, nil
}
Expand All @@ -166,7 +175,7 @@ func (c *Client) GetInstance(name, namespace string) (*v1beta1.Instance, error)
// GetOperatorVersion queries kubernetes api for operatorversion of given name in given namespace
// returns error for all other errors that not found, not found is treated as result being 'nil, nil'
func (c *Client) GetOperatorVersion(name, namespace string) (*v1beta1.OperatorVersion, error) {
ov, err := c.clientset.KudoV1beta1().OperatorVersions(namespace).Get(name, v1.GetOptions{})
ov, err := c.kudoClientset.KudoV1beta1().OperatorVersions(namespace).Get(name, v1.GetOptions{})
if apierrors.IsNotFound(err) {
return nil, nil
}
Expand Down Expand Up @@ -205,7 +214,7 @@ func (c *Client) UpdateInstance(instanceName, namespace string, operatorVersion
if err != nil {
return err
}
_, err = c.clientset.KudoV1beta1().Instances(namespace).Patch(instanceName, types.MergePatchType, serializedPatch)
_, err = c.kudoClientset.KudoV1beta1().Instances(namespace).Patch(instanceName, types.MergePatchType, serializedPatch)
return err
}

Expand Down Expand Up @@ -278,7 +287,7 @@ func (c *Client) IsInstanceByNameDone(name string, namespace string, oldInstance

// ListInstances lists all instances of given operator installed in the cluster in a given ns
func (c *Client) ListInstances(namespace string) ([]string, error) {
instances, err := c.clientset.KudoV1beta1().Instances(namespace).List(v1.ListOptions{})
instances, err := c.kudoClientset.KudoV1beta1().Instances(namespace).List(v1.ListOptions{})
if err != nil {
return nil, err
}
Expand All @@ -292,7 +301,7 @@ func (c *Client) ListInstances(namespace string) ([]string, error) {

// OperatorVersionsInstalled lists all the versions of given operator installed in the cluster in given ns
func (c *Client) OperatorVersionsInstalled(operatorName, namespace string) ([]string, error) {
ov, err := c.clientset.KudoV1beta1().OperatorVersions(namespace).List(v1.ListOptions{})
ov, err := c.kudoClientset.KudoV1beta1().OperatorVersions(namespace).List(v1.ListOptions{})
if err != nil {
return nil, err
}
Expand All @@ -308,7 +317,7 @@ func (c *Client) OperatorVersionsInstalled(operatorName, namespace string) ([]st

// InstallOperatorObjToCluster expects a valid Operator obj to install
func (c *Client) InstallOperatorObjToCluster(obj *v1beta1.Operator, namespace string) (*v1beta1.Operator, error) {
createdObj, err := c.clientset.KudoV1beta1().Operators(namespace).Create(obj)
createdObj, err := c.kudoClientset.KudoV1beta1().Operators(namespace).Create(obj)
if err != nil {
// we do NOT wrap timeouts
if os.IsTimeout(err) {
Expand All @@ -321,7 +330,7 @@ func (c *Client) InstallOperatorObjToCluster(obj *v1beta1.Operator, namespace st

// InstallOperatorVersionObjToCluster expects a valid Operator obj to install
func (c *Client) InstallOperatorVersionObjToCluster(obj *v1beta1.OperatorVersion, namespace string) (*v1beta1.OperatorVersion, error) {
createdObj, err := c.clientset.KudoV1beta1().OperatorVersions(namespace).Create(obj)
createdObj, err := c.kudoClientset.KudoV1beta1().OperatorVersions(namespace).Create(obj)
if err != nil {
// we do NOT wrap timeouts
if os.IsTimeout(err) {
Expand All @@ -334,7 +343,7 @@ func (c *Client) InstallOperatorVersionObjToCluster(obj *v1beta1.OperatorVersion

// InstallInstanceObjToCluster expects a valid Instance obj to install
func (c *Client) InstallInstanceObjToCluster(obj *v1beta1.Instance, namespace string) (*v1beta1.Instance, error) {
createdObj, err := c.clientset.KudoV1beta1().Instances(namespace).Create(obj)
createdObj, err := c.kudoClientset.KudoV1beta1().Instances(namespace).Create(obj)
if err != nil {
// we do NOT wrap timeouts
if os.IsTimeout(err) {
Expand All @@ -353,7 +362,7 @@ func (c *Client) DeleteInstance(instanceName, namespace string) error {
PropagationPolicy: &propagationPolicy,
}

return c.clientset.KudoV1beta1().Instances(namespace).Delete(instanceName, options)
return c.kudoClientset.KudoV1beta1().Instances(namespace).Delete(instanceName, options)
}

// ValidateServerForOperator validates that the k8s server version and kudo version are valid for operator
Expand All @@ -369,7 +378,7 @@ func (c *Client) ValidateServerForOperator(operator *v1beta1.Operator) error {
// return fmt.Errorf("Unable to parse operators kudo version: %w", err)
//}
// semvar compares patch, for which we do not want to... compare maj, min only
kVer, err := getKubeVersion(c.clientset.Discovery())
kVer, err := getKubeVersion(c.kudoClientset.Discovery())
if err != nil {
return err
}
Expand Down
31 changes: 16 additions & 15 deletions pkg/kudoctl/util/kudo/kudo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"gotest.tools/assert"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kubefake "k8s.io/client-go/kubernetes/fake"

"github.com/kudobuilder/kudo/pkg/apis/kudo/v1beta1"
"github.com/kudobuilder/kudo/pkg/client/clientset/versioned/fake"
Expand All @@ -16,7 +17,7 @@ import (
)

func newTestSimpleK2o() *Client {
return NewClientFromK8s(fake.NewSimpleClientset())
return NewClientFromK8s(fake.NewSimpleClientset(), kubefake.NewSimpleClientset())
}

func TestKudoClientValidate(t *testing.T) {
Expand Down Expand Up @@ -62,7 +63,7 @@ func TestKudoClient_OperatorExistsInCluster(t *testing.T) {
k2o := newTestSimpleK2o()

// create Operator
_, err := k2o.clientset.KudoV1beta1().Operators(tt.createns).Create(tt.obj)
_, err := k2o.kudoClientset.KudoV1beta1().Operators(tt.createns).Create(tt.obj)
if err != nil {
if err.Error() != "object does not implement the Object interfaces" {
t.Errorf("unexpected error: %+v", err)
Expand Down Expand Up @@ -135,7 +136,7 @@ func TestKudoClient_InstanceExistsInCluster(t *testing.T) {

// create Instance
if tt.obj != nil {
_, err := k2o.clientset.KudoV1beta1().Instances(instanceNamespace).Create(tt.obj)
_, err := k2o.kudoClientset.KudoV1beta1().Instances(instanceNamespace).Create(tt.obj)
if err != nil {
t.Fatalf("%s: Error during test setup, cannot create test instance %v", tt.name, err)
}
Expand Down Expand Up @@ -185,7 +186,7 @@ func TestKudoClient_ListInstances(t *testing.T) {

// create Instance
if tt.obj != nil {
_, err := k2o.clientset.KudoV1beta1().Instances(installNamespace).Create(tt.obj)
_, err := k2o.kudoClientset.KudoV1beta1().Instances(installNamespace).Create(tt.obj)
if err != nil {
t.Errorf("%d: Error creating instance in tests setup", i+1)
}
Expand Down Expand Up @@ -238,7 +239,7 @@ func TestKudoClient_OperatorVersionsInstalled(t *testing.T) {

// create Instance
if tt.obj != nil {
_, err := k2o.clientset.KudoV1beta1().OperatorVersions(installNamespace).Create(tt.obj)
_, err := k2o.kudoClientset.KudoV1beta1().OperatorVersions(installNamespace).Create(tt.obj)
if err != nil {
t.Errorf("Error creating operator version in tests setup for %s", tt.name)
}
Expand Down Expand Up @@ -280,12 +281,12 @@ func TestKudoClient_InstallOperatorObjToCluster(t *testing.T) {
k2o := newTestSimpleK2o()

// create Operator
k2o.clientset.KudoV1beta1().Operators(tt.createns).Create(tt.obj) //nolint:errcheck
k2o.kudoClientset.KudoV1beta1().Operators(tt.createns).Create(tt.obj) //nolint:errcheck

// test if Operator exists in namespace
k2o.InstallOperatorObjToCluster(tt.obj, tt.createns) //nolint:errcheck

_, err := k2o.clientset.KudoV1beta1().Operators(tt.createns).Get(tt.name, metav1.GetOptions{})
_, err := k2o.kudoClientset.KudoV1beta1().Operators(tt.createns).Get(tt.name, metav1.GetOptions{})
if tt.err != "" {
assert.ErrorContains(t, err, tt.err, "failure in %v test case", i+1)
}
Expand Down Expand Up @@ -320,12 +321,12 @@ func TestKudoClient_InstallOperatorVersionObjToCluster(t *testing.T) {
k2o := newTestSimpleK2o()

// create Operator
k2o.clientset.KudoV1beta1().OperatorVersions(tt.createns).Create(tt.obj) //nolint:errcheck
k2o.kudoClientset.KudoV1beta1().OperatorVersions(tt.createns).Create(tt.obj) //nolint:errcheck

// test if Operator exists in namespace
k2o.InstallOperatorVersionObjToCluster(tt.obj, tt.createns) //nolint:errcheck

_, err := k2o.clientset.KudoV1beta1().OperatorVersions(tt.createns).Get(tt.name, metav1.GetOptions{})
_, err := k2o.kudoClientset.KudoV1beta1().OperatorVersions(tt.createns).Get(tt.name, metav1.GetOptions{})
if tt.err != "" {
assert.ErrorContains(t, err, tt.err, "failure in %v test case", i+1)
}
Expand Down Expand Up @@ -360,12 +361,12 @@ func TestKudoClient_InstallInstanceObjToCluster(t *testing.T) {
k2o := newTestSimpleK2o()

// create Operator
k2o.clientset.KudoV1beta1().Instances(tt.createns).Create(tt.obj) //nolint:errcheck
k2o.kudoClientset.KudoV1beta1().Instances(tt.createns).Create(tt.obj) //nolint:errcheck

// test if Operator exists in namespace
k2o.InstallInstanceObjToCluster(tt.obj, tt.createns) //nolint:errcheck

_, err := k2o.clientset.KudoV1beta1().Instances(tt.createns).Get(tt.name, metav1.GetOptions{})
_, err := k2o.kudoClientset.KudoV1beta1().Instances(tt.createns).Get(tt.name, metav1.GetOptions{})
if tt.err != "" {
assert.ErrorContains(t, err, tt.err, "failure in %v test case", i+1)
}
Expand Down Expand Up @@ -408,7 +409,7 @@ func TestKudoClient_GetInstance(t *testing.T) {

// create Instance
if tt.storedInstance != nil {
_, err := k2o.clientset.KudoV1beta1().Instances(installNamespace).Create(tt.storedInstance)
_, err := k2o.kudoClientset.KudoV1beta1().Instances(installNamespace).Create(tt.storedInstance)
if err != nil {
t.Errorf("%d: Error creating instance in tests setup", i+1)
}
Expand Down Expand Up @@ -454,7 +455,7 @@ func TestKudoClient_GetOperatorVersion(t *testing.T) {

// create Instance
if tt.storedOv != nil {
_, err := k2o.clientset.KudoV1beta1().OperatorVersions(installNamespace).Create(tt.storedOv)
_, err := k2o.kudoClientset.KudoV1beta1().OperatorVersions(installNamespace).Create(tt.storedOv)
if err != nil {
t.Errorf("Error creating operator version in tests setup for %s", tt.name)
}
Expand Down Expand Up @@ -510,7 +511,7 @@ func TestKudoClient_UpdateOperatorVersion(t *testing.T) {
// create Instance
instanceToCreate := testInstance
instanceToCreate.Spec.Parameters = tt.existingParameters
_, err := k2o.clientset.KudoV1beta1().Instances(installNamespace).Create(&instanceToCreate)
_, err := k2o.kudoClientset.KudoV1beta1().Instances(installNamespace).Create(&instanceToCreate)
if err != nil {
t.Errorf("Error creating operator version in tests setup for %s", tt.name)
}
Expand Down Expand Up @@ -587,7 +588,7 @@ func TestKudoClient_DeleteInstance(t *testing.T) {
for _, test := range tests {
k2o := newTestSimpleK2o()

_, err := k2o.clientset.KudoV1beta1().Instances(installNamespace).Create(&testInstance)
_, err := k2o.kudoClientset.KudoV1beta1().Instances(installNamespace).Create(&testInstance)
if err != nil {
t.Fatalf("error creating instance in tests setup for")
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/kudoctl/util/kudo/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kubefake "k8s.io/client-go/kubernetes/fake"

"github.com/kudobuilder/kudo/pkg/apis/kudo/v1beta1"
"github.com/kudobuilder/kudo/pkg/client/clientset/versioned/fake"
Expand Down Expand Up @@ -64,7 +65,7 @@ func Test_UpgradeOperatorVersion(t *testing.T) {
}

for _, tt := range tests {
c := NewClientFromK8s(fake.NewSimpleClientset())
c := NewClientFromK8s(fake.NewSimpleClientset(), kubefake.NewSimpleClientset())
if tt.instanceExists {
if _, err := c.InstallInstanceObjToCluster(&testInstance, installNamespace); err != nil {
t.Errorf("%s: failed to install instance: %v", tt.name, err)
Expand Down

0 comments on commit 87b7966

Please sign in to comment.