From 2a585aed83a3d9b3668b316e49308cae0f81065c Mon Sep 17 00:00:00 2001 From: Jan Schlicht Date: Fri, 8 Nov 2019 09:52:49 +0100 Subject: [PATCH] Improve the check for KUDO CRDs when creating a client (#1037) CRDs aren't tied to a namespace. By checking for them directly through the 'apiextensions' client is a cleaner approach than checking if listing CRDs objects succeed. --- go.mod | 1 - go.sum | 2 -- pkg/kudoctl/env/environment.go | 2 +- pkg/kudoctl/util/kudo/kudo.go | 17 +++++++++++++---- pkg/kudoctl/util/kudo/kudo_test.go | 2 +- 5 files changed, 15 insertions(+), 9 deletions(-) diff --git a/go.mod b/go.mod index 606eaa546..1f8aec42e 100644 --- a/go.mod +++ b/go.mod @@ -14,7 +14,6 @@ require ( github.com/dustinkirkland/golang-petname v0.0.0-20170921220637-d3c2ba80e75e github.com/go-test/deep v1.0.1 github.com/gogo/protobuf v1.3.1 // indirect - github.com/golang-collections/collections v0.0.0-20130729185459-604e922904d3 github.com/golangci/golangci-lint v1.21.0 github.com/google/shlex v0.0.0-20181106134648-c34317bd91bf github.com/gophercloud/gophercloud v0.2.0 // indirect diff --git a/go.sum b/go.sum index f0b8a2bb8..9de025264 100644 --- a/go.sum +++ b/go.sum @@ -217,8 +217,6 @@ github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zV github.com/gogo/protobuf v1.2.2-0.20190723190241-65acae22fc9d/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o= github.com/gogo/protobuf v1.3.1 h1:DqDEcV5aeaTmdFBePNpYsp3FlcVH/2ISVVM9Qf8PSls= github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o= -github.com/golang-collections/collections v0.0.0-20130729185459-604e922904d3 h1:zN2lZNZRflqFyxVaTIU61KNKQ9C0055u9CAfpmqUvo4= -github.com/golang-collections/collections v0.0.0-20130729185459-604e922904d3/go.mod h1:nPpo7qLxd6XL3hWJG/O60sR8ZKfMCiIoNap5GvD12KU= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/groupcache v0.0.0-20160516000752-02826c3e7903/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= diff --git a/pkg/kudoctl/env/environment.go b/pkg/kudoctl/env/environment.go index 237e2d96a..c5ac304cf 100644 --- a/pkg/kudoctl/env/environment.go +++ b/pkg/kudoctl/env/environment.go @@ -68,5 +68,5 @@ func (s *Settings) OverrideDefault(fs *pflag.FlagSet, name, value string) string // GetClient is a helper function that takes the Settings struct and returns a new KUDO Client func GetClient(s *Settings) (*kudo.Client, error) { - return kudo.NewClient(s.Namespace, s.KubeConfig, s.RequestTimeout) + return kudo.NewClient(s.KubeConfig, s.RequestTimeout) } diff --git a/pkg/kudoctl/util/kudo/kudo.go b/pkg/kudoctl/util/kudo/kudo.go index b2a9468d4..7ff97cea2 100644 --- a/pkg/kudoctl/util/kudo/kudo.go +++ b/pkg/kudoctl/util/kudo/kudo.go @@ -14,6 +14,7 @@ import ( "github.com/pkg/errors" v1core "k8s.io/api/core/v1" + extensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/typed/apiextensions/v1beta1" apierrors "k8s.io/apimachinery/pkg/api/errors" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" @@ -30,7 +31,7 @@ type Client struct { } // NewClient creates new KUDO Client -func NewClient(namespace, kubeConfigPath string, requestTimeout int64) (*Client, error) { +func NewClient(kubeConfigPath string, requestTimeout int64) (*Client, error) { // use the current context in kubeconfig config, err := clientcmd.BuildConfigFromFlags("", kubeConfigPath) @@ -47,15 +48,23 @@ func NewClient(namespace, kubeConfigPath string, requestTimeout int64) (*Client, return nil, err } - _, err = kudoClientset.KudoV1beta1().Operators(namespace).List(v1.ListOptions{}) + // use the apiextensions clientset to check for the existence of KUDO CRDs in the cluster + extensionsClientset, err := extensionsclient.NewForConfig(config) + if err != nil { + return nil, err + } + + _, err = extensionsClientset.CustomResourceDefinitions().Get("operators.kudo.dev", v1.GetOptions{}) if err != nil { return nil, errors.WithMessage(err, "operators") } - _, err = kudoClientset.KudoV1beta1().OperatorVersions(namespace).List(v1.ListOptions{}) + + _, err = extensionsClientset.CustomResourceDefinitions().Get("operatorversions.kudo.dev", v1.GetOptions{}) if err != nil { return nil, errors.WithMessage(err, "operatorversions") } - _, err = kudoClientset.KudoV1beta1().Instances(namespace).List(v1.ListOptions{}) + + _, err = extensionsClientset.CustomResourceDefinitions().Get("instances.kudo.dev", v1.GetOptions{}) if err != nil { return nil, errors.WithMessage(err, "instances") } diff --git a/pkg/kudoctl/util/kudo/kudo_test.go b/pkg/kudoctl/util/kudo/kudo_test.go index dd92b6f3a..8299b3158 100644 --- a/pkg/kudoctl/util/kudo/kudo_test.go +++ b/pkg/kudoctl/util/kudo/kudo_test.go @@ -28,7 +28,7 @@ func TestNewK2oClient(t *testing.T) { for _, tt := range tests { // Just interested in errors - _, err := NewClient("default", "", 0) + _, err := NewClient("", 0) assert.ErrorContains(t, err, tt.err) } }