From d9920c53a575a7c017a9cd120220ef2ad761a962 Mon Sep 17 00:00:00 2001 From: Chao Xu Date: Mon, 10 Apr 2017 14:20:02 -0700 Subject: [PATCH] move ref.go to its own subpackage --- pkg/api/BUILD | 12 +- pkg/api/objectreference.go | 34 +++++ pkg/api/ref/BUILD | 46 ++++++ pkg/api/{ => ref}/ref.go | 25 +--- pkg/api/{ => ref}/ref_test.go | 23 +-- pkg/api/{methods.go => taint.go} | 10 -- pkg/api/{methods_test.go => taint_test.go} | 0 pkg/api/toleration.go | 30 ++++ .../typed/core/internalversion/BUILD | 1 + .../core/internalversion/event_expansion.go | 3 +- pkg/kubectl/cmd/BUILD | 1 + pkg/kubectl/cmd/drain_test.go | 3 +- pkg/printers/internalversion/BUILD | 1 + pkg/printers/internalversion/describe.go | 5 +- pkg/proxy/userspace/proxier.go | 2 +- plugin/pkg/admission/podpreset/BUILD | 1 + plugin/pkg/admission/podpreset/admission.go | 3 +- .../client-go/pkg/api/objectreference.go | 34 +++++ staging/src/k8s.io/client-go/pkg/api/ref.go | 132 ------------------ .../pkg/api/{methods.go => taint.go} | 10 -- .../k8s.io/client-go/pkg/api/toleration.go | 30 ++++ vendor/BUILD | 6 +- 22 files changed, 215 insertions(+), 197 deletions(-) create mode 100644 pkg/api/objectreference.go create mode 100644 pkg/api/ref/BUILD rename pkg/api/{ => ref}/ref.go (80%) rename pkg/api/{ => ref}/ref_test.go (88%) rename pkg/api/{methods.go => taint.go} (70%) rename pkg/api/{methods_test.go => taint_test.go} (100%) create mode 100644 pkg/api/toleration.go create mode 100644 staging/src/k8s.io/client-go/pkg/api/objectreference.go delete mode 100644 staging/src/k8s.io/client-go/pkg/api/ref.go rename staging/src/k8s.io/client-go/pkg/api/{methods.go => taint.go} (70%) create mode 100644 staging/src/k8s.io/client-go/pkg/api/toleration.go diff --git a/pkg/api/BUILD b/pkg/api/BUILD index 71bf4f8e11f..9ba133e16e6 100644 --- a/pkg/api/BUILD +++ b/pkg/api/BUILD @@ -16,16 +16,16 @@ go_library( "doc.go", "field_constants.go", "json.go", - "methods.go", - "ref.go", + "objectreference.go", "register.go", "resource_helpers.go", + "taint.go", + "toleration.go", "types.go", "zz_generated.deepcopy.go", ], tags = ["automanaged"], deps = [ - "//vendor:k8s.io/apimachinery/pkg/api/meta", "//vendor:k8s.io/apimachinery/pkg/api/resource", "//vendor:k8s.io/apimachinery/pkg/apimachinery/announced", "//vendor:k8s.io/apimachinery/pkg/apimachinery/registered", @@ -44,17 +44,14 @@ go_library( go_test( name = "go_default_test", srcs = [ - "methods_test.go", - "ref_test.go", "resource_helpers_test.go", + "taint_test.go", ], library = ":go_default_library", tags = ["automanaged"], deps = [ "//vendor:k8s.io/apimachinery/pkg/api/resource", "//vendor:k8s.io/apimachinery/pkg/apis/meta/v1", - "//vendor:k8s.io/apimachinery/pkg/runtime", - "//vendor:k8s.io/apimachinery/pkg/runtime/schema", ], ) @@ -120,6 +117,7 @@ filegroup( "//pkg/api/install:all-srcs", "//pkg/api/meta:all-srcs", "//pkg/api/pod:all-srcs", + "//pkg/api/ref:all-srcs", "//pkg/api/resource:all-srcs", "//pkg/api/service:all-srcs", "//pkg/api/testapi:all-srcs", diff --git a/pkg/api/objectreference.go b/pkg/api/objectreference.go new file mode 100644 index 00000000000..b36ecab27ff --- /dev/null +++ b/pkg/api/objectreference.go @@ -0,0 +1,34 @@ +/* +Copyright 2017 The Kubernetes 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. +*/ + +//TODO: consider making these methods functions, because we don't want helper +//functions in the k8s.io/api repo. + +package api + +import ( + "k8s.io/apimachinery/pkg/runtime/schema" +) + +func (obj *ObjectReference) SetGroupVersionKind(gvk schema.GroupVersionKind) { + obj.APIVersion, obj.Kind = gvk.ToAPIVersionAndKind() +} + +func (obj *ObjectReference) GroupVersionKind() schema.GroupVersionKind { + return schema.FromAPIVersionAndKind(obj.APIVersion, obj.Kind) +} + +func (obj *ObjectReference) GetObjectKind() schema.ObjectKind { return obj } diff --git a/pkg/api/ref/BUILD b/pkg/api/ref/BUILD new file mode 100644 index 00000000000..f4be5a7c906 --- /dev/null +++ b/pkg/api/ref/BUILD @@ -0,0 +1,46 @@ +package(default_visibility = ["//visibility:public"]) + +licenses(["notice"]) + +load( + "@io_bazel_rules_go//go:def.bzl", + "go_library", + "go_test", +) + +go_test( + name = "go_default_test", + srcs = ["ref_test.go"], + library = ":go_default_library", + tags = ["automanaged"], + deps = [ + "//pkg/api:go_default_library", + "//vendor:k8s.io/apimachinery/pkg/apis/meta/v1", + "//vendor:k8s.io/apimachinery/pkg/runtime", + "//vendor:k8s.io/apimachinery/pkg/runtime/schema", + ], +) + +go_library( + name = "go_default_library", + srcs = ["ref.go"], + tags = ["automanaged"], + deps = [ + "//pkg/api:go_default_library", + "//vendor:k8s.io/apimachinery/pkg/api/meta", + "//vendor:k8s.io/apimachinery/pkg/runtime", + ], +) + +filegroup( + name = "package-srcs", + srcs = glob(["**"]), + tags = ["automanaged"], + visibility = ["//visibility:private"], +) + +filegroup( + name = "all-srcs", + srcs = [":package-srcs"], + tags = ["automanaged"], +) diff --git a/pkg/api/ref.go b/pkg/api/ref/ref.go similarity index 80% rename from pkg/api/ref.go rename to pkg/api/ref/ref.go index 370cf5513a4..52a4b1d7589 100644 --- a/pkg/api/ref.go +++ b/pkg/api/ref/ref.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package api +package ref import ( "errors" @@ -24,7 +24,7 @@ import ( "k8s.io/apimachinery/pkg/api/meta" "k8s.io/apimachinery/pkg/runtime" - "k8s.io/apimachinery/pkg/runtime/schema" + "k8s.io/kubernetes/pkg/api" ) var ( @@ -37,11 +37,11 @@ var ( // object, or an error if the object doesn't follow the conventions // that would allow this. // TODO: should take a meta.Interface see http://issue.k8s.io/7127 -func GetReference(scheme *runtime.Scheme, obj runtime.Object) (*ObjectReference, error) { +func GetReference(scheme *runtime.Scheme, obj runtime.Object) (*api.ObjectReference, error) { if obj == nil { return nil, ErrNilObject } - if ref, ok := obj.(*ObjectReference); ok { + if ref, ok := obj.(*api.ObjectReference); ok { // Don't make a reference to a reference. return ref, nil } @@ -93,14 +93,14 @@ func GetReference(scheme *runtime.Scheme, obj runtime.Object) (*ObjectReference, // only has list metadata if objectMeta == nil { - return &ObjectReference{ + return &api.ObjectReference{ Kind: kind, APIVersion: version, ResourceVersion: listMeta.GetResourceVersion(), }, nil } - return &ObjectReference{ + return &api.ObjectReference{ Kind: kind, APIVersion: version, Name: objectMeta.GetName(), @@ -111,7 +111,7 @@ func GetReference(scheme *runtime.Scheme, obj runtime.Object) (*ObjectReference, } // GetPartialReference is exactly like GetReference, but allows you to set the FieldPath. -func GetPartialReference(scheme *runtime.Scheme, obj runtime.Object, fieldPath string) (*ObjectReference, error) { +func GetPartialReference(scheme *runtime.Scheme, obj runtime.Object, fieldPath string) (*api.ObjectReference, error) { ref, err := GetReference(scheme, obj) if err != nil { return nil, err @@ -119,14 +119,3 @@ func GetPartialReference(scheme *runtime.Scheme, obj runtime.Object, fieldPath s ref.FieldPath = fieldPath return ref, nil } - -// IsAnAPIObject allows clients to preemptively get a reference to an API object and pass it to places that -// intend only to get a reference to that object. This simplifies the event recording interface. -func (obj *ObjectReference) SetGroupVersionKind(gvk schema.GroupVersionKind) { - obj.APIVersion, obj.Kind = gvk.ToAPIVersionAndKind() -} -func (obj *ObjectReference) GroupVersionKind() schema.GroupVersionKind { - return schema.FromAPIVersionAndKind(obj.APIVersion, obj.Kind) -} - -func (obj *ObjectReference) GetObjectKind() schema.ObjectKind { return obj } diff --git a/pkg/api/ref_test.go b/pkg/api/ref/ref_test.go similarity index 88% rename from pkg/api/ref_test.go rename to pkg/api/ref/ref_test.go index ee06a1ed82c..10a99abad7b 100644 --- a/pkg/api/ref_test.go +++ b/pkg/api/ref/ref_test.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package api +package ref import ( "reflect" @@ -23,6 +23,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" + "k8s.io/kubernetes/pkg/api" ) type FakeAPIObject struct{} @@ -41,18 +42,18 @@ func TestGetReference(t *testing.T) { // when vendoring kube, if you don't force the set of registered versions (like make test does) // then you run into trouble because the types aren't registered in the scheme by anything. This does the // register manually to allow unit test execution - if _, _, err := Scheme.ObjectKinds(&Pod{}); err != nil { - AddToScheme(Scheme) + if _, _, err := api.Scheme.ObjectKinds(&api.Pod{}); err != nil { + api.AddToScheme(api.Scheme) } table := map[string]struct { obj runtime.Object - ref *ObjectReference + ref *api.ObjectReference fieldPath string shouldErr bool }{ "pod": { - obj: &Pod{ + obj: &api.Pod{ ObjectMeta: metav1.ObjectMeta{ Name: "foo", UID: "bar", @@ -61,7 +62,7 @@ func TestGetReference(t *testing.T) { }, }, fieldPath: ".desiredState.containers[0]", - ref: &ObjectReference{ + ref: &api.ObjectReference{ Kind: "Pod", APIVersion: "version1", Name: "foo", @@ -71,13 +72,13 @@ func TestGetReference(t *testing.T) { }, }, "serviceList": { - obj: &ServiceList{ + obj: &api.ServiceList{ ListMeta: metav1.ListMeta{ ResourceVersion: "42", SelfLink: "/api/version2/services", }, }, - ref: &ObjectReference{ + ref: &api.ObjectReference{ Kind: "ServiceList", APIVersion: "version2", ResourceVersion: "42", @@ -95,7 +96,7 @@ func TestGetReference(t *testing.T) { SelfLink: "/custom_prefix/version1/extensions/foo", }, }, - ref: &ObjectReference{ + ref: &api.ObjectReference{ Kind: "ExtensionAPIObject", APIVersion: "version1", Name: "foo", @@ -104,7 +105,7 @@ func TestGetReference(t *testing.T) { }, }, "badSelfLink": { - obj: &ServiceList{ + obj: &api.ServiceList{ ListMeta: metav1.ListMeta{ ResourceVersion: "42", SelfLink: "version2/services", @@ -125,7 +126,7 @@ func TestGetReference(t *testing.T) { } for name, item := range table { - ref, err := GetPartialReference(Scheme, item.obj, item.fieldPath) + ref, err := GetPartialReference(api.Scheme, item.obj, item.fieldPath) if e, a := item.shouldErr, (err != nil); e != a { t.Errorf("%v: expected %v, got %v, err %v", name, e, a, err) continue diff --git a/pkg/api/methods.go b/pkg/api/taint.go similarity index 70% rename from pkg/api/methods.go rename to pkg/api/taint.go index b3bebfee18f..173e4e60161 100644 --- a/pkg/api/methods.go +++ b/pkg/api/taint.go @@ -21,16 +21,6 @@ package api import "fmt" -// MatchToleration checks if the toleration matches tolerationToMatch. Tolerations are unique by , -// if the two tolerations have same combination, regard as they match. -// TODO: uniqueness check for tolerations in api validations. -func (t *Toleration) MatchToleration(tolerationToMatch *Toleration) bool { - return t.Key == tolerationToMatch.Key && - t.Effect == tolerationToMatch.Effect && - t.Operator == tolerationToMatch.Operator && - t.Value == tolerationToMatch.Value -} - // MatchTaint checks if the taint matches taintToMatch. Taints are unique by key:effect, // if the two taints have same key:effect, regard as they match. func (t *Taint) MatchTaint(taintToMatch Taint) bool { diff --git a/pkg/api/methods_test.go b/pkg/api/taint_test.go similarity index 100% rename from pkg/api/methods_test.go rename to pkg/api/taint_test.go diff --git a/pkg/api/toleration.go b/pkg/api/toleration.go new file mode 100644 index 00000000000..edb73b74e1a --- /dev/null +++ b/pkg/api/toleration.go @@ -0,0 +1,30 @@ +/* +Copyright 2017 The Kubernetes 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. +*/ + +//TODO: consider making these methods functions, because we don't want helper +//functions in the k8s.io/api repo. + +package api + +// MatchToleration checks if the toleration matches tolerationToMatch. Tolerations are unique by , +// if the two tolerations have same combination, regard as they match. +// TODO: uniqueness check for tolerations in api validations. +func (t *Toleration) MatchToleration(tolerationToMatch *Toleration) bool { + return t.Key == tolerationToMatch.Key && + t.Effect == tolerationToMatch.Effect && + t.Operator == tolerationToMatch.Operator && + t.Value == tolerationToMatch.Value +} diff --git a/pkg/client/clientset_generated/internalclientset/typed/core/internalversion/BUILD b/pkg/client/clientset_generated/internalclientset/typed/core/internalversion/BUILD index 1aaf08d8d9c..e33564d88a9 100644 --- a/pkg/client/clientset_generated/internalclientset/typed/core/internalversion/BUILD +++ b/pkg/client/clientset_generated/internalclientset/typed/core/internalversion/BUILD @@ -38,6 +38,7 @@ go_library( tags = ["automanaged"], deps = [ "//pkg/api:go_default_library", + "//pkg/api/ref:go_default_library", "//pkg/api/v1:go_default_library", "//pkg/client/clientset_generated/internalclientset/scheme:go_default_library", "//vendor:k8s.io/apimachinery/pkg/apis/meta/v1", diff --git a/pkg/client/clientset_generated/internalclientset/typed/core/internalversion/event_expansion.go b/pkg/client/clientset_generated/internalclientset/typed/core/internalversion/event_expansion.go index 7d0c407b96c..fee59424f5b 100644 --- a/pkg/client/clientset_generated/internalclientset/typed/core/internalversion/event_expansion.go +++ b/pkg/client/clientset_generated/internalclientset/typed/core/internalversion/event_expansion.go @@ -24,6 +24,7 @@ import ( "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" "k8s.io/kubernetes/pkg/api" + "k8s.io/kubernetes/pkg/api/ref" "k8s.io/kubernetes/pkg/api/v1" ) @@ -100,7 +101,7 @@ func (e *events) PatchWithEventNamespace(incompleteEvent *api.Event, data []byte // object must match this event's client namespace unless the event client // was made with the "" namespace. func (e *events) Search(scheme *runtime.Scheme, objOrRef runtime.Object) (*api.EventList, error) { - ref, err := api.GetReference(scheme, objOrRef) + ref, err := ref.GetReference(scheme, objOrRef) if err != nil { return nil, err } diff --git a/pkg/kubectl/cmd/BUILD b/pkg/kubectl/cmd/BUILD index a98a7ceba60..6d5b77547cb 100644 --- a/pkg/kubectl/cmd/BUILD +++ b/pkg/kubectl/cmd/BUILD @@ -195,6 +195,7 @@ go_test( deps = [ "//pkg/api:go_default_library", "//pkg/api/annotations:go_default_library", + "//pkg/api/ref:go_default_library", "//pkg/api/testapi:go_default_library", "//pkg/api/testing:go_default_library", "//pkg/api/unversioned:go_default_library", diff --git a/pkg/kubectl/cmd/drain_test.go b/pkg/kubectl/cmd/drain_test.go index d122f4df4c4..94560147e62 100644 --- a/pkg/kubectl/cmd/drain_test.go +++ b/pkg/kubectl/cmd/drain_test.go @@ -41,6 +41,7 @@ import ( "k8s.io/apimachinery/pkg/util/wait" "k8s.io/client-go/rest/fake" "k8s.io/kubernetes/pkg/api" + "k8s.io/kubernetes/pkg/api/ref" "k8s.io/kubernetes/pkg/api/testapi" "k8s.io/kubernetes/pkg/apis/batch" "k8s.io/kubernetes/pkg/apis/extensions" @@ -795,7 +796,7 @@ func (m *MyReq) isFor(method string, path string) bool { } func refJson(t *testing.T, o runtime.Object) string { - ref, err := api.GetReference(api.Scheme, o) + ref, err := ref.GetReference(api.Scheme, o) if err != nil { t.Fatalf("unexpected error: %v", err) } diff --git a/pkg/printers/internalversion/BUILD b/pkg/printers/internalversion/BUILD index 77741d516ae..2b73b4d3ce8 100644 --- a/pkg/printers/internalversion/BUILD +++ b/pkg/printers/internalversion/BUILD @@ -63,6 +63,7 @@ go_library( "//pkg/api/annotations:go_default_library", "//pkg/api/events:go_default_library", "//pkg/api/helper:go_default_library", + "//pkg/api/ref:go_default_library", "//pkg/apis/apps:go_default_library", "//pkg/apis/autoscaling:go_default_library", "//pkg/apis/batch:go_default_library", diff --git a/pkg/printers/internalversion/describe.go b/pkg/printers/internalversion/describe.go index 14b750ef40c..e436c40e2ec 100644 --- a/pkg/printers/internalversion/describe.go +++ b/pkg/printers/internalversion/describe.go @@ -48,6 +48,7 @@ import ( "k8s.io/kubernetes/pkg/api/annotations" "k8s.io/kubernetes/pkg/api/events" "k8s.io/kubernetes/pkg/api/helper" + "k8s.io/kubernetes/pkg/api/ref" "k8s.io/kubernetes/pkg/apis/apps" "k8s.io/kubernetes/pkg/apis/autoscaling" "k8s.io/kubernetes/pkg/apis/batch" @@ -521,7 +522,7 @@ func (d *PodDescriber) Describe(namespace, name string, describerSettings printe var events *api.EventList if describerSettings.ShowEvents { - if ref, err := api.GetReference(api.Scheme, pod); err != nil { + if ref, err := ref.GetReference(api.Scheme, pod); err != nil { glog.Errorf("Unable to construct reference to '%#v': %v", pod, err) } else { ref.Kind = "" @@ -2033,7 +2034,7 @@ func (d *NodeDescriber) Describe(namespace, name string, describerSettings print var events *api.EventList if describerSettings.ShowEvents { - if ref, err := api.GetReference(api.Scheme, node); err != nil { + if ref, err := ref.GetReference(api.Scheme, node); err != nil { glog.Errorf("Unable to construct reference to '%#v': %v", node, err) } else { // TODO: We haven't decided the namespace for Node object yet. diff --git a/pkg/proxy/userspace/proxier.go b/pkg/proxy/userspace/proxier.go index 52aeb410dd4..d1fcc2dc981 100644 --- a/pkg/proxy/userspace/proxier.go +++ b/pkg/proxy/userspace/proxier.go @@ -411,7 +411,7 @@ func (proxier *Proxier) OnServiceUpdate(services []*api.Service) { continue } - // TODO: should this just be api.GetReference? + // TODO: should this just be ref.GetReference? svcGVK := service.GetObjectKind().GroupVersionKind() svcRef := api.ObjectReference{ Kind: svcGVK.Kind, diff --git a/plugin/pkg/admission/podpreset/BUILD b/plugin/pkg/admission/podpreset/BUILD index d276e215106..7720e680994 100644 --- a/plugin/pkg/admission/podpreset/BUILD +++ b/plugin/pkg/admission/podpreset/BUILD @@ -33,6 +33,7 @@ go_library( tags = ["automanaged"], deps = [ "//pkg/api:go_default_library", + "//pkg/api/ref:go_default_library", "//pkg/apis/settings:go_default_library", "//pkg/client/clientset_generated/internalclientset:go_default_library", "//pkg/client/informers/informers_generated/internalversion:go_default_library", diff --git a/plugin/pkg/admission/podpreset/admission.go b/plugin/pkg/admission/podpreset/admission.go index 832f7d84198..5948de35866 100644 --- a/plugin/pkg/admission/podpreset/admission.go +++ b/plugin/pkg/admission/podpreset/admission.go @@ -28,6 +28,7 @@ import ( "k8s.io/apimachinery/pkg/labels" "k8s.io/apiserver/pkg/admission" "k8s.io/kubernetes/pkg/api" + "k8s.io/kubernetes/pkg/api/ref" "k8s.io/kubernetes/pkg/apis/settings" "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset" informers "k8s.io/kubernetes/pkg/client/informers/informers_generated/internalversion" @@ -298,7 +299,7 @@ func mergeVolumes(pip *settings.PodPreset, original []api.Volume) ([]api.Volume, } func (c *podPresetPlugin) addEvent(pod *api.Pod, pip *settings.PodPreset, message string) { - ref, err := api.GetReference(api.Scheme, pod) + ref, err := ref.GetReference(api.Scheme, pod) if err != nil { glog.Errorf("pip %s: get reference for pod %s failed: %v", pip.GetName(), pod.GetName(), err) return diff --git a/staging/src/k8s.io/client-go/pkg/api/objectreference.go b/staging/src/k8s.io/client-go/pkg/api/objectreference.go new file mode 100644 index 00000000000..b36ecab27ff --- /dev/null +++ b/staging/src/k8s.io/client-go/pkg/api/objectreference.go @@ -0,0 +1,34 @@ +/* +Copyright 2017 The Kubernetes 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. +*/ + +//TODO: consider making these methods functions, because we don't want helper +//functions in the k8s.io/api repo. + +package api + +import ( + "k8s.io/apimachinery/pkg/runtime/schema" +) + +func (obj *ObjectReference) SetGroupVersionKind(gvk schema.GroupVersionKind) { + obj.APIVersion, obj.Kind = gvk.ToAPIVersionAndKind() +} + +func (obj *ObjectReference) GroupVersionKind() schema.GroupVersionKind { + return schema.FromAPIVersionAndKind(obj.APIVersion, obj.Kind) +} + +func (obj *ObjectReference) GetObjectKind() schema.ObjectKind { return obj } diff --git a/staging/src/k8s.io/client-go/pkg/api/ref.go b/staging/src/k8s.io/client-go/pkg/api/ref.go deleted file mode 100644 index 370cf5513a4..00000000000 --- a/staging/src/k8s.io/client-go/pkg/api/ref.go +++ /dev/null @@ -1,132 +0,0 @@ -/* -Copyright 2014 The Kubernetes 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 api - -import ( - "errors" - "fmt" - "net/url" - "strings" - - "k8s.io/apimachinery/pkg/api/meta" - "k8s.io/apimachinery/pkg/runtime" - "k8s.io/apimachinery/pkg/runtime/schema" -) - -var ( - // Errors that could be returned by GetReference. - ErrNilObject = errors.New("can't reference a nil object") - ErrNoSelfLink = errors.New("selfLink was empty, can't make reference") -) - -// GetReference returns an ObjectReference which refers to the given -// object, or an error if the object doesn't follow the conventions -// that would allow this. -// TODO: should take a meta.Interface see http://issue.k8s.io/7127 -func GetReference(scheme *runtime.Scheme, obj runtime.Object) (*ObjectReference, error) { - if obj == nil { - return nil, ErrNilObject - } - if ref, ok := obj.(*ObjectReference); ok { - // Don't make a reference to a reference. - return ref, nil - } - - gvk := obj.GetObjectKind().GroupVersionKind() - - // if the object referenced is actually persisted, we can just get kind from meta - // if we are building an object reference to something not yet persisted, we should fallback to scheme - kind := gvk.Kind - if len(kind) == 0 { - // TODO: this is wrong - gvks, _, err := scheme.ObjectKinds(obj) - if err != nil { - return nil, err - } - kind = gvks[0].Kind - } - - // An object that implements only List has enough metadata to build a reference - var listMeta meta.List - objectMeta, err := meta.Accessor(obj) - if err != nil { - listMeta, err = meta.ListAccessor(obj) - if err != nil { - return nil, err - } - } else { - listMeta = objectMeta - } - - // if the object referenced is actually persisted, we can also get version from meta - version := gvk.GroupVersion().String() - if len(version) == 0 { - selfLink := listMeta.GetSelfLink() - if len(selfLink) == 0 { - return nil, ErrNoSelfLink - } - selfLinkUrl, err := url.Parse(selfLink) - if err != nil { - return nil, err - } - // example paths: ///* - parts := strings.Split(selfLinkUrl.Path, "/") - if len(parts) < 3 { - return nil, fmt.Errorf("unexpected self link format: '%v'; got version '%v'", selfLink, version) - } - version = parts[2] - } - - // only has list metadata - if objectMeta == nil { - return &ObjectReference{ - Kind: kind, - APIVersion: version, - ResourceVersion: listMeta.GetResourceVersion(), - }, nil - } - - return &ObjectReference{ - Kind: kind, - APIVersion: version, - Name: objectMeta.GetName(), - Namespace: objectMeta.GetNamespace(), - UID: objectMeta.GetUID(), - ResourceVersion: objectMeta.GetResourceVersion(), - }, nil -} - -// GetPartialReference is exactly like GetReference, but allows you to set the FieldPath. -func GetPartialReference(scheme *runtime.Scheme, obj runtime.Object, fieldPath string) (*ObjectReference, error) { - ref, err := GetReference(scheme, obj) - if err != nil { - return nil, err - } - ref.FieldPath = fieldPath - return ref, nil -} - -// IsAnAPIObject allows clients to preemptively get a reference to an API object and pass it to places that -// intend only to get a reference to that object. This simplifies the event recording interface. -func (obj *ObjectReference) SetGroupVersionKind(gvk schema.GroupVersionKind) { - obj.APIVersion, obj.Kind = gvk.ToAPIVersionAndKind() -} -func (obj *ObjectReference) GroupVersionKind() schema.GroupVersionKind { - return schema.FromAPIVersionAndKind(obj.APIVersion, obj.Kind) -} - -func (obj *ObjectReference) GetObjectKind() schema.ObjectKind { return obj } diff --git a/staging/src/k8s.io/client-go/pkg/api/methods.go b/staging/src/k8s.io/client-go/pkg/api/taint.go similarity index 70% rename from staging/src/k8s.io/client-go/pkg/api/methods.go rename to staging/src/k8s.io/client-go/pkg/api/taint.go index b3bebfee18f..173e4e60161 100644 --- a/staging/src/k8s.io/client-go/pkg/api/methods.go +++ b/staging/src/k8s.io/client-go/pkg/api/taint.go @@ -21,16 +21,6 @@ package api import "fmt" -// MatchToleration checks if the toleration matches tolerationToMatch. Tolerations are unique by , -// if the two tolerations have same combination, regard as they match. -// TODO: uniqueness check for tolerations in api validations. -func (t *Toleration) MatchToleration(tolerationToMatch *Toleration) bool { - return t.Key == tolerationToMatch.Key && - t.Effect == tolerationToMatch.Effect && - t.Operator == tolerationToMatch.Operator && - t.Value == tolerationToMatch.Value -} - // MatchTaint checks if the taint matches taintToMatch. Taints are unique by key:effect, // if the two taints have same key:effect, regard as they match. func (t *Taint) MatchTaint(taintToMatch Taint) bool { diff --git a/staging/src/k8s.io/client-go/pkg/api/toleration.go b/staging/src/k8s.io/client-go/pkg/api/toleration.go new file mode 100644 index 00000000000..edb73b74e1a --- /dev/null +++ b/staging/src/k8s.io/client-go/pkg/api/toleration.go @@ -0,0 +1,30 @@ +/* +Copyright 2017 The Kubernetes 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. +*/ + +//TODO: consider making these methods functions, because we don't want helper +//functions in the k8s.io/api repo. + +package api + +// MatchToleration checks if the toleration matches tolerationToMatch. Tolerations are unique by , +// if the two tolerations have same combination, regard as they match. +// TODO: uniqueness check for tolerations in api validations. +func (t *Toleration) MatchToleration(tolerationToMatch *Toleration) bool { + return t.Key == tolerationToMatch.Key && + t.Effect == tolerationToMatch.Effect && + t.Operator == tolerationToMatch.Operator && + t.Value == tolerationToMatch.Value +} diff --git a/vendor/BUILD b/vendor/BUILD index e2c324931dc..ee456161817 100644 --- a/vendor/BUILD +++ b/vendor/BUILD @@ -13903,16 +13903,16 @@ go_library( "k8s.io/client-go/pkg/api/doc.go", "k8s.io/client-go/pkg/api/field_constants.go", "k8s.io/client-go/pkg/api/json.go", - "k8s.io/client-go/pkg/api/methods.go", - "k8s.io/client-go/pkg/api/ref.go", + "k8s.io/client-go/pkg/api/objectreference.go", "k8s.io/client-go/pkg/api/register.go", "k8s.io/client-go/pkg/api/resource_helpers.go", + "k8s.io/client-go/pkg/api/taint.go", + "k8s.io/client-go/pkg/api/toleration.go", "k8s.io/client-go/pkg/api/types.go", "k8s.io/client-go/pkg/api/zz_generated.deepcopy.go", ], tags = ["automanaged"], deps = [ - "//vendor:k8s.io/apimachinery/pkg/api/meta", "//vendor:k8s.io/apimachinery/pkg/api/resource", "//vendor:k8s.io/apimachinery/pkg/apimachinery/announced", "//vendor:k8s.io/apimachinery/pkg/apimachinery/registered",