Skip to content

Commit dbe2611

Browse files
committed
encoding/jsonschema: CRDs always define resources
The top level schema in a Kubernetes CRD is by definition a resource, so should allow the usual metadata fields. For #3907 Signed-off-by: Roger Peppe <[email protected]> Change-Id: I8fac511d74f7008da751a9864ed9155780311ee1 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1214626 TryBot-Result: CUEcueckoo <[email protected]> Reviewed-by: Daniel Martí <[email protected]> Unity-Result: CUE porcuepine <[email protected]>
1 parent f712e19 commit dbe2611

File tree

6 files changed

+54
-1
lines changed

6 files changed

+54
-1
lines changed

encoding/jsonschema/decode.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,11 @@ func (d *decoder) decode(v cue.Value) *ast.File {
182182
// containing a field for each definition.
183183
constraintAddDefinitions("schemas", defsRoot, root)
184184
} else {
185-
expr, state := root.schemaState(v, allTypes, nil)
185+
expr, state := root.schemaState(v, allTypes, func(s *state) {
186+
// We want the top level state to be treated as root even
187+
// though it's some levels below the actual document top level.
188+
s.isRoot = true
189+
})
186190
if state.allowedTypes == 0 {
187191
root.errf(v, "constraints are not possible to satisfy")
188192
return nil
@@ -887,6 +891,18 @@ func (s0 *state) schemaState(n cue.Value, types cue.Kind, init func(*state)) (ex
887891
}
888892
c.fn(key, value, s)
889893
})
894+
if s.schemaVersion == VersionKubernetesCRD && s.isRoot {
895+
// The root of a CRD is always a resource, so treat it as if it contained
896+
// the x-kubernetes-embedded-resource keyword
897+
c := constraintMap["x-kubernetes-embedded-resource"]
898+
if c.phase != pass {
899+
continue
900+
}
901+
// Note: there is no field value for the embedded-resource keyword,
902+
// but it's not actually used except for its position so passing
903+
// the parent object should work fine.
904+
c.fn("x-kubernetes-embedded-resource", n, s)
905+
}
890906
}
891907
if s.id != nil {
892908
// If there's an ID, it can be referred to.

encoding/jsonschema/testdata/txtar/crd_embeddedresource.txtar

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
}
5656
}
5757
-- out/decode/extract --
58+
_embeddedResource
5859
apiVersion?: string
5960
kind?: string
6061
metadata?: {}

encoding/jsonschema/testdata/txtar/crd_intorstring.txtar

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,13 @@ spec:
2929
x-kubernetes-int-or-string: true
3030

3131
-- out/decode/extract --
32+
_embeddedResource
3233
spec?: int | string
34+
35+
_embeddedResource: {
36+
apiVersion!: string
37+
kind!: string
38+
metadata!: {
39+
...
40+
}
41+
}

encoding/jsonschema/testdata/txtar/crd_intorstring_with_anyof.txtar

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,13 @@ spec:
3535
- type: string
3636

3737
-- out/decode/extract --
38+
_embeddedResource
3839
spec?: matchN(>=1, [int, string]) & (int | string)
40+
41+
_embeddedResource: {
42+
apiVersion!: string
43+
kind!: string
44+
metadata!: {
45+
...
46+
}
47+
}

encoding/jsonschema/testdata/txtar/crd_preserve_unknown.txtar

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
}
7777

7878
-- out/decode/extract --
79+
_embeddedResource
7980
alpha?: string
8081
beta?: number
8182
preserving?: {
@@ -92,4 +93,12 @@ pruning?: {
9293
}
9394
pruning?: {}
9495
}
96+
97+
_embeddedResource: {
98+
apiVersion!: string
99+
kind!: string
100+
metadata!: {
101+
...
102+
}
103+
}
95104
...

encoding/jsonschema/testdata/txtar/crd_simple.txtar

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ spec:
3030
x-kubernetes-preserve-unknown-fields: true
3131

3232
-- out/decode/extract --
33+
_embeddedResource
3334
spec?: {
3435
...
3536
}
37+
38+
_embeddedResource: {
39+
apiVersion!: string
40+
kind!: string
41+
metadata!: {
42+
...
43+
}
44+
}

0 commit comments

Comments
 (0)