Skip to content

Commit

Permalink
fix(serving): Remove hardcoded GVK and look it up from schema
Browse files Browse the repository at this point in the history
  • Loading branch information
rhuss committed Jun 8, 2019
1 parent 1b471d5 commit fbb0a48
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 30 deletions.
9 changes: 4 additions & 5 deletions pkg/kn/commands/revision/revision_describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/knative/client/pkg/kn/commands"
"github.com/spf13/cobra"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/cli-runtime/pkg/genericclioptions"
)

Expand Down Expand Up @@ -52,10 +51,10 @@ func NewRevisionDescribeCommand(p *commands.KnParams) *cobra.Command {
if err != nil {
return err
}
revision.GetObjectKind().SetGroupVersionKind(schema.GroupVersionKind{
Group: "knative.dev",
Version: "v1alpha1",
Kind: "Revision"})
if err := client.UpdateGroupVersionKind(revision); err != nil {
return err
}

err = printer.PrintObj(revision, cmd.OutOrStdout())
if err != nil {
return err
Expand Down
9 changes: 4 additions & 5 deletions pkg/kn/commands/revision/revision_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/knative/client/pkg/kn/commands"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
)

// NewRevisionGetCommand represents 'kn revision get' command
Expand All @@ -47,10 +46,10 @@ func NewRevisionGetCommand(p *commands.KnParams) *cobra.Command {
fmt.Fprintf(cmd.OutOrStdout(), "No resources found.\n")
return nil
}
revision.GetObjectKind().SetGroupVersionKind(schema.GroupVersionKind{
Group: "knative.dev",
Version: "v1alpha1",
Kind: "revision"})
if err := client.UpdateGroupVersionKind(revision); err != nil {
return err
}

printer, err := revisionGetFlags.ToPrinter()
if err != nil {
return err
Expand Down
9 changes: 4 additions & 5 deletions pkg/kn/commands/service/service_describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/knative/client/pkg/kn/commands"
"github.com/spf13/cobra"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/cli-runtime/pkg/genericclioptions"
)

Expand Down Expand Up @@ -51,10 +50,10 @@ func NewServiceDescribeCommand(p *commands.KnParams) *cobra.Command {
if err != nil {
return err
}
describeService.GetObjectKind().SetGroupVersionKind(schema.GroupVersionKind{
Group: "knative.dev",
Version: "v1alpha1",
Kind: "Service"})
if err := client.UpdateGroupVersionKind(describeService); err != nil {
return err
}

err = printer.PrintObj(describeService, cmd.OutOrStdout())
if err != nil {
return err
Expand Down
8 changes: 3 additions & 5 deletions pkg/kn/commands/service/service_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/knative/client/pkg/kn/commands"
"github.com/spf13/cobra"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
)

// NewServiceGetCommand represents 'kn service get' command
Expand All @@ -47,10 +46,9 @@ func NewServiceGetCommand(p *commands.KnParams) *cobra.Command {
fmt.Fprintf(cmd.OutOrStdout(), "No resources found.\n")
return nil
}
service.GetObjectKind().SetGroupVersionKind(schema.GroupVersionKind{
Group: "knative.dev",
Version: "v1alpha1",
Kind: "Service"})
if err := client.UpdateGroupVersionKind(service); err != nil {
return err
}

printer, err := serviceGetFlags.ToPrinter()
if err != nil {
Expand Down
22 changes: 17 additions & 5 deletions pkg/kn/commands/test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,23 @@ package commands
import (
"bytes"
"flag"
"k8s.io/apimachinery/pkg/runtime"

serving "github.com/knative/serving/pkg/client/clientset/versioned/typed/serving/v1alpha1"
"github.com/knative/serving/pkg/client/clientset/versioned/typed/serving/v1alpha1/fake"
"github.com/spf13/cobra"
client_testing "k8s.io/client-go/testing"

servinglib "github.com/knative/client/pkg/serving"
serving_v1alpha1_api "github.com/knative/serving/pkg/apis/serving/v1alpha1"
)

func CreateTestKnCommand(cmd *cobra.Command, knParams *KnParams) (*cobra.Command, *fake.FakeServingV1alpha1, *bytes.Buffer) {
func CreateTestKnCommand(cmd *cobra.Command, knParams *KnParams) (*cobra.Command, *fakeServingV1alpha1Wrapper, *bytes.Buffer) {
buf := new(bytes.Buffer)
fakeServing := &fake.FakeServingV1alpha1{&client_testing.Fake{}}
fakeServing := fakeServingV1alpha1Wrapper{&fake.FakeServingV1alpha1{&client_testing.Fake{}}}
knParams.Output = buf
knParams.ServingFactory = func() (serving.ServingV1alpha1Interface, error) { return fakeServing, nil }
knParams.ServingFactory = func() (ServingV1alpha1InterfaceWithGvkUpdater, error) { return fakeServing, nil }
knCommand := newKnCommand(cmd, knParams)
return knCommand, fakeServing, buf
return knCommand, &fakeServing, buf
}

func newKnCommand(subCommand *cobra.Command, params *KnParams) *cobra.Command {
Expand Down Expand Up @@ -64,3 +67,12 @@ Eventing: Manage event subscriptions and channels. Connect up event sources.`,
flag.CommandLine.Parse([]string{})
return rootCmd
}

// Helper struct to adding UpdateGroupVersionKind to the fake client, too
type fakeServingV1alpha1Wrapper struct {
*fake.FakeServingV1alpha1
}

func (fakeServingV1alpha1Wrapper) UpdateGroupVersionKind(obj runtime.Object) error {
return servinglib.UpdateGroupVersionKind(obj, serving_v1alpha1_api.SchemeGroupVersion)
}
32 changes: 27 additions & 5 deletions pkg/kn/commands/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ package commands

import (
"io"
"k8s.io/apimachinery/pkg/runtime"

serving "github.com/knative/serving/pkg/client/clientset/versioned/typed/serving/v1alpha1"
servinglib "github.com/knative/client/pkg/serving"

serving_v1alpha1_api "github.com/knative/serving/pkg/apis/serving/v1alpha1"
serving_v1alpha1_client "github.com/knative/serving/pkg/client/clientset/versioned/typed/serving/v1alpha1"
"k8s.io/client-go/tools/clientcmd"
)

Expand All @@ -27,10 +31,28 @@ var CfgFile string
// KubeCfgFile is the path for the Kubernetes config
var KubeCfgFile string

// Extend v1alpha1 serving client by a method for updating GVK
type ServingV1alpha1InterfaceWithGvkUpdater interface {
serving_v1alpha1_client.ServingV1alpha1Interface

// Update with the registered V1alpha1 GVK
UpdateGroupVersionKind(obj runtime.Object) error
}

// Delegate to v1alpha1 client
type ServingV1alphaClientWithGvkUpdater struct {
*serving_v1alpha1_client.ServingV1alpha1Client
}

// Update GVK from v1alpha1 schema. Needs to be called by any object returned from the client
func (cl ServingV1alphaClientWithGvkUpdater) UpdateGroupVersionKind(obj runtime.Object) error {
return servinglib.UpdateGroupVersionKind(obj, serving_v1alpha1_api.SchemeGroupVersion)
}

// Parameters for creating commands. Useful for inserting mocks for testing.
type KnParams struct {
Output io.Writer
ServingFactory func() (serving.ServingV1alpha1Interface, error)
ServingFactory func() (ServingV1alpha1InterfaceWithGvkUpdater, error)
}

func (c *KnParams) Initialize() {
Expand All @@ -39,14 +61,14 @@ func (c *KnParams) Initialize() {
}
}

func GetConfig() (serving.ServingV1alpha1Interface, error) {
func GetConfig() (ServingV1alpha1InterfaceWithGvkUpdater, error) {
config, err := clientcmd.BuildConfigFromFlags("", KubeCfgFile)
if err != nil {
return nil, err
}
client, err := serving.NewForConfig(config)
client, err := serving_v1alpha1_client.NewForConfig(config)
if err != nil {
return nil, err
}
return client, nil
return ServingV1alphaClientWithGvkUpdater{client}, nil
}
34 changes: 34 additions & 0 deletions pkg/serving/schema_handling.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package serving

import (
"errors"
"fmt"

"github.com/knative/serving/pkg/client/clientset/versioned/scheme"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)

// Update the GVK on the given object, based on the GVK registered in into the serving scheme
// for the given GroupVersion
func UpdateGroupVersionKind(obj runtime.Object, gv schema.GroupVersion) error {
gvk, err := getGroupVersionKind(obj, gv)
if err != nil {
return err
}
obj.GetObjectKind().SetGroupVersionKind(*gvk)
return nil
}

func getGroupVersionKind(obj runtime.Object, gv schema.GroupVersion) (*schema.GroupVersionKind, error) {
gvks, _, err := scheme.Scheme.ObjectKinds(obj)
if err != nil {
return nil, err
}
for _, gvk := range gvks {
if gvk.GroupVersion() == gv {
return &gvk, nil
}
}
return nil, errors.New(fmt.Sprintf("no group version %s registered in %s", gv, scheme.Scheme.Name()))
}
29 changes: 29 additions & 0 deletions pkg/serving/schema_handling_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package serving

import (
"github.com/knative/serving/pkg/apis/serving/v1alpha1"
"k8s.io/apimachinery/pkg/runtime/schema"
"testing"
)

func TestGVKUpdate(t *testing.T) {
service := v1alpha1.Service{}
err := UpdateGroupVersionKind(&service, v1alpha1.SchemeGroupVersion)
if err != nil {
t.Fatalf("cannot update GVK to a service %v", err)
}
if service.Kind != "Service" {
t.Fatalf("wrong kind '%s'", service.Kind)
}
if service.APIVersion != v1alpha1.SchemeGroupVersion.Group+"/"+v1alpha1.SchemeGroupVersion.Version {
t.Fatalf("wrong version '%s'", service.APIVersion)
}
}

func TestGVKUpdateNegative(t *testing.T) {
service := v1alpha1.Service{}
err := UpdateGroupVersionKind(&service, schema.GroupVersion{Group: "bla", Version: "blub"})
if err == nil {
t.Fatal("expect an error for an unregistered group version")
}
}

0 comments on commit fbb0a48

Please sign in to comment.