-
Notifications
You must be signed in to change notification settings - Fork 274
fix(serving): Update GVK on objects within a ResourcePrinter. #153
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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())) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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") | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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()?There was a problem hiding this comment.
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.