Skip to content
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

Move pkg/api/ref.go to a subpackage #44299

Merged
merged 1 commit into from
Apr 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 5 additions & 7 deletions pkg/api/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
],
)

Expand Down Expand Up @@ -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",
Expand Down
34 changes: 34 additions & 0 deletions pkg/api/objectreference.go
Original file line number Diff line number Diff line change
@@ -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 }
46 changes: 46 additions & 0 deletions pkg/api/ref/BUILD
Original file line number Diff line number Diff line change
@@ -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"],
)
25 changes: 7 additions & 18 deletions pkg/api/ref.go → pkg/api/ref/ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package api
package ref

import (
"errors"
Expand All @@ -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 (
Expand All @@ -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
}
Expand Down Expand Up @@ -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(),
Expand All @@ -111,22 +111,11 @@ 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
}
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 }
23 changes: 12 additions & 11 deletions pkg/api/ref_test.go → pkg/api/ref/ref_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package api
package ref

import (
"reflect"
Expand All @@ -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{}
Expand All @@ -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",
Expand All @@ -61,7 +62,7 @@ func TestGetReference(t *testing.T) {
},
},
fieldPath: ".desiredState.containers[0]",
ref: &ObjectReference{
ref: &api.ObjectReference{
Kind: "Pod",
APIVersion: "version1",
Name: "foo",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -104,7 +105,7 @@ func TestGetReference(t *testing.T) {
},
},
"badSelfLink": {
obj: &ServiceList{
obj: &api.ServiceList{
ListMeta: metav1.ListMeta{
ResourceVersion: "42",
SelfLink: "version2/services",
Expand All @@ -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
Expand Down
10 changes: 0 additions & 10 deletions pkg/api/methods.go → pkg/api/taint.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,6 @@ package api

import "fmt"

// MatchToleration checks if the toleration matches tolerationToMatch. Tolerations are unique by <key,effect,operator,value>,
// if the two tolerations have same <key,effect,operator,value> 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 {
Expand Down
File renamed without changes.
30 changes: 30 additions & 0 deletions pkg/api/toleration.go
Original file line number Diff line number Diff line change
@@ -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 <key,effect,operator,value>,
// if the two tolerations have same <key,effect,operator,value> 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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions pkg/kubectl/cmd/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion pkg/kubectl/cmd/drain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/printers/internalversion/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down