Skip to content

Commit 9752c3a

Browse files
committed
encoding/jsonschema: fix x-kubernetes-group-version-kind
It turns out that the `group` field should also be considered when determining the value of the `apiVersion` field, so we get versions like `apps/v1` as well as versions like plain `v1`. Signed-off-by: Roger Peppe <[email protected]> Change-Id: Iec4026e6935528a03c7e0f0a0b973d9f2f0fae4d Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1214817 Reviewed-by: Daniel Martí <[email protected]> TryBot-Result: CUEcueckoo <[email protected]> Unity-Result: CUE porcuepine <[email protected]>
1 parent e231c49 commit 9752c3a

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

encoding/jsonschema/constraints_object.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,25 +57,31 @@ func constraintGroupVersionKind(key string, n cue.Value, s *state) {
5757
// TODO implement support for multiple items
5858
return
5959
}
60+
var group, version string
6061
s.processMap(items[0], func(key string, n cue.Value) {
6162
if strings.HasPrefix(key, "x-") {
6263
// TODO are x- extension properties actually allowed in this context?
6364
return
6465
}
6566
switch key {
6667
case "group":
67-
return
68+
group, _ = s.strValue(n)
6869
case "kind":
6970
s.k8sResourceKind, _ = s.strValue(n)
7071
case "version":
71-
s.k8sAPIVersion, _ = s.strValue(n)
72+
version, _ = s.strValue(n)
7273
default:
7374
s.errf(n, "unknown field %q in x-kubernetes-group-version-kind item", key)
7475
}
7576
})
76-
if s.k8sResourceKind == "" || s.k8sAPIVersion == "" {
77+
if s.k8sResourceKind == "" || version == "" {
7778
s.errf(n, "x-kubernetes-group-version-kind needs both kind and version fields")
7879
}
80+
if group == "" {
81+
s.k8sAPIVersion = version
82+
} else {
83+
s.k8sAPIVersion = group + "/" + version
84+
}
7985
}
8086

8187
func constraintAdditionalProperties(key string, n cue.Value, s *state) {

encoding/jsonschema/testdata/txtar/k8s_group_kind_version.txtar

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
Note: the schema.json file here is derived from the
2-
file api/openapi-spec/v3/api_openapi.json in
1+
Note: the schema.json file here has components
2+
derived from the api/openapi-spec/v3 directory
33
the Kubernetes repository.
44

55
#version: k8sAPI
@@ -8,6 +8,24 @@ the Kubernetes repository.
88
{
99
"components": {
1010
"schemas": {
11+
"io.k8s.api.apps.v1.Deployment": {
12+
"properties": {
13+
"apiVersion": {
14+
"type": "string"
15+
},
16+
"kind": {
17+
"type": "string"
18+
}
19+
},
20+
"type": "object",
21+
"x-kubernetes-group-version-kind": [
22+
{
23+
"group": "apps",
24+
"kind": "Deployment",
25+
"version": "v1"
26+
}
27+
]
28+
},
1129
"io.k8s.apimachinery.pkg.apis.meta.v1.APIVersions": {
1230
"properties": {
1331
"apiVersion": {
@@ -84,6 +102,11 @@ the Kubernetes repository.
84102
"paths": {}
85103
}
86104
-- out/decode/extract --
105+
_#defs: "/components/schemas/io.k8s.api.apps.v1.Deployment": {
106+
apiVersion: "apps/v1"
107+
kind: "Deployment"
108+
}
109+
87110
_#defs: "/components/schemas/io.k8s.apimachinery.pkg.apis.meta.v1.APIVersions": {
88111
apiVersion: "v1"
89112
kind: "APIVersions"

0 commit comments

Comments
 (0)