2019-08-13 23:36:03 +00:00
package common
2019-08-08 05:43:10 +00:00
import (
2025-01-07 20:20:26 +00:00
"net/http"
2025-06-16 22:33:28 +00:00
"slices"
"strconv"
2020-02-14 23:20:04 +00:00
"strings"
2025-06-16 22:33:28 +00:00
"time"
2020-02-14 23:20:04 +00:00
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"
2025-05-08 09:42:35 +00:00
"github.com/rancher/steve/pkg/resources/virtual/common"
2025-06-16 22:33:28 +00:00
"github.com/sirupsen/logrus"
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"
2024-06-04 18:52:48 +00:00
"github.com/rancher/wrangler/v3/pkg/data"
corecontrollers "github.com/rancher/wrangler/v3/pkg/generated/controllers/core/v1"
"github.com/rancher/wrangler/v3/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"
2025-06-16 22:33:28 +00:00
"k8s.io/apimachinery/pkg/util/duration"
2019-08-08 05:43:10 +00:00
)
2025-06-16 22:33:28 +00:00
type TemplateOptions struct {
InSQLMode bool
}
2020-06-22 15:49:49 +00:00
func DefaultTemplate ( clientGetter proxy . ClientGetter ,
summaryCache * summarycache . SummaryCache ,
2023-05-10 22:31:01 +00:00
asl accesscontrol . AccessSetLookup ,
2025-06-16 22:33:28 +00:00
namespaceCache corecontrollers . NamespaceCache ,
options TemplateOptions ) schema . Template {
2020-01-31 05:37:59 +00:00
return schema . Template {
2023-05-10 22:31:01 +00:00
Store : metricsStore . NewMetricsStore ( proxy . NewProxyStore ( clientGetter , summaryCache , asl , namespaceCache ) ) ,
2025-06-16 22:33:28 +00:00
Formatter : formatter ( summaryCache , asl , options ) ,
2020-01-31 05:37:59 +00:00
}
2019-08-13 23:36:03 +00:00
}
2024-06-05 14:17:12 +00:00
// DefaultTemplateForStore provides a default schema template which uses a provided, pre-initialized store. Primarily used when creating a Template that uses a Lasso SQL store internally.
2025-01-07 20:20:26 +00:00
func DefaultTemplateForStore ( store types . Store ,
summaryCache * summarycache . SummaryCache ,
2025-06-16 22:33:28 +00:00
asl accesscontrol . AccessSetLookup ,
options TemplateOptions ) schema . Template {
2024-06-05 14:17:12 +00:00
return schema . Template {
Store : store ,
2025-06-16 22:33:28 +00:00
Formatter : formatter ( summaryCache , asl , options ) ,
2024-06-05 14:17:12 +00:00
}
}
2021-01-06 17:50:59 +00:00
func selfLink ( gvr schema2 . GroupVersionResource , meta metav1 . Object ) ( prefix string ) {
2025-05-08 09:42:35 +00:00
return buildBasePath ( gvr , meta . GetNamespace ( ) , meta . GetName ( ) )
}
func buildBasePath ( gvr schema2 . GroupVersionResource , namespace string , includeName string ) string {
2021-01-06 17:50:59 +00:00
buf := & strings . Builder { }
2025-05-08 09:42:35 +00:00
2022-12-06 15:04:31 +00:00
if gvr . Group == "management.cattle.io" && gvr . Version == "v3" {
buf . WriteString ( "/v1/" )
2021-01-06 17:50:59 +00:00
buf . WriteString ( gvr . Group )
2022-12-06 15:04:31 +00:00
buf . WriteString ( "." )
buf . WriteString ( gvr . Resource )
2025-05-08 09:42:35 +00:00
if namespace != "" {
2022-12-06 15:04:31 +00:00
buf . WriteString ( "/" )
2025-05-08 09:42:35 +00:00
buf . WriteString ( namespace )
2022-12-06 15:04:31 +00:00
}
} else {
if gvr . Group == "" {
buf . WriteString ( "/api/v1/" )
} else {
buf . WriteString ( "/apis/" )
buf . WriteString ( gvr . Group )
buf . WriteString ( "/" )
buf . WriteString ( gvr . Version )
buf . WriteString ( "/" )
}
2025-05-08 09:42:35 +00:00
if namespace != "" {
2022-12-06 15:04:31 +00:00
buf . WriteString ( "namespaces/" )
2025-05-08 09:42:35 +00:00
buf . WriteString ( namespace )
2022-12-06 15:04:31 +00:00
buf . WriteString ( "/" )
}
buf . WriteString ( gvr . Resource )
2021-01-06 17:50:59 +00:00
}
2025-05-08 09:42:35 +00:00
if includeName != "" {
buf . WriteString ( "/" )
buf . WriteString ( includeName )
}
2021-01-06 17:50:59 +00:00
return buf . String ( )
}
2025-06-16 22:33:28 +00:00
func formatter ( summarycache common . SummaryCache , asl accesscontrol . AccessSetLookup , options TemplateOptions ) types . Formatter {
2020-06-22 15:49:49 +00:00
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
}
2025-01-07 20:20:26 +00:00
userInfo , ok := request . GetUserInfo ( )
if ! ok {
return
}
2025-05-30 14:32:56 +00:00
accessSet := accesscontrol . AccessSetFromAPIRequest ( request )
2025-01-07 20:20:26 +00:00
if accessSet == nil {
2025-05-30 14:32:56 +00:00
accessSet = asl . AccessFor ( userInfo )
if accessSet == nil {
return
}
2025-01-07 20:20:26 +00:00
}
hasUpdate := accessSet . Grants ( "update" , gvr . GroupResource ( ) , resource . APIObject . Namespace ( ) , resource . APIObject . Name ( ) )
hasDelete := accessSet . Grants ( "delete" , gvr . GroupResource ( ) , resource . APIObject . Namespace ( ) , resource . APIObject . Name ( ) )
2025-02-14 11:12:17 +00:00
hasPatch := accessSet . Grants ( "patch" , gvr . GroupResource ( ) , resource . APIObject . Namespace ( ) , resource . APIObject . Name ( ) )
2025-01-07 20:20:26 +00:00
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
2025-01-07 20:20:26 +00:00
if hasUpdate {
if attributes . DisallowMethods ( resource . Schema ) [ http . MethodPut ] {
resource . Links [ "update" ] = "blocked"
}
} else {
delete ( resource . Links , "update" )
2021-08-16 22:41:36 +00:00
}
2025-01-07 20:20:26 +00:00
if hasDelete {
if attributes . DisallowMethods ( resource . Schema ) [ http . MethodDelete ] {
resource . Links [ "remove" ] = "blocked"
}
} else {
delete ( resource . Links , "remove" )
2021-08-16 22:41:36 +00:00
}
2025-02-14 11:12:17 +00:00
if hasPatch {
if attributes . DisallowMethods ( resource . Schema ) [ http . MethodPatch ] {
resource . Links [ "patch" ] = "blocked"
}
} else {
delete ( resource . Links , "patch" )
}
2021-08-16 22:41:36 +00:00
2025-06-16 22:33:28 +00:00
gvk := attributes . GVK ( resource . Schema )
2020-06-22 15:49:49 +00:00
if unstr , ok := resource . APIObject . Object . ( * unstructured . Unstructured ) ; ok {
2024-10-18 17:15:42 +00:00
// with the sql cache, these were already added by the indexer. However, the sql cache
// is only used for lists, so we need to re-add here for get/watch
s , rel := summarycache . SummaryAndRelationship ( unstr )
data . PutValue ( unstr . Object , map [ string ] interface { } {
"name" : s . State ,
"error" : s . Error ,
"transitioning" : s . Transitioning ,
"message" : strings . Join ( s . Message , ":" ) ,
} , "metadata" , "state" )
data . PutValue ( unstr . Object , rel , "metadata" , "relationships" )
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 )
2025-06-16 22:33:28 +00:00
if options . InSQLMode {
convertMetadataTimestampFields ( request , gvk , 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
}
2025-05-08 09:42:35 +00:00
if permsQuery := request . Query . Get ( "checkPermissions" ) ; permsQuery != "" {
ns := getNamespaceFromResource ( resource . APIObject )
permissions := map [ string ] map [ string ] string { }
for _ , res := range strings . Split ( permsQuery , "," ) {
s := request . Schemas . LookupSchema ( res )
if s == nil {
continue
}
gvr := attributes . GVR ( s )
gr := schema2 . GroupResource { Group : gvr . Group , Resource : gvr . Resource }
perms := map [ string ] string { }
for _ , verb := range [ ] string { "create" , "update" , "delete" , "list" , "get" , "watch" , "patch" } {
if accessSet . Grants ( verb , gr , ns , "" ) {
url := request . URLBuilder . RelativeToRoot ( buildBasePath ( gvr , ns , "" ) )
perms [ verb ] = url
}
}
if len ( perms ) > 0 {
permissions [ res ] = perms
}
}
if unstr , ok := resource . APIObject . Object . ( * unstructured . Unstructured ) ; ok {
data . PutValue ( unstr . Object , permissions , "resourcePermissions" )
}
}
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
}
}
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 ... )
}
}
}
2025-06-16 22:33:28 +00:00
// convertMetadataTimestampFields updates metadata timestamp fields to ensure they remain fresh and human-readable when sent back
// to the client. Internally, fields are stored as Unix timestamps; on each request, we calculate the elapsed time since
// those timestamps by subtracting them from time.Now(), then format the resulting duration into a human-friendly string.
// This prevents cached durations (e.g. “2d” - 2 days) from becoming stale over time.
func convertMetadataTimestampFields ( request * types . APIRequest , gvk schema2 . GroupVersionKind , unstr * unstructured . Unstructured ) {
if request . Schema != nil {
cols := GetColumnDefinitions ( request . Schema )
for _ , col := range cols {
gvkDateFields , gvkFound := DateFieldsByGVKBuiltins [ gvk ]
if col . Type == "date" || ( gvkFound && slices . Contains ( gvkDateFields , col . Name ) ) {
index := GetIndexValueFromString ( col . Field )
if index == - 1 {
logrus . Errorf ( "field index not found at column.Field struct variable: %s" , col . Field )
return
}
curValue , got , err := unstructured . NestedSlice ( unstr . Object , "metadata" , "fields" )
if err != nil {
logrus . Errorf ( "failed to get metadata.fields slice from unstr.Object: %s" , err . Error ( ) )
}
if ! got {
logrus . Debugf ( "couldn't find metadata.fields at unstr.Object" )
return
}
timeValue , ok := curValue [ index ] . ( string )
if ! ok {
logrus . Debugf ( "time field isn't a string" )
return
}
millis , err := strconv . ParseInt ( timeValue , 10 , 64 )
if err != nil {
logrus . Warnf ( "failed to convert timestamp value: %s" , err . Error ( ) )
return
}
timestamp := time . Unix ( 0 , millis * int64 ( time . Millisecond ) )
dur := time . Since ( timestamp )
curValue [ index ] = duration . HumanDuration ( dur )
if err := unstructured . SetNestedSlice ( unstr . Object , curValue , "metadata" , "fields" ) ; err != nil {
logrus . Errorf ( "failed to set value back to metadata.fields slice: %s" , err . Error ( ) )
return
}
}
}
}
}
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
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
}
2025-05-08 09:42:35 +00:00
func getNamespaceFromResource ( obj types . APIObject ) string {
unstr , ok := obj . Object . ( * unstructured . Unstructured )
if ! ok {
return ""
}
// If we have a backingNamespace, use that
if statusRaw , ok := unstr . Object [ "status" ] ; ok {
if statusMap , ok := statusRaw . ( map [ string ] interface { } ) ; ok {
if backingNamespace , ok := statusMap [ "backingNamespace" ] . ( string ) ; ok && backingNamespace != "" {
return backingNamespace
}
}
}
// Otherwise, if the id has a slash, we will interpret that.
// This is used to determine a project's namespace when there is no backingNamespace present.
// For cases where there is no slash, we use the object's ID, which is the same as the namespace.
return strings . Replace ( obj . ID , "/" , "-" , 1 )
}