-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
1,082 additions
and
219 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,223 @@ | ||
package definitions | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/rancher/apiserver/pkg/types" | ||
wapiextv1 "github.com/rancher/wrangler/v2/pkg/generated/controllers/apiextensions.k8s.io/v1" | ||
wranglerDefinition "github.com/rancher/wrangler/v2/pkg/schemas/definition" | ||
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/kube-openapi/pkg/util/proto" | ||
) | ||
|
||
var ( | ||
ErrNotFound = errors.New("not found") | ||
ErrNotRefreshed = errors.New("not refreshed") | ||
) | ||
|
||
// crdToDefinition builds a schemaDefinition for a CustomResourceDefinition | ||
func crdToDefinition(crdCache wapiextv1.CustomResourceDefinitionCache, crdName string, modelName string, version string) (schemaDefinition, error) { | ||
crd, err := crdCache.Get(crdName) | ||
if err != nil { | ||
if apierrors.IsNotFound(err) { | ||
return schemaDefinition{}, ErrNotFound | ||
} | ||
return schemaDefinition{}, err | ||
} | ||
|
||
var jsonSchemaProps *apiextv1.JSONSchemaProps | ||
for _, crdVersion := range crd.Spec.Versions { | ||
if crdVersion.Name == version { | ||
jsonSchemaProps = crdVersion.Schema.OpenAPIV3Schema | ||
break | ||
} | ||
} | ||
|
||
if jsonSchemaProps == nil { | ||
return schemaDefinition{}, fmt.Errorf("unknown version %q for CRD %q", version, crdName) | ||
} | ||
|
||
// CRD definitions generally has more information than the OpenAPI V2 | ||
// because it embeds an OpenAPI V3 document. However, these 3 fields | ||
// are the exception where the Open API V2 endpoint has more | ||
// information. | ||
// | ||
// To avoid overriding these later on, we remove them here. Yeah, really. | ||
delete(jsonSchemaProps.Properties, "apiVersion") | ||
delete(jsonSchemaProps.Properties, "kind") | ||
delete(jsonSchemaProps.Properties, "metadata") | ||
|
||
path := proto.NewPath(modelName) | ||
|
||
definitions := make(map[string]definition) | ||
convertJSONSchemaPropsToDefinition(*jsonSchemaProps, path, definitions) | ||
|
||
return schemaDefinition{ | ||
DefinitionType: modelName, | ||
Definitions: definitions, | ||
}, nil | ||
} | ||
|
||
func convertJSONSchemaPropsToDefinition(props apiextv1.JSONSchemaProps, path proto.Path, definitions map[string]definition) { | ||
if props.Type != "array" && props.Type != "object" { | ||
return | ||
} | ||
|
||
// Get the properties of the items inside the array | ||
if props.Type == "array" { | ||
items := getItemsSchema(props) | ||
if items == nil { | ||
return | ||
} | ||
props = *items | ||
} | ||
|
||
def := definition{ | ||
Type: path.String(), | ||
Description: props.Description, | ||
ResourceFields: map[string]definitionField{}, | ||
} | ||
|
||
requiredSet := make(map[string]struct{}) | ||
for _, name := range props.Required { | ||
requiredSet[name] = struct{}{} | ||
} | ||
|
||
for name, prop := range props.Properties { | ||
_, required := requiredSet[name] | ||
field := convertJSONSchemaPropsToDefinitionField(prop, path.FieldPath(name), required) | ||
def.ResourceFields[name] = field | ||
|
||
convertJSONSchemaPropsToDefinition(prop, path.FieldPath(name), definitions) | ||
} | ||
definitions[path.String()] = def | ||
} | ||
|
||
func convertJSONSchemaPropsToDefinitionField(props apiextv1.JSONSchemaProps, path proto.Path, required bool) definitionField { | ||
field := definitionField{ | ||
Description: props.Description, | ||
Required: required, | ||
Type: getPrimitiveType(props.Type), | ||
} | ||
switch props.Type { | ||
case "array": | ||
field.Type = "array" | ||
if item := getItemsSchema(props); item != nil { | ||
if item.Type == "object" || item.Type == "array" { | ||
field.SubType = path.String() | ||
} else { | ||
field.SubType = getPrimitiveType(item.Type) | ||
} | ||
} | ||
case "object": | ||
field.Type = path.String() | ||
} | ||
return field | ||
} | ||
|
||
func getPrimitiveType(typ string) string { | ||
switch typ { | ||
case "string": | ||
return "string" | ||
case "boolean": | ||
return "boolean" | ||
case "integer", "number": | ||
return "int" | ||
} | ||
return "" | ||
} | ||
|
||
func getItemsSchema(props apiextv1.JSONSchemaProps) *apiextv1.JSONSchemaProps { | ||
if props.Items == nil { | ||
return nil | ||
} | ||
|
||
if props.Items.Schema != nil { | ||
return props.Items.Schema | ||
} else if len(props.Items.JSONSchemas) > 0 { | ||
return &props.Items.JSONSchemas[0] | ||
} | ||
return nil | ||
} | ||
|
||
// openAPIV2ToDefinition builds a schemaDefinition for the given schemaID based on | ||
// Resource information from OpenAPI v2 endpoint | ||
func openAPIV2ToDefinition(models proto.Models, modelName string, version string) (schemaDefinition, error) { | ||
protoSchema := models.LookupModel(modelName) | ||
switch m := protoSchema.(type) { | ||
case *proto.Map: | ||
// If the schema is a *proto.Map, it will not have any Fields associated with it | ||
// even though all Kubernetes resources have at least apiVersion, kind and metadata. | ||
// | ||
// We transform this Map to a Kind and inject these fields from | ||
// a known existing Kubernetes resource (ConfigMap). | ||
configMap := models.LookupModel("io.k8s.api.core.v1.ConfigMap") | ||
apiVersion := configMap.(*proto.Kind).Fields["apiVersion"] | ||
apiVersion.(*proto.Primitive).Path = m.Path.FieldPath("apiVersion") | ||
kind := configMap.(*proto.Kind).Fields["kind"] | ||
kind.(*proto.Primitive).Path = m.Path.FieldPath("kind") | ||
metadata := configMap.(*proto.Kind).Fields["metadata"] | ||
metadata.(*proto.Ref).Path = m.Path.FieldPath("metadata") | ||
protoSchema = &proto.Kind{ | ||
BaseSchema: m.BaseSchema, | ||
Fields: map[string]proto.Schema{ | ||
"apiVersion": apiVersion, | ||
"kind": kind, | ||
"metadata": metadata, | ||
}, | ||
} | ||
case *proto.Kind: | ||
default: | ||
return schemaDefinition{}, fmt.Errorf("model for %s was type %T, not a *proto.Kind nor *proto.Map", modelName, protoSchema) | ||
} | ||
definitions := map[string]definition{} | ||
visitor := schemaFieldVisitor{ | ||
definitions: definitions, | ||
models: models, | ||
} | ||
protoSchema.Accept(&visitor) | ||
|
||
return schemaDefinition{ | ||
DefinitionType: modelName, | ||
Definitions: definitions, | ||
}, nil | ||
} | ||
|
||
// baseSchemaToDefinition converts a given schema to the definition map. This should only be used with baseSchemas, whose definitions | ||
// are expected to be set by another application and may not be k8s resources. | ||
func baseSchemaToDefinition(schema types.APISchema) map[string]definition { | ||
definitions := map[string]definition{} | ||
def := definition{ | ||
Description: schema.Description, | ||
Type: schema.ID, | ||
ResourceFields: map[string]definitionField{}, | ||
} | ||
for fieldName, field := range schema.ResourceFields { | ||
fieldType, subType := parseFieldType(field.Type) | ||
def.ResourceFields[fieldName] = definitionField{ | ||
Type: fieldType, | ||
SubType: subType, | ||
Description: field.Description, | ||
Required: field.Required, | ||
} | ||
} | ||
definitions[schema.ID] = def | ||
return definitions | ||
} | ||
|
||
// parseFieldType parses a schemas.Field's type to a type (first return) and subType (second return) | ||
func parseFieldType(fieldType string) (string, string) { | ||
subType := wranglerDefinition.SubType(fieldType) | ||
if wranglerDefinition.IsMapType(fieldType) { | ||
return "map", subType | ||
} | ||
if wranglerDefinition.IsArrayType(fieldType) { | ||
return "array", subType | ||
} | ||
if wranglerDefinition.IsReferenceType(fieldType) { | ||
return "reference", subType | ||
} | ||
return fieldType, "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.