Skip to content

Commit

Permalink
Add missing error handling in schema-related code
Browse files Browse the repository at this point in the history
  • Loading branch information
ash2k committed Jun 23, 2018
1 parent 3079c1d commit bfe313d
Show file tree
Hide file tree
Showing 73 changed files with 208 additions and 119 deletions.
8 changes: 6 additions & 2 deletions cmd/cloud-controller-manager/app/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,12 @@ func NewCloudControllerManagerOptions() (*CloudControllerManagerOptions, error)
// NewDefaultComponentConfig returns cloud-controller manager configuration object.
func NewDefaultComponentConfig(insecurePort int32) (componentconfig.CloudControllerManagerConfiguration, error) {
scheme := runtime.NewScheme()
componentconfigv1alpha1.AddToScheme(scheme)
componentconfig.AddToScheme(scheme)
if err := componentconfigv1alpha1.AddToScheme(scheme); err != nil {
return componentconfig.CloudControllerManagerConfiguration{}, err
}
if err := componentconfig.AddToScheme(scheme); err != nil {
return componentconfig.CloudControllerManagerConfiguration{}, err
}

versioned := componentconfigv1alpha1.CloudControllerManagerConfiguration{}
scheme.Default(&versioned)
Expand Down
8 changes: 6 additions & 2 deletions cmd/kube-controller-manager/app/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,12 @@ func NewKubeControllerManagerOptions() (*KubeControllerManagerOptions, error) {
// NewDefaultComponentConfig returns kube-controller manager configuration object.
func NewDefaultComponentConfig(insecurePort int32) (componentconfig.KubeControllerManagerConfiguration, error) {
scheme := runtime.NewScheme()
componentconfigv1alpha1.AddToScheme(scheme)
componentconfig.AddToScheme(scheme)
if err := componentconfigv1alpha1.AddToScheme(scheme); err != nil {
return componentconfig.KubeControllerManagerConfiguration{}, err
}
if err := componentconfig.AddToScheme(scheme); err != nil {
return componentconfig.KubeControllerManagerConfiguration{}, err
}

versioned := componentconfigv1alpha1.KubeControllerManagerConfiguration{}
scheme.Default(&versioned)
Expand Down
1 change: 1 addition & 0 deletions cmd/kube-scheduler/app/options/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ go_library(
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/runtime/serializer:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/runtime/serializer/json:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/runtime:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/uuid:go_default_library",
"//staging/src/k8s.io/apiserver/pkg/server/options:go_default_library",
"//staging/src/k8s.io/apiserver/pkg/util/feature:go_default_library",
Expand Down
5 changes: 3 additions & 2 deletions cmd/kube-scheduler/app/options/configfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/apimachinery/pkg/runtime/serializer/json"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/kubernetes/pkg/apis/componentconfig"
componentconfigv1alpha1 "k8s.io/kubernetes/pkg/apis/componentconfig/v1alpha1"
)
Expand All @@ -35,8 +36,8 @@ var (
)

func init() {
componentconfig.AddToScheme(configScheme)
componentconfigv1alpha1.AddToScheme(configScheme)
utilruntime.Must(componentconfig.AddToScheme(configScheme))
utilruntime.Must(componentconfigv1alpha1.AddToScheme(configScheme))
}

func loadConfigFromFile(file string) (*componentconfig.KubeSchedulerConfiguration, error) {
Expand Down
1 change: 1 addition & 0 deletions cmd/kubeadm/app/apis/kubeadm/scheme/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ go_library(
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/runtime/serializer:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/runtime:go_default_library",
],
)

Expand Down
9 changes: 5 additions & 4 deletions cmd/kubeadm/app/apis/kubeadm/scheme/scheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
"k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1alpha1"
"k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1alpha2"
Expand All @@ -39,8 +40,8 @@ func init() {

// AddToScheme builds the Kubeadm scheme using all known versions of the kubeadm api.
func AddToScheme(scheme *runtime.Scheme) {
kubeadm.AddToScheme(scheme)
v1alpha1.AddToScheme(scheme)
v1alpha2.AddToScheme(scheme)
scheme.SetVersionPriority(v1alpha1.SchemeGroupVersion)
utilruntime.Must(kubeadm.AddToScheme(scheme))
utilruntime.Must(v1alpha1.AddToScheme(scheme))
utilruntime.Must(v1alpha2.AddToScheme(scheme))
utilruntime.Must(scheme.SetVersionPriority(v1alpha1.SchemeGroupVersion))
}
1 change: 1 addition & 0 deletions cmd/kubeadm/app/util/config/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ go_test(
"//staging/src/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/runtime/serializer:go_default_library",
"//vendor/github.com/pmezard/go-difflib/difflib:go_default_library",
"//vendor/github.com/stretchr/testify/require:go_default_library",
],
)

Expand Down
3 changes: 2 additions & 1 deletion cmd/kubeadm/app/util/config/masterconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"testing"

"github.com/pmezard/go-difflib/difflib"
"github.com/stretchr/testify/require"

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand Down Expand Up @@ -159,7 +160,7 @@ func TestUpgrade(t *testing.T) {
}

scheme := runtime.NewScheme()
v1alpha1.AddToScheme(scheme)
require.NoError(t, v1alpha1.AddToScheme(scheme))
codecs := serializer.NewCodecFactory(scheme)

obj := &v1alpha1.MasterConfiguration{}
Expand Down
1 change: 1 addition & 0 deletions pkg/api/ref/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ go_test(
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
"//vendor/github.com/stretchr/testify/require:go_default_library",
],
)

Expand Down
4 changes: 3 additions & 1 deletion pkg/api/ref/ref_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"reflect"
"testing"

"github.com/stretchr/testify/require"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand Down Expand Up @@ -53,7 +55,7 @@ func TestGetReference(t *testing.T) {
// 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 := legacyscheme.Scheme.ObjectKinds(&api.Pod{}); err != nil {
api.AddToScheme(legacyscheme.Scheme)
require.NoError(t, api.AddToScheme(legacyscheme.Scheme))
}

table := map[string]struct {
Expand Down
1 change: 1 addition & 0 deletions pkg/kubectl/cmd/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ go_test(
"//staging/src/k8s.io/apimachinery/pkg/types:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/diff:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/intstr:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/runtime:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/sets:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/strategicpatch:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/strategicpatch/testing:go_default_library",
Expand Down
3 changes: 2 additions & 1 deletion pkg/kubectl/cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
restclient "k8s.io/client-go/rest"
"k8s.io/kubernetes/pkg/api/testapi"
apitesting "k8s.io/kubernetes/pkg/api/testing"
Expand All @@ -43,7 +44,7 @@ import (

// This init should be removed after switching this command and its tests to user external types.
func init() {
api.AddToScheme(scheme.Scheme)
utilruntime.Must(api.AddToScheme(scheme.Scheme))
}

func initTestErrorHandler(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion pkg/kubectl/cmd/expose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/intstr"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/rest/fake"
"k8s.io/kubernetes/pkg/api/legacyscheme"
api "k8s.io/kubernetes/pkg/apis/core"
Expand All @@ -36,7 +37,7 @@ import (

// This init should be removed after switching this command and its tests to user external types.
func init() {
api.AddToScheme(scheme.Scheme)
utilruntime.Must(api.AddToScheme(scheme.Scheme))
}

func TestRunExposeService(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions pkg/kubectl/cmd/get/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ go_test(
"//staging/src/k8s.io/apimachinery/pkg/runtime/serializer/json:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/runtime/serializer/streaming:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/diff:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/runtime:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/watch:go_default_library",
"//staging/src/k8s.io/client-go/rest:go_default_library",
"//staging/src/k8s.io/client-go/rest/fake:go_default_library",
Expand Down
7 changes: 4 additions & 3 deletions pkg/kubectl/cmd/get/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer/json"
"k8s.io/apimachinery/pkg/runtime/serializer/streaming"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/watch"
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/rest/fake"
Expand All @@ -56,9 +57,9 @@ var openapiSchemaPath = filepath.Join("..", "..", "..", "..", "api", "openapi-sp

// This init should be removed after switching this command and its tests to user external types.
func init() {
api.AddToScheme(scheme.Scheme)
scheme.Scheme.AddConversionFuncs(v1.Convert_core_PodSpec_To_v1_PodSpec)
scheme.Scheme.AddConversionFuncs(v1.Convert_v1_PodSecurityContext_To_core_PodSecurityContext)
utilruntime.Must(api.AddToScheme(scheme.Scheme))
utilruntime.Must(scheme.Scheme.AddConversionFuncs(v1.Convert_core_PodSpec_To_v1_PodSpec))
utilruntime.Must(scheme.Scheme.AddConversionFuncs(v1.Convert_v1_PodSecurityContext_To_core_PodSecurityContext))
}

var unstructuredSerializer = resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer
Expand Down
3 changes: 2 additions & 1 deletion pkg/kubectl/cmd/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/intstr"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/rest/fake"
"k8s.io/kubernetes/pkg/api/legacyscheme"
Expand All @@ -46,7 +47,7 @@ import (

// This init should be removed after switching this command and its tests to user external types.
func init() {
api.AddToScheme(scheme.Scheme)
utilruntime.Must(api.AddToScheme(scheme.Scheme))
}

func TestGetRestartPolicy(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions pkg/proxy/apis/kubeproxyconfig/scheme/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ go_library(
"//pkg/proxy/apis/kubeproxyconfig/v1alpha1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/runtime/serializer:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/runtime:go_default_library",
],
)

Expand Down
5 changes: 3 additions & 2 deletions pkg/proxy/apis/kubeproxyconfig/scheme/scheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package scheme
import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/kubernetes/pkg/proxy/apis/kubeproxyconfig"
"k8s.io/kubernetes/pkg/proxy/apis/kubeproxyconfig/v1alpha1"
)
Expand All @@ -37,6 +38,6 @@ func init() {

// AddToScheme adds the types of this group into the given scheme.
func AddToScheme(scheme *runtime.Scheme) {
v1alpha1.AddToScheme(scheme)
kubeproxyconfig.AddToScheme(scheme)
utilruntime.Must(v1alpha1.AddToScheme(scheme))
utilruntime.Must(kubeproxyconfig.AddToScheme(scheme))
}
1 change: 1 addition & 0 deletions staging/src/k8s.io/api/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ go_test(
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/runtime/serializer:go_default_library",
"//vendor/github.com/stretchr/testify/require:go_default_library",
],
)

Expand Down
12 changes: 12 additions & 0 deletions staging/src/k8s.io/api/Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions staging/src/k8s.io/api/roundtrip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import (
storagev1alpha1 "k8s.io/api/storage/v1alpha1"
storagev1beta1 "k8s.io/api/storage/v1beta1"

"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/api/testing/fuzzer"
"k8s.io/apimachinery/pkg/api/testing/roundtrip"
genericfuzzer "k8s.io/apimachinery/pkg/apis/meta/fuzzer"
Expand Down Expand Up @@ -100,7 +101,7 @@ func TestRoundTripExternalTypes(t *testing.T) {
scheme := runtime.NewScheme()
codecs := serializer.NewCodecFactory(scheme)

builder.AddToScheme(scheme)
require.NoError(t, builder.AddToScheme(scheme))
seed := rand.Int63()
// I'm only using the generic fuzzer funcs, but at some point in time we might need to
// switch to specialized. For now we're happy with the current serialization test.
Expand All @@ -119,7 +120,7 @@ func TestFailRoundTrip(t *testing.T) {
metav1.AddToGroupVersion(scheme, groupVersion)
return nil
})
builder.AddToScheme(scheme)
require.NoError(t, builder.AddToScheme(scheme))
seed := rand.Int63()
fuzzer := fuzzer.FuzzerFor(genericfuzzer.Funcs, rand.NewSource(seed), codecs)
tmpT := new(testing.T)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func addToGroupVersion(scheme *runtime.Scheme, groupVersion schema.GroupVersion)
if err := scheme.AddIgnoredConversionType(&metav1.TypeMeta{}, &metav1.TypeMeta{}); err != nil {
return err
}
scheme.AddConversionFuncs(
err := scheme.AddConversionFuncs(
metav1.Convert_string_To_labels_Selector,
metav1.Convert_labels_Selector_To_string,

Expand All @@ -70,6 +70,9 @@ func addToGroupVersion(scheme *runtime.Scheme, groupVersion schema.GroupVersion)
Convert_internalversion_ListOptions_To_v1_ListOptions,
Convert_v1_ListOptions_To_internalversion_ListOptions,
)
if err != nil {
return err
}
// ListOptions is the only options struct which needs conversion (it exposes labels and fields
// as selectors for convenience). The other types have only a single representation today.
scheme.AddKnownTypes(SchemeGroupVersion,
Expand Down
1 change: 1 addition & 0 deletions staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ go_library(
"//staging/src/k8s.io/apimachinery/pkg/selection:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/types:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/intstr:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/runtime:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/watch:go_default_library",
"//vendor/github.com/gogo/protobuf/proto:go_default_library",
"//vendor/github.com/gogo/protobuf/sortkeys:go_default_library",
Expand Down
12 changes: 6 additions & 6 deletions staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package v1
import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
)

// GroupName is the group name for this API.
Expand Down Expand Up @@ -53,13 +54,12 @@ func AddToGroupVersion(scheme *runtime.Scheme, groupVersion schema.GroupVersion)
&GetOptions{},
&DeleteOptions{},
)
scheme.AddConversionFuncs(
utilruntime.Must(scheme.AddConversionFuncs(
Convert_versioned_Event_to_watch_Event,
Convert_versioned_InternalEvent_to_versioned_Event,
Convert_watch_Event_to_versioned_Event,
Convert_versioned_Event_to_versioned_InternalEvent,
)

))
// Register Unversioned types under their own special group
scheme.AddUnversionedTypes(Unversioned,
&Status{},
Expand All @@ -70,8 +70,8 @@ func AddToGroupVersion(scheme *runtime.Scheme, groupVersion schema.GroupVersion)
)

// register manually. This usually goes through the SchemeBuilder, which we cannot use here.
AddConversionFuncs(scheme)
RegisterDefaults(scheme)
utilruntime.Must(AddConversionFuncs(scheme))
utilruntime.Must(RegisterDefaults(scheme))
}

// scheme is the registry for the common types that adhere to the meta v1 API spec.
Expand All @@ -89,5 +89,5 @@ func init() {
)

// register manually. This usually goes through the SchemeBuilder, which we cannot use here.
RegisterDefaults(scheme)
utilruntime.Must(RegisterDefaults(scheme))
}
1 change: 1 addition & 0 deletions staging/src/k8s.io/apimachinery/pkg/runtime/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ go_test(
"//staging/src/k8s.io/apimachinery/pkg/runtime/testing:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/diff:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/json:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/runtime:go_default_library",
"//vendor/github.com/stretchr/testify/assert:go_default_library",
"//vendor/github.com/stretchr/testify/require:go_default_library",
],
Expand Down
Loading

0 comments on commit bfe313d

Please sign in to comment.