2019-08-13 23:36:03 +00:00
|
|
|
package common
|
2019-08-08 05:43:10 +00:00
|
|
|
|
|
|
|
import (
|
2020-02-14 23:20:04 +00:00
|
|
|
"strings"
|
|
|
|
|
2020-06-12 04:50:59 +00:00
|
|
|
"github.com/rancher/apiserver/pkg/types"
|
2020-02-10 17:18:20 +00:00
|
|
|
"github.com/rancher/steve/pkg/accesscontrol"
|
2021-01-06 17:50:59 +00:00
|
|
|
"github.com/rancher/steve/pkg/attributes"
|
2020-01-31 05:37:59 +00:00
|
|
|
"github.com/rancher/steve/pkg/schema"
|
2020-06-12 04:50:59 +00:00
|
|
|
"github.com/rancher/steve/pkg/stores/proxy"
|
2020-06-22 15:49:49 +00:00
|
|
|
"github.com/rancher/steve/pkg/summarycache"
|
2020-02-14 23:20:04 +00:00
|
|
|
"github.com/rancher/wrangler/pkg/data"
|
2020-09-29 17:47:59 +00:00
|
|
|
"github.com/rancher/wrangler/pkg/slice"
|
2020-09-22 20:46:46 +00:00
|
|
|
"github.com/rancher/wrangler/pkg/summary"
|
2020-01-31 05:37:59 +00:00
|
|
|
"k8s.io/apimachinery/pkg/api/meta"
|
2021-01-06 17:50:59 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2020-02-14 23:20:04 +00:00
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
2021-01-06 17:50:59 +00:00
|
|
|
schema2 "k8s.io/apimachinery/pkg/runtime/schema"
|
2019-08-08 05:43:10 +00:00
|
|
|
)
|
|
|
|
|
2020-06-22 15:49:49 +00:00
|
|
|
func DefaultTemplate(clientGetter proxy.ClientGetter,
|
|
|
|
summaryCache *summarycache.SummaryCache,
|
|
|
|
asl accesscontrol.AccessSetLookup) schema.Template {
|
2020-01-31 05:37:59 +00:00
|
|
|
return schema.Template{
|
2020-06-22 15:49:49 +00:00
|
|
|
Store: proxy.NewProxyStore(clientGetter, summaryCache, asl),
|
|
|
|
Formatter: formatter(summaryCache),
|
2020-01-31 05:37:59 +00:00
|
|
|
}
|
2019-08-13 23:36:03 +00:00
|
|
|
}
|
|
|
|
|
2021-01-06 17:50:59 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2020-06-22 15:49:49 +00:00
|
|
|
func formatter(summarycache *summarycache.SummaryCache) types.Formatter {
|
|
|
|
return func(request *types.APIRequest, resource *types.RawResource) {
|
2021-01-06 17:50:59 +00:00
|
|
|
if resource.Schema == nil {
|
2020-06-22 15:49:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-06 17:50:59 +00:00
|
|
|
gvr := attributes.GVR(resource.Schema)
|
|
|
|
if gvr.Version == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
meta, err := meta.Accessor(resource.APIObject.Object)
|
|
|
|
if err != nil {
|
2020-06-22 15:49:49 +00:00
|
|
|
return
|
|
|
|
}
|
2021-01-06 17:50:59 +00:00
|
|
|
selfLink := selfLink(gvr, meta)
|
2020-06-22 15:49:49 +00:00
|
|
|
|
|
|
|
u := request.URLBuilder.RelativeToRoot(selfLink)
|
|
|
|
resource.Links["view"] = u
|
|
|
|
|
2020-09-29 17:47:59 +00:00
|
|
|
if _, ok := resource.Links["update"]; !ok && slice.ContainsString(resource.Schema.CollectionMethods, "PUT") {
|
2020-06-22 15:49:49 +00:00
|
|
|
resource.Links["update"] = u
|
|
|
|
}
|
|
|
|
|
|
|
|
if unstr, ok := resource.APIObject.Object.(*unstructured.Unstructured); ok {
|
2020-09-22 20:46:46 +00:00
|
|
|
s, rel := summarycache.SummaryAndRelationship(unstr)
|
2020-06-22 15:49:49 +00:00
|
|
|
data.PutValue(unstr.Object, map[string]interface{}{
|
2020-09-22 20:46:46 +00:00
|
|
|
"name": s.State,
|
|
|
|
"error": s.Error,
|
|
|
|
"transitioning": s.Transitioning,
|
|
|
|
"message": strings.Join(s.Message, ":"),
|
2020-06-22 15:49:49 +00:00
|
|
|
}, "metadata", "state")
|
|
|
|
data.PutValue(unstr.Object, rel, "metadata", "relationships")
|
2020-09-22 20:46:46 +00:00
|
|
|
|
|
|
|
summary.NormalizeConditions(unstr)
|
2020-06-22 15:49:49 +00:00
|
|
|
}
|
2020-02-14 23:20:04 +00:00
|
|
|
}
|
2019-08-08 05:43:10 +00:00
|
|
|
}
|