Skip to content

Commit

Permalink
Fix discovery restmapper finding resources in non-preferred versions
Browse files Browse the repository at this point in the history
  • Loading branch information
liggitt committed Sep 9, 2017
1 parent a5f7660 commit a6316fb
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 5 deletions.
6 changes: 3 additions & 3 deletions hack/make-rules/test-cmd-util.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1249,13 +1249,13 @@ run_kubectl_run_tests() {
kubectl delete deployment nginx-apps "${kube_flags[@]}"

# Pre-Condition: no Job exists
kube::test::get_object_assert cronjob.v1beta1.batch "{{range.items}}{{$id_field}}:{{end}}" ''
kube::test::get_object_assert cronjobs "{{range.items}}{{$id_field}}:{{end}}" ''
# Command
kubectl run pi --schedule="*/5 * * * *" --generator=cronjob/v1beta1 "--image=$IMAGE_PERL" --restart=OnFailure -- perl -Mbignum=bpi -wle 'print bpi(20)' "${kube_flags[@]}"
# Post-Condition: CronJob "pi" is created
kube::test::get_object_assert cronjob.v1beta1.batch "{{range.items}}{{$id_field}}:{{end}}" 'pi:'
kube::test::get_object_assert cronjobs "{{range.items}}{{$id_field}}:{{end}}" 'pi:'
# Clean up
kubectl delete cronjob.v1beta1.batch pi "${kube_flags[@]}"
kubectl delete cronjobs pi "${kube_flags[@]}"

set +o nounset
set +o errexit
Expand Down
16 changes: 16 additions & 0 deletions staging/src/k8s.io/client-go/discovery/restmapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func NewRESTMapper(groupResources []*APIGroupResources, versionInterfaces meta.V
for _, group := range groupResources {
groupPriority = append(groupPriority, group.Group.Name)

// Make sure the preferred version comes first
if len(group.Group.PreferredVersion.Version) != 0 {
preferred := group.Group.PreferredVersion.Version
if _, ok := group.VersionedResources[preferred]; ok {
Expand All @@ -72,6 +73,21 @@ func NewRESTMapper(groupResources []*APIGroupResources, versionInterfaces meta.V
continue
}

// Add non-preferred versions after the preferred version, in case there are resources that only exist in those versions
if discoveryVersion.Version != group.Group.PreferredVersion.Version {
resourcePriority = append(resourcePriority, schema.GroupVersionResource{
Group: group.Group.Name,
Version: discoveryVersion.Version,
Resource: meta.AnyResource,
})

kindPriority = append(kindPriority, schema.GroupVersionKind{
Group: group.Group.Name,
Version: discoveryVersion.Version,
Kind: meta.AnyKind,
})
}

gv := schema.GroupVersion{Group: group.Group.Name, Version: discoveryVersion.Version}
versionMapper := meta.NewDefaultRESTMapper([]schema.GroupVersion{gv}, versionInterfaces)

Expand Down
36 changes: 36 additions & 0 deletions staging/src/k8s.io/client-go/discovery/restmapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,32 @@ func TestRESTMapper(t *testing.T) {
},
},
},

// This group tests finding and prioritizing resources that only exist in non-preferred versions
{
Group: metav1.APIGroup{
Name: "unpreferred",
Versions: []metav1.GroupVersionForDiscovery{
{Version: "v1"},
{Version: "v2beta1"},
{Version: "v2alpha1"},
},
PreferredVersion: metav1.GroupVersionForDiscovery{Version: "v1"},
},
VersionedResources: map[string][]metav1.APIResource{
"v1": {
{Name: "broccoli", Namespaced: true, Kind: "Broccoli"},
},
"v2beta1": {
{Name: "broccoli", Namespaced: true, Kind: "Broccoli"},
{Name: "peas", Namespaced: true, Kind: "Pea"},
},
"v2alpha1": {
{Name: "broccoli", Namespaced: true, Kind: "Broccoli"},
{Name: "peas", Namespaced: true, Kind: "Pea"},
},
},
},
}

restMapper := NewRESTMapper(resources, nil)
Expand Down Expand Up @@ -123,6 +149,16 @@ func TestRESTMapper(t *testing.T) {
Kind: "Job",
},
},
{
input: schema.GroupVersionResource{
Resource: "peas",
},
want: schema.GroupVersionKind{
Group: "unpreferred",
Version: "v2beta1",
Kind: "Pea",
},
},
}

for _, tc := range kindTCs {
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/kubectl/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ var _ = SIGDescribe("Kubectl alpha client", func() {
})

AfterEach(func() {
framework.RunKubectlOrDie("delete", "cronjob.v2alpha1.batch", cjName, nsFlag)
framework.RunKubectlOrDie("delete", "cronjobs", cjName, nsFlag)
})

It("should create a CronJob", func() {
Expand Down Expand Up @@ -1380,7 +1380,7 @@ metadata:
})

AfterEach(func() {
framework.RunKubectlOrDie("delete", "cronjob.v1beta1.batch", cjName, nsFlag)
framework.RunKubectlOrDie("delete", "cronjobs", cjName, nsFlag)
})

It("should create a CronJob", func() {
Expand Down

0 comments on commit a6316fb

Please sign in to comment.