Skip to content

Commit

Permalink
Fix os calls (kptdev#2718)
Browse files Browse the repository at this point in the history
  • Loading branch information
phanimarupaka authored and martinmaly committed Feb 18, 2022
1 parent 927d14f commit 8f4e559
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 14 deletions.
2 changes: 1 addition & 1 deletion internal/util/render/executor.go
Expand Up @@ -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)
}

Expand Down
20 changes: 10 additions & 10 deletions pkg/api/kptfile/v1/validation.go
Expand Up @@ -16,14 +16,14 @@ package v1

import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"

"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"
Expand All @@ -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
Expand All @@ -45,28 +45,28 @@ 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)
}
}
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),
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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")
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/api/kptfile/v1/validation_test.go
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion thirdparty/kyaml/runfn/runfn.go
Expand Up @@ -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
Expand Down

0 comments on commit 8f4e559

Please sign in to comment.