forked from knative/client
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(serving): Remove hardcoded GVK and look it up from schema
Fixes knative#133.
- Loading branch information
Showing
8 changed files
with
122 additions
and
30 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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,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())) | ||
} |
This file contains 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,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") | ||
} | ||
} |