|
| 1 | +/* |
| 2 | +Copyright 2021 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +// main is the main package for openapi-gen. |
| 18 | +package main |
| 19 | + |
| 20 | +import ( |
| 21 | + "encoding/json" |
| 22 | + "fmt" |
| 23 | + "os" |
| 24 | + "path" |
| 25 | + |
| 26 | + "github.com/pkg/errors" |
| 27 | + flag "github.com/spf13/pflag" |
| 28 | + apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" |
| 29 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 30 | + "k8s.io/apimachinery/pkg/util/validation/field" |
| 31 | + "k8s.io/klog/v2" |
| 32 | + "k8s.io/utils/pointer" |
| 33 | + "sigs.k8s.io/controller-tools/pkg/crd" |
| 34 | + "sigs.k8s.io/controller-tools/pkg/loader" |
| 35 | + "sigs.k8s.io/controller-tools/pkg/markers" |
| 36 | + |
| 37 | + clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" |
| 38 | +) |
| 39 | + |
| 40 | +var ( |
| 41 | + paths = flag.String("paths", "", "Paths with the variable types.") |
| 42 | + outputFile = flag.String("output-file", "zz_generated.variables.json", "Output file name.") |
| 43 | +) |
| 44 | + |
| 45 | +// FIXME: re-evaluate if we should still use openapi-gen in the other case. |
| 46 | +func main() { |
| 47 | + flag.Parse() |
| 48 | + |
| 49 | + if *paths == "" { |
| 50 | + klog.Exit("--paths must be specified") |
| 51 | + } |
| 52 | + |
| 53 | + if *outputFile == "" { |
| 54 | + klog.Exit("--output-file must be specified") |
| 55 | + } |
| 56 | + |
| 57 | + outputFileExt := path.Ext(*outputFile) |
| 58 | + if outputFileExt != ".json" { |
| 59 | + klog.Exit("--output-file must have 'json' extension") |
| 60 | + } |
| 61 | + |
| 62 | + // FIXME: |
| 63 | + // * compare clusterv1.JsonSchemaProps vs kubebuilder marker if something is missing |
| 64 | + // * example marker |
| 65 | + // * cleanup code here |
| 66 | + |
| 67 | + if err := run(*paths, *outputFile); err != nil { |
| 68 | + fmt.Println(err) |
| 69 | + os.Exit(1) |
| 70 | + } |
| 71 | + |
| 72 | + // FIXME: Current state |
| 73 | + // * variable go type => apiextensionsv1.CustomResourceDefinition |
| 74 | + // * apiextensionsv1.CustomResourceDefinition => clusterv1.JsonSchemaProps |
| 75 | + // * Write schema as go structs to a file |
| 76 | + // * Validate: existing util (clusterv1.JsonSchemaProps) => validation result |
| 77 | +} |
| 78 | + |
| 79 | +func run(paths, outputFile string) error { |
| 80 | + crdGen := crd.Generator{} |
| 81 | + |
| 82 | + roots, err := loader.LoadRoots(paths) |
| 83 | + if err != nil { |
| 84 | + fmt.Println(err) |
| 85 | + } |
| 86 | + |
| 87 | + collector := &markers.Collector{ |
| 88 | + Registry: &markers.Registry{}, |
| 89 | + } |
| 90 | + if err = crdGen.RegisterMarkers(collector.Registry); err != nil { |
| 91 | + return err |
| 92 | + } |
| 93 | + |
| 94 | + parser := &crd.Parser{ |
| 95 | + Collector: collector, |
| 96 | + Checker: &loader.TypeChecker{ |
| 97 | + NodeFilters: []loader.NodeFilter{crdGen.CheckFilter()}, |
| 98 | + }, |
| 99 | + IgnoreUnexportedFields: true, |
| 100 | + AllowDangerousTypes: false, |
| 101 | + GenerateEmbeddedObjectMeta: false, |
| 102 | + } |
| 103 | + |
| 104 | + crd.AddKnownTypes(parser) |
| 105 | + for _, root := range roots { |
| 106 | + parser.NeedPackage(root) |
| 107 | + } |
| 108 | + |
| 109 | + kubeKinds := []schema.GroupKind{} |
| 110 | + for typeIdent := range parser.Types { |
| 111 | + // If we need another way to identify "variable structs": look at: crd.FindKubeKinds(parser, metav1Pkg) |
| 112 | + if typeIdent.Name == "Variables" { |
| 113 | + kubeKinds = append(kubeKinds, schema.GroupKind{ |
| 114 | + Group: parser.GroupVersions[typeIdent.Package].Group, |
| 115 | + Kind: typeIdent.Name, |
| 116 | + }) |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + // For inspiration: parser.NeedCRDFor(groupKind, nil) |
| 121 | + var variables []clusterv1.ClusterClassVariable |
| 122 | + for _, groupKind := range kubeKinds { |
| 123 | + // Get package for the current GroupKind |
| 124 | + var packages []*loader.Package |
| 125 | + for pkg, gv := range parser.GroupVersions { |
| 126 | + if gv.Group != groupKind.Group { |
| 127 | + continue |
| 128 | + } |
| 129 | + packages = append(packages, pkg) |
| 130 | + } |
| 131 | + |
| 132 | + var apiExtensionsSchema *apiextensionsv1.JSONSchemaProps |
| 133 | + for _, pkg := range packages { |
| 134 | + typeIdent := crd.TypeIdent{Package: pkg, Name: groupKind.Kind} |
| 135 | + typeInfo := parser.Types[typeIdent] |
| 136 | + |
| 137 | + // Didn't find type in pkg. |
| 138 | + if typeInfo == nil { |
| 139 | + continue |
| 140 | + } |
| 141 | + |
| 142 | + parser.NeedFlattenedSchemaFor(typeIdent) |
| 143 | + fullSchema := parser.FlattenedSchemata[typeIdent] |
| 144 | + apiExtensionsSchema = fullSchema.DeepCopy() // don't mutate the cache (we might be truncating description, etc) |
| 145 | + } |
| 146 | + |
| 147 | + if apiExtensionsSchema == nil { |
| 148 | + return errors.Errorf("Couldn't find schema for %s", groupKind) |
| 149 | + } |
| 150 | + |
| 151 | + for variableName, variableSchema := range apiExtensionsSchema.Properties { |
| 152 | + vs := variableSchema |
| 153 | + openAPIV3Schema, errs := convertToJSONSchemaProps(&vs, field.NewPath("schema")) |
| 154 | + if len(errs) > 0 { |
| 155 | + return errs.ToAggregate() |
| 156 | + } |
| 157 | + |
| 158 | + variable := clusterv1.ClusterClassVariable{ |
| 159 | + Name: variableName, |
| 160 | + Schema: clusterv1.VariableSchema{ |
| 161 | + OpenAPIV3Schema: *openAPIV3Schema, |
| 162 | + }, |
| 163 | + } |
| 164 | + |
| 165 | + for _, requiredVariable := range apiExtensionsSchema.Required { |
| 166 | + if variableName == requiredVariable { |
| 167 | + variable.Required = true |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + variables = append(variables, variable) |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + res, err := json.MarshalIndent(variables, "", " ") |
| 176 | + if err != nil { |
| 177 | + return err |
| 178 | + } |
| 179 | + |
| 180 | + if err := os.WriteFile(outputFile, res, 0600); err != nil { |
| 181 | + return errors.Wrapf(err, "failed to write generated file") |
| 182 | + } |
| 183 | + |
| 184 | + return nil |
| 185 | +} |
| 186 | + |
| 187 | +// JSONSchemaProps converts a apiextensions.JSONSchemaProp to a clusterv1.JSONSchemaProps. |
| 188 | +func convertToJSONSchemaProps(schema *apiextensionsv1.JSONSchemaProps, fldPath *field.Path) (*clusterv1.JSONSchemaProps, field.ErrorList) { |
| 189 | + var allErrs field.ErrorList |
| 190 | + |
| 191 | + props := &clusterv1.JSONSchemaProps{ |
| 192 | + Description: schema.Description, |
| 193 | + Type: schema.Type, |
| 194 | + Required: schema.Required, |
| 195 | + MaxItems: schema.MaxItems, |
| 196 | + MinItems: schema.MinItems, |
| 197 | + UniqueItems: schema.UniqueItems, |
| 198 | + Format: schema.Format, |
| 199 | + MaxLength: schema.MaxLength, |
| 200 | + MinLength: schema.MinLength, |
| 201 | + Pattern: schema.Pattern, |
| 202 | + ExclusiveMaximum: schema.ExclusiveMaximum, |
| 203 | + ExclusiveMinimum: schema.ExclusiveMinimum, |
| 204 | + Default: schema.Default, |
| 205 | + Enum: schema.Enum, |
| 206 | + Example: schema.Example, |
| 207 | + XPreserveUnknownFields: pointer.BoolDeref(schema.XPreserveUnknownFields, false), |
| 208 | + } |
| 209 | + |
| 210 | + if schema.Maximum != nil { |
| 211 | + f := int64(*schema.Maximum) |
| 212 | + props.Maximum = &f |
| 213 | + } |
| 214 | + |
| 215 | + if schema.Minimum != nil { |
| 216 | + f := int64(*schema.Minimum) |
| 217 | + props.Minimum = &f |
| 218 | + } |
| 219 | + |
| 220 | + if schema.AdditionalProperties != nil { |
| 221 | + jsonSchemaProps, err := convertToJSONSchemaProps(schema.AdditionalProperties.Schema, fldPath.Child("additionalProperties")) |
| 222 | + if err != nil { |
| 223 | + allErrs = append(allErrs, field.Invalid(fldPath.Child("additionalProperties"), "", |
| 224 | + fmt.Sprintf("failed to convert schema: %v", err))) |
| 225 | + } else { |
| 226 | + props.AdditionalProperties = jsonSchemaProps |
| 227 | + } |
| 228 | + } |
| 229 | + |
| 230 | + if len(schema.Properties) > 0 { |
| 231 | + props.Properties = map[string]clusterv1.JSONSchemaProps{} |
| 232 | + for propertyName, propertySchema := range schema.Properties { |
| 233 | + p := propertySchema |
| 234 | + jsonSchemaProps, err := convertToJSONSchemaProps(&p, fldPath.Child("properties").Key(propertyName)) |
| 235 | + if err != nil { |
| 236 | + allErrs = append(allErrs, field.Invalid(fldPath.Child("properties").Key(propertyName), "", |
| 237 | + fmt.Sprintf("failed to convert schema: %v", err))) |
| 238 | + } else { |
| 239 | + props.Properties[propertyName] = *jsonSchemaProps |
| 240 | + } |
| 241 | + } |
| 242 | + } |
| 243 | + |
| 244 | + if schema.Items != nil { |
| 245 | + jsonSchemaProps, err := convertToJSONSchemaProps(schema.Items.Schema, fldPath.Child("items")) |
| 246 | + if err != nil { |
| 247 | + allErrs = append(allErrs, field.Invalid(fldPath.Child("items"), "", |
| 248 | + fmt.Sprintf("failed to convert schema: %v", err))) |
| 249 | + } else { |
| 250 | + props.Items = jsonSchemaProps |
| 251 | + } |
| 252 | + } |
| 253 | + |
| 254 | + return props, allErrs |
| 255 | +} |
0 commit comments