Merge pull request #45823 from deads2k/tpr-13-selflink

Automatic merge from submit-queue (batch tested with PRs 43696, 45823)

Handle self links on customresources

Fixes https://github.com/kubernetes/kubernetes/issues/45776

I started from https://github.com/kubernetes/kubernetes/pull/45777 and wrote the code to make it work properly.

@sdminonne  ptal
This commit is contained in:
Kubernetes Submit Queue 2017-05-16 06:59:48 -07:00 committed by GitHub
commit 278b1e56c2
2 changed files with 45 additions and 2 deletions

View File

@ -20,6 +20,7 @@ import (
"bytes"
"fmt"
"net/http"
"path"
"sync"
"sync/atomic"
"time"
@ -257,14 +258,23 @@ func (r *customResourceDefinitionHandler) getServingInfoFor(customResourceDefini
parameterScheme.AddGeneratedDeepCopyFuncs(metav1.GetGeneratedDeepCopyFuncs()...)
parameterCodec := runtime.NewParameterCodec(parameterScheme)
selfLinkPrefix := ""
switch customResourceDefinition.Spec.Scope {
case apiextensions.ClusterScoped:
selfLinkPrefix = "/" + path.Join("apis", customResourceDefinition.Spec.Group, customResourceDefinition.Spec.Version) + "/"
case apiextensions.NamespaceScoped:
selfLinkPrefix = "/" + path.Join("apis", customResourceDefinition.Spec.Group, customResourceDefinition.Spec.Version, "namespaces") + "/"
}
requestScope := handlers.RequestScope{
Namer: handlers.ContextBasedNaming{
GetContext: func(req *http.Request) apirequest.Context {
ret, _ := r.requestContextMapper.Get(req)
return ret
},
SelfLinker: meta.NewAccessor(),
ClusterScoped: customResourceDefinition.Spec.Scope == apiextensions.ClusterScoped,
SelfLinker: meta.NewAccessor(),
ClusterScoped: customResourceDefinition.Spec.Scope == apiextensions.ClusterScoped,
SelfLinkPathPrefix: selfLinkPrefix,
},
ContextFunc: func(req *http.Request) apirequest.Context {
ret, _ := r.requestContextMapper.Get(req)

View File

@ -167,3 +167,36 @@ func TestSimpleCRUD(t *testing.T) {
t.Errorf("missing watch event")
}
}
func TestSelfLink(t *testing.T) {
stopCh, apiExtensionClient, clientPool, err := testserver.StartDefaultServer()
if err != nil {
t.Fatal(err)
}
defer close(stopCh)
noxuDefinition := testserver.NewNoxuCustomResourceDefinition()
noxuVersionClient, err := testserver.CreateNewCustomResourceDefinition(noxuDefinition, apiExtensionClient, clientPool)
if err != nil {
t.Fatal(err)
}
ns := "not-the-default"
noxuNamespacedResourceClient := noxuVersionClient.Resource(&metav1.APIResource{
Name: noxuDefinition.Spec.Names.Plural,
Namespaced: true,
}, ns)
noxuInstanceToCreate := testserver.NewNoxuInstance(ns, "foo")
createdNoxuInstance, err := noxuNamespacedResourceClient.Create(noxuInstanceToCreate)
if err != nil {
t.Fatal(err)
}
if e, a := "/apis/mygroup.example.com/v1alpha1/namespaces/not-the-default/noxus/foo", createdNoxuInstance.GetSelfLink(); e != a {
t.Errorf("expected %v, got %v", e, a)
}
// TODO add test for cluster scoped self-link when its available
}