Skip to content

Commit

Permalink
Fix kpt file system interactions (#2746)
Browse files Browse the repository at this point in the history
* missing file.Close() call (filesys' memory filesystem doesn't support
  files opened multiple times)
* pass filesystem into newFnConfig (it reads fn config from file)
  • Loading branch information
martinmaly committed Feb 5, 2022
1 parent 3a53c84 commit 3f08852
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
11 changes: 6 additions & 5 deletions internal/fnruntime/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
goerrors "errors"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
Expand All @@ -33,6 +32,7 @@ import (
kptfilev1 "github.com/GoogleContainerTools/kpt/pkg/api/kptfile/v1"
"github.com/GoogleContainerTools/kpt/pkg/fn"
"github.com/google/shlex"
"sigs.k8s.io/kustomize/kyaml/filesys"
"sigs.k8s.io/kustomize/kyaml/fn/framework"
"sigs.k8s.io/kustomize/kyaml/fn/runtime/runtimeutil"
"sigs.k8s.io/kustomize/kyaml/kio"
Expand All @@ -43,12 +43,12 @@ import (
// NewRunner returns a kio.Filter given a specification of a function
// and it's config.
func NewRunner(
ctx context.Context, f *kptfilev1.Function,
ctx context.Context, fsys filesys.FileSystem, f *kptfilev1.Function,
pkgPath types.UniquePath, fnResults *fnresult.ResultList,
imagePullPolicy ImagePullPolicy, displayResourceCount bool,
runtime fn.FunctionRuntime) (kio.Filter, error) {

config, err := newFnConfig(f, pkgPath)
config, err := newFnConfig(fsys, f, pkgPath)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -469,19 +469,20 @@ func (ri *multiLineFormatter) String() string {
return b.String()
}

func newFnConfig(f *kptfilev1.Function, pkgPath types.UniquePath) (*yaml.RNode, error) {
func newFnConfig(fsys filesys.FileSystem, f *kptfilev1.Function, pkgPath types.UniquePath) (*yaml.RNode, error) {
const op errors.Op = "fn.readConfig"
var fn errors.Fn = errors.Fn(f.Image)

var node *yaml.RNode
switch {
case f.ConfigPath != "":
path := filepath.Join(string(pkgPath), f.ConfigPath)
file, err := os.Open(path)
file, err := fsys.Open(path)
if err != nil {
return nil, errors.E(op, fn,
fmt.Errorf("missing function config %q", f.ConfigPath))
}
defer file.Close()
b, err := ioutil.ReadAll(file)
if err != nil {
return nil, errors.E(op, fn, err)
Expand Down
4 changes: 3 additions & 1 deletion internal/fnruntime/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/GoogleContainerTools/kpt/internal/types"
kptfilev1 "github.com/GoogleContainerTools/kpt/pkg/api/kptfile/v1"
"github.com/stretchr/testify/assert"
"sigs.k8s.io/kustomize/kyaml/filesys"
"sigs.k8s.io/kustomize/kyaml/fn/framework"
"sigs.k8s.io/kustomize/kyaml/kio"
"sigs.k8s.io/kustomize/kyaml/yaml"
Expand Down Expand Up @@ -89,7 +90,8 @@ data: {foo: bar}
assert.NoError(t, err, "unexpected error")
c.fn.ConfigPath = path.Base(tmp.Name())
}
cn, err := newFnConfig(&c.fn, types.UniquePath(os.TempDir()))
fsys := filesys.MakeFsOnDisk()
cn, err := newFnConfig(fsys, &c.fn, types.UniquePath(os.TempDir()))
assert.NoError(t, err, "unexpected error")
actual, err := cn.String()
assert.NoError(t, err, "unexpected error")
Expand Down
4 changes: 2 additions & 2 deletions internal/util/render/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ func (pn *pkgNode) runValidators(ctx context.Context, hctx *hydrationContext, in
}
hctx.dockerCheckDone = true
}
validator, err = fnruntime.NewRunner(ctx, &function, pn.pkg.UniquePath, hctx.fnResults, hctx.imagePullPolicy, displayResourceCount, hctx.runtime)
validator, err = fnruntime.NewRunner(ctx, hctx.fileSystem, &function, pn.pkg.UniquePath, hctx.fnResults, hctx.imagePullPolicy, displayResourceCount, hctx.runtime)
if err != nil {
return err
}
Expand Down Expand Up @@ -605,7 +605,7 @@ func fnChain(ctx context.Context, hctx *hydrationContext, pkgPath types.UniquePa
}
hctx.dockerCheckDone = true
}
runner, err = fnruntime.NewRunner(ctx, &function, pkgPath, hctx.fnResults, hctx.imagePullPolicy, displayResourceCount, hctx.runtime)
runner, err = fnruntime.NewRunner(ctx, hctx.fileSystem, &function, pkgPath, hctx.fnResults, hctx.imagePullPolicy, displayResourceCount, hctx.runtime)
if err != nil {
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions pkg/api/kptfile/v1/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ func GetValidatedFnConfigFromPath(fsys filesys.FileSystem, pkgPath types.UniqueP
if err != nil {
return nil, fmt.Errorf("functionConfig must exist in the current package")
}
defer file.Close()
reader := kio.ByteReader{Reader: file, PreserveSeqIndent: true, WrapBareSeqNode: true, DisableUnwrapping: true}
nodes, err := reader.Read()
if err != nil {
Expand Down

0 comments on commit 3f08852

Please sign in to comment.