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"
|
2022-02-03 00:54:08 +00:00
|
|
|
metricsStore "github.com/rancher/steve/pkg/stores/metrics"
|
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{
|
2022-02-03 00:54:08 +00:00
|
|
|
Store: metricsStore.NewMetricsStore(proxy.NewProxyStore(clientGetter, summaryCache, asl)),
|
2020-06-22 15:49:49 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-08-16 22:41:36 +00:00
|
|
|
if _, ok := resource.Links["update"]; !ok && slice.ContainsString(resource.Schema.ResourceMethods, "blocked-PUT") {
|
|
|
|
resource.Links["update"] = "blocked"
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := resource.Links["remove"]; !ok && slice.ContainsString(resource.Schema.ResourceMethods, "blocked-DELETE") {
|
|
|
|
resource.Links["remove"] = "blocked"
|
|
|
|
}
|
|
|
|
|
2020-06-22 15:49:49 +00:00
|
|
|
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)
|
Add field filtering for resources
This change enables steve to work with three new query parameters:
"include": only include the named fields from the kubernetes object.
Subfields are denoted with ".". Subfields within arrays are ignored.
Multiple fields can be included by repeating the parameter. Example:
GET /v1/configmaps?include=kind&include=apiVersion =>
{
"type": "collection",
"links": {
"self": "http://server/v1/configmaps"
},
"createTypes": {
"configmap": "http://server/v1/configmaps"
},
"actions": {},
"resourceType": "configmap",
"revision": "327238",
"data": [
{
"id": "c-m-w466b2vg/kube-root-ca.crt",
"type": "configmap",
"links": {
"remove": "http://server/v1/configmaps/c-m-w466b2vg/kube-root-ca.crt",
"self": "http://server/v1/configmaps/c-m-w466b2vg/kube-root-ca.crt",
"update": "http://server/v1/configmaps/c-m-w466b2vg/kube-root-ca.crt",
"view": "http://server/api/v1/namespaces/c-m-w466b2vg/configmaps/kube-root-ca.crt"
},
"apiVersion": "v1",
"kind": "ConfigMap"
},
}
...
}
"exclude": exclude the named fields from the kubernetes object.
Subfields are denoted with ".". Subfields within arrays are ignored.
Multiple fields can be excluded by repeating the parameter. Example:
GET /v1/configmaps?exclude=data&exclude=metadata.managedFields =>
{
"type": "collection",
"links": {
"self": "http://server/v1/configmaps"
},
"createTypes": {
"configmap": "http://server/v1/configmaps"
},
"actions": {},
"resourceType": "configmap",
"revision": "328086",
"data": [
{
"id": "c-m-w466b2vg/kube-root-ca.crt",
"type": "configmap",
"links": {
"remove": "http://server/v1/configmaps/c-m-w466b2vg/kube-root-ca.crt",
"self": "http://server/v1/configmaps/c-m-w466b2vg/kube-root-ca.crt",
"update": "http://server/v1/configmaps/c-m-w466b2vg/kube-root-ca.crt",
"view": "http://server/api/v1/namespaces/c-m-w466b2vg/configmaps/kube-root-ca.crt"
},
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": {
"creationTimestamp": "2022-04-11T22:05:27Z",
"fields": [
"kube-root-ca.crt",
1,
"25h"
],
"name": "kube-root-ca.crt",
"namespace": "c-m-w466b2vg",
"relationships": null,
"resourceVersion": "36948",
"state": {
"error": false,
"message": "Resource is always ready",
"name": "active",
"transitioning": false
},
"uid": "1c497934-52cb-42ab-a613-dedfd5fb207b"
}
},
...
}
"excludeValues": replace the values of an object with empty strings, leaving
the keys in place. Useful for showing a summary of an object with large
values, such as the data in a ConfigMap. Only works on fields that are
object. Multiple fields can have values excluded by repeating the
parameter. Example:
GET /v1/configmaps?excludeValues=data =>
{
"type": "collection",
...
"data": [
{
...
"data": {
"ca.crt": ""
},
...
},
...
]
}
2022-04-12 23:17:28 +00:00
|
|
|
|
|
|
|
includeFields(request, unstr)
|
|
|
|
excludeFields(request, unstr)
|
|
|
|
excludeValues(request, unstr)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func includeFields(request *types.APIRequest, unstr *unstructured.Unstructured) {
|
|
|
|
if fields, ok := request.Query["include"]; ok {
|
|
|
|
newObj := map[string]interface{}{}
|
|
|
|
for _, f := range fields {
|
|
|
|
fieldParts := strings.Split(f, ".")
|
|
|
|
if val, ok := data.GetValue(unstr.Object, fieldParts...); ok {
|
|
|
|
data.PutValue(newObj, val, fieldParts...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unstr.Object = newObj
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func excludeFields(request *types.APIRequest, unstr *unstructured.Unstructured) {
|
|
|
|
if fields, ok := request.Query["exclude"]; ok {
|
|
|
|
for _, f := range fields {
|
|
|
|
fieldParts := strings.Split(f, ".")
|
|
|
|
data.RemoveValue(unstr.Object, fieldParts...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func excludeValues(request *types.APIRequest, unstr *unstructured.Unstructured) {
|
|
|
|
if values, ok := request.Query["excludeValues"]; ok {
|
|
|
|
for _, f := range values {
|
|
|
|
fieldParts := strings.Split(f, ".")
|
|
|
|
fieldValues := data.GetValueN(unstr.Object, fieldParts...)
|
|
|
|
if obj, ok := fieldValues.(map[string]interface{}); ok {
|
|
|
|
for k := range obj {
|
|
|
|
data.PutValue(unstr.Object, "", append(fieldParts, k)...)
|
|
|
|
}
|
|
|
|
}
|
2020-06-22 15:49:49 +00:00
|
|
|
}
|
2020-02-14 23:20:04 +00:00
|
|
|
}
|
2019-08-08 05:43:10 +00:00
|
|
|
}
|