Skip to content

Commit

Permalink
Merge pull request #139 from MbolotSuse/resource-schema
Browse files Browse the repository at this point in the history
Resource schema
  • Loading branch information
MbolotSuse authored Feb 28, 2024
2 parents cecc8da + 099ebc5 commit 641178e
Show file tree
Hide file tree
Showing 20 changed files with 2,599 additions and 234 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ replace (
require (
github.com/adrg/xdg v0.4.0
github.com/golang/mock v1.6.0
github.com/google/gnostic-models v0.6.8
github.com/gorilla/mux v1.8.0
github.com/gorilla/websocket v1.5.0
github.com/pborman/uuid v1.2.1
Expand All @@ -28,6 +29,7 @@ require (
github.com/urfave/cli v1.22.14
github.com/urfave/cli/v2 v2.25.7
golang.org/x/sync v0.5.0
gopkg.in/yaml.v3 v3.0.1
k8s.io/api v0.28.6
k8s.io/apiextensions-apiserver v0.28.6
k8s.io/apimachinery v0.28.6
Expand Down Expand Up @@ -56,7 +58,6 @@ require (
github.com/go-openapi/swag v0.22.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.3.0 // indirect
Expand Down Expand Up @@ -102,7 +103,6 @@ require (
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/component-base v0.28.6 // indirect
k8s.io/klog/v2 v2.100.1 // indirect
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 // indirect
Expand Down
3 changes: 1 addition & 2 deletions pkg/resources/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/rancher/steve/pkg/resources/formatters"
"github.com/rancher/steve/pkg/resources/userpreferences"
"github.com/rancher/steve/pkg/schema"
steveschema "github.com/rancher/steve/pkg/schema"
"github.com/rancher/steve/pkg/stores/proxy"
"github.com/rancher/steve/pkg/summarycache"
corecontrollers "github.com/rancher/wrangler/v2/pkg/generated/controllers/core/v1"
Expand All @@ -25,7 +24,7 @@ import (
)

func DefaultSchemas(ctx context.Context, baseSchema *types.APISchemas, ccache clustercache.ClusterCache,
cg proxy.ClientGetter, schemaFactory steveschema.Factory, serverVersion string) error {
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())
Expand Down
22 changes: 6 additions & 16 deletions pkg/schema/converter/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ var (
}
)

func AddCustomResources(crd apiextv1.CustomResourceDefinitionClient, schemas map[string]*types.APISchema) error {
// addCustomResources uses the openAPISchema defined on CRDs to provide field definitions to previously discovered schemas.
// Note that this function does not create new schemas - it only adds details to resources already present in the schemas map.
func addCustomResources(crd apiextv1.CustomResourceDefinitionClient, schemas map[string]*types.APISchema) error {
crds, err := crd.List(metav1.ListOptions{})
if err != nil {
return nil
Expand All @@ -41,14 +43,14 @@ func AddCustomResources(crd apiextv1.CustomResourceDefinitionClient, schemas map
group, kind := crd.Spec.Group, crd.Status.AcceptedNames.Kind

for _, version := range crd.Spec.Versions {
forVersion(&crd, group, kind, version, schemas)
forVersion(group, kind, version, schemas)
}
}

return nil
}

func forVersion(crd *v1.CustomResourceDefinition, group, kind string, version v1.CustomResourceDefinitionVersion, schemasMap map[string]*types.APISchema) {
func forVersion(group, kind string, version v1.CustomResourceDefinitionVersion, schemasMap map[string]*types.APISchema) {
var versionColumns []table.Column
for _, col := range version.AdditionalPrinterColumns {
versionColumns = append(versionColumns, table.Column{
Expand All @@ -73,18 +75,6 @@ func forVersion(crd *v1.CustomResourceDefinition, group, kind string, version v1
attributes.SetColumns(schema, versionColumns)
}
if version.Schema != nil && version.Schema.OpenAPIV3Schema != nil {
if fieldsSchema := modelV3ToSchema(id, crd.Spec.Versions[0].Schema.OpenAPIV3Schema, schemasMap); fieldsSchema != nil {
for k, v := range staticFields {
fieldsSchema.ResourceFields[k] = v
}
for k, v := range fieldsSchema.ResourceFields {
if schema.ResourceFields == nil {
schema.ResourceFields = map[string]schemas.Field{}
}
if _, ok := schema.ResourceFields[k]; !ok {
schema.ResourceFields[k] = v
}
}
}
schema.Description = version.Schema.OpenAPIV3Schema.Description
}
}
Loading

0 comments on commit 641178e

Please sign in to comment.