1
0
mirror of https://github.com/ko-build/ko.git synced 2025-07-15 23:54:17 +02:00

Remove dependency on cli-runtime (#413)

* first pass: kubectl flags must be passed after '--'

* add warning when using non-separated flags

* mark flags as deprecated

* drop defaultCacheDir and homedir dependency
This commit is contained in:
Jason Hall
2021-08-10 13:57:23 -04:00
committed by GitHub
parent 9f6e0d305b
commit 466dbab6c4
1235 changed files with 235 additions and 507904 deletions

View File

@ -17,19 +17,35 @@ package commands
import (
"errors"
"fmt"
"log"
"os"
"os/exec"
"strings"
"github.com/google/ko/internal"
"github.com/google/ko/pkg/commands/options"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"golang.org/x/sync/errgroup"
"k8s.io/cli-runtime/pkg/genericclioptions"
)
const kubectlFlagsWarningTemplate = `NOTICE!
-----------------------------------------------------------------
Passing kubectl global flags to ko directly is deprecated.
Instead of passing:
ko %s ... %s
Pass kubectl global flags separated by "--":
ko %s ... -- %s
For more information see:
https://github.com/google/ko/issues/317
-----------------------------------------------------------------
`
// addApply augments our CLI surface with apply.
func addApply(topLevel *cobra.Command) {
koApplyFlags := []string{}
var kf internal.KubectlFlags
po := &options.PublishOptions{}
fo := &options.FilenameOptions{}
so := &options.SelectorOptions{}
@ -60,8 +76,11 @@ func addApply(topLevel *cobra.Command) {
ko apply --local -f config/
# Apply from stdin:
cat config.yaml | ko apply -f -`,
Args: cobra.NoArgs,
cat config.yaml | ko apply -f -
# Any flags passed after '--' are passed to 'kubectl apply' directly:
ko apply -f config -- --namespace=foo --kubeconfig=cfg.yaml
`,
RunE: func(cmd *cobra.Command, args []string) error {
if !isKubectlAvailable() {
return errors.New("error: kubectl is not available. kubectl must be installed to use ko apply")
@ -80,25 +99,19 @@ func addApply(topLevel *cobra.Command) {
return fmt.Errorf("error creating publisher: %v", err)
}
defer publisher.Close()
// 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.
// to which we will pipe the resolved files, and any
// remaining flags passed after '--'.
argv := []string{"apply", "-f", "-"}
argv = append(argv, kubectlFlags...)
if kflags := kf.Values(); len(kflags) != 0 {
skflags := strings.Join(kflags, " ")
log.Printf(kubectlFlagsWarningTemplate,
"apply", skflags,
"apply", skflags)
argv = append(argv, kflags...)
}
argv = append(argv, args...)
kubectlCmd := exec.CommandContext(ctx, "kubectl", argv...)
// Pass through our environment
@ -145,17 +158,7 @@ func addApply(topLevel *cobra.Command) {
options.AddFileArg(apply, fo)
options.AddSelectorArg(apply, so)
options.AddBuildOptions(apply, bo)
// 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(false)
kubeConfigFlags.AddFlags(apply.Flags())
internal.AddFlags(&kf, apply.Flags())
topLevel.AddCommand(apply)
}