Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Populate GVK in objects fetched from API server. #1280

Merged
merged 3 commits into from
Jan 17, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions pkg/kudoctl/util/kudo/kudo.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
v1core "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/discovery"

Expand All @@ -19,6 +20,7 @@ import (

"github.com/kudobuilder/kudo/pkg/apis/kudo/v1beta1"
"github.com/kudobuilder/kudo/pkg/client/clientset/versioned"
"github.com/kudobuilder/kudo/pkg/client/clientset/versioned/scheme"
"github.com/kudobuilder/kudo/pkg/kudoctl/clog"
"github.com/kudobuilder/kudo/pkg/kudoctl/kube"
"github.com/kudobuilder/kudo/pkg/kudoctl/kudoinit/crd"
Expand Down Expand Up @@ -127,13 +129,33 @@ func (c *Client) InstanceExistsInCluster(operatorName, namespace, version, insta
return true, nil
}

// Populate the GVK from scheme, since it is cleared by design on typed objects.
// https://github.com/kubernetes/client-go/issues/413
func setGVKFromScheme(object runtime.Object) error {
gvks, unversioned, err := scheme.Scheme.ObjectKinds(object)
if err != nil {
return err
}
if len(gvks) == 0 {
return fmt.Errorf("no ObjectKinds available for %T", object)
}
if !unversioned {
object.GetObjectKind().SetGroupVersionKind(gvks[0])
}
return nil
}

// 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{})
if apierrors.IsNotFound(err) {
return nil, nil
}
if err != nil {
return instance, err
}
err = setGVKFromScheme(instance)
return instance, err
}

Expand All @@ -144,6 +166,10 @@ func (c *Client) GetOperatorVersion(name, namespace string) (*v1beta1.OperatorVe
if apierrors.IsNotFound(err) {
return nil, nil
}
if err != nil {
return ov, err
}
err = setGVKFromScheme(ov)
return ov, err
}

Expand Down