Skip to content

Commit

Permalink
Merge pull request #4567 from mvgmb/master
Browse files Browse the repository at this point in the history
Add support for remote OpenAPI schema
  • Loading branch information
k8s-ci-robot committed May 10, 2022
2 parents c3b5d8f + d49e5aa commit d3d9215
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 5 deletions.
4 changes: 1 addition & 3 deletions api/internal/target/kusttarget.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package target
import (
"encoding/json"
"fmt"
"path/filepath"
"strings"

"github.com/pkg/errors"
Expand Down Expand Up @@ -481,9 +480,8 @@ func (kt *KustTarget) accumulateDirectory(
subKt.kustomization.BuildMetadata = kt.kustomization.BuildMetadata
subKt.origin = kt.origin
var bytes []byte
path := ldr.Root()
if openApiPath, exists := subKt.Kustomization().OpenAPI["path"]; exists {
bytes, err = ldr.Load(filepath.Join(path, openApiPath))
bytes, err = ldr.Load(openApiPath)
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions api/krusty/kustomizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package krusty

import (
"fmt"
"path/filepath"

"sigs.k8s.io/kustomize/api/internal/builtins"
pLdr "sigs.k8s.io/kustomize/api/internal/plugins/loader"
Expand Down Expand Up @@ -76,7 +75,7 @@ func (b *Kustomizer) Run(
}
var bytes []byte
if openApiPath, exists := kt.Kustomization().OpenAPI["path"]; exists {
bytes, err = ldr.Load(filepath.Join(ldr.Root(), openApiPath))
bytes, err = ldr.Load(openApiPath)
if err != nil {
return nil, err
}
Expand Down
63 changes: 63 additions & 0 deletions api/krusty/openapitests/openapicustomschema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,23 @@ openapi:
})
}

func TestCustomOpenApiFieldBasicUsageWithRemoteSchema(t *testing.T) {
runOpenApiTest(t, func(t *testing.T) {
t.Helper()
th := kusttest_test.MakeHarness(t)
th.WriteK(".", `
resources:
- mycrd.yaml
openapi:
path: https://github.com/kubernetes-sigs/kustomize/raw/master/api/krusty/testdata/customschema.json
`+customSchemaPatch)
writeCustomResource(th, "mycrd.yaml")
writeTestSchema(th, "./")
m := th.Run(".", th.MakeDefaultOptions())
th.AssertActualEqualsExpected(m, patchedCustomResource)
})
}

func TestCustomOpenApiFieldWithTwoGvks(t *testing.T) {
runOpenApiTest(t, func(t *testing.T) {
t.Helper()
Expand Down Expand Up @@ -303,6 +320,29 @@ resources:
})
}

func TestCustomOpenApiFieldFromBaseWithRemoteSchema(t *testing.T) {
runOpenApiTest(t, func(t *testing.T) {
t.Helper()
th := kusttest_test.MakeHarness(t)
th.WriteK("base", `
resources:
- mycrd.yaml
openapi:
path: https://github.com/kubernetes-sigs/kustomize/raw/master/api/krusty/testdata/customschema.json
`)
th.WriteK("overlay", `
resources:
- ../base
`+customSchemaPatch)
writeCustomResource(th, "base/mycrd.yaml")
writeTestSchema(th, "base/")
m := th.Run("overlay", th.MakeDefaultOptions())
th.AssertActualEqualsExpected(m, patchedCustomResource)
assert.Equal(t, "using custom schema from file provided",
openapi.GetSchemaVersion())
})
}

func TestCustomOpenApiFieldFromOverlay(t *testing.T) {
runOpenApiTest(t, func(t *testing.T) {
t.Helper()
Expand All @@ -326,6 +366,29 @@ openapi:
})
}

func TestCustomOpenApiFieldFromOverlayWithRemoteSchema(t *testing.T) {
runOpenApiTest(t, func(t *testing.T) {
t.Helper()
th := kusttest_test.MakeHarness(t)
th.WriteK("base", `
resources:
- mycrd.yaml
`)
th.WriteK("overlay", `
resources:
- ../base
openapi:
path: https://github.com/kubernetes-sigs/kustomize/raw/master/api/krusty/testdata/customschema.json
`+customSchemaPatch)
writeCustomResource(th, "base/mycrd.yaml")
writeTestSchema(th, "overlay/")
m := th.Run("overlay", th.MakeDefaultOptions())
th.AssertActualEqualsExpected(m, patchedCustomResource)
assert.Equal(t, "using custom schema from file provided",
openapi.GetSchemaVersion())
})
}

func TestCustomOpenApiFieldOverlayTakesPrecedence(t *testing.T) {
runOpenApiTest(t, func(t *testing.T) {
t.Helper()
Expand Down

0 comments on commit d3d9215

Please sign in to comment.