1
0
mirror of https://github.com/rancher/steve.git synced 2025-08-21 15:54:19 +00:00
steve/pkg/resources/schema.go
Chad Roberts 3f39749872
[v0.4] Forwardport accessSet changes to main formatter and add schema links and resource methods for patch verb (#609)
* Finish the AccessSet changes to the main formatter. (#308)

The original PR, steve/pull/158 drifted too far from changes
in main, so it's easier to create a new PR:

* Bring in and update DefaultSchemaTemplatesForStore
* Move from based on `master` to `main`
* Update k8s version

* [main&2.10.3] Add schema links and resource methods for resource verb patch (#450)

* Show patch link on the API resource when patch permission is present and add patch ResourceMethod to the schema.

* Added tests for new functionality and corrected disallowed method for patch

* [v0.4] Add schema links and resource methods for resource verb patch (#450)

* Show patch link on the API resource when patch permission is present and add patch ResourceMethod to the schema.

* Added tests for new functionality and corrected disallowed method for patch
# Conflicts:
#	pkg/resources/common/formatter.go
#	pkg/resources/common/formatter_test.go

---------

Co-authored-by: Eric Promislow <epromislow@suse.com>
2025-05-01 11:54:23 -04:00

105 lines
3.2 KiB
Go

package resources
import (
"context"
"github.com/rancher/apiserver/pkg/store/apiroot"
"github.com/rancher/apiserver/pkg/subscribe"
"github.com/rancher/apiserver/pkg/types"
"github.com/rancher/steve/pkg/accesscontrol"
"github.com/rancher/steve/pkg/client"
"github.com/rancher/steve/pkg/clustercache"
"github.com/rancher/steve/pkg/resources/apigroups"
"github.com/rancher/steve/pkg/resources/cluster"
"github.com/rancher/steve/pkg/resources/common"
"github.com/rancher/steve/pkg/resources/counts"
"github.com/rancher/steve/pkg/resources/formatters"
"github.com/rancher/steve/pkg/resources/userpreferences"
"github.com/rancher/steve/pkg/schema"
"github.com/rancher/steve/pkg/stores/proxy"
"github.com/rancher/steve/pkg/summarycache"
corecontrollers "github.com/rancher/wrangler/v3/pkg/generated/controllers/core/v1"
"k8s.io/apiserver/pkg/endpoints/request"
"k8s.io/client-go/discovery"
)
func DefaultSchemas(ctx context.Context, baseSchema *types.APISchemas, ccache clustercache.ClusterCache,
cg proxy.ClientGetter, schemaFactory schema.Factory, serverVersion string) error {
counts.Register(baseSchema, ccache)
subscribe.Register(baseSchema, func(apiOp *types.APIRequest) *types.APISchemas {
user, ok := request.UserFrom(apiOp.Context())
if ok {
schemas, err := schemaFactory.Schemas(user)
if err == nil {
return schemas
}
}
return apiOp.Schemas
}, serverVersion)
apiroot.Register(baseSchema, []string{"v1"}, "proxy:/apis")
cluster.Register(ctx, baseSchema, cg, schemaFactory)
userpreferences.Register(baseSchema)
return nil
}
func DefaultSchemaTemplates(cf *client.Factory,
baseSchemas *types.APISchemas,
summaryCache *summarycache.SummaryCache,
lookup accesscontrol.AccessSetLookup,
discovery discovery.DiscoveryInterface,
namespaceCache corecontrollers.NamespaceCache) []schema.Template {
return []schema.Template{
common.DefaultTemplate(cf, summaryCache, lookup, namespaceCache),
apigroups.Template(discovery),
{
ID: "configmap",
Formatter: formatters.HandleHelmData,
},
{
ID: "secret",
Formatter: formatters.HandleHelmData,
},
{
ID: "pod",
Formatter: formatters.Pod,
},
{
ID: "management.cattle.io.cluster",
Customize: func(apiSchema *types.APISchema) {
cluster.AddApply(baseSchemas, apiSchema)
},
},
}
}
// DefaultSchemaTemplatesForStore returns the same default templates as DefaultSchemaTemplates, only using DefaultSchemaTemplateFoStore internally to construct the templates.
func DefaultSchemaTemplatesForStore(store types.Store,
baseSchemas *types.APISchemas,
summaryCache *summarycache.SummaryCache,
lookup accesscontrol.AccessSetLookup,
discovery discovery.DiscoveryInterface) []schema.Template {
return []schema.Template{
common.DefaultTemplateForStore(store, summaryCache, lookup),
apigroups.Template(discovery),
{
ID: "configmap",
Formatter: formatters.HandleHelmData,
},
{
ID: "secret",
Formatter: formatters.HandleHelmData,
},
{
ID: "pod",
Formatter: formatters.Pod,
},
{
ID: "management.cattle.io.cluster",
Customize: func(apiSchema *types.APISchema) {
cluster.AddApply(baseSchemas, apiSchema)
},
},
}
}