in scripts/generate-gvks/main.go [39:159]
func main() {
flag.StringVar(&inputDir, "input-dir", "", "Input directory to read CRD listing.")
flag.StringVar(&outputFile, "output-file", "", "Output file to write GVK -> reconciler type mapping.")
flag.Parse()
if inputDir == "" {
fmt.Fprintf(os.Stderr, "error: invalid value for input directory: '%s'\n", "empty string")
flag.PrintDefaults()
os.Exit(1)
}
if outputFile == "" {
fmt.Fprintf(os.Stderr, "error: invalid value for output file: '%s'\n", "empty string")
flag.PrintDefaults()
os.Exit(1)
}
crds := make([]apiextensions.CustomResourceDefinition, 0)
err := filepath.WalkDir(inputDir, func(path string, info fs.DirEntry, err error) error {
if err != nil {
return err
}
if info.IsDir() || !strings.HasSuffix(path, ".yaml") {
return nil
}
y, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("failed to read CRD file %s: %w", path, err)
}
var crd apiextensions.CustomResourceDefinition
if err := yaml.Unmarshal(y, &crd); err != nil {
return fmt.Errorf("error unmarshalling bytes into CRD: %w", err)
}
crds = append(crds, crd)
return nil
})
if err != nil {
fmt.Fprintf(os.Stderr, "failed to read CRDs: %v\n", err)
os.Exit(1)
}
out := `// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Code generated by "scripts/generate-gvks" in "make manifests"; DO NOT EDIT.
package supportedgvks
import "k8s.io/apimachinery/pkg/runtime/schema"
type GVKMetadata struct {
Labels map[string]string
}
var SupportedGVKs = map[schema.GroupVersionKind]GVKMetadata{`
for _, crd := range crds {
for _, version := range crd.Spec.Versions {
gvk := schema.GroupVersionKind{
Group: crd.Spec.Group,
Kind: crd.Spec.Names.Kind,
Version: version.Name,
}
gvkEntry := `
{
Group: "%s",
Version: "%s",
Kind: "%s",
}: {
Labels: map[string]string{`
// Iterate over the labels in sorted order to produce deterministic output.
keys := make([]string, 0)
for k, _ := range crd.Labels {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
labelEntry := `
"%s": "%s",`
gvkEntry += fmt.Sprintf(labelEntry, k, crd.Labels[k])
}
gvkEntry += `
},
},`
out += fmt.Sprintf(gvkEntry, gvk.Group, gvk.Version, gvk.Kind)
}
}
out += `}`
outFormatted, err := format.Source([]byte(out))
if err != nil {
fmt.Fprintf(os.Stderr, "Error formatting output file: %s\n", err)
os.Exit(1)
}
file, err := os.OpenFile(outputFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
fmt.Fprintf(os.Stderr, "Error opening output file: %s\n", err)
os.Exit(1)
}
defer file.Close()
_, err = file.Write(outFormatted)
if err != nil {
fmt.Fprintf(os.Stderr, "Error writing to output file: %s\n", err)
os.Exit(1)
}
}