Skip to content

Commit

Permalink
Improve the check for KUDO CRDs when creating a client (#1037)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Jan Schlicht authored Nov 8, 2019
1 parent 433e820 commit 2a585ae
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 9 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
2 changes: 1 addition & 1 deletion pkg/kudoctl/env/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
17 changes: 13 additions & 4 deletions pkg/kudoctl/util/kudo/kudo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand All @@ -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")
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/kudoctl/util/kudo/kudo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down

0 comments on commit 2a585ae

Please sign in to comment.