Skip to content

Commit

Permalink
Merge pull request #4163 from natasha41575/multipleGvksInOpenApi
Browse files Browse the repository at this point in the history
support multiple gvks in custom openapi schema
  • Loading branch information
k8s-ci-robot committed Sep 16, 2021
2 parents 65e7529 + 02cb395 commit c1ae234
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 5 deletions.
94 changes: 94 additions & 0 deletions api/krusty/openapicustomschema_test.go
Expand Up @@ -42,6 +42,26 @@ spec:
`)
}

func writeOtherCustomResource(th kusttest_test.Harness, filepath string) {
th.WriteF(filepath, `
apiVersion: v1alpha1
kind: MyCRD
metadata:
name: service
spec:
template:
spec:
containers:
- name: server
image: server
command: example
ports:
- name: grpc
protocol: TCP
containerPort: 8080
`)
}

func writeTestComponentWithCustomSchema(th kusttest_test.Harness) {
writeTestSchema(th, "comp/")
openapi.ResetOpenAPI()
Expand Down Expand Up @@ -74,6 +94,32 @@ patchesStrategicMerge:
image: nginx
`

const customSchemaPatchMultipleGvks = `
patchesStrategicMerge:
- |-
apiVersion: example.com/v1alpha1
kind: MyCRD
metadata:
name: service
spec:
template:
spec:
containers:
- name: server
image: nginx
- |-
apiVersion: v1alpha1
kind: MyCRD
metadata:
name: service
spec:
template:
spec:
containers:
- name: server
image: nginx
`

const patchedCustomResource = `
apiVersion: example.com/v1alpha1
kind: MyCRD
Expand Down Expand Up @@ -108,6 +154,54 @@ openapi:
th.AssertActualEqualsExpected(m, patchedCustomResource)
}

func TestCustomOpenApiFieldWithTwoGvks(t *testing.T) {
th := kusttest_test.MakeHarness(t)
th.WriteK(".", `
resources:
- mycrd.yaml
- myothercrd.yaml
openapi:
path: mycrd_schema.json
`+customSchemaPatchMultipleGvks)
writeCustomResource(th, "mycrd.yaml")
writeOtherCustomResource(th, "myothercrd.yaml")
writeTestSchema(th, "./")
openapi.ResetOpenAPI()
m := th.Run(".", th.MakeDefaultOptions())
th.AssertActualEqualsExpected(m, `apiVersion: example.com/v1alpha1
kind: MyCRD
metadata:
name: service
spec:
template:
spec:
containers:
- command: example
image: nginx
name: server
ports:
- containerPort: 8080
name: grpc
protocol: TCP
---
apiVersion: v1alpha1
kind: MyCRD
metadata:
name: service
spec:
template:
spec:
containers:
- command: example
image: nginx
name: server
ports:
- containerPort: 8080
name: grpc
protocol: TCP
`)
}

func TestCustomOpenApiFieldYaml(t *testing.T) {
th := kusttest_test.MakeHarness(t)
th.WriteK(".", `
Expand Down
5 changes: 5 additions & 0 deletions api/krusty/testdata/customschema.json
Expand Up @@ -34,6 +34,11 @@
"group": "example.com",
"kind": "MyCRD",
"version": "v1alpha1"
},
{
"group": "",
"kind": "MyCRD",
"version": "v1alpha1"
}
]
},
Expand Down
3 changes: 3 additions & 0 deletions api/krusty/testdata/customschema.yaml
Expand Up @@ -22,6 +22,9 @@ definitions:
- group: example.com
kind: MyCRD
version: v1alpha1
- group: ""
kind: MyCRD
version: v1alpha1
io.k8s.api.core.v1.PodTemplateSpec:
properties:
metadata:
Expand Down
12 changes: 7 additions & 5 deletions kyaml/openapi/openapi.go
Expand Up @@ -296,15 +296,17 @@ func AddDefinitions(definitions spec.Definitions) {
}
// cast the extension to a []map[string]string
exts, ok := gvk.([]interface{})
if !ok || len(exts) != 1 {
if !ok {
continue
}

typeMeta, ok := toTypeMeta(exts[0])
if !ok {
continue
for i := range exts {
typeMeta, ok := toTypeMeta(exts[i])
if !ok {
continue
}
globalSchema.schemaByResourceType[typeMeta] = &d
}
globalSchema.schemaByResourceType[typeMeta] = &d
}
}

Expand Down

0 comments on commit c1ae234

Please sign in to comment.