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

Extending Workflow for Kpt Deployer (accepting additional arguments) #4736

Merged
merged 4 commits into from
Aug 28, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
91 changes: 81 additions & 10 deletions docs/content/en/schemas/v2beta7.json
Original file line number Diff line number Diff line change
Expand Up @@ -1639,15 +1639,45 @@
"description": "a directory to read functions from instead of the configuration directory.",
"x-intellij-html-description": "a directory to read functions from instead of the configuration directory."
},
"globalScope": {
"type": "boolean",
"description": "sets global scope for functions.",
"x-intellij-html-description": "sets global scope for functions.",
"default": "false"
},
"image": {
"type": "string",
"description": "an image to be run as a function in lieu of running functions from a directory.",
"x-intellij-html-description": "an image to be run as a function in lieu of running functions from a directory."
},
"mount": {
"items": {
"type": "string"
},
"type": "array",
"description": "a list of storage options to mount to the fn image.",
"x-intellij-html-description": "a list of storage options to mount to the fn image.",
"default": "[]"
},
"network": {
"type": "boolean",
"description": "enables network access for functions that declare it.",
"x-intellij-html-description": "enables network access for functions that declare it.",
"default": "false"
},
"networkName": {
"type": "string",
"description": "docker network to run the container in (default \"bridge\").",
"x-intellij-html-description": "docker network to run the container in (default "bridge")."
}
},
"preferredOrder": [
"fnPath",
"image"
"image",
yuwenma marked this conversation as resolved.
Show resolved Hide resolved
"networkName",
"network",
"globalScope",
"mount"
],
"additionalProperties": false,
"description": "adds additional configurations used when calling `kpt fn`.",
Expand All @@ -1656,21 +1686,62 @@
"KptLive": {
"properties": {
"apply": {
"items": {
"type": "string"
},
"type": "array",
"description": "additional flags passed on creations (`kpt live apply`).",
"x-intellij-html-description": "additional flags passed on creations (<code>kpt live apply</code>).",
"default": "[]"
"$ref": "#/definitions/KptLiveApply",
"description": "adds additional configurations for `kpt live apply` commands.",
"x-intellij-html-description": "adds additional configurations for <code>kpt live apply</code> commands."
},
"inventoryID": {
"type": "string",
"description": "identifier for a group of applied resources. This configuration is used when users do not specify `KptDeploy.ApplyDir` and `.kpt-hydrated/inventory-template.yaml` does not exist.",
"x-intellij-html-description": "identifier for a group of applied resources. This configuration is used when users do not specify <code>KptDeploy.ApplyDir</code> and <code>.kpt-hydrated/inventory-template.yaml</code> does not exist."
},
"inventoryNamespace": {
"type": "string",
"description": "sets the namespace scope for `kpt live init`.",
"x-intellij-html-description": "sets the namespace scope for <code>kpt live init</code>."
}
},
"preferredOrder": [
"inventoryID",
"inventoryNamespace",
"apply"
],
"additionalProperties": false,
"description": "adds additional configurations used when calling `kpt live` on every command (Global), creations (Apply), or deletions (Destroy).",
"x-intellij-html-description": "adds additional configurations used when calling <code>kpt live</code> on every command (Global), creations (Apply), or deletions (Destroy)."
"description": "adds additional configurations used when calling `kpt live`.",
"x-intellij-html-description": "adds additional configurations used when calling <code>kpt live</code>."
},
"KptLiveApply": {
"properties": {
"pollPeriod": {
"type": "string",
"description": "sets for the polling period for resource statuses. Default to 2s.",
"x-intellij-html-description": "sets for the polling period for resource statuses. Default to 2s."
},
"prunePropagationPolicy": {
"type": "string",
"description": "sets the propagation policy for pruning. Possible settings are Background, Foreground, Orphan. Default to \"Background\".",
"x-intellij-html-description": "sets the propagation policy for pruning. Possible settings are Background, Foreground, Orphan. Default to &quot;Background&quot;."
},
"pruneTimeout": {
"type": "string",
"description": "sets the time threshold to wait for all pruned resources to be deleted.",
"x-intellij-html-description": "sets the time threshold to wait for all pruned resources to be deleted."
},
"reconcileTimeout": {
"type": "string",
"description": "sets the time threshold to wait for all resources to reach the current status.",
"x-intellij-html-description": "sets the time threshold to wait for all resources to reach the current status."
}
},
"preferredOrder": [
"pollPeriod",
"prunePropagationPolicy",
"pruneTimeout",
"reconcileTimeout"
],
"additionalProperties": false,
"description": "adds additional configurations used when calling `kpt live apply`.",
"x-intellij-html-description": "adds additional configurations used when calling <code>kpt live apply</code>."
},
"KubectlDeploy": {
"properties": {
Expand Down
101 changes: 84 additions & 17 deletions pkg/skaffold/deploy/kpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (k *KptDeployer) Deploy(ctx context.Context, out io.Writer, builds []build.

outputRenderedManifests(manifests.String(), filepath.Join(applyDir, "resources.yaml"), out)

cmd := exec.CommandContext(ctx, "kpt", kptCommandArgs(applyDir, []string{"live", "apply"}, k.Live.Apply, nil)...)
cmd := exec.CommandContext(ctx, "kpt", kptCommandArgs(applyDir, []string{"live", "apply"}, k.getKptLiveApplyArgs(), nil)...)
cmd.Stdout = out
cmd.Stderr = out
if err := util.RunCmd(cmd); err != nil {
Expand Down Expand Up @@ -253,21 +253,9 @@ func (k *KptDeployer) kustomizeBuild(ctx context.Context) error {
func (k *KptDeployer) kptFnRun(ctx context.Context) (deploy.ManifestList, error) {
var manifests deploy.ManifestList

// --dry-run sets the pipeline's output to STDOUT, otherwise output is set to sinkDir.
// For now, k.Dir will be treated as sinkDir (and sourceDir).
flags := []string{"--dry-run"}
count := 0

if len(k.Fn.FnPath) > 0 {
flags = append(flags, "--fn-path", k.Fn.FnPath)
count++
}
if len(k.Fn.Image) > 0 {
flags = append(flags, "--image", k.Fn.Image)
count++
}
if count > 1 {
return nil, errors.New("only one of `fn-path` or `image` configs can be specified at most")
flags, err := k.getKptFnRunArgs()
if err != nil {
return nil, fmt.Errorf("getting kpt fn run args: %w", err)
}

cmd := exec.CommandContext(ctx, "kpt", kptCommandArgs(pipeline, []string{"fn", "run"}, flags, nil)...)
Expand Down Expand Up @@ -300,7 +288,7 @@ func (k *KptDeployer) getApplyDir(ctx context.Context) (string, error) {
}

if _, err := os.Stat(filepath.Join(kptHydrated, inventoryTemplate)); os.IsNotExist(err) {
cmd := exec.CommandContext(ctx, "kpt", kptCommandArgs(kptHydrated, []string{"live", "init"}, nil, nil)...)
cmd := exec.CommandContext(ctx, "kpt", kptCommandArgs(kptHydrated, []string{"live", "init"}, k.getKptLiveInitArgs(), nil)...)
if _, err := util.RunCmdOut(cmd); err != nil {
return "", err
}
Expand Down Expand Up @@ -360,3 +348,82 @@ func getResources(root string) ([]string, error) {

return files, err
}

// getFlags returns a list of arguments that the user specified for the `kpt fn run` command.
felixtran39 marked this conversation as resolved.
Show resolved Hide resolved
func (k *KptDeployer) getKptFnRunArgs() ([]string, error) {
// --dry-run sets the pipeline's output to STDOUT, otherwise output is set to sinkDir.
// For now, k.Dir will be treated as sinkDir (and sourceDir).
flags := []string{"--dry-run"}

if k.Fn.GlobalScope {
flags = append(flags, "--global-scope")
}

if len(k.Fn.Mount) > 0 {
flags = append(flags, "--mount", strings.Join(k.Fn.Mount, ","))
}

if k.Fn.Network {
flags = append(flags, "--network")
}

if len(k.Fn.NetworkName) > 0 {
flags = append(flags, "--network-name", k.Fn.NetworkName)
}

count := 0

if len(k.Fn.FnPath) > 0 {
flags = append(flags, "--fn-path", k.Fn.FnPath)
count++
}

if len(k.Fn.Image) > 0 {
flags = append(flags, "--image", k.Fn.Image)
count++
}

if count > 1 {
return nil, errors.New("only one of `fn-path` or `image` configs can be specified at most")
felixtran39 marked this conversation as resolved.
Show resolved Hide resolved
}

return flags, nil
}

// getKptLiveApplyArgs returns a list of arguments that the user specified for the `kpt live apply` command.
func (k *KptDeployer) getKptLiveApplyArgs() []string {
var flags []string

if len(k.Live.Apply.PollPeriod) > 0 {
flags = append(flags, "--poll-period", k.Live.Apply.PollPeriod)
}

if len(k.Live.Apply.PrunePropagationPolicy) > 0 {
flags = append(flags, "--prune-propagation-policy", k.Live.Apply.PrunePropagationPolicy)
}

if len(k.Live.Apply.PruneTimeout) > 0 {
flags = append(flags, "--prune-timeout", k.Live.Apply.PruneTimeout)
}

if len(k.Live.Apply.ReconcileTimeout) > 0 {
flags = append(flags, "--reconcile-timeout", k.Live.Apply.ReconcileTimeout)
}

return flags
}

// getKptLiveInitArgs returns a list of arguments that the user specified for the `kpt live init` command.
func (k *KptDeployer) getKptLiveInitArgs() []string {
var flags []string

if len(k.Live.InventoryID) > 0 {
flags = append(flags, "--inventory-id", k.Live.InventoryID)
}

if len(k.Live.InventoryNamespace) > 0 {
flags = append(flags, "--namespace", k.Live.InventoryNamespace)
}

return flags
}
98 changes: 96 additions & 2 deletions pkg/skaffold/deploy/kpt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,42 @@ spec:
AndRunErr("kpt live apply .kpt-hydrated", errors.New("BUG")),
shouldErr: true,
},
{
description: "user specifies reconcile timeout and poll period",
cfg: &latest.KptDeploy{
Dir: ".",
ApplyDir: "valid_path",
Live: latest.KptLive{
Apply: latest.KptLiveApply{
PollPeriod: "5s",
ReconcileTimeout: "2m",
},
},
},
commands: testutil.
CmdRunOut("kpt fn source .", ``).
AndRunOut("kpt fn sink .pipeline", ``).
AndRunOut("kpt fn run .pipeline --dry-run", output).
AndRun("kpt live apply valid_path --poll-period 5s --reconcile-timeout 2m"),
},
{
description: "user specifies prune propagation policy and prune timeout",
cfg: &latest.KptDeploy{
Dir: ".",
ApplyDir: "valid_path",
Live: latest.KptLive{
Apply: latest.KptLiveApply{
PrunePropagationPolicy: "Orphan",
PruneTimeout: "2m",
felixtran39 marked this conversation as resolved.
Show resolved Hide resolved
},
},
},
commands: testutil.
CmdRunOut("kpt fn source .", ``).
AndRunOut("kpt fn sink .pipeline", ``).
AndRunOut("kpt fn run .pipeline --dry-run", output).
AndRun("kpt live apply valid_path --prune-propagation-policy Orphan --prune-timeout 2m"),
},
}
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
Expand Down Expand Up @@ -277,6 +313,7 @@ func TestKpt_Cleanup(t *testing.T) {
tests := []struct {
description string
applyDir string
globalFlags []string
commands util.Command
shouldErr bool
}{
Expand Down Expand Up @@ -604,6 +641,52 @@ spec:
AndRunOutErr("kpt fn run .pipeline --dry-run", "invalid pipeline", errors.New("BUG")),
shouldErr: true,
},
{
description: "kpt fn run with --global-scope",
cfg: &latest.KptDeploy{
Dir: ".",
Fn: latest.KptFn{
Image: "gcr.io/example.com/my-fn:v1.0.0 -- foo=bar",
GlobalScope: true,
},
},
commands: testutil.
CmdRunOut("kpt fn source .", ``).
AndRunOut("kpt fn sink .pipeline", ``).
AndRunOut("kpt fn run .pipeline --dry-run --global-scope --image gcr.io/example.com/my-fn:v1.0.0 -- foo=bar", ``),
expected: "\n",
},
{
description: "kpt fn run with --mount arguments",
cfg: &latest.KptDeploy{
Dir: ".",
Fn: latest.KptFn{
Image: "gcr.io/example.com/my-fn:v1.0.0 -- foo=bar",
Mount: []string{"type=bind", "src=$(pwd)", "dst=/source"},
},
},
commands: testutil.
CmdRunOut("kpt fn source .", ``).
AndRunOut("kpt fn sink .pipeline", ``).
AndRunOut("kpt fn run .pipeline --dry-run --mount type=bind,src=$(pwd),dst=/source --image gcr.io/example.com/my-fn:v1.0.0 -- foo=bar", ``),
expected: "\n",
},
{
description: "kpt fn run flag with --network and --network-name arguments",
cfg: &latest.KptDeploy{
Dir: ".",
Fn: latest.KptFn{
Image: "gcr.io/example.com/my-fn:v1.0.0 -- foo=bar",
Network: true,
NetworkName: "foo",
},
},
commands: testutil.
CmdRunOut("kpt fn source .", ``).
AndRunOut("kpt fn sink .pipeline", ``).
AndRunOut("kpt fn run .pipeline --dry-run --network --network-name foo --image gcr.io/example.com/my-fn:v1.0.0 -- foo=bar", ``),
expected: "\n",
},
}
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
Expand Down Expand Up @@ -639,6 +722,7 @@ func TestKpt_GetApplyDir(t *testing.T) {
tests := []struct {
description string
applyDir string
live latest.KptLive
expected string
commands util.Command
shouldErr bool
Expand All @@ -658,6 +742,15 @@ func TestKpt_GetApplyDir(t *testing.T) {
expected: ".kpt-hydrated",
commands: testutil.CmdRunOut("kpt live init .kpt-hydrated", ""),
},
{
description: "unspecified applyDir with specified inventory-id and namespace",
live: latest.KptLive{
InventoryID: "1a23bcde-4f56-7891-a2bc-de34fabcde5f6",
InventoryNamespace: "foo",
},
expected: ".kpt-hydrated",
commands: testutil.CmdRunOut("kpt live init .kpt-hydrated --inventory-id 1a23bcde-4f56-7891-a2bc-de34fabcde5f6 --namespace foo", ""),
},
{
description: "existing template resource in .kpt-hydrated",
expected: ".kpt-hydrated",
Expand Down Expand Up @@ -685,6 +778,7 @@ func TestKpt_GetApplyDir(t *testing.T) {
DeployType: latest.DeployType{
KptDeploy: &latest.KptDeploy{
ApplyDir: test.applyDir,
Live: test.live,
},
},
},
Expand Down Expand Up @@ -722,8 +816,8 @@ func TestKpt_KptCommandArgs(t *testing.T) {
description: "empty dir",
commands: []string{"live", "apply"},
flags: []string{"--fn-path", "kpt-func.yaml"},
globalFlags: []string{"-h"},
expected: strings.Split("live apply --fn-path kpt-func.yaml -h", " "),
globalFlags: []string{"-v", "3"},
expected: strings.Split("live apply --fn-path kpt-func.yaml -v 3", " "),
},
{
description: "empty commands",
Expand Down
Loading