Reset openapi in Filter and show use of pkger with filepath

This commit is contained in:
Katrina Verey
2021-05-13 14:46:34 -07:00
parent 0fdf0f825f
commit 53c87a32e9
3 changed files with 36 additions and 18 deletions

View File

@@ -9,7 +9,6 @@ import (
"log" "log"
"path/filepath" "path/filepath"
"github.com/markbates/pkger"
"sigs.k8s.io/kustomize/kyaml/errors" "sigs.k8s.io/kustomize/kyaml/errors"
"sigs.k8s.io/kustomize/kyaml/fn/framework" "sigs.k8s.io/kustomize/kyaml/fn/framework"
"sigs.k8s.io/kustomize/kyaml/fn/framework/command" "sigs.k8s.io/kustomize/kyaml/fn/framework/command"
@@ -222,7 +221,7 @@ func ExampleTemplateProcessor_generate_files() {
// Templates // Templates
ResourceTemplates: []framework.ResourceTemplate{{ ResourceTemplates: []framework.ResourceTemplate{{
Templates: framework.TemplatesFromFile( Templates: framework.TemplatesFromFile(
pkger.Include("/fn/framework/testdata/example/templatefiles/deployment.template"), filepath.FromSlash("/fn/framework/testdata/example/templatefiles/deployment.template"),
), ),
}}, }},
} }

View File

@@ -224,6 +224,7 @@ func (tp TemplateProcessor) Filter(items []*yaml.RNode) ([]*yaml.RNode, error) {
if err != nil { if err != nil {
return nil, errors.WrapPrefixf(err, "parsing AdditionalSchemas") return nil, errors.WrapPrefixf(err, "parsing AdditionalSchemas")
} }
defer openapi.ResetOpenAPI()
for i := range defs { for i := range defs {
openapi.AddDefinitions(*defs[i]) openapi.AddDefinitions(*defs[i])
} }
@@ -368,6 +369,10 @@ func StringTemplates(data ...string) TemplatesFunc {
} }
// TemplatesFromFile returns a TemplatesFunc that will generate templates from the provided files. // TemplatesFromFile returns a TemplatesFunc that will generate templates from the provided files.
// Paths must be absolute using the module root as the filesystem root:
// TemplatesFromFile(filepath.FromSlash("/kyaml/fn/framework/templates/foo.template.yaml")).
// This function works with pkger-embedded static files. To use pkger's auto-detection feature:
// TemplatesFromFile(filepath.FromSlash(pkger.Include("/kyaml/fn/framework/templates/foo.template.yaml")))
// This is a helper to facilitate providing ResourceTemplates, PatchTemplates and // This is a helper to facilitate providing ResourceTemplates, PatchTemplates and
// ContainerPatchTemplates to a TemplateProcessor. // ContainerPatchTemplates to a TemplateProcessor.
func TemplatesFromFile(files ...string) TemplatesFunc { func TemplatesFromFile(files ...string) TemplatesFunc {
@@ -404,13 +409,16 @@ func parseTemplate(filename string) (*template.Template, error) {
// TemplatesFromDir returns a TemplatesFunc that will generate templates from the provided // TemplatesFromDir returns a TemplatesFunc that will generate templates from the provided
// directories. Only files suffixed with .template.yaml will be included. // directories. Only files suffixed with .template.yaml will be included.
// Paths must be absolute using the module root as the filesystem root:
// TemplatesFromDir(filepath.FromSlash("/kyaml/fn/framework/templates")).
// This function works with pkger-embedded static dirs. To use pkger's auto-detection feature:
// TemplatesFromDir(filepath.FromSlash(pkger.Include("/kyaml/fn/framework/templates")))
// This is a helper to facilitate providing ResourceTemplates, PatchTemplates and // This is a helper to facilitate providing ResourceTemplates, PatchTemplates and
// ContainerPatchTemplates to a TemplateProcessor. // ContainerPatchTemplates to a TemplateProcessor.
func TemplatesFromDir(dirs ...pkger.Dir) TemplatesFunc { func TemplatesFromDir(dirs ...string) TemplatesFunc {
return func() ([]*template.Template, error) { return func() ([]*template.Template, error) {
var pt []*template.Template var pt []*template.Template
for i := range dirs { for _, dir := range dirs {
dir := string(dirs[i])
err := pkger.Walk(dir, func(p string, info os.FileInfo, err error) error { err := pkger.Walk(dir, func(p string, info os.FileInfo, err error) error {
if err != nil { if err != nil {
return err return err
@@ -435,6 +443,10 @@ func TemplatesFromDir(dirs ...pkger.Dir) TemplatesFunc {
} }
// SchemaDefinitionsFromFile returns a SchemaDefinitionFunc that will load schemas from the provided files. // SchemaDefinitionsFromFile returns a SchemaDefinitionFunc that will load schemas from the provided files.
// Paths must be absolute using the module root as the filesystem root:
// SchemaDefinitionsFromFile(filepath.FromSlash("/kyaml/fn/framework/foo/schema.json")).
// This function works with pkger-embedded static files. To use pkger's auto-detection feature:
// SchemaDefinitionsFromFile(filepath.FromSlash(pkger.Include("/kyaml/fn/framework/foo/schema.json")))
// This is a helper to facilitate providing custom resource schemas to a TemplateProcessor. // This is a helper to facilitate providing custom resource schemas to a TemplateProcessor.
func SchemaDefinitionsFromFile(files ...string) SchemaDefinitionFunc { func SchemaDefinitionsFromFile(files ...string) SchemaDefinitionFunc {
return func() ([]*spec.Definitions, error) { return func() ([]*spec.Definitions, error) {
@@ -451,12 +463,15 @@ func SchemaDefinitionsFromFile(files ...string) SchemaDefinitionFunc {
} }
// SchemaDefinitionsFromDir returns a SchemaDefinitionFunc that will load schemas from the provided directories. // SchemaDefinitionsFromDir returns a SchemaDefinitionFunc that will load schemas from the provided directories.
// Paths must be absolute using the module root as the filesystem root:
// SchemaDefinitionsFromDir(filepath.FromSlash("/kyaml/fn/framework/schemas")).
// This function works with pkger-embedded static dirs. To use pkger's auto-detection feature:
// SchemaDefinitionsFromDir(filepath.FromSlash(pkger.Include("/kyaml/fn/framework/schemas")))
// This is a helper to facilitate providing custom resource schemas to a TemplateProcessor. // This is a helper to facilitate providing custom resource schemas to a TemplateProcessor.
func SchemaDefinitionsFromDir(dirs ...pkger.Dir) SchemaDefinitionFunc { func SchemaDefinitionsFromDir(dirs ...string) SchemaDefinitionFunc {
return func() ([]*spec.Definitions, error) { return func() ([]*spec.Definitions, error) {
var defs []*spec.Definitions var defs []*spec.Definitions
for i := range dirs { for _, dir := range dirs {
dir := string(dirs[i])
err := pkger.Walk(dir, func(p string, info os.FileInfo, err error) error { err := pkger.Walk(dir, func(p string, info os.FileInfo, err error) error {
if err != nil { if err != nil {
return err return err

View File

@@ -5,10 +5,10 @@ package framework_test
import ( import (
"bytes" "bytes"
"path/filepath"
"strings" "strings"
"testing" "testing"
"github.com/markbates/pkger"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"k8s.io/kube-openapi/pkg/validation/spec" "k8s.io/kube-openapi/pkg/validation/spec"
@@ -28,8 +28,8 @@ func TestTemplateProcessor_ResourceTemplates(t *testing.T) {
p := framework.TemplateProcessor{ p := framework.TemplateProcessor{
TemplateData: &API{}, TemplateData: &API{},
ResourceTemplates: []framework.ResourceTemplate{{ ResourceTemplates: []framework.ResourceTemplate{{
Templates: framework.TemplatesFromDir(pkger.Dir( Templates: framework.TemplatesFromDir(
"/fn/framework/testdata/template-processor/templates/basic")), filepath.FromSlash("/fn/framework/testdata/template-processor/templates/basic")),
}}, }},
} }
@@ -82,7 +82,7 @@ func TestTemplateProcessor_PatchTemplates(t *testing.T) {
PatchTemplates: []framework.PatchTemplate{ PatchTemplates: []framework.PatchTemplate{
// Patch from dir with no selector templating // Patch from dir with no selector templating
&framework.ResourcePatchTemplate{ &framework.ResourcePatchTemplate{
Templates: framework.TemplatesFromDir(pkger.Dir( Templates: framework.TemplatesFromDir(filepath.FromSlash(
"/fn/framework/testdata/template-processor/patches/basic")), "/fn/framework/testdata/template-processor/patches/basic")),
Selector: &framework.Selector{Names: []string{"foo"}}, Selector: &framework.Selector{Names: []string{"foo"}},
}, },
@@ -179,7 +179,7 @@ func TestTemplateProcessor_ContainerPatchTemplates(t *testing.T) {
PatchTemplates: []framework.PatchTemplate{ PatchTemplates: []framework.PatchTemplate{
// patch from dir with no selector templating // patch from dir with no selector templating
&framework.ContainerPatchTemplate{ &framework.ContainerPatchTemplate{
Templates: framework.TemplatesFromDir(pkger.Dir( Templates: framework.TemplatesFromDir(filepath.FromSlash(
"/fn/framework/testdata/template-processor/container-patches")), "/fn/framework/testdata/template-processor/container-patches")),
Selector: &framework.Selector{Names: []string{"foo"}}, Selector: &framework.Selector{Names: []string{"foo"}},
}, },
@@ -532,22 +532,22 @@ func TestTemplateProcessor_AdditionalSchemas(t *testing.T) {
p := framework.TemplateProcessor{ p := framework.TemplateProcessor{
AdditionalSchemas: func() ([]*spec.Definitions, error) { AdditionalSchemas: func() ([]*spec.Definitions, error) {
// This adds the same thing twice, just to exercise both the ...FromDir and the ...FromFile helpers // This adds the same thing twice, just to exercise both the ...FromDir and the ...FromFile helpers
c1, err := framework.SchemaDefinitionsFromDir("/fn/framework/testdata/template-processor/schemas")() c1, err := framework.SchemaDefinitionsFromDir(filepath.FromSlash("/fn/framework/testdata/template-processor/schemas"))()
if err != nil { if err != nil {
return nil, errors.WrapPrefixf(err, "schema from dir") return nil, errors.WrapPrefixf(err, "schema from dir")
} }
c2, err := framework.SchemaDefinitionsFromFile("/fn/framework/testdata/template-processor/schemas/foo.json")() c2, err := framework.SchemaDefinitionsFromFile(filepath.FromSlash("/fn/framework/testdata/template-processor/schemas/foo.json"))()
if err != nil { if err != nil {
return nil, errors.WrapPrefixf(err, "schema from file") return nil, errors.WrapPrefixf(err, "schema from file")
} }
return append(c1, c2...), nil return append(c1, c2...), nil
}, },
ResourceTemplates: []framework.ResourceTemplate{{ ResourceTemplates: []framework.ResourceTemplate{{
Templates: framework.TemplatesFromFile("/fn/framework/testdata/template-processor/templates/custom-resource/foo.yaml"), Templates: framework.TemplatesFromFile(filepath.FromSlash("/fn/framework/testdata/template-processor/templates/custom-resource/foo.yaml")),
}}, }},
PatchTemplates: []framework.PatchTemplate{ PatchTemplates: []framework.PatchTemplate{
&framework.ResourcePatchTemplate{ &framework.ResourcePatchTemplate{
Templates: framework.TemplatesFromFile("/fn/framework/testdata/template-processor/patches/custom-resource/patch.template.yaml")}, Templates: framework.TemplatesFromFile(filepath.FromSlash("/fn/framework/testdata/template-processor/patches/custom-resource/patch.template.yaml"))},
}, },
} }
out := new(bytes.Buffer) out := new(bytes.Buffer)
@@ -566,7 +566,6 @@ items:
size: medium size: medium
`), `),
Writer: out} Writer: out}
defer openapi.ResetOpenAPI()
require.NoError(t, framework.Execute(p, rw)) require.NoError(t, framework.Execute(p, rw))
require.Equal(t, strings.TrimSpace(` require.Equal(t, strings.TrimSpace(`
apiVersion: config.kubernetes.io/v1alpha1 apiVersion: config.kubernetes.io/v1alpha1
@@ -599,4 +598,9 @@ items:
type: Ruby type: Ruby
size: large size: large
`), strings.TrimSpace(out.String())) `), strings.TrimSpace(out.String()))
found := openapi.SchemaForResourceType(yaml.TypeMeta{
APIVersion: "example.com/v1",
Kind: "Foo",
})
require.Nil(t, found, "openAPI schema was not reset")
} }