Skip to content

Commit

Permalink
[v2] Improve user experience on specifying inventory in kpt v2 deploy…
Browse files Browse the repository at this point in the history
  • Loading branch information
yuwenma committed Nov 8, 2021
1 parent 3e2d252 commit 76b908d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 21 deletions.
2 changes: 1 addition & 1 deletion pkg/skaffold/deploy/v2/kpt/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func liveApplyErr(err error, path string) error {
Action: fmt.Sprintln("if you encounter an inventory mismatch (or can't adopt) error, it indicates " +
"the manifests have been deployed before and may not be properly cleaned up. We provide two solutions:\n " +
"#1: Update your skaffold.yaml by adding the `--inventory-policy=adopt` in the " +
"`.deploy.kpt.flags`. This will override the resources' inventory.\n " +
"`.deploy.kpt.applyFlags`. This will override the resources' inventory.\n " +
"#2: Find the existing inventory from your cluster resource's annotation \"cli-utils.sigs.k8s.io/inventory-id\", " +
" then use this inventory-id to look for the inventory name and namespace by running " +
"`kubectl get resourcegroups.kpt.dev -oyaml | grep <YOUR_INVENTORY_ID> -C20 | grep \"name: inventory-\" -C1` " +
Expand Down
45 changes: 30 additions & 15 deletions pkg/skaffold/deploy/v2/kpt/kpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ import (
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
)

const deployerName = "kptV2"
const (
deployerName = "kptV2"
defaultNs = "default"
)

var (
openFile = os.Open
Expand Down Expand Up @@ -167,7 +170,7 @@ func kptfileInitIfNot(ctx context.Context, out io.Writer, k *Deployer) error {
kptFilePath := filepath.Join(k.applyDir, kptfile.KptFileName)
if _, err := os.Stat(kptFilePath); os.IsNotExist(err) {
_, endTrace := instrumentation.StartTrace(ctx, "Deploy_InitKptfile")
cmd := exec.CommandContext(ctx, "kpt", k.kptArgs("pkg", "init", k.applyDir)...)
cmd := exec.CommandContext(ctx, "kpt", "pkg", "init", k.applyDir)
cmd.Stdout = out
cmd.Stderr = out
if err := util.RunCmd(cmd); err != nil {
Expand All @@ -189,7 +192,8 @@ func kptfileInitIfNot(ctx context.Context, out io.Writer, k *Deployer) error {
// If "Inventory" already exist, running `kpt live init` raises error.
if kfConfig.Inventory == nil {
_, endTrace := instrumentation.StartTrace(ctx, "Deploy_InitKptfileInventory")
args := k.kptArgs("live", "init", k.applyDir)
args := []string{"live", "init", k.applyDir}
args = append(args, k.KptV2Deploy.Flags...)
if k.Name != "" {
args = append(args, "--name", k.Name)
}
Expand All @@ -201,6 +205,9 @@ func kptfileInitIfNot(ctx context.Context, out io.Writer, k *Deployer) error {
} else if k.InventoryNamespace != "" {
args = append(args, "--namespace", k.InventoryNamespace)
}
if k.Force {
args = append(args, "--force", "true")
}
cmd := exec.CommandContext(ctx, "kpt", args...)
cmd.Stdout = out
cmd.Stderr = out
Expand All @@ -209,18 +216,25 @@ func kptfileInitIfNot(ctx context.Context, out io.Writer, k *Deployer) error {
return liveInitErr(err, k.applyDir)
}
} else {
if kfConfig.Inventory.InventoryID != k.InventoryID {
if k.InventoryID != "" && k.InventoryID != kfConfig.Inventory.InventoryID {
logrus.Warnf("Updating Kptfile inventory from %v to %v", kfConfig.Inventory.InventoryID, k.InventoryID)
kfConfig.Inventory.InventoryID = k.InventoryID
}
if kfConfig.Inventory.Name != k.Name {
if k.Name != "" && k.Name != kfConfig.Inventory.Name {
logrus.Warnf("Updating Kptfile name from %v to %v", kfConfig.Inventory.Name, k.Name)
kfConfig.Inventory.Name = k.Name
}
if k.namespace != "" && k.namespace != kfConfig.Inventory.Namespace {
// Set the namespace to be a valid kubernetes namespace value. If not specified, the value shall be "default".
if k.namespace == "" {
k.namespace = defaultNs
}
if k.InventoryNamespace == "" {
k.InventoryNamespace = defaultNs
}
if k.namespace != kfConfig.Inventory.Namespace {
logrus.Warnf("Updating Kptfile namespace from %v to %v", kfConfig.Inventory.Namespace, k.namespace)
kfConfig.Inventory.Namespace = k.namespace
} else if k.InventoryNamespace != "" && k.InventoryNamespace != kfConfig.Inventory.Namespace {
} else if k.InventoryNamespace != kfConfig.Inventory.Namespace {
logrus.Warnf("Updating Kptfile namespace from %v to %v", kfConfig.Inventory.Namespace, k.InventoryNamespace)
kfConfig.Inventory.Namespace = k.InventoryNamespace
}
Expand Down Expand Up @@ -259,7 +273,11 @@ func (k *Deployer) Deploy(ctx context.Context, out io.Writer, builds []graph.Art
endTrace()

childCtx, endTrace := instrumentation.StartTrace(ctx, "Deploy_execKptCommand")
cmd := exec.CommandContext(childCtx, "kpt", k.kptArgs("live", "apply", k.applyDir)...)
args := []string{"live", "apply", k.applyDir}

args = append(args, k.Flags...)
args = append(args, k.ApplyFlags...)
cmd := exec.CommandContext(childCtx, "kpt", args...)
cmd.Stdout = out
cmd.Stderr = out
if err := util.RunCmd(cmd); err != nil {
Expand Down Expand Up @@ -289,7 +307,10 @@ func (k *Deployer) Cleanup(ctx context.Context, out io.Writer) error {
if err := kptInitFunc(ctx, out, k); err != nil {
return err
}
cmd := exec.CommandContext(ctx, "kpt", k.kptArgs("live", "destroy", k.applyDir)...)

args := []string{"live", "destroy", k.applyDir}
args = append(args, k.Flags...)
cmd := exec.CommandContext(ctx, "kpt", args...)
cmd.Stdout = out
cmd.Stderr = out
if err := util.RunCmd(cmd); err != nil {
Expand All @@ -298,9 +319,3 @@ func (k *Deployer) Cleanup(ctx context.Context, out io.Writer) error {

return nil
}

// kptArgs returns the `kpt` args and global flags.
func (k *Deployer) kptArgs(args ...string) []string {
args = append(args, k.KptV2Deploy.Flags...)
return args
}
8 changes: 7 additions & 1 deletion pkg/skaffold/schema/latest/v2/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,10 @@ type KptV2Deploy struct {
// hydrated path `<WORKDIR>/.kpt-pipeline`.
Dir string `yaml:"dir,omitempty"`

// Flags are additional flags passed to `kpt live apply`.
// ApplyFlags are additional flags passed to `kpt live apply`.
ApplyFlags []string `yaml:"applyFlags,omitempty"`

// Flags are kpt global flags.
Flags []string `yaml:"flags,omitempty"`

// Name *alpha* is the inventory object name.
Expand All @@ -565,6 +568,9 @@ type KptV2Deploy struct {
// InventoryNamespace *alpha* sets the inventory namespace.
InventoryNamespace string `yaml:"namespace,omitempty"`

// Forces is used in `kpt live init`, which forces the inventory values to be updated, even if they are already set.
Force bool `yaml:"false,omitempty"`

// LifecycleHooks describes a set of lifecycle hooks that are executed before and after every deploy.
LifecycleHooks DeployHooks `yaml:"-"`
}
Expand Down
13 changes: 9 additions & 4 deletions pkg/skaffold/schema/v3alpha1/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,12 +551,14 @@ type Validator struct {

// KptV2Deploy contains all the configuration needed by the deploy steps.
type KptV2Deploy struct {

// Dir is equivalent to the dir in `kpt live apply <dir>`. If not provided, skaffold renders the raw manifests
// and store them to a a hidden directory `.kpt-pipeline`, and deploys the hidden directory.
// Dir is equivalent to the dir in `kpt live apply <dir>`. If not provided, skaffold deploys from the default
// hydrated path `<WORKDIR>/.kpt-pipeline`.
Dir string `yaml:"dir,omitempty"`

// Flags are additional flags passed to `kpt live apply`.
// ApplyFlags are additional flags passed to `kpt live apply`.
ApplyFlags []string `yaml:"applyFlags,omitempty"`

// Flags are kpt global flags.
Flags []string `yaml:"flags,omitempty"`

// Name *alpha* is the inventory object name.
Expand All @@ -566,6 +568,9 @@ type KptV2Deploy struct {
// InventoryNamespace *alpha* sets the inventory namespace.
InventoryNamespace string `yaml:"namespace,omitempty"`

// Forces is used in `kpt live init`, which forces the inventory values to be updated, even if they are already set.
Force bool `yaml:"false,omitempty"`

// LifecycleHooks describes a set of lifecycle hooks that are executed before and after every deploy.
LifecycleHooks DeployHooks `yaml:"-"`
}
Expand Down

0 comments on commit 76b908d

Please sign in to comment.