Merge pull request #13 from StrongMonkey/k8s.20

Add self link
This commit is contained in:
Darren Shepherd 2021-01-06 11:58:22 -07:00 committed by GitHub
commit 0badd210b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,6 +5,7 @@ import (
"github.com/rancher/apiserver/pkg/types"
"github.com/rancher/steve/pkg/accesscontrol"
"github.com/rancher/steve/pkg/attributes"
"github.com/rancher/steve/pkg/schema"
"github.com/rancher/steve/pkg/stores/proxy"
"github.com/rancher/steve/pkg/summarycache"
@ -12,7 +13,9 @@ import (
"github.com/rancher/wrangler/pkg/slice"
"github.com/rancher/wrangler/pkg/summary"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
schema2 "k8s.io/apimachinery/pkg/runtime/schema"
)
func DefaultTemplate(clientGetter proxy.ClientGetter,
@ -24,17 +27,44 @@ func DefaultTemplate(clientGetter proxy.ClientGetter,
}
}
func selfLink(gvr schema2.GroupVersionResource, meta metav1.Object) (prefix string) {
buf := &strings.Builder{}
if gvr.Group == "" {
buf.WriteString("/api/v1/")
} else {
buf.WriteString("/apis/")
buf.WriteString(gvr.Group)
buf.WriteString("/")
buf.WriteString(gvr.Version)
buf.WriteString("/")
}
if meta.GetNamespace() != "" {
buf.WriteString("namespaces/")
buf.WriteString(meta.GetNamespace())
buf.WriteString("/")
}
buf.WriteString(gvr.Resource)
buf.WriteString("/")
buf.WriteString(meta.GetName())
return buf.String()
}
func formatter(summarycache *summarycache.SummaryCache) types.Formatter {
return func(request *types.APIRequest, resource *types.RawResource) {
if resource.Schema == nil {
return
}
gvr := attributes.GVR(resource.Schema)
if gvr.Version == "" {
return
}
meta, err := meta.Accessor(resource.APIObject.Object)
if err != nil {
return
}
selfLink := meta.GetSelfLink()
if selfLink == "" {
return
}
selfLink := selfLink(gvr, meta)
u := request.URLBuilder.RelativeToRoot(selfLink)
resource.Links["view"] = u