Skip to content

Commit

Permalink
Allow kubectl global flags in ko apply (#297)
Browse files Browse the repository at this point in the history
* Vendor genericclioptions

* Pass kubectl global flags through for ko apply

This registers the global flags with the kubectl apply command and
passes them through to kubectl.
  • Loading branch information
jonjohnsonjr committed Nov 30, 2018
1 parent 1b04aba commit 6853702
Show file tree
Hide file tree
Showing 83 changed files with 16,966 additions and 23 deletions.
48 changes: 42 additions & 6 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
25 changes: 25 additions & 0 deletions vendor/github.com/evanphx/json-patch/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6853702

Please sign in to comment.