diff --git a/kyaml/fn/framework/example_test.go b/kyaml/fn/framework/example_test.go index aa284cc46..c0776d6fd 100644 --- a/kyaml/fn/framework/example_test.go +++ b/kyaml/fn/framework/example_test.go @@ -9,7 +9,6 @@ import ( "log" "path/filepath" - "github.com/markbates/pkger" "sigs.k8s.io/kustomize/kyaml/errors" "sigs.k8s.io/kustomize/kyaml/fn/framework" "sigs.k8s.io/kustomize/kyaml/fn/framework/command" @@ -222,7 +221,7 @@ func ExampleTemplateProcessor_generate_files() { // Templates ResourceTemplates: []framework.ResourceTemplate{{ Templates: framework.TemplatesFromFile( - pkger.Include("/fn/framework/testdata/example/templatefiles/deployment.template"), + filepath.FromSlash("/fn/framework/testdata/example/templatefiles/deployment.template"), ), }}, } diff --git a/kyaml/fn/framework/processors.go b/kyaml/fn/framework/processors.go index 7f684519a..1fa861ece 100644 --- a/kyaml/fn/framework/processors.go +++ b/kyaml/fn/framework/processors.go @@ -224,6 +224,7 @@ func (tp TemplateProcessor) Filter(items []*yaml.RNode) ([]*yaml.RNode, error) { if err != nil { return nil, errors.WrapPrefixf(err, "parsing AdditionalSchemas") } + defer openapi.ResetOpenAPI() for i := range defs { 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. +// 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 // ContainerPatchTemplates to a TemplateProcessor. 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 // 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 // ContainerPatchTemplates to a TemplateProcessor. -func TemplatesFromDir(dirs ...pkger.Dir) TemplatesFunc { +func TemplatesFromDir(dirs ...string) TemplatesFunc { return func() ([]*template.Template, error) { var pt []*template.Template - for i := range dirs { - dir := string(dirs[i]) + for _, dir := range dirs { err := pkger.Walk(dir, func(p string, info os.FileInfo, err error) error { if err != nil { return err @@ -435,6 +443,10 @@ func TemplatesFromDir(dirs ...pkger.Dir) TemplatesFunc { } // 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. func SchemaDefinitionsFromFile(files ...string) SchemaDefinitionFunc { 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. +// 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. -func SchemaDefinitionsFromDir(dirs ...pkger.Dir) SchemaDefinitionFunc { +func SchemaDefinitionsFromDir(dirs ...string) SchemaDefinitionFunc { return func() ([]*spec.Definitions, error) { var defs []*spec.Definitions - for i := range dirs { - dir := string(dirs[i]) + for _, dir := range dirs { err := pkger.Walk(dir, func(p string, info os.FileInfo, err error) error { if err != nil { return err diff --git a/kyaml/fn/framework/processors_test.go b/kyaml/fn/framework/processors_test.go index fa8adb1dd..d010ec66a 100644 --- a/kyaml/fn/framework/processors_test.go +++ b/kyaml/fn/framework/processors_test.go @@ -5,10 +5,10 @@ package framework_test import ( "bytes" + "path/filepath" "strings" "testing" - "github.com/markbates/pkger" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "k8s.io/kube-openapi/pkg/validation/spec" @@ -28,8 +28,8 @@ func TestTemplateProcessor_ResourceTemplates(t *testing.T) { p := framework.TemplateProcessor{ TemplateData: &API{}, ResourceTemplates: []framework.ResourceTemplate{{ - Templates: framework.TemplatesFromDir(pkger.Dir( - "/fn/framework/testdata/template-processor/templates/basic")), + Templates: framework.TemplatesFromDir( + filepath.FromSlash("/fn/framework/testdata/template-processor/templates/basic")), }}, } @@ -82,7 +82,7 @@ func TestTemplateProcessor_PatchTemplates(t *testing.T) { PatchTemplates: []framework.PatchTemplate{ // Patch from dir with no selector templating &framework.ResourcePatchTemplate{ - Templates: framework.TemplatesFromDir(pkger.Dir( + Templates: framework.TemplatesFromDir(filepath.FromSlash( "/fn/framework/testdata/template-processor/patches/basic")), Selector: &framework.Selector{Names: []string{"foo"}}, }, @@ -179,7 +179,7 @@ func TestTemplateProcessor_ContainerPatchTemplates(t *testing.T) { PatchTemplates: []framework.PatchTemplate{ // patch from dir with no selector templating &framework.ContainerPatchTemplate{ - Templates: framework.TemplatesFromDir(pkger.Dir( + Templates: framework.TemplatesFromDir(filepath.FromSlash( "/fn/framework/testdata/template-processor/container-patches")), Selector: &framework.Selector{Names: []string{"foo"}}, }, @@ -532,22 +532,22 @@ func TestTemplateProcessor_AdditionalSchemas(t *testing.T) { p := framework.TemplateProcessor{ AdditionalSchemas: func() ([]*spec.Definitions, error) { // 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 { 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 { return nil, errors.WrapPrefixf(err, "schema from file") } return append(c1, c2...), nil }, 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{ &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) @@ -566,7 +566,6 @@ items: size: medium `), Writer: out} - defer openapi.ResetOpenAPI() require.NoError(t, framework.Execute(p, rw)) require.Equal(t, strings.TrimSpace(` apiVersion: config.kubernetes.io/v1alpha1 @@ -599,4 +598,9 @@ items: type: Ruby size: large `), strings.TrimSpace(out.String())) + found := openapi.SchemaForResourceType(yaml.TypeMeta{ + APIVersion: "example.com/v1", + Kind: "Foo", + }) + require.Nil(t, found, "openAPI schema was not reset") }