Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate porch with renderer interface #2710

Merged
merged 3 commits into from
Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 34 additions & 14 deletions internal/util/render/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/GoogleContainerTools/kpt/internal/util/printerutil"
fnresult "github.com/GoogleContainerTools/kpt/pkg/api/fnresult/v1"
kptfilev1 "github.com/GoogleContainerTools/kpt/pkg/api/kptfile/v1"
"github.com/GoogleContainerTools/kpt/pkg/fn"
"sigs.k8s.io/kustomize/kyaml/filesys"
"sigs.k8s.io/kustomize/kyaml/kio"
"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
Expand All @@ -46,9 +47,16 @@ type Renderer struct {
// PkgPath is the absolute path to the root package
PkgPath string

// Evaluator evaluates the function, if specified, this can override the default
// kpt function evaluator
Evaluator fn.Evaluator

// ResultsDirPath is absolute path to the directory to write results
ResultsDirPath string

// fnResultsList is the list of results from the pipeline execution
fnResultsList *fnresult.ResultList

// Output is the writer to which the output resources are written
Output io.Writer

Expand Down Expand Up @@ -82,6 +90,7 @@ func (e *Renderer) Execute(ctx context.Context) error {
imagePullPolicy: e.ImagePullPolicy,
allowExec: e.AllowExec,
fileSystem: e.FileSystem,
evaluator: e.Evaluator,
}

if _, err = hydrate(ctx, root, hctx); err != nil {
Expand Down Expand Up @@ -122,7 +131,7 @@ func (e *Renderer) Execute(ctx context.Context) error {
return fmt.Errorf("failed to save resources: %w", err)
}

if err = pruneResources(hctx); err != nil {
if err = pruneResources(e.FileSystem, hctx); err != nil {
return err
}
pr.Printf("Successfully executed %d function(s) in %d package(s).\n", hctx.executedFunctionCnt, len(hctx.pkgs))
Expand All @@ -145,6 +154,7 @@ func (e *Renderer) Execute(ctx context.Context) error {
}

func (e *Renderer) saveFnResults(ctx context.Context, fnResults *fnresult.ResultList) error {
e.fnResultsList = fnResults
resultsFile, err := fnruntime.SaveResults(e.FileSystem, e.ResultsDirPath, fnResults)
if err != nil {
return fmt.Errorf("failed to save function results: %w", err)
Expand Down Expand Up @@ -194,6 +204,8 @@ type hydrationContext struct {
dockerCheckDone bool

fileSystem filesys.FileSystem

evaluator fn.Evaluator
}

//
Expand Down Expand Up @@ -451,29 +463,33 @@ func (pn *pkgNode) runValidators(ctx context.Context, hctx *hydrationContext, in
}

for i := range pl.Validators {
fn := pl.Validators[i]
function := pl.Validators[i]
// validators are run on a copy of mutated resources to ensure
// resources are not mutated.
selectedResources, err := fnruntime.SelectInput(input, fn.Selectors, &fnruntime.SelectionContext{RootPackagePath: hctx.root.pkg.UniquePath})
selectedResources, err := fnruntime.SelectInput(input, function.Selectors, &fnruntime.SelectionContext{RootPackagePath: hctx.root.pkg.UniquePath})
if err != nil {
return err
}
var validator kio.Filter
displayResourceCount := false
if len(fn.Selectors) > 0 {
if len(function.Selectors) > 0 {
displayResourceCount = true
}
if fn.Exec != "" && !hctx.allowExec {
if function.Exec != "" && !hctx.allowExec {
return errAllowedExecNotSpecified
}
if fn.Image != "" && !hctx.dockerCheckDone {
if function.Image != "" && !hctx.dockerCheckDone {
err := cmdutil.DockerCmdAvailable()
if err != nil {
return err
}
hctx.dockerCheckDone = true
}
validator, err = fnruntime.NewRunner(ctx, &fn, pn.pkg.UniquePath, hctx.fnResults, hctx.imagePullPolicy, displayResourceCount)
if hctx.evaluator != nil {
validator, err = hctx.evaluator.NewRunner(ctx, &function, fn.EvalOptions{ResultList: hctx.fnResults})
} else {
validator, err = fnruntime.NewRunner(ctx, &function, pn.pkg.UniquePath, hctx.fnResults, hctx.imagePullPolicy, displayResourceCount)
}
if err != nil {
return err
}
Expand Down Expand Up @@ -572,22 +588,26 @@ func fnChain(ctx context.Context, hctx *hydrationContext, pkgPath types.UniquePa
for i := range fns {
var err error
var runner kio.Filter
fn := fns[i]
function := fns[i]
displayResourceCount := false
if len(fn.Selectors) > 0 {
if len(function.Selectors) > 0 {
displayResourceCount = true
}
if fn.Exec != "" && !hctx.allowExec {
if function.Exec != "" && !hctx.allowExec {
return nil, errAllowedExecNotSpecified
}
if fn.Image != "" && !hctx.dockerCheckDone {
if function.Image != "" && !hctx.dockerCheckDone {
err := cmdutil.DockerCmdAvailable()
if err != nil {
return nil, err
}
hctx.dockerCheckDone = true
}
runner, err = fnruntime.NewRunner(ctx, &fn, pkgPath, hctx.fnResults, hctx.imagePullPolicy, displayResourceCount)
if hctx.evaluator != nil {
runner, err = hctx.evaluator.NewRunner(ctx, &function, fn.EvalOptions{ResultList: hctx.fnResults})
} else {
runner, err = fnruntime.NewRunner(ctx, &function, pkgPath, hctx.fnResults, hctx.imagePullPolicy, displayResourceCount)
}
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -630,10 +650,10 @@ func trackOutputFiles(hctx *hydrationContext) error {

// pruneResources compares the input and output of the hydration and prunes
// resources that are no longer present in the output of the hydration.
func pruneResources(hctx *hydrationContext) error {
func pruneResources(fsys filesys.FileSystem, hctx *hydrationContext) error {
filesToBeDeleted := hctx.inputFiles.Difference(hctx.outputFiles)
for f := range filesToBeDeleted {
if err := os.Remove(filepath.Join(string(hctx.root.pkg.UniquePath), f)); err != nil {
if err := fsys.RemoveAll(filepath.Join(string(hctx.root.pkg.UniquePath), f)); err != nil {
return fmt.Errorf("failed to delete file: %w", err)
}
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/fn/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@ package fn
import (
"context"

fnresult "github.com/GoogleContainerTools/kpt/pkg/api/fnresult/v1"
v1 "github.com/GoogleContainerTools/kpt/pkg/api/kptfile/v1"
"sigs.k8s.io/kustomize/kyaml/filesys"
"sigs.k8s.io/kustomize/kyaml/kio"
)

type EvalOptions struct {
// ResultList stores the result of the function evaluation
ResultList *fnresult.ResultList
}

type Evaluator interface {
Eval(ctx context.Context, pkg filesys.FileSystem, fn v1.Function, opts EvalOptions) error
phanimarupaka marked this conversation as resolved.
Show resolved Hide resolved
NewRunner(ctx context.Context, fn *v1.Function, opts EvalOptions) (kio.Filter, error)
phanimarupaka marked this conversation as resolved.
Show resolved Hide resolved
}
3 changes: 2 additions & 1 deletion pkg/fn/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import (
)

type RenderOptions struct {
Eval Evaluator
Eval Evaluator
PkgPath string
}

type Renderer interface {
Expand Down
5 changes: 5 additions & 0 deletions porch/engine/pkg/kpt/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/GoogleContainerTools/kpt/pkg/fn"
"github.com/GoogleContainerTools/kpt/porch/engine/pkg/internal"
"sigs.k8s.io/kustomize/kyaml/filesys"
"sigs.k8s.io/kustomize/kyaml/kio"
)

func NewPlaceholderEvaluator() fn.Evaluator {
Expand All @@ -35,3 +36,7 @@ var _ fn.Evaluator = &evaluator{}
func (e *evaluator) Eval(ctx context.Context, pkg filesys.FileSystem, fn v1.Function, opts fn.EvalOptions) error {
return internal.Eval(ctx, pkg, fn, opts)
}

func (e *evaluator) NewRunner(ctx context.Context, fn *v1.Function, opts fn.EvalOptions) (kio.Filter, error) {
return nil, nil
}