Skip to content

Commit

Permalink
Move cluster-related flags to live subcommand (#1891)
Browse files Browse the repository at this point in the history
* Move cluster-related flags to live subcommand

* Addressed comments
  • Loading branch information
mortent committed May 6, 2021
1 parent 7e85e46 commit d36d049
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 410 deletions.
5 changes: 2 additions & 3 deletions commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"strings"

"github.com/spf13/cobra"
"k8s.io/kubectl/pkg/cmd/util"
)

// NormalizeCommand will modify commands to be consistent, e.g. silencing errors
Expand All @@ -31,11 +30,11 @@ func NormalizeCommand(c ...*cobra.Command) {
}

// GetKptCommands returns the set of kpt commands to be registered
func GetKptCommands(ctx context.Context, name string, f util.Factory) []*cobra.Command {
func GetKptCommands(ctx context.Context, name, version string) []*cobra.Command {
var c []*cobra.Command
fnCmd := GetFnCommand(ctx, name)
pkgCmd := GetPkgCommand(ctx, name)
liveCmd := GetLiveCommand(name, f)
liveCmd := GetLiveCommand(name, version)

c = append(c, pkgCmd, fnCmd, liveCmd)

Expand Down
33 changes: 27 additions & 6 deletions commands/livecmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
package commands

import (
"flag"
"fmt"
"os"

"github.com/GoogleContainerTools/kpt/internal/cmdfetchk8sschema"
"github.com/GoogleContainerTools/kpt/internal/cmdliveinit"
"github.com/GoogleContainerTools/kpt/internal/docs/generated/livedocs"
"github.com/GoogleContainerTools/kpt/internal/util/cfgflags"
"github.com/GoogleContainerTools/kpt/pkg/live"
"github.com/GoogleContainerTools/kpt/thirdparty/cli-utils/destroy"
"github.com/GoogleContainerTools/kpt/thirdparty/cli-utils/diff"
Expand All @@ -28,12 +30,13 @@ import (
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/klog"
"k8s.io/kubectl/pkg/cmd/util"
cluster "k8s.io/kubectl/pkg/cmd/util"
"sigs.k8s.io/cli-utils/pkg/manifestreader"
"sigs.k8s.io/cli-utils/pkg/provider"
"sigs.k8s.io/cli-utils/pkg/util/factory"
)

func GetLiveCommand(name string, f util.Factory) *cobra.Command {
func GetLiveCommand(_, version string) *cobra.Command {
liveCmd := &cobra.Command{
Use: "live",
Short: livedocs.LiveShort,
Expand All @@ -56,6 +59,8 @@ func GetLiveCommand(name string, f util.Factory) *cobra.Command {
ErrOut: os.Stderr,
}

f := newFactory(liveCmd, version)

// The provider handles both ConfigMap and ResourceGroup inventory objects.
// If a package has both inventory objects, then an error is thrown.
klog.V(2).Infoln("provider supports ResourceGroup and ConfigMap inventory")
Expand Down Expand Up @@ -95,10 +100,8 @@ func GetLiveCommand(name string, f util.Factory) *cobra.Command {
statusCmd.Long = livedocs.StatusLong
statusCmd.Example = livedocs.StatusExamples

fetchOpenAPICmd := cmdfetchk8sschema.NewCommand(name, f, ioStreams)

liveCmd.AddCommand(initCmd, applyCmd, previewCmd, diffCmd, destroyCmd,
fetchOpenAPICmd, statusCmd)
statusCmd)

// Add the migrate command to change from ConfigMap to ResourceGroup inventory
// object. Also add the install-resource-group command.
Expand All @@ -113,3 +116,21 @@ func GetLiveCommand(name string, f util.Factory) *cobra.Command {

return liveCmd
}

func newFactory(cmd *cobra.Command, version string) cluster.Factory {
flags := cmd.PersistentFlags()
kubeConfigFlags := genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag()
kubeConfigFlags.AddFlags(flags)
userAgentKubeConfigFlags := &cfgflags.UserAgentKubeConfigFlags{
Delegate: kubeConfigFlags,
UserAgent: fmt.Sprintf("kpt/%s", version),
}
matchVersionKubeConfigFlags := cluster.NewMatchVersionFlags(
&factory.CachingRESTClientGetter{
Delegate: userAgentKubeConfigFlags,
},
)
matchVersionKubeConfigFlags.AddFlags(cmd.PersistentFlags())
cmd.PersistentFlags().AddGoFlagSet(flag.CommandLine)
return cluster.NewFactory(matchVersionKubeConfigFlags)
}
92 changes: 0 additions & 92 deletions internal/cmdfetchk8sschema/cmdfetchk8sschema.go

This file was deleted.

8 changes: 0 additions & 8 deletions internal/util/cmdutil/cmdutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,6 @@ func PrintErrorStacktrace() bool {
// StackOnError if true, will print a stack trace on failure.
var StackOnError bool

// K8sSchemaSource defines where we should look for the kubernetes openAPI
// schema
var K8sSchemaSource string

// K8sSchemaPath defines the path to the openAPI schema if we are reading from
// a file
var K8sSchemaPath string

func ResolveAbsAndRelPaths(path string) (string, string, error) {
cwd, err := os.Getwd()
if err != nil {
Expand Down
90 changes: 0 additions & 90 deletions internal/util/openapi/openapi.go

This file was deleted.

Loading

0 comments on commit d36d049

Please sign in to comment.