Skip to content

Commit

Permalink
Merge pull request #27733 from caesarxuchao/gc-parametercodec
Browse files Browse the repository at this point in the history
Automatic merge from submit-queue

let dynamic client handle non-registered ListOptions

And register v1.ListOptions in the policy group.

Fix #27622

@lavalamp @smarterclayton @krousey
  • Loading branch information
k8s-merge-robot authored Jun 23, 2016
2 parents 70fc517 + d9f0792 commit d363759
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 8 deletions.
14 changes: 14 additions & 0 deletions pkg/api/serialization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,20 @@ var nonRoundTrippableTypes = sets.NewString(
"WatchEvent",
)

var commonKinds = []string{"ListOptions", "DeleteOptions"}

// verify all external group/versions have the common kinds like the ListOptions, DeleteOptions are registered.
func TestCommonKindsRegistered(t *testing.T) {
for _, kind := range commonKinds {
for _, group := range testapi.Groups {
gv := group.GroupVersion()
if _, err := api.Scheme.New(gv.WithKind(kind)); err != nil {
t.Error(err)
}
}
}
}

var nonInternalRoundTrippableTypes = sets.NewString("List", "ListOptions", "ExportOptions")
var nonRoundTrippableTypesByVersion = map[string][]string{}

Expand Down
1 change: 1 addition & 0 deletions pkg/apis/apps/v1alpha1/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func addKnownTypes(scheme *runtime.Scheme) {
&PetSet{},
&PetSetList{},
&v1.ListOptions{},
&v1.DeleteOptions{},
)
versionedwatch.AddToGroupVersion(scheme, SchemeGroupVersion)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/autoscaling/v1/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func addKnownTypes(scheme *runtime.Scheme) {
&HorizontalPodAutoscalerList{},
&Scale{},
&v1.ListOptions{},
&v1.DeleteOptions{},
)
versionedwatch.AddToGroupVersion(scheme, SchemeGroupVersion)
}
1 change: 1 addition & 0 deletions pkg/apis/batch/v1/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func addKnownTypes(scheme *runtime.Scheme) {
&Job{},
&JobList{},
&v1.ListOptions{},
&v1.DeleteOptions{},
)
versionedwatch.AddToGroupVersion(scheme, SchemeGroupVersion)
}
1 change: 1 addition & 0 deletions pkg/apis/batch/v2alpha1/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func addKnownTypes(scheme *runtime.Scheme) {
&ScheduledJob{},
&ScheduledJobList{},
&v1.ListOptions{},
&v1.DeleteOptions{},
)
versionedwatch.AddToGroupVersion(scheme, SchemeGroupVersion)
}
3 changes: 3 additions & 0 deletions pkg/apis/policy/v1alpha1/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package v1alpha1

import (
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/api/v1"
"k8s.io/kubernetes/pkg/runtime"
versionedwatch "k8s.io/kubernetes/pkg/watch/versioned"
)
Expand All @@ -41,6 +42,8 @@ func addKnownTypes(scheme *runtime.Scheme) {
scheme.AddKnownTypes(SchemeGroupVersion,
&PodDisruptionBudget{},
&PodDisruptionBudgetList{},
&v1.ListOptions{},
&v1.DeleteOptions{},
)
// Add the watch version that applies
versionedwatch.AddToGroupVersion(scheme, SchemeGroupVersion)
Expand Down
23 changes: 23 additions & 0 deletions pkg/client/typed/dynamic/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,26 @@ func (parameterCodec) DecodeParameters(parameters url.Values, from unversioned.G
}

var defaultParameterEncoder runtime.ParameterCodec = parameterCodec{}

type versionedParameterEncoderWithV1Fallback struct{}

func (versionedParameterEncoderWithV1Fallback) EncodeParameters(obj runtime.Object, to unversioned.GroupVersion) (url.Values, error) {
ret, err := api.ParameterCodec.EncodeParameters(obj, to)
if err != nil && runtime.IsNotRegisteredError(err) {
// fallback to v1
return api.ParameterCodec.EncodeParameters(obj, v1.SchemeGroupVersion)
}
return ret, err
}

func (versionedParameterEncoderWithV1Fallback) DecodeParameters(parameters url.Values, from unversioned.GroupVersion, into runtime.Object) error {
return errors.New("DecodeParameters not implemented on versionedParameterEncoderWithV1Fallback")
}

// VersionedParameterEncoderWithV1Fallback is useful for encoding query
// parameters for thirdparty resources. It tries to convert object to the
// specified version before converting it to query parameters, and falls back to
// converting to v1 if the object is not registered in the specified version.
// For the record, currently API server always treats query parameters sent to a
// thirdparty resource endpoint as v1.
var VersionedParameterEncoderWithV1Fallback runtime.ParameterCodec = versionedParameterEncoderWithV1Fallback{}
14 changes: 6 additions & 8 deletions pkg/controller/garbagecollector/garbagecollector.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,21 +443,19 @@ func gcListWatcher(client *dynamic.Client, resource unversioned.GroupVersionReso
// namespaces if it's namespace scoped, so leave
// APIResource.Namespaced as false is all right.
apiResource := unversioned.APIResource{Name: resource.Resource}
// The default parameter codec used by the dynamic client cannot
// encode api.ListOptions.
// TODO: api.ParameterCodec doesn't support thirdparty objects.
// We need a generic parameter codec.
return client.ParameterCodec(api.ParameterCodec).Resource(&apiResource, api.NamespaceAll).List(&options)
return client.ParameterCodec(dynamic.VersionedParameterEncoderWithV1Fallback).
Resource(&apiResource, api.NamespaceAll).
List(&options)
},
WatchFunc: func(options api.ListOptions) (watch.Interface, error) {
// APIResource.Kind is not used by the dynamic client, so
// leave it empty. We want to list this resource in all
// namespaces if it's namespace scoped, so leave
// APIResource.Namespaced as false is all right.
apiResource := unversioned.APIResource{Name: resource.Resource}
// The default parameter codec used by the dynamic client cannot
// encode api.ListOptions.
return client.ParameterCodec(api.ParameterCodec).Resource(&apiResource, api.NamespaceAll).Watch(&options)
return client.ParameterCodec(dynamic.VersionedParameterEncoderWithV1Fallback).
Resource(&apiResource, api.NamespaceAll).
Watch(&options)
},
}
}
Expand Down

0 comments on commit d363759

Please sign in to comment.