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 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
95 changes: 83 additions & 12 deletions docs/content/en/schemas/v2beta7.json
Original file line number Diff line number Diff line change
Expand Up @@ -1623,8 +1623,8 @@
}
},
"preferredOrder": [
"dir",
"applyDir",
"dir",
"fn",
"live"
],
Expand All @@ -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",
"globalScope",
"network",
"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": [
"apply",
"inventoryID",
"inventoryNamespace"
],
"additionalProperties": false,
"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": [
"apply"
"pollPeriod",
"prunePropagationPolicy",
"pruneTimeout",
"reconcileTimeout"
],
"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 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
}

// getKptFnRunArgs returns a list of arguments that the user specified for the `kpt fn run` command.
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` may be specified")
}

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
}
Loading