Skip to content
New issue

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

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

Already on GitHub? # to your account

cue/cmd: import crd for importing Kubernetes CustomResourceDefinitions #2701

Draft
wants to merge 21 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
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
Prev Previous commit
Next Next commit
wire up to the command for testing the experiment
  • Loading branch information
justenstall committed Nov 14, 2023
commit 5f0237cfb06737719a7be001cf33e50504b244c7
32 changes: 23 additions & 9 deletions cmd/cue/cmd/get_crd.go
Original file line number Diff line number Diff line change
@@ -15,8 +15,12 @@
package cmd

import (
"cuelang.org/go/cue/build"
"cuelang.org/go/cue/load"
"os"
"path/filepath"
"strings"

"cuelang.org/go/cue/cuecontext"
"cuelang.org/go/encoding/crd"
"github.com/spf13/cobra"
)

@@ -50,24 +54,34 @@ CustomResourceDefinitions are converted to cue structs adhering to the following
- The @x-kubernetes-validation attribute is added if the field utilizes the "x-kubernetes-validation" extension.
`,

RunE: mkRunE(c, extract),
RunE: mkRunE(c, runGetCRD),
}

return cmd
}

func runGetCRD(cmd *Command, args []string) error {
c := &config{
fileFilter: `\.(json|yaml|yml|jsonl|ldjson)$`,
interpretation: build.CustomResourceDefinition,
encoding: "yaml",
loadCfg: &load.Config{DataFiles: true},
decoder := crd.NewDecoder(cuecontext.New(), "cue get crd "+strings.Join(args, " "))

data, err := os.ReadFile(args[0])
if err != nil {
return err
}

p, err := newBuildPlan(cmd, c)
defs, err := decoder.Generate(data)
if err != nil {
return err
}

for path, crd := range defs {
if err = os.MkdirAll(path, 0644); err != nil {
return err
}

if err = os.WriteFile(filepath.Join(path, "types_gen.cue"), crd, 0644); err != nil {
return err
}
}

return nil
}
21 changes: 7 additions & 14 deletions encoding/crd/extract.go
Original file line number Diff line number Diff line change
@@ -212,6 +212,9 @@ func convertCRD(crd cue.Value) (*IntermediateCRD, error) {
}
`, cc.Props.Spec.Group, ver, kname, nsConstraint)))

// Add x-kubernetes-* OpenAPI extensions as attributes
sch = sch.FillPath(defpath, exts(ctx, defpath, *cc.Props.Spec.Versions[i].Schema.OpenAPIV3Schema))

// now, go back to an AST because it's easier to manipulate references there
var schast *ast.File
switch x := sch.Syntax(cue.All(), cue.Docs(true)).(type) {
@@ -254,11 +257,6 @@ func convertCRD(crd cue.Value) (*IntermediateCRD, error) {
return nil
}

extensions, err := exts(ctx, defpath, *cc.Props.Spec.Versions[0].Schema.OpenAPIV3Schema)
if err != nil {
return nil, err
}

// Have to prepend with the defpath where the CUE CRD representation
// lives because the astutil walker to remove ellipses operates over the
// whole file, and therefore will be looking for full paths, extending
@@ -409,7 +407,7 @@ func parentPath(c astutil.Cursor) (cue.Selector, astutil.Cursor) {
}

// exts preserves k8s OpenAPI extensions as attributes
func exts(ctx *cue.Context, path cue.Path, props v1.JSONSchemaProps) (cue.Value, error) {
func exts(ctx *cue.Context, path cue.Path, props v1.JSONSchemaProps) cue.Value {
extensions := cue.Value{}

if props.XPreserveUnknownFields != nil {
@@ -441,14 +439,9 @@ func exts(ctx *cue.Context, path cue.Path, props v1.JSONSchemaProps) (cue.Value,
}

for name, subProp := range props.Properties {
subExtensions, err := exts(ctx, cue.MakePath(cue.Str(name)), subProp)
if err != nil {
return extensions, err
}

// Merge subextensions at this level
extensions = extensions.FillPath(path, subExtensions)
// Recursively add subextensions for each property
extensions = extensions.FillPath(path, exts(ctx, cue.MakePath(cue.Str(name)), subProp))
}

return extensions, nil
return extensions
}