Remove AirshipCTLSettings from root level cmd

AirshipCTLSettings will no longer be used by any command, so this
patch removes it from root level.

Change-Id: I2aa1d80ab0785b498af1ac2c86334f9b0d9ff047
Signed-off-by: Ruslan Aliev <raliev@mirantis.com>
Relates-To: #327
This commit is contained in:
Ruslan Aliev 2020-08-29 18:38:46 -05:00
parent 2166062f2e
commit fba618225a
15 changed files with 59 additions and 81 deletions

View File

@ -18,7 +18,6 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"opendev.org/airship/airshipctl/pkg/config" "opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/pkg/environment"
"opendev.org/airship/airshipctl/pkg/remote" "opendev.org/airship/airshipctl/pkg/remote"
) )
@ -36,14 +35,12 @@ const (
) )
// NewBaremetalCommand creates a new command for interacting with baremetal using airshipctl. // NewBaremetalCommand creates a new command for interacting with baremetal using airshipctl.
func NewBaremetalCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Command { func NewBaremetalCommand(cfgFactory config.Factory) *cobra.Command {
baremetalRootCmd := &cobra.Command{ baremetalRootCmd := &cobra.Command{
Use: "baremetal", Use: "baremetal",
Short: "Perform actions on baremetal hosts", Short: "Perform actions on baremetal hosts",
} }
cfgFactory := config.CreateFactory(&rootSettings.AirshipConfigPath, &rootSettings.KubeConfigPath)
baremetalRootCmd.AddCommand(NewEjectMediaCommand(cfgFactory)) baremetalRootCmd.AddCommand(NewEjectMediaCommand(cfgFactory))
baremetalRootCmd.AddCommand(NewPowerOffCommand(cfgFactory)) baremetalRootCmd.AddCommand(NewPowerOffCommand(cfgFactory))
baremetalRootCmd.AddCommand(NewPowerOnCommand(cfgFactory)) baremetalRootCmd.AddCommand(NewPowerOnCommand(cfgFactory))

View File

@ -20,7 +20,6 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"opendev.org/airship/airshipctl/cmd/baremetal" "opendev.org/airship/airshipctl/cmd/baremetal"
"opendev.org/airship/airshipctl/pkg/environment"
"opendev.org/airship/airshipctl/testutil" "opendev.org/airship/airshipctl/testutil"
) )
@ -29,7 +28,7 @@ func TestBaremetal(t *testing.T) {
{ {
Name: "baremetal-with-help", Name: "baremetal-with-help",
CmdLine: "-h", CmdLine: "-h",
Cmd: baremetal.NewBaremetalCommand(&environment.AirshipCTLSettings{}), Cmd: baremetal.NewBaremetalCommand(nil),
}, },
{ {
Name: "baremetal-ejectmedia-with-help", Name: "baremetal-ejectmedia-with-help",

View File

@ -18,7 +18,6 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"opendev.org/airship/airshipctl/pkg/config" "opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/pkg/environment"
"opendev.org/airship/airshipctl/pkg/k8s/client" "opendev.org/airship/airshipctl/pkg/k8s/client"
) )
@ -31,15 +30,13 @@ such as getting status and deploying initial infrastructure.
) )
// NewClusterCommand creates a command for interacting with a Kubernetes cluster. // NewClusterCommand creates a command for interacting with a Kubernetes cluster.
func NewClusterCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Command { func NewClusterCommand(cfgFactory config.Factory) *cobra.Command {
clusterRootCmd := &cobra.Command{ clusterRootCmd := &cobra.Command{
Use: "cluster", Use: "cluster",
Short: "Manage Kubernetes clusters", Short: "Manage Kubernetes clusters",
Long: clusterLong[1:], Long: clusterLong[1:],
} }
cfgFactory := config.CreateFactory(&rootSettings.AirshipConfigPath, &rootSettings.KubeConfigPath)
clusterRootCmd.AddCommand(NewInitCommand(cfgFactory)) clusterRootCmd.AddCommand(NewInitCommand(cfgFactory))
clusterRootCmd.AddCommand(NewMoveCommand(cfgFactory)) clusterRootCmd.AddCommand(NewMoveCommand(cfgFactory))
clusterRootCmd.AddCommand(NewStatusCommand(cfgFactory, client.DefaultClient)) clusterRootCmd.AddCommand(NewStatusCommand(cfgFactory, client.DefaultClient))

View File

@ -18,22 +18,15 @@ import (
"testing" "testing"
"opendev.org/airship/airshipctl/cmd/cluster" "opendev.org/airship/airshipctl/cmd/cluster"
"opendev.org/airship/airshipctl/pkg/environment"
"opendev.org/airship/airshipctl/testutil" "opendev.org/airship/airshipctl/testutil"
) )
func TestNewClusterCommand(t *testing.T) { func TestNewClusterCommand(t *testing.T) {
fakeRootSettings := &environment.AirshipCTLSettings{
AirshipConfigPath: "../../testdata/k8s/config.yaml",
KubeConfigPath: "../../testdata/k8s/kubeconfig.yaml",
}
fakeRootSettings.InitConfig()
tests := []*testutil.CmdTest{ tests := []*testutil.CmdTest{
{ {
Name: "cluster-cmd-with-help", Name: "cluster-cmd-with-help",
CmdLine: "--help", CmdLine: "--help",
Cmd: cluster.NewClusterCommand(fakeRootSettings), Cmd: cluster.NewClusterCommand(nil),
}, },
} }
for _, testcase := range tests { for _, testcase := range tests {

View File

@ -18,30 +18,16 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"opendev.org/airship/airshipctl/pkg/config" "opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/pkg/environment"
) )
// NewConfigCommand creates a command for interacting with the airshipctl configuration. // NewConfigCommand creates a command for interacting with the airshipctl configuration.
func NewConfigCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Command { func NewConfigCommand(cfgFactory config.Factory) *cobra.Command {
configRootCmd := &cobra.Command{ configRootCmd := &cobra.Command{
Use: "config", Use: "config",
DisableFlagsInUseLine: true, DisableFlagsInUseLine: true,
Short: "Manage the airshipctl config file", Short: "Manage the airshipctl config file",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if parentPreRun := cmd.Root().PersistentPreRun; parentPreRun != nil {
parentPreRun(cmd.Root(), args)
} }
if cmd.Use == "init" {
rootSettings.Create = true
}
// Load or Initialize airship Config
rootSettings.InitConfig()
},
}
cfgFactory := config.CreateFactory(&rootSettings.AirshipConfigPath, &rootSettings.KubeConfigPath)
configRootCmd.AddCommand(NewGetContextCommand(cfgFactory)) configRootCmd.AddCommand(NewGetContextCommand(cfgFactory))
configRootCmd.AddCommand(NewSetContextCommand(cfgFactory)) configRootCmd.AddCommand(NewSetContextCommand(cfgFactory))

View File

@ -20,7 +20,6 @@ import (
"testing" "testing"
cmd "opendev.org/airship/airshipctl/cmd/config" cmd "opendev.org/airship/airshipctl/cmd/config"
"opendev.org/airship/airshipctl/pkg/environment"
"opendev.org/airship/airshipctl/testutil" "opendev.org/airship/airshipctl/testutil"
) )
@ -29,7 +28,7 @@ func TestConfig(t *testing.T) {
{ {
Name: "config-cmd-with-help", Name: "config-cmd-with-help",
CmdLine: "--help", CmdLine: "--help",
Cmd: cmd.NewConfigCommand(&environment.AirshipCTLSettings{}), Cmd: cmd.NewConfigCommand(nil),
}, },
} }

View File

@ -18,18 +18,15 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"opendev.org/airship/airshipctl/pkg/config" "opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/pkg/environment"
) )
// NewDocumentCommand creates a new command for managing airshipctl documents // NewDocumentCommand creates a new command for managing airshipctl documents
func NewDocumentCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Command { func NewDocumentCommand(cfgFactory config.Factory) *cobra.Command {
documentRootCmd := &cobra.Command{ documentRootCmd := &cobra.Command{
Use: "document", Use: "document",
Short: "Manage deployment documents", Short: "Manage deployment documents",
} }
cfgFactory := config.CreateFactory(&rootSettings.AirshipConfigPath, &rootSettings.KubeConfigPath)
documentRootCmd.AddCommand(NewPullCommand(cfgFactory)) documentRootCmd.AddCommand(NewPullCommand(cfgFactory))
documentRootCmd.AddCommand(NewPluginCommand()) documentRootCmd.AddCommand(NewPluginCommand())

View File

@ -18,17 +18,15 @@ import (
"testing" "testing"
"opendev.org/airship/airshipctl/cmd/document" "opendev.org/airship/airshipctl/cmd/document"
"opendev.org/airship/airshipctl/pkg/environment"
"opendev.org/airship/airshipctl/testutil" "opendev.org/airship/airshipctl/testutil"
) )
func TestDocument(t *testing.T) { func TestDocument(t *testing.T) {
rootSettings := &environment.AirshipCTLSettings{}
tests := []*testutil.CmdTest{ tests := []*testutil.CmdTest{
{ {
Name: "document-with-help", Name: "document-with-help",
CmdLine: "-h", CmdLine: "-h",
Cmd: document.NewDocumentCommand(rootSettings), Cmd: document.NewDocumentCommand(nil),
}, },
{ {
Name: "document-plugin-with-help", Name: "document-plugin-with-help",

View File

@ -18,18 +18,15 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"opendev.org/airship/airshipctl/pkg/config" "opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/pkg/environment"
) )
// NewImageCommand creates a new command for managing ISO images using airshipctl. // NewImageCommand creates a new command for managing ISO images using airshipctl.
func NewImageCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Command { func NewImageCommand(cfgFactory config.Factory) *cobra.Command {
imageRootCmd := &cobra.Command{ imageRootCmd := &cobra.Command{
Use: "image", Use: "image",
Short: "Manage ISO image creation", Short: "Manage ISO image creation",
} }
cfgFactory := config.CreateFactory(&rootSettings.AirshipConfigPath, &rootSettings.KubeConfigPath)
imageRootCmd.AddCommand(NewImageBuildCommand(cfgFactory)) imageRootCmd.AddCommand(NewImageBuildCommand(cfgFactory))
return imageRootCmd return imageRootCmd

View File

@ -18,7 +18,6 @@ import (
"testing" "testing"
"opendev.org/airship/airshipctl/cmd/image" "opendev.org/airship/airshipctl/cmd/image"
"opendev.org/airship/airshipctl/pkg/environment"
"opendev.org/airship/airshipctl/testutil" "opendev.org/airship/airshipctl/testutil"
) )
@ -27,7 +26,7 @@ func TestImage(t *testing.T) {
{ {
Name: "image-with-help", Name: "image-with-help",
CmdLine: "-h", CmdLine: "-h",
Cmd: image.NewImageCommand(&environment.AirshipCTLSettings{}), Cmd: image.NewImageCommand(nil),
}, },
} }

View File

@ -18,7 +18,6 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"opendev.org/airship/airshipctl/pkg/config" "opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/pkg/environment"
) )
const ( const (
@ -29,15 +28,13 @@ such as getting list and applying specific one.
) )
// NewPhaseCommand creates a command for interacting with phases // NewPhaseCommand creates a command for interacting with phases
func NewPhaseCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Command { func NewPhaseCommand(cfgFactory config.Factory) *cobra.Command {
phaseRootCmd := &cobra.Command{ phaseRootCmd := &cobra.Command{
Use: "phase", Use: "phase",
Short: "Manage phases", Short: "Manage phases",
Long: clusterLong[1:], Long: clusterLong[1:],
} }
cfgFactory := config.CreateFactory(&rootSettings.AirshipConfigPath, &rootSettings.KubeConfigPath)
phaseRootCmd.AddCommand(NewApplyCommand(cfgFactory)) phaseRootCmd.AddCommand(NewApplyCommand(cfgFactory))
phaseRootCmd.AddCommand(NewRenderCommand(cfgFactory)) phaseRootCmd.AddCommand(NewRenderCommand(cfgFactory))
phaseRootCmd.AddCommand(NewPlanCommand(cfgFactory)) phaseRootCmd.AddCommand(NewPlanCommand(cfgFactory))

View File

@ -18,22 +18,15 @@ import (
"testing" "testing"
"opendev.org/airship/airshipctl/cmd/phase" "opendev.org/airship/airshipctl/cmd/phase"
"opendev.org/airship/airshipctl/pkg/environment"
"opendev.org/airship/airshipctl/testutil" "opendev.org/airship/airshipctl/testutil"
) )
func TestNewPhaseCommand(t *testing.T) { func TestNewPhaseCommand(t *testing.T) {
fakeRootSettings := &environment.AirshipCTLSettings{
AirshipConfigPath: "../../testdata/k8s/config.yaml",
KubeConfigPath: "../../testdata/k8s/kubeconfig.yaml",
}
fakeRootSettings.InitConfig()
tests := []*testutil.CmdTest{ tests := []*testutil.CmdTest{
{ {
Name: "phase-cmd-with-help", Name: "phase-cmd-with-help",
CmdLine: "--help", CmdLine: "--help",
Cmd: phase.NewPhaseCommand(fakeRootSettings), Cmd: phase.NewPhaseCommand(nil),
}, },
} }
for _, testcase := range tests { for _, testcase := range tests {

View File

@ -16,8 +16,10 @@ package cmd
import ( import (
"io" "io"
"path/filepath"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"k8s.io/client-go/tools/clientcmd"
// Import to initialize client auth plugins. // Import to initialize client auth plugins.
_ "k8s.io/client-go/plugin/pkg/client/auth" _ "k8s.io/client-go/plugin/pkg/client/auth"
@ -30,54 +32,76 @@ import (
"opendev.org/airship/airshipctl/cmd/image" "opendev.org/airship/airshipctl/cmd/image"
"opendev.org/airship/airshipctl/cmd/phase" "opendev.org/airship/airshipctl/cmd/phase"
"opendev.org/airship/airshipctl/cmd/secret" "opendev.org/airship/airshipctl/cmd/secret"
"opendev.org/airship/airshipctl/pkg/environment" cfg "opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/pkg/log" "opendev.org/airship/airshipctl/pkg/log"
) )
// RootOptions stores global flags values
type RootOptions struct {
Debug bool
AirshipConfigPath string
KubeConfigPath string
}
// NewAirshipCTLCommand creates a root `airshipctl` command with the default commands attached // NewAirshipCTLCommand creates a root `airshipctl` command with the default commands attached
func NewAirshipCTLCommand(out io.Writer) *cobra.Command { func NewAirshipCTLCommand(out io.Writer) *cobra.Command {
rootCmd, settings := NewRootCommand(out) rootCmd, settings := NewRootCommand(out)
return AddDefaultAirshipCTLCommands(rootCmd, settings) return AddDefaultAirshipCTLCommands(rootCmd,
cfg.CreateFactory(&settings.AirshipConfigPath, &settings.KubeConfigPath))
} }
// NewRootCommand creates the root `airshipctl` command. All other commands are // NewRootCommand creates the root `airshipctl` command. All other commands are
// subcommands branching from this one // subcommands branching from this one
func NewRootCommand(out io.Writer) (*cobra.Command, *environment.AirshipCTLSettings) { func NewRootCommand(out io.Writer) (*cobra.Command, *RootOptions) {
var debug bool options := &RootOptions{}
rootCmd := &cobra.Command{ rootCmd := &cobra.Command{
Use: "airshipctl", Use: "airshipctl",
Short: "A unified entrypoint to various airship components", Short: "A unified entrypoint to various airship components",
SilenceErrors: true, SilenceErrors: true,
SilenceUsage: true, SilenceUsage: true,
PersistentPreRun: func(cmd *cobra.Command, args []string) { PersistentPreRun: func(cmd *cobra.Command, args []string) {
log.Init(debug, cmd.OutOrStdout()) log.Init(options.Debug, cmd.OutOrStdout())
}, },
} }
rootCmd.SetOut(out) rootCmd.SetOut(out)
rootCmd.PersistentFlags().BoolVar(&debug, "debug", false, "enable verbose output") initFlags(options, rootCmd)
return rootCmd, makeRootSettings(rootCmd) return rootCmd, options
} }
// AddDefaultAirshipCTLCommands is a convenience function for adding all of the // AddDefaultAirshipCTLCommands is a convenience function for adding all of the
// default commands to airshipctl // default commands to airshipctl
func AddDefaultAirshipCTLCommands(cmd *cobra.Command, settings *environment.AirshipCTLSettings) *cobra.Command { func AddDefaultAirshipCTLCommands(cmd *cobra.Command, factory cfg.Factory) *cobra.Command {
cmd.AddCommand(baremetal.NewBaremetalCommand(settings)) cmd.AddCommand(baremetal.NewBaremetalCommand(factory))
cmd.AddCommand(cluster.NewClusterCommand(settings)) cmd.AddCommand(cluster.NewClusterCommand(factory))
cmd.AddCommand(completion.NewCompletionCommand()) cmd.AddCommand(completion.NewCompletionCommand())
cmd.AddCommand(document.NewDocumentCommand(settings)) cmd.AddCommand(document.NewDocumentCommand(factory))
cmd.AddCommand(config.NewConfigCommand(settings)) cmd.AddCommand(config.NewConfigCommand(factory))
cmd.AddCommand(image.NewImageCommand(settings)) cmd.AddCommand(image.NewImageCommand(factory))
cmd.AddCommand(secret.NewSecretCommand()) cmd.AddCommand(secret.NewSecretCommand())
cmd.AddCommand(phase.NewPhaseCommand(settings)) cmd.AddCommand(phase.NewPhaseCommand(factory))
cmd.AddCommand(NewVersionCommand()) cmd.AddCommand(NewVersionCommand())
return cmd return cmd
} }
// makeRootSettings holds all actions about environment.AirshipCTLSettings func initFlags(options *RootOptions, cmd *cobra.Command) {
func makeRootSettings(cmd *cobra.Command) *environment.AirshipCTLSettings { flags := cmd.PersistentFlags()
settings := &environment.AirshipCTLSettings{} flags.BoolVar(&options.Debug, "debug", false, "enable verbose output")
settings.InitFlags(cmd)
return settings defaultAirshipConfigDir := filepath.Join(cfg.HomeEnvVar, cfg.AirshipConfigDir)
defaultAirshipConfigPath := filepath.Join(defaultAirshipConfigDir, cfg.AirshipConfig)
flags.StringVar(
&options.AirshipConfigPath,
"airshipconf",
"",
`Path to file for airshipctl configuration. (default "`+defaultAirshipConfigPath+`")`)
defaultKubeConfigPath := filepath.Join(defaultAirshipConfigDir, cfg.AirshipKubeConfig)
flags.StringVar(
&options.KubeConfigPath,
clientcmd.RecommendedConfigPathFlag,
"",
`Path to kubeconfig associated with airshipctl configuration. (default "`+defaultKubeConfigPath+`")`)
} }

View File

@ -24,7 +24,6 @@ import (
"opendev.org/airship/airshipctl/cmd" "opendev.org/airship/airshipctl/cmd"
"opendev.org/airship/airshipctl/cmd/baremetal" "opendev.org/airship/airshipctl/cmd/baremetal"
"opendev.org/airship/airshipctl/pkg/environment"
"opendev.org/airship/airshipctl/testutil" "opendev.org/airship/airshipctl/testutil"
) )
@ -101,6 +100,6 @@ func getDefaultRootCommand(t *testing.T) *cobra.Command {
func getSpecializedRootCommand(t *testing.T) *cobra.Command { func getSpecializedRootCommand(t *testing.T) *cobra.Command {
t.Helper() t.Helper()
rootCmd := getVanillaRootCommand(t) rootCmd := getVanillaRootCommand(t)
rootCmd.AddCommand(baremetal.NewBaremetalCommand(&environment.AirshipCTLSettings{})) rootCmd.AddCommand(baremetal.NewBaremetalCommand(nil))
return rootCmd return rootCmd
} }

View File

@ -57,6 +57,9 @@ const (
// Modules // Modules
AirshipDefaultManagementType = redfish.ClientType AirshipDefaultManagementType = redfish.ClientType
//HomeEnvVar holds value of HOME directory from env
HomeEnvVar = "$HOME"
) )
// Default values for remote operations // Default values for remote operations