Skip to content

Commit

Permalink
Adds migrate command hidden behind env var
Browse files Browse the repository at this point in the history
  • Loading branch information
seans3 committed Nov 6, 2020
1 parent ca15200 commit 42e9f07
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 6 deletions.
40 changes: 35 additions & 5 deletions commands/livecmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ import (

"github.com/GoogleContainerTools/kpt/internal/cmdfetchk8sschema"
"github.com/GoogleContainerTools/kpt/internal/docs/generated/livedocs"
"github.com/GoogleContainerTools/kpt/pkg/live"
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/klog"
"k8s.io/kubectl/pkg/cmd/util"
"sigs.k8s.io/cli-utils/cmd/apply"
"sigs.k8s.io/cli-utils/cmd/destroy"
Expand All @@ -31,6 +33,10 @@ import (
"sigs.k8s.io/cli-utils/pkg/provider"
)

// resourceGroupEnv is an environment variable which hides code to implement
// the ResourceGroup inventory object and migrating to ResourceGroup inventory.
const resourceGroupEnv = "RESOURCE_GROUP_INVENTORY"

func GetLiveCommand(name string, f util.Factory) *cobra.Command {
liveCmd := &cobra.Command{
Use: "live",
Expand All @@ -54,20 +60,35 @@ func GetLiveCommand(name string, f util.Factory) *cobra.Command {
ErrOut: os.Stderr,
}

provider := provider.NewProvider(f)
// The default provider is for ConfigMap inventory, but if the magic env
// var exists, then the provider which handles both ConfigMap and ResourceGroup
// inventory objects is used. If a package has both inventory objects, then
// an error is thrown.
var p provider.Provider = provider.NewProvider(f)
if _, exists := os.LookupEnv(resourceGroupEnv); exists {
klog.V(2).Infoln("provider supports ResourceGroup and ConfigMap inventory")
p = live.NewDualDelegatingProvider(f)
}

// The default init command creates the ConfigMap inventory yaml. If the magic
// env var exists, then we use the init command which updates a Kptfile for
// the ResourceGroup inventory object.
initCmd := initcmd.NewCmdInit(f, ioStreams)
if _, exists := os.LookupEnv(resourceGroupEnv); exists {
klog.V(2).Infoln("init command updates Kptfile for ResourceGroup inventory")
initCmd = NewCmdInit(f, ioStreams)
}
initCmd.Short = livedocs.InitShort
initCmd.Long = livedocs.InitShort + "\n" + livedocs.InitLong
initCmd.Example = livedocs.InitExamples

applyCmd := apply.GetApplyRunner(provider, ioStreams).Command
applyCmd := apply.GetApplyRunner(p, ioStreams).Command
_ = applyCmd.Flags().MarkHidden("no-prune")
applyCmd.Short = livedocs.ApplyShort
applyCmd.Long = livedocs.ApplyShort + "\n" + livedocs.ApplyLong
applyCmd.Example = livedocs.ApplyExamples

previewCmd := preview.GetPreviewRunner(provider, ioStreams).Command
previewCmd := preview.GetPreviewRunner(p, ioStreams).Command
previewCmd.Short = livedocs.PreviewShort
previewCmd.Long = livedocs.PreviewShort + "\n" + livedocs.PreviewLong
previewCmd.Example = livedocs.PreviewExamples
Expand All @@ -77,12 +98,12 @@ func GetLiveCommand(name string, f util.Factory) *cobra.Command {
diffCmd.Long = livedocs.DiffShort + "\n" + livedocs.DiffLong
diffCmd.Example = livedocs.DiffExamples

destroyCmd := destroy.GetDestroyRunner(provider, ioStreams).Command
destroyCmd := destroy.GetDestroyRunner(p, ioStreams).Command
destroyCmd.Short = livedocs.DestroyShort
destroyCmd.Long = livedocs.DestroyShort + "\n" + livedocs.DestroyLong
destroyCmd.Example = livedocs.DestroyExamples

statusCmd := status.GetStatusRunner(provider).Command
statusCmd := status.GetStatusRunner(p).Command
statusCmd.Short = livedocs.StatusShort
statusCmd.Long = livedocs.StatusLong
statusCmd.Example = livedocs.StatusExamples
Expand All @@ -92,5 +113,14 @@ func GetLiveCommand(name string, f util.Factory) *cobra.Command {
liveCmd.AddCommand(initCmd, applyCmd, previewCmd, diffCmd, destroyCmd,
fetchOpenAPICmd, statusCmd)

// If the magic env var exists, then add the migrate command to change
// from ConfigMap to ResourceGroup inventory object.
if _, exists := os.LookupEnv(resourceGroupEnv); exists {
klog.V(2).Infoln("adding kpt live migrate command")
rgProvider := live.NewResourceGroupProvider(f)
migrateCmd := GetMigrateRunner(p, rgProvider, ioStreams).Command
liveCmd.AddCommand(migrateCmd)
}

return liveCmd
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ require (
// Currently, we have to import the latest version of kubectl.
// Once there is a 0.18 release, we can import a semver release.
k8s.io/kubectl v0.0.0-20191219154910-1528d4eea6dd
sigs.k8s.io/cli-utils v0.21.0
sigs.k8s.io/cli-utils v0.21.1-0.20201104195052-79dde05e8f42
sigs.k8s.io/kustomize/cmd/config v0.8.4
sigs.k8s.io/kustomize/kyaml v0.9.3
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,8 @@ modernc.org/xc v1.0.0/go.mod h1:mRNCo0bvLjGhHO9WsyuKVU4q0ceiDDDoEeWDJHrNx8I=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
sigs.k8s.io/cli-utils v0.21.0 h1:yvLvbzDyiFSmJFpjb6C6tqU2EqQ+hPK4GBUhS+myUm8=
sigs.k8s.io/cli-utils v0.21.0/go.mod h1:0n6pW2yhMbb0HxIcg8UeI5/Bi+Dh+7NOsXFdTudB/KY=
sigs.k8s.io/cli-utils v0.21.1-0.20201104195052-79dde05e8f42 h1:ysEePu4IFENwXqr8PBiF6IPx99poQabIbIzFsNM3cFQ=
sigs.k8s.io/cli-utils v0.21.1-0.20201104195052-79dde05e8f42/go.mod h1:0n6pW2yhMbb0HxIcg8UeI5/Bi+Dh+7NOsXFdTudB/KY=
sigs.k8s.io/controller-runtime v0.4.0 h1:wATM6/m+3w8lj8FXNaO6Fs/rq/vqoOjO1Q116Z9NPsg=
sigs.k8s.io/controller-runtime v0.4.0/go.mod h1:ApC79lpY3PHW9xj/w9pj+lYkLgwAAUZwfXkME1Lajns=
sigs.k8s.io/kustomize v2.0.3+incompatible h1:JUufWFNlI44MdtnjUqVnvh29rR37PQFzPbLXqhyOyX0=
Expand Down
2 changes: 2 additions & 0 deletions pkg/live/dual-delegating-provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"sigs.k8s.io/cli-utils/pkg/provider"
)

var _ provider.Provider = &DualDelegatingProvider{}

// DualDelegatingProvider encapsulates another Provider to which it
// delegates most functions, enabling a Provider which will return
// values based on the inventory object found from ManifestReader()
Expand Down
3 changes: 3 additions & 0 deletions pkg/live/rgprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ import (
"k8s.io/kubectl/pkg/cmd/util"
"sigs.k8s.io/cli-utils/pkg/inventory"
"sigs.k8s.io/cli-utils/pkg/manifestreader"
"sigs.k8s.io/cli-utils/pkg/provider"
)

var _ provider.Provider = &ResourceGroupProvider{}

// ResourceGroupProvider implements the Provider interface, returning
// ResourceGroup versions of some kpt live apply structures.
type ResourceGroupProvider struct {
Expand Down

0 comments on commit 42e9f07

Please sign in to comment.