Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance: support v1 crd with multiple versions #63

Merged
merged 1 commit into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 45 additions & 29 deletions pkg/kube_resource/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,29 +122,60 @@ func crdObj2CrdInternal(crdObj runtime.Object) (*apiextensions.CustomResourceDef
default:
return nil, errors.New(fmt.Sprintf("unknown crd object type %v", crdObj.GetObjectKind()))
}
if crd.Spec.Validation == nil || crd.Spec.Validation.OpenAPIV3Schema == nil {
return nil, errors.New("no openapi schema found in the crd file. Please check following fields: spec.Validation.OpenAPIV3Schema, spec.Versions.0.Schema")

if !CRDContainsValidation(crd) {
return nil, errors.New("no openapi schema found in the crd file. Please check following fields: \nspec.Versions.<n>.Schema, spec.Versions.<n>.Schema.OpenAPIV3Schema, spec.Validation.OpenAPIV3Schema, spec.Versions.0.Schema")
}
return crd, nil
}

func CRDContainsValidation(crd *apiextensions.CustomResourceDefinition) bool {
if crd.Spec.Validation != nil && crd.Spec.Validation.OpenAPIV3Schema != nil {
return true
}
for _, version := range crd.Spec.Versions {
if version.Schema != nil && version.Schema.OpenAPIV3Schema != nil {
return true
}
}
return false
}

func buildSwagger(crd *apiextensions.CustomResourceDefinition) (*spec.Swagger, error) {
var schema spec.Schema
err := validation.ConvertJSONSchemaProps(crd.Spec.Validation.OpenAPIV3Schema, &schema)
if err != nil {
return nil, err
var schemas spec.Definitions = map[string]spec.Schema{}
group, kind := crd.Spec.Group, crd.Spec.Names.Kind
if crd.Spec.Validation != nil && crd.Spec.Validation.OpenAPIV3Schema != nil {
var schema spec.Schema
err := validation.ConvertJSONSchemaProps(crd.Spec.Validation.OpenAPIV3Schema, &schema)
if err != nil {
return nil, err
}
version := crd.Spec.Version
setKubeNative(&schema, group, version, kind)
name := fmt.Sprintf("%s.%s.%s", group, version, kind)
schemas[name] = schema
} else if len(crd.Spec.Versions) > 0 {
for _, version := range crd.Spec.Versions {
if version.Schema != nil && version.Schema.OpenAPIV3Schema != nil {
var schema spec.Schema
err := validation.ConvertJSONSchemaProps(version.Schema.OpenAPIV3Schema, &schema)
if err != nil {
return nil, err
}
version := version.Name
setKubeNative(&schema, group, version, kind)
name := fmt.Sprintf("%s.%s.%s", group, version, kind)
schemas[name] = schema
}
}
}
group, kind, version := kvg(crd)
setKubeNative(&schema, group, version, kind)

// todo: set extensions, include kcl-type and user-defined extensions
name := fmt.Sprintf("%s.%s.%s", group, version, kind)
return &spec.Swagger{
SwaggerProps: spec.SwaggerProps{
Swagger: "2.0", // todo: support swagger 3.0
Definitions: spec.Definitions{
name: schema,
},
Paths: &spec.Paths{},
Swagger: "2.0", // todo: support swagger 3.0
Definitions: schemas,
Paths: &spec.Paths{},
Info: &spec.Info{
InfoProps: spec.InfoProps{
Title: "Kubernetes CRD Swagger",
Expand Down Expand Up @@ -173,18 +204,3 @@ func setKubeNative(schema *spec.Schema, group string, version string, kind strin
WithDescription(swaggerPartialObjectMetadataDescriptions["metadata"]))
// todo: update more k8s refs to kcl format
}

func kvg(crd *apiextensions.CustomResourceDefinition) (string, string, string) {
group := crd.Spec.Group
kind := crd.Spec.Names.Kind
var version string
if crd.Spec.Version == "" {
if len(crd.Spec.Versions) != 0 {
// todo: fix hack: use last version
version = crd.Spec.Versions[len(crd.Spec.Versions)-1].Name
}
} else {
version = crd.Spec.Version
}
return group, kind, version
}
Loading
Loading