Skip to content
Closed
Show file tree
Hide file tree
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
7 changes: 2 additions & 5 deletions pkg/kn/commands/revision_describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ package commands

import (
"errors"
"github.com/knative/client/pkg/printers"

"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 +51,7 @@ func NewRevisionDescribeCommand(p *KnParams) *cobra.Command {
if err != nil {
return err
}
revision.GetObjectKind().SetGroupVersionKind(schema.GroupVersionKind{
Group: "knative.dev",
Version: "v1alpha1",
Kind: "Revision"})
printer = printers.NewGvkUpdatePrinter(printer)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In #134 you would call out to serving.UpdateGroupVersionKind() instead of wrapping the printer, which calls this method on print. Still, you have a specialized handling.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we hide printers.NewGvkUpdatePrinter(printer) in revisionDescribePrintFlags.ToPrinter()?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, let me align it with the service handling.

err = printer.PrintObj(revision, cmd.OutOrStdout())
if err != nil {
return err
Expand Down
7 changes: 1 addition & 6 deletions pkg/kn/commands/revision_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ package commands

import (
"fmt"

"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 @@ -46,10 +44,7 @@ func NewRevisionGetCommand(p *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"})

printer, err := revisionGetFlags.ToPrinter()
if err != nil {
return err
Expand Down
7 changes: 4 additions & 3 deletions pkg/kn/commands/revision_get_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ package commands

import (
hprinters "github.com/knative/client/pkg/printers"
serving "github.com/knative/serving/pkg/apis/serving"
"github.com/knative/serving/pkg/apis/serving"

servingv1alpha1 "github.com/knative/serving/pkg/apis/serving/v1alpha1"
"github.com/spf13/cobra"
metav1beta1 "k8s.io/apimachinery/pkg/apis/meta/v1beta1"
Expand Down Expand Up @@ -47,14 +48,14 @@ func (f *RevisionGetFlags) ToPrinter() (hprinters.ResourcePrinter, error) {
if err != nil {
return nil, err
}
return p, nil
return hprinters.NewGvkUpdatePrinter(p), nil
}
// if no flags specified, use the table printing
p, err := f.HumanReadableFlags.ToPrinter()
if err != nil {
return nil, err
}
return p, nil
return hprinters.NewGvkUpdatePrinter(p), nil
}

// AddFlags receives a *cobra.Command reference and binds
Expand Down
2 changes: 1 addition & 1 deletion pkg/kn/commands/revision_get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func createMockRevisionWithParams(name, svcName string) *v1alpha1.Revision {
revision := &v1alpha1.Revision{
TypeMeta: metav1.TypeMeta{
Kind: "Revision",
APIVersion: "knative.dev/v1alpha1",
APIVersion: "serving.knative.dev/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Expand Down
8 changes: 2 additions & 6 deletions pkg/kn/commands/service_describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ package commands

import (
"errors"

"github.com/knative/client/pkg/printers"
"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,7 @@ func NewServiceDescribeCommand(p *KnParams) *cobra.Command {
if err != nil {
return err
}
describeService.GetObjectKind().SetGroupVersionKind(schema.GroupVersionKind{
Group: "knative.dev",
Version: "v1alpha1",
Kind: "Service"})
printer = printers.NewGvkUpdatePrinter(printer)
err = printer.PrintObj(describeService, cmd.OutOrStdout())
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion pkg/kn/commands/service_describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestServiceDescribeDefaultOutput(t *testing.T) {
expectedService := v1alpha1.Service{
TypeMeta: metav1.TypeMeta{
Kind: "Service",
APIVersion: "knative.dev/v1alpha1",
APIVersion: "serving.knative.dev/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Expand Down
5 changes: 0 additions & 5 deletions pkg/kn/commands/service_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (

"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 @@ -46,10 +45,6 @@ func NewServiceGetCommand(p *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"})

printer, err := serviceGetFlags.ToPrinter()
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/kn/commands/service_get_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ func (f *ServiceGetFlags) ToPrinter() (hprinters.ResourcePrinter, error) {
if err != nil {
return nil, err
}
return p, nil
return hprinters.NewGvkUpdatePrinter(p), nil
}
// if no flags specified, use the table printing
p, err := f.HumanReadableFlags.ToPrinter()
if err != nil {
return nil, err
}
return p, nil
return hprinters.NewGvkUpdatePrinter(p), nil
}

// AddFlags receives a *cobra.Command reference and binds
Expand Down
2 changes: 1 addition & 1 deletion pkg/kn/commands/service_get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func createMockServiceWithParams(name, domain string, generation int64) *v1alpha
service := &v1alpha1.Service{
TypeMeta: metav1.TypeMeta{
Kind: "Service",
APIVersion: "knative.dev/v1alpha1",
APIVersion: "serving.knative.dev/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Expand Down
6 changes: 3 additions & 3 deletions pkg/kn/commands/service_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestServiceUpdateImage(t *testing.T) {
orig := &v1alpha1.Service{
TypeMeta: metav1.TypeMeta{
Kind: "Service",
APIVersion: "knative.dev/v1alpha1",
APIVersion: "serving.knative.dev/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Expand Down Expand Up @@ -120,7 +120,7 @@ func TestServiceUpdateEnv(t *testing.T) {
orig := &v1alpha1.Service{
TypeMeta: metav1.TypeMeta{
Kind: "Service",
APIVersion: "knative.dev/v1alpha1",
APIVersion: "serving.knative.dev/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Expand Down Expand Up @@ -289,7 +289,7 @@ func createMockServiceWithResources(t *testing.T, requestCPU, requestMemory, lim
service := &v1alpha1.Service{
TypeMeta: metav1.TypeMeta{
Kind: "Service",
APIVersion: "knative.dev/v1alpha1",
APIVersion: "serving.knative.dev/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Expand Down
40 changes: 40 additions & 0 deletions pkg/printers/gvk_update_writer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright © 2019 The Knative Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package printers

import (
"github.com/knative/client/pkg/serving"
"github.com/knative/serving/pkg/apis/serving/v1alpha1"
"io"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/cli-runtime/pkg/genericclioptions/printers"
)

type wrappedGvkUpdatePrinter struct {
printer printers.ResourcePrinter
}

// Wrapper printer for updating an object with GVK just before printing
func NewGvkUpdatePrinter(delegate printers.ResourcePrinter) printers.ResourcePrinter {
return wrappedGvkUpdatePrinter{delegate}
}

func (gvkPrinter wrappedGvkUpdatePrinter) PrintObj(obj runtime.Object, writer io.Writer) error {
// TODO: How to deal with multiple versions which needs to be supported soon ?
if err := serving.UpdateGroupVersionKind(obj, v1alpha1.SchemeGroupVersion); err != nil {
return err
}
return gvkPrinter.printer.PrintObj(obj, writer)
}
48 changes: 48 additions & 0 deletions pkg/serving/schema_handling.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright © 2019 The Knative Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

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()))
}
43 changes: 43 additions & 0 deletions pkg/serving/schema_handling_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright © 2019 The Knative Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

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")
}
}