Skip to content

Commit

Permalink
fix register.go files up + add test import
Browse files Browse the repository at this point in the history
  • Loading branch information
lavalamp committed Aug 12, 2016
1 parent caed8d8 commit f1fd638
Show file tree
Hide file tree
Showing 81 changed files with 714 additions and 483 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,18 @@ func interfacesFor(version unversioned.GroupVersion) (*meta.VersionInterfaces, e

func addVersionsToScheme(externalVersions ...unversioned.GroupVersion) {
// add the internal version to Scheme
testgroup.AddToScheme(api.Scheme)
if err := testgroup.AddToScheme(api.Scheme); err != nil {
// Programmer error, detect immediately
panic(err)
}
// add the enabled external versions to Scheme
for _, v := range externalVersions {
switch v {
case v1.SchemeGroupVersion:
v1.AddToScheme(api.Scheme)
if err := v1.AddToScheme(api.Scheme); err != nil {
// Programmer error, detect immediately
panic(err)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ import (

var SchemeGroupVersion = unversioned.GroupVersion{Group: "testgroup.k8s.io", Version: runtime.APIVersionInternal}

func AddToScheme(scheme *runtime.Scheme) {
// Add the API to Scheme.
addKnownTypes(scheme)
}
var (
SchemeBuilder = runtime.NewSchemeBuilder(addKnownTypes)
AddToScheme = SchemeBuilder.AddToScheme
)

// Adds the list of known types to api.Scheme.
func addKnownTypes(scheme *runtime.Scheme) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ import (

var SchemeGroupVersion = unversioned.GroupVersion{Group: "testgroup.k8s.io", Version: "v1"}

func AddToScheme(scheme *runtime.Scheme) {
// Add the API to Scheme.
addKnownTypes(scheme)
}
var (
SchemeBuilder = runtime.NewSchemeBuilder(addKnownTypes)
AddToScheme = SchemeBuilder.AddToScheme
)

// Adds the list of known types to api.Scheme.
func addKnownTypes(scheme *runtime.Scheme) {
Expand Down
23 changes: 2 additions & 21 deletions federation/apis/core/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,12 @@ package core

import (
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/fields"
"k8s.io/kubernetes/pkg/labels"
"k8s.io/kubernetes/pkg/runtime"
)

func addDefaultingFuncs(scheme *runtime.Scheme) {
scheme.AddDefaultingFuncs(
func(obj *api.ListOptions) {
if obj.LabelSelector == nil {
obj.LabelSelector = labels.Everything()
}
if obj.FieldSelector == nil {
obj.FieldSelector = fields.Everything()
}
},
)
}

func addConversionFuncs(scheme *runtime.Scheme) {
func addConversionFuncs(scheme *runtime.Scheme) error {
// Add non-generated conversion functions
err := scheme.AddConversionFuncs(
return scheme.AddConversionFuncs(
api.Convert_unversioned_TypeMeta_To_unversioned_TypeMeta,
api.Convert_unversioned_ListMeta_To_unversioned_ListMeta,
api.Convert_intstr_IntOrString_To_intstr_IntOrString,
Expand All @@ -54,8 +39,4 @@ func addConversionFuncs(scheme *runtime.Scheme) {
api.Convert_fields_Selector_To_string,
api.Convert_resource_Quantity_To_resource_Quantity,
)
if err != nil {
// If one of the conversion functions is malformed, detect it immediately.
panic(err)
}
}
37 changes: 37 additions & 0 deletions federation/apis/core/defaults.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
Copyright 2016 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 core

import (
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/fields"
"k8s.io/kubernetes/pkg/labels"
"k8s.io/kubernetes/pkg/runtime"
)

func addDefaultingFuncs(scheme *runtime.Scheme) error {
return scheme.AddDefaultingFuncs(
func(obj *api.ListOptions) {
if obj.LabelSelector == nil {
obj.LabelSelector = labels.Everything()
}
if obj.FieldSelector == nil {
obj.FieldSelector = fields.Everything()
}
},
)
}
32 changes: 8 additions & 24 deletions federation/apis/core/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@ import (
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/meta"
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/api/v1"
"k8s.io/kubernetes/pkg/apimachinery"
"k8s.io/kubernetes/pkg/apimachinery/registered"
"k8s.io/kubernetes/pkg/conversion"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/util/sets"
)
Expand Down Expand Up @@ -125,7 +123,10 @@ func interfacesFor(version unversioned.GroupVersion) (*meta.VersionInterfaces, e

func addVersionsToScheme(externalVersions ...unversioned.GroupVersion) {
// add the internal version to Scheme
core.AddToScheme(core.Scheme)
if err := core.AddToScheme(core.Scheme); err != nil {
// Programmer error, detect immediately
panic(err)
}
// add the enabled external versions to Scheme
for _, v := range externalVersions {
if !registered.IsEnabledVersion(v) {
Expand All @@ -134,27 +135,10 @@ func addVersionsToScheme(externalVersions ...unversioned.GroupVersion) {
}
switch v {
case core_v1.SchemeGroupVersion:
core_v1.AddToScheme(core.Scheme)
}
}

// This is a "fast-path" that avoids reflection for common types. It focuses on the objects that are
// converted the most in the cluster.
// TODO: generate one of these for every external API group - this is to prove the impact
core.Scheme.AddGenericConversionFunc(func(objA, objB interface{}, s conversion.Scope) (bool, error) {
switch a := objA.(type) {
case *v1.Service:
switch b := objB.(type) {
case *api.Service:
return true, v1.Convert_v1_Service_To_api_Service(a, b, s)
if err := core_v1.AddToScheme(core.Scheme); err != nil {
// Programmer error, detect immediately
panic(err)
}
case *api.Service:
switch b := objB.(type) {
case *v1.Service:
return true, v1.Convert_api_Service_To_v1_Service(a, b, s)
}

}
return false, nil
})
}
}
17 changes: 10 additions & 7 deletions federation/apis/core/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,14 @@ func Resource(resource string) unversioned.GroupResource {
return SchemeGroupVersion.WithResource(resource).GroupResource()
}

func AddToScheme(scheme *runtime.Scheme) {
if err := Scheme.AddIgnoredConversionType(&unversioned.TypeMeta{}, &unversioned.TypeMeta{}); err != nil {
panic(err)
var (
SchemeBuilder = runtime.NewSchemeBuilder(addKnownTypes, addDefaultingFuncs, addConversionFuncs)
AddToScheme = SchemeBuilder.AddToScheme
)

func addKnownTypes(scheme *runtime.Scheme) error {
if err := scheme.AddIgnoredConversionType(&unversioned.TypeMeta{}, &unversioned.TypeMeta{}); err != nil {
return err
}
scheme.AddKnownTypes(SchemeGroupVersion,
&api.ServiceList{},
Expand All @@ -68,15 +73,13 @@ func AddToScheme(scheme *runtime.Scheme) {
)

// Register Unversioned types under their own special group
Scheme.AddUnversionedTypes(Unversioned,
scheme.AddUnversionedTypes(Unversioned,
&unversioned.ExportOptions{},
&unversioned.Status{},
&unversioned.APIVersions{},
&unversioned.APIGroupList{},
&unversioned.APIGroup{},
&unversioned.APIResourceList{},
)

addDefaultingFuncs(scheme)
addConversionFuncs(scheme)
return nil
}
9 changes: 4 additions & 5 deletions federation/apis/core/v1/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"k8s.io/kubernetes/pkg/runtime"
)

func addConversionFuncs(scheme *runtime.Scheme) {
func addConversionFuncs(scheme *runtime.Scheme) error {
// Add non-generated conversion functions
err := scheme.AddConversionFuncs(
v1.Convert_v1_DeleteOptions_To_api_DeleteOptions,
Expand Down Expand Up @@ -60,8 +60,7 @@ func addConversionFuncs(scheme *runtime.Scheme) {
v1.Convert_api_ServiceStatus_To_v1_ServiceStatus,
)
if err != nil {
// If one of the conversion functions is malformed, detect it immediately.
panic(err)
return err
}

// Add field label conversions for kinds having selectable nothing but ObjectMeta fields.
Expand All @@ -79,8 +78,8 @@ func addConversionFuncs(scheme *runtime.Scheme) {
}
})
if err != nil {
// If one of the conversion functions is malformed, detect it immediately.
panic(err)
return err
}
}
return nil
}
4 changes: 2 additions & 2 deletions federation/apis/core/v1/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import (
"k8s.io/kubernetes/pkg/runtime"
)

func addDefaultingFuncs(scheme *runtime.Scheme) {
scheme.AddDefaultingFuncs(
func addDefaultingFuncs(scheme *runtime.Scheme) error {
return scheme.AddDefaultingFuncs(
v1.SetDefaults_Secret,
v1.SetDefaults_ServiceSpec,
v1.SetDefaults_NamespaceStatus,
Expand Down
13 changes: 6 additions & 7 deletions federation/apis/core/v1/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,13 @@ const GroupName = ""
// SchemeGroupVersion is group version used to register these objects
var SchemeGroupVersion = unversioned.GroupVersion{Group: GroupName, Version: "v1"}

func AddToScheme(scheme *runtime.Scheme) {
// Add the API to Scheme.
addKnownTypes(scheme)
addConversionFuncs(scheme)
addDefaultingFuncs(scheme)
}
var (
SchemeBuilder = runtime.NewSchemeBuilder(addKnownTypes, addConversionFuncs, addDefaultingFuncs)
AddToScheme = SchemeBuilder.AddToScheme
)

// Adds the list of known types to api.Scheme.
func addKnownTypes(scheme *runtime.Scheme) {
func addKnownTypes(scheme *runtime.Scheme) error {
scheme.AddKnownTypes(SchemeGroupVersion,
&v1.Service{},
&v1.Namespace{},
Expand All @@ -54,4 +52,5 @@ func addKnownTypes(scheme *runtime.Scheme) {

// Add the watch version that applies
versionedwatch.AddToGroupVersion(scheme, SchemeGroupVersion)
return nil
}
10 changes: 8 additions & 2 deletions federation/apis/federation/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,10 @@ func interfacesFor(version unversioned.GroupVersion) (*meta.VersionInterfaces, e

func addVersionsToScheme(externalVersions ...unversioned.GroupVersion) {
// add the internal version to Scheme
federation.AddToScheme(api.Scheme)
if err := federation.AddToScheme(api.Scheme); err != nil {
// Programmer error, detect immediately
panic(err)
}
// add the enabled external versions to Scheme
for _, v := range externalVersions {
if !registered.IsEnabledVersion(v) {
Expand All @@ -123,7 +126,10 @@ func addVersionsToScheme(externalVersions ...unversioned.GroupVersion) {
}
switch v {
case v1beta1.SchemeGroupVersion:
v1beta1.AddToScheme(api.Scheme)
if err := v1beta1.AddToScheme(api.Scheme); err != nil {
// Programmer error, detect immediately
panic(err)
}
}
}
}
11 changes: 6 additions & 5 deletions federation/apis/federation/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,19 @@ func Resource(resource string) unversioned.GroupResource {
return SchemeGroupVersion.WithResource(resource).GroupResource()
}

// Adds the list of known types to api.Scheme.
func AddToScheme(scheme *runtime.Scheme) {
addKnownTypes(scheme)
}
var (
SchemeBuilder = runtime.NewSchemeBuilder(addKnownTypes)
AddToScheme = SchemeBuilder.AddToScheme
)

func addKnownTypes(scheme *runtime.Scheme) {
func addKnownTypes(scheme *runtime.Scheme) error {
scheme.AddKnownTypes(SchemeGroupVersion,
&Cluster{},
&ClusterList{},
&api.ListOptions{},
&api.DeleteOptions{},
)
return nil
}

func (obj *Cluster) GetObjectKind() unversioned.ObjectKind { return &obj.TypeMeta }
Expand Down
12 changes: 4 additions & 8 deletions federation/apis/federation/v1beta1/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,18 @@ package v1beta1
import (
"fmt"

"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/runtime"
)

func addConversionFuncs(scheme *runtime.Scheme) {
err := api.Scheme.AddFieldLabelConversionFunc(SchemeGroupVersion.String(), "Cluster",
func addConversionFuncs(scheme *runtime.Scheme) error {
return scheme.AddFieldLabelConversionFunc(SchemeGroupVersion.String(), "Cluster",
func(label, value string) (string, string, error) {
switch label {
case "metadata.name":
return label, value, nil
default:
return "", "", fmt.Errorf("field label not supported: %s", label)
}
})
if err != nil {
// If one of the conversion functions is malformed, detect it immediately.
panic(err)
}
},
)
}
3 changes: 2 additions & 1 deletion federation/apis/federation/v1beta1/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ import (
"k8s.io/kubernetes/pkg/runtime"
)

func addDefaultingFuncs(scheme *runtime.Scheme) {
func addDefaultingFuncs(scheme *runtime.Scheme) error {
return nil
}
13 changes: 6 additions & 7 deletions federation/apis/federation/v1beta1/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,20 @@ const GroupName = "federation"
// SchemeGroupVersion is group version used to register these objects
var SchemeGroupVersion = unversioned.GroupVersion{Group: GroupName, Version: "v1beta1"}

// Adds the list of known types to api.Scheme.
func AddToScheme(scheme *runtime.Scheme) {
addKnownTypes(scheme)
addDefaultingFuncs(scheme)
addConversionFuncs(scheme)
}
var (
SchemeBuilder = runtime.NewSchemeBuilder(addKnownTypes, addDefaultingFuncs, addConversionFuncs)
AddToScheme = SchemeBuilder.AddToScheme
)

func addKnownTypes(scheme *runtime.Scheme) {
func addKnownTypes(scheme *runtime.Scheme) error {
scheme.AddKnownTypes(SchemeGroupVersion,
&Cluster{},
&ClusterList{},
&v1.ListOptions{},
&v1.DeleteOptions{},
)
versionedwatch.AddToGroupVersion(scheme, SchemeGroupVersion)
return nil
}

func (obj *Cluster) GetObjectKind() unversioned.ObjectKind { return &obj.TypeMeta }
Expand Down
Loading

0 comments on commit f1fd638

Please sign in to comment.