Skip to content

Commit

Permalink
Pass kubectl global flags through for ko apply
Browse files Browse the repository at this point in the history
This registers the global flags with the kubectl apply command and
passes them through to kubectl.
  • Loading branch information
jonjohnsonjr committed Nov 13, 2018
1 parent e01b6d2 commit 139e54b
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion cmd/ko/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"os/exec"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
)

// runCmd is suitable for use with cobra.Command's Run field.
Expand Down Expand Up @@ -63,6 +65,7 @@ func addKubeCommands(topLevel *cobra.Command) {
},
})

koApplyFlags := []string{}
lo := &LocalOptions{}
no := &NameOptions{}
fo := &FilenameOptions{}
Expand Down Expand Up @@ -99,9 +102,26 @@ func addKubeCommands(topLevel *cobra.Command) {
buf := bytes.NewBuffer(nil)
resolveFilesToWriter(fo, no, lo, buf)

// Create a set of ko-specific flags to ignore when passing through
// kubectl global flags.
ignoreSet := make(map[string]struct{})
for _, s := range koApplyFlags {
ignoreSet[s] = struct{}{}
}

// Filter out ko flags from what we will pass through to kubectl.
kubectlFlags := []string{}
cmd.Flags().Visit(func(flag *pflag.Flag) {
if _, ok := ignoreSet[flag.Name]; !ok {
kubectlFlags = append(kubectlFlags, "--"+flag.Name, flag.Value.String())
}
})

// Issue a "kubectl apply" command reading from stdin,
// to which we will pipe the resolved files.
kubectlCmd := exec.Command("kubectl", "apply", "-f", "-")
argv := []string{"apply", "-f", "-"}
argv = append(argv, kubectlFlags...)
kubectlCmd := exec.Command("kubectl", argv...)

// Pass through our environment
kubectlCmd.Env = os.Environ()
Expand All @@ -119,6 +139,18 @@ func addKubeCommands(topLevel *cobra.Command) {
addLocalArg(apply, lo)
addNamingArgs(apply, no)
addFileArg(apply, fo)

// Collect the ko-specific apply flags before registering the kubectl global
// flags so that we can ignore them when passing kubectl global flags through
// to kubectl.
apply.Flags().VisitAll(func(flag *pflag.Flag) {
koApplyFlags = append(koApplyFlags, flag.Name)
})

// Register the kubectl global flags.
kubeConfigFlags := genericclioptions.NewConfigFlags()
kubeConfigFlags.AddFlags(apply.Flags())

topLevel.AddCommand(apply)

resolve := &cobra.Command{
Expand Down

0 comments on commit 139e54b

Please sign in to comment.