From 8f4e559367476916bd9082a8ac3eee3351f05bdc Mon Sep 17 00:00:00 2001 From: phani Date: Wed, 2 Feb 2022 16:40:41 -0800 Subject: [PATCH] Fix os calls (#2718) --- internal/util/render/executor.go | 2 +- pkg/api/kptfile/v1/validation.go | 20 ++++++++++---------- pkg/api/kptfile/v1/validation_test.go | 5 +++-- thirdparty/kyaml/runfn/runfn.go | 2 +- 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/internal/util/render/executor.go b/internal/util/render/executor.go index 71acf9e67a..7ba09312ce 100644 --- a/internal/util/render/executor.go +++ b/internal/util/render/executor.go @@ -244,7 +244,7 @@ func newPkgNode(fsys filesys.FileSystem, path string, p *pkg.Pkg) (pn *pkgNode, return pn, errors.E(op, p.UniquePath, err) } - if err := kf.Validate(p.UniquePath); err != nil { + if err := kf.Validate(fsys, p.UniquePath); err != nil { return pn, errors.E(op, p.UniquePath, err) } diff --git a/pkg/api/kptfile/v1/validation.go b/pkg/api/kptfile/v1/validation.go index 6489afd7f3..2591493751 100644 --- a/pkg/api/kptfile/v1/validation.go +++ b/pkg/api/kptfile/v1/validation.go @@ -16,7 +16,6 @@ package v1 import ( "fmt" - "os" "path/filepath" "regexp" "strings" @@ -24,6 +23,7 @@ import ( "github.com/GoogleContainerTools/kpt/internal/types" "sigs.k8s.io/kustomize/api/konfig" kustomizetypes "sigs.k8s.io/kustomize/api/types" + "sigs.k8s.io/kustomize/kyaml/filesys" "sigs.k8s.io/kustomize/kyaml/kio" "sigs.k8s.io/kustomize/kyaml/kio/kioutil" "sigs.k8s.io/kustomize/kyaml/yaml" @@ -34,8 +34,8 @@ const ( kustomizationAPIGroup = "kustomize.config.k8s.io" ) -func (kf *KptFile) Validate(pkgPath types.UniquePath) error { - if err := kf.Pipeline.validate(pkgPath); err != nil { +func (kf *KptFile) Validate(fsys filesys.FileSystem, pkgPath types.UniquePath) error { + if err := kf.Pipeline.validate(fsys, pkgPath); err != nil { return fmt.Errorf("invalid pipeline: %w", err) } // TODO: validate other fields @@ -45,20 +45,20 @@ func (kf *KptFile) Validate(pkgPath types.UniquePath) error { // validate will validate all fields in the Pipeline // 'mutators' and 'validators' share same schema and // they are valid if all functions in them are ALL valid. -func (p *Pipeline) validate(pkgPath types.UniquePath) error { +func (p *Pipeline) validate(fsys filesys.FileSystem, pkgPath types.UniquePath) error { if p == nil { return nil } for i := range p.Mutators { f := p.Mutators[i] - err := f.validate("mutators", i, pkgPath) + err := f.validate(fsys, "mutators", i, pkgPath) if err != nil { return fmt.Errorf("function %q: %w", f.Image, err) } } for i := range p.Validators { f := p.Validators[i] - err := f.validate("validators", i, pkgPath) + err := f.validate(fsys, "validators", i, pkgPath) if err != nil { return fmt.Errorf("function %q: %w", f.Image, err) } @@ -66,7 +66,7 @@ func (p *Pipeline) validate(pkgPath types.UniquePath) error { return nil } -func (f *Function) validate(fnType string, idx int, pkgPath types.UniquePath) error { +func (f *Function) validate(fsys filesys.FileSystem, fnType string, idx int, pkgPath types.UniquePath) error { if f.Image == "" && f.Exec == "" { return &ValidateError{ Field: fmt.Sprintf("pipeline.%s[%d]", fnType, idx), @@ -106,7 +106,7 @@ func (f *Function) validate(fnType string, idx int, pkgPath types.UniquePath) er Reason: err.Error(), } } - if _, err := GetValidatedFnConfigFromPath(pkgPath, f.ConfigPath); err != nil { + if _, err := GetValidatedFnConfigFromPath(fsys, pkgPath, f.ConfigPath); err != nil { return &ValidateError{ Field: fmt.Sprintf("pipeline.%s[%d].configPath", fnType, idx), Value: f.ConfigPath, @@ -171,9 +171,9 @@ func validateFnConfigPathSyntax(p string) error { // GetValidatedFnConfigFromPath validates the functionConfig at the path specified by // the package path (pkgPath) and configPath, returning the functionConfig as an // RNode if the validation is successful. -func GetValidatedFnConfigFromPath(pkgPath types.UniquePath, configPath string) (*yaml.RNode, error) { +func GetValidatedFnConfigFromPath(fsys filesys.FileSystem, pkgPath types.UniquePath, configPath string) (*yaml.RNode, error) { path := filepath.Join(string(pkgPath), configPath) - file, err := os.Open(path) + file, err := fsys.Open(path) if err != nil { return nil, fmt.Errorf("functionConfig must exist in the current package") } diff --git a/pkg/api/kptfile/v1/validation_test.go b/pkg/api/kptfile/v1/validation_test.go index 4ac2636655..f0069981dd 100644 --- a/pkg/api/kptfile/v1/validation_test.go +++ b/pkg/api/kptfile/v1/validation_test.go @@ -20,6 +20,7 @@ import ( "github.com/GoogleContainerTools/kpt/internal/types" "github.com/stretchr/testify/assert" + "sigs.k8s.io/kustomize/kyaml/filesys" "sigs.k8s.io/kustomize/kyaml/yaml" ) @@ -153,7 +154,7 @@ func TestKptfileValidate(t *testing.T) { for _, c := range cases { c := c t.Run(c.name, func(t *testing.T) { - err := c.kptfile.Validate("") + err := c.kptfile.Validate(filesys.FileSystemOrOnDisk{}, "") if c.valid && err != nil { t.Fatalf("kptfile should be valid, %s", err) } @@ -508,7 +509,7 @@ metadata: assert.NoError(t, err) err = ioutil.WriteFile(filepath.Join(d, "f1.yaml"), []byte(tc.input), 0700) assert.NoError(t, err) - got, err := GetValidatedFnConfigFromPath(types.UniquePath(d), "f1.yaml") + got, err := GetValidatedFnConfigFromPath(filesys.FileSystemOrOnDisk{}, types.UniquePath(d), "f1.yaml") if tc.errMsg != "" { assert.Error(t, err) assert.Equal(t, tc.errMsg, err.Error()) diff --git a/thirdparty/kyaml/runfn/runfn.go b/thirdparty/kyaml/runfn/runfn.go index 4c1ba2a4d7..9159d7534c 100644 --- a/thirdparty/kyaml/runfn/runfn.go +++ b/thirdparty/kyaml/runfn/runfn.go @@ -334,7 +334,7 @@ func getUIDGID(asCurrentUser bool, currentUser currentUserFunc) (string, error) // getFunctionConfig returns yaml representation of functionConfig that can // be provided to a function as input. func (r *RunFns) getFunctionConfig() (*yaml.RNode, error) { - return kptfile.GetValidatedFnConfigFromPath("", r.FnConfigPath) + return kptfile.GetValidatedFnConfigFromPath(filesys.FileSystemOrOnDisk{}, "", r.FnConfigPath) } // defaultFnFilterProvider provides function filters