1
0
mirror of https://github.com/rancher/steve.git synced 2025-04-27 11:00:48 +00:00
steve/pkg/schema/converter/crd.go

81 lines
2.1 KiB
Go
Raw Normal View History

2019-08-14 18:08:34 +00:00
package converter
import (
"github.com/rancher/apiserver/pkg/types"
2019-09-11 21:05:00 +00:00
"github.com/rancher/steve/pkg/attributes"
2020-01-31 05:37:59 +00:00
"github.com/rancher/steve/pkg/schema/table"
apiextv1 "github.com/rancher/wrangler/v3/pkg/generated/controllers/apiextensions.k8s.io/v1"
"github.com/rancher/wrangler/v3/pkg/schemas"
v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
2019-08-14 18:08:34 +00:00
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
)
2020-01-31 05:01:21 +00:00
var (
2020-01-31 05:37:59 +00:00
staticFields = map[string]schemas.Field{
2020-01-31 05:01:21 +00:00
"apiVersion": {
Type: "string",
},
"kind": {
Type: "string",
},
"metadata": {
Type: "io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta",
Create: true,
Update: true,
2020-01-31 05:01:21 +00:00
},
}
)
// 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 {
2019-08-14 18:08:34 +00:00
crds, err := crd.List(metav1.ListOptions{})
if err != nil {
return nil
}
for _, crd := range crds.Items {
if crd.Status.AcceptedNames.Plural == "" {
continue
}
group, kind := crd.Spec.Group, crd.Status.AcceptedNames.Kind
2019-08-14 18:08:34 +00:00
for _, version := range crd.Spec.Versions {
forVersion(group, kind, version, schemas)
2019-08-14 18:08:34 +00:00
}
}
return nil
}
func forVersion(group, kind string, version v1.CustomResourceDefinitionVersion, schemasMap map[string]*types.APISchema) {
2019-08-14 18:08:34 +00:00
var versionColumns []table.Column
2021-04-21 16:49:03 +00:00
for _, col := range version.AdditionalPrinterColumns {
2019-08-14 18:08:34 +00:00
versionColumns = append(versionColumns, table.Column{
2019-08-14 18:16:37 +00:00
Name: col.Name,
Field: col.JSONPath,
Type: col.Type,
Format: col.Format,
2019-08-14 18:08:34 +00:00
})
}
id := GVKToVersionedSchemaID(schema.GroupVersionKind{
Group: group,
2021-04-21 16:49:03 +00:00
Version: version.Name,
Kind: kind,
2019-08-14 18:08:34 +00:00
})
2020-01-31 05:37:59 +00:00
schema := schemasMap[id]
2019-08-14 18:08:34 +00:00
if schema == nil {
return
}
2021-04-21 16:49:03 +00:00
if len(versionColumns) > 0 {
attributes.SetColumns(schema, versionColumns)
2019-08-16 18:40:42 +00:00
}
2021-04-21 16:49:03 +00:00
if version.Schema != nil && version.Schema.OpenAPIV3Schema != nil {
schema.Description = version.Schema.OpenAPIV3Schema.Description
2020-01-31 05:01:21 +00:00
}
2019-08-14 18:08:34 +00:00
}