Remove kubeconfig from config struct

Change-Id: I5ab8722dd151d9e652e0b20e1d82988cd98505af
Signed-off-by: Ruslan Aliev <raliev@mirantis.com>
changes/03/752103/17
Ruslan Aliev 3 years ago
parent 10620da2cc
commit fc33287204

@ -18,7 +18,6 @@ import (
"github.com/spf13/cobra"
"opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/pkg/k8s/client"
)
const (
@ -39,7 +38,7 @@ func NewClusterCommand(cfgFactory config.Factory) *cobra.Command {
clusterRootCmd.AddCommand(NewInitCommand(cfgFactory))
clusterRootCmd.AddCommand(NewMoveCommand(cfgFactory))
clusterRootCmd.AddCommand(NewStatusCommand(cfgFactory, client.DefaultClient))
clusterRootCmd.AddCommand(NewStatusCommand(cfgFactory))
return clusterRootCmd
}

@ -70,18 +70,26 @@ airshipctl cluster init
// NewInitCommand creates a command to deploy cluster-api
func NewInitCommand(cfgFactory config.Factory) *cobra.Command {
var kubeconfig string
initCmd := &cobra.Command{
Use: "init",
Short: "Deploy cluster-api provider components",
Long: initLong,
Example: initExample,
RunE: func(cmd *cobra.Command, args []string) error {
command, err := clusterctlcmd.NewCommand(cfgFactory)
command, err := clusterctlcmd.NewCommand(cfgFactory, kubeconfig)
if err != nil {
return err
}
return command.Init()
},
}
initCmd.Flags().StringVar(
&kubeconfig,
"kubeconfig",
"",
"Path to kubeconfig associated with cluster being managed")
return initCmd
}

@ -37,14 +37,14 @@ Move Cluster API objects, provider specific objects and all dependencies to the
// NewMoveCommand creates a command to move capi and bmo resources to the target cluster
func NewMoveCommand(cfgFactory config.Factory) *cobra.Command {
var toKubeconfigContext string
var toKubeconfigContext, kubeconfig string
moveCmd := &cobra.Command{
Use: "move",
Short: "Move Cluster API objects, provider specific objects and all dependencies to the target cluster",
Long: moveLong[1:],
Example: moveExample,
RunE: func(cmd *cobra.Command, args []string) error {
command, err := clusterctlcmd.NewCommand(cfgFactory)
command, err := clusterctlcmd.NewCommand(cfgFactory, kubeconfig)
if err != nil {
return err
}
@ -52,6 +52,11 @@ func NewMoveCommand(cfgFactory config.Factory) *cobra.Command {
},
}
moveCmd.Flags().StringVar(
&kubeconfig,
"kubeconfig",
"",
"Path to kubeconfig associated with cluster being managed")
moveCmd.Flags().StringVar(&toKubeconfigContext, "target-context", "",
"Context to be used within the kubeconfig file for the target cluster. If empty, current context will be used.")
return moveCmd

@ -23,18 +23,25 @@ import (
)
// NewStatusCommand creates a command which reports the statuses of a cluster's deployed components.
func NewStatusCommand(cfgFactory config.Factory, factory client.Factory) *cobra.Command {
o := cluster.NewStatusOptions(cfgFactory, factory)
cmd := &cobra.Command{
func NewStatusCommand(cfgFactory config.Factory) *cobra.Command {
var kubeconfig string
statusCmd := &cobra.Command{
Use: "status",
Short: "Retrieve statuses of deployed cluster components",
RunE: clusterStatusRunE(o),
RunE: clusterStatusRunE(cfgFactory, kubeconfig),
}
return cmd
statusCmd.Flags().StringVar(
&kubeconfig,
"kubeconfig",
"",
"Path to kubeconfig associated with cluster being managed")
return statusCmd
}
func clusterStatusRunE(o cluster.StatusOptions) func(cmd *cobra.Command, args []string) error {
func clusterStatusRunE(cfgFactory config.Factory, kubeconfig string) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
return cluster.StatusRunner(o, cmd.OutOrStdout())
return cluster.StatusRunner(cluster.NewStatusOptions(cfgFactory, client.DefaultClient, kubeconfig), cmd.OutOrStdout())
}
}

@ -26,7 +26,7 @@ func TestNewClusterStatusCmd(t *testing.T) {
{
Name: "cluster-status-cmd-with-help",
CmdLine: "--help",
Cmd: cluster.NewStatusCommand(nil, nil),
Cmd: cluster.NewStatusCommand(nil),
},
}
for _, testcase := range tests {

@ -47,4 +47,5 @@ airshipctl cluster init
Flags:
-h, --help help for init
-h, --help help for init
--kubeconfig string Path to kubeconfig associated with cluster being managed

@ -14,4 +14,5 @@ Move Cluster API objects, provider specific objects and all dependencies to the
Flags:
-h, --help help for move
--kubeconfig string Path to kubeconfig associated with cluster being managed
--target-context string Context to be used within the kubeconfig file for the target cluster. If empty, current context will be used.

@ -4,4 +4,5 @@ Usage:
status [flags]
Flags:
-h, --help help for status
-h, --help help for status
--kubeconfig string Path to kubeconfig associated with cluster being managed

@ -47,11 +47,7 @@ func NewInitCommand() *cobra.Command {
airshipConfigPath = ""
}
kubeConfigPath, err := cmd.Flags().GetString("kubeconfig")
if err != nil {
kubeConfigPath = ""
}
return config.CreateConfig(airshipConfigPath, kubeConfigPath)
return config.CreateConfig(airshipConfigPath)
},
}

@ -92,11 +92,7 @@ func NewSetManagementConfigCommand(cfgFactory config.Factory) *cobra.Command {
return nil
}
if err = cfg.PersistConfig(true); err != nil {
return err
}
return nil
return cfg.PersistConfig()
},
}

@ -34,8 +34,8 @@ airshipctl phase run ephemeral-control-plane
// NewRunCommand creates a command to run specific phase
func NewRunCommand(cfgFactory config.Factory) *cobra.Command {
p := &phase.RunCommand{
Options: phase.RunFlags{},
Factory: cfgFactory,
Options: phase.RunFlags{},
}
runCmd := &cobra.Command{
@ -60,5 +60,10 @@ func NewRunCommand(cfgFactory config.Factory) *cobra.Command {
"wait-timeout",
0,
"wait timeout")
flags.StringVar(
&p.Options.Kubeconfig,
"kubeconfig",
"",
"Path to kubeconfig associated with site being managed")
return runCmd
}

@ -12,4 +12,5 @@ airshipctl phase run ephemeral-control-plane
Flags:
--dry-run simulate phase execution
-h, --help help for run
--kubeconfig string Path to kubeconfig associated with site being managed
--wait-timeout duration wait timeout

@ -19,8 +19,6 @@ import (
"path/filepath"
"github.com/spf13/cobra"
"k8s.io/client-go/tools/clientcmd"
// Import to initialize client auth plugins.
_ "k8s.io/client-go/plugin/pkg/client/auth"
@ -40,14 +38,13 @@ import (
type RootOptions struct {
Debug bool
AirshipConfigPath string
KubeConfigPath string
}
// NewAirshipCTLCommand creates a root `airshipctl` command with the default commands attached
func NewAirshipCTLCommand(out io.Writer) *cobra.Command {
rootCmd, settings := NewRootCommand(out)
return AddDefaultAirshipCTLCommands(rootCmd,
cfg.CreateFactory(&settings.AirshipConfigPath, &settings.KubeConfigPath))
cfg.CreateFactory(&settings.AirshipConfigPath))
}
// NewRootCommand creates the root `airshipctl` command. All other commands are
@ -97,11 +94,4 @@ func initFlags(options *RootOptions, cmd *cobra.Command) {
"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+`")`)
}

@ -19,6 +19,5 @@ Flags:
--airshipconf string Path to file for airshipctl configuration. (default "$HOME/.airship/config")
--debug enable verbose output
-h, --help help for airshipctl
--kubeconfig string Path to kubeconfig associated with airshipctl configuration. (default "$HOME/.airship/kubeconfig")
Use "airshipctl [command] --help" for more information about a command.

@ -11,6 +11,5 @@ Flags:
--airshipconf string Path to file for airshipctl configuration. (default "$HOME/.airship/config")
--debug enable verbose output
-h, --help help for airshipctl
--kubeconfig string Path to kubeconfig associated with airshipctl configuration. (default "$HOME/.airship/kubeconfig")
Use "airshipctl [command] --help" for more information about a command.

@ -12,7 +12,6 @@ A unified entrypoint to various airship components
--airshipconf string Path to file for airshipctl configuration. (default "$HOME/.airship/config")
--debug enable verbose output
-h, --help help for airshipctl
--kubeconfig string Path to kubeconfig associated with airshipctl configuration. (default "$HOME/.airship/kubeconfig")
```
### SEE ALSO

@ -17,7 +17,6 @@ Perform actions on baremetal hosts
```
--airshipconf string Path to file for airshipctl configuration. (default "$HOME/.airship/config")
--debug enable verbose output
--kubeconfig string Path to kubeconfig associated with airshipctl configuration. (default "$HOME/.airship/kubeconfig")
```
### SEE ALSO

@ -24,7 +24,6 @@ airshipctl baremetal ejectmedia [flags]
```
--airshipconf string Path to file for airshipctl configuration. (default "$HOME/.airship/config")
--debug enable verbose output
--kubeconfig string Path to kubeconfig associated with airshipctl configuration. (default "$HOME/.airship/kubeconfig")
```
### SEE ALSO

@ -24,7 +24,6 @@ airshipctl baremetal poweroff [flags]
```
--airshipconf string Path to file for airshipctl configuration. (default "$HOME/.airship/config")
--debug enable verbose output
--kubeconfig string Path to kubeconfig associated with airshipctl configuration. (default "$HOME/.airship/kubeconfig")
```
### SEE ALSO

@ -24,7 +24,6 @@ airshipctl baremetal poweron [flags]
```
--airshipconf string Path to file for airshipctl configuration. (default "$HOME/.airship/config")
--debug enable verbose output
--kubeconfig string Path to kubeconfig associated with airshipctl configuration. (default "$HOME/.airship/kubeconfig")
```
### SEE ALSO

@ -24,7 +24,6 @@ airshipctl baremetal powerstatus [flags]
```
--airshipconf string Path to file for airshipctl configuration. (default "$HOME/.airship/config")
--debug enable verbose output
--kubeconfig string Path to kubeconfig associated with airshipctl configuration. (default "$HOME/.airship/kubeconfig")
```
### SEE ALSO

@ -24,7 +24,6 @@ airshipctl baremetal reboot [flags]
```
--airshipconf string Path to file for airshipctl configuration. (default "$HOME/.airship/config")
--debug enable verbose output
--kubeconfig string Path to kubeconfig associated with airshipctl configuration. (default "$HOME/.airship/kubeconfig")
```
### SEE ALSO

@ -21,7 +21,6 @@ airshipctl baremetal remotedirect [flags]
```
--airshipconf string Path to file for airshipctl configuration. (default "$HOME/.airship/config")
--debug enable verbose output
--kubeconfig string Path to kubeconfig associated with airshipctl configuration. (default "$HOME/.airship/kubeconfig")
```
### SEE ALSO

@ -19,7 +19,6 @@ such as getting status and deploying initial infrastructure.
```
--airshipconf string Path to file for airshipctl configuration. (default "$HOME/.airship/config")
--debug enable verbose output
--kubeconfig string Path to kubeconfig associated with airshipctl configuration. (default "$HOME/.airship/kubeconfig")
```
### SEE ALSO

@ -60,7 +60,8 @@ airshipctl cluster init
### Options
```
-h, --help help for init
-h, --help help for init
--kubeconfig string Path to kubeconfig associated with cluster being managed
```
### Options inherited from parent commands
@ -68,7 +69,6 @@ airshipctl cluster init
```
--airshipconf string Path to file for airshipctl configuration. (default "$HOME/.airship/config")
--debug enable verbose output
--kubeconfig string Path to kubeconfig associated with airshipctl configuration. (default "$HOME/.airship/kubeconfig")
```
### SEE ALSO

@ -27,6 +27,7 @@ Move Cluster API objects, provider specific objects and all dependencies to the
```
-h, --help help for move
--kubeconfig string Path to kubeconfig associated with cluster being managed
--target-context string Context to be used within the kubeconfig file for the target cluster. If empty, current context will be used.
```
@ -35,7 +36,6 @@ Move Cluster API objects, provider specific objects and all dependencies to the
```
--airshipconf string Path to file for airshipctl configuration. (default "$HOME/.airship/config")
--debug enable verbose output
--kubeconfig string Path to kubeconfig associated with airshipctl configuration. (default "$HOME/.airship/kubeconfig")
```
### SEE ALSO

@ -13,7 +13,8 @@ airshipctl cluster status [flags]
### Options
```
-h, --help help for status
-h, --help help for status
--kubeconfig string Path to kubeconfig associated with cluster being managed
```
### Options inherited from parent commands
@ -21,7 +22,6 @@ airshipctl cluster status [flags]
```
--airshipconf string Path to file for airshipctl configuration. (default "$HOME/.airship/config")
--debug enable verbose output
--kubeconfig string Path to kubeconfig associated with airshipctl configuration. (default "$HOME/.airship/kubeconfig")
```
### SEE ALSO

@ -34,7 +34,6 @@ source <(airshipctl completion bash)
```
--airshipconf string Path to file for airshipctl configuration. (default "$HOME/.airship/config")
--debug enable verbose output
--kubeconfig string Path to kubeconfig associated with airshipctl configuration. (default "$HOME/.airship/kubeconfig")
```
### SEE ALSO

@ -17,7 +17,6 @@ Manage the airshipctl config file
```
--airshipconf string Path to file for airshipctl configuration. (default "$HOME/.airship/config")
--debug enable verbose output
--kubeconfig string Path to kubeconfig associated with airshipctl configuration. (default "$HOME/.airship/kubeconfig")
```
### SEE ALSO

@ -38,7 +38,6 @@ airshipctl config get-context exampleContext
```
--airshipconf string Path to file for airshipctl configuration. (default "$HOME/.airship/config")
--debug enable verbose output
--kubeconfig string Path to kubeconfig associated with airshipctl configuration. (default "$HOME/.airship/kubeconfig")
```
### SEE ALSO

@ -34,7 +34,6 @@ airshipctl config get-encryption-config exampleConfig
```
--airshipconf string Path to file for airshipctl configuration. (default "$HOME/.airship/config")
--debug enable verbose output
--kubeconfig string Path to kubeconfig associated with airshipctl configuration. (default "$HOME/.airship/kubeconfig")
```
### SEE ALSO

@ -33,7 +33,6 @@ airshipctl config get-management-config default
```
--airshipconf string Path to file for airshipctl configuration. (default "$HOME/.airship/config")
--debug enable verbose output
--kubeconfig string Path to kubeconfig associated with airshipctl configuration. (default "$HOME/.airship/kubeconfig")
```
### SEE ALSO

@ -34,7 +34,6 @@ airshipctl config get-manifest e2e
```
--airshipconf string Path to file for airshipctl configuration. (default "$HOME/.airship/config")
--debug enable verbose output
--kubeconfig string Path to kubeconfig associated with airshipctl configuration. (default "$HOME/.airship/kubeconfig")
```
### SEE ALSO

@ -26,7 +26,6 @@ airshipctl config init [flags]
```
--airshipconf string Path to file for airshipctl configuration. (default "$HOME/.airship/config")
--debug enable verbose output
--kubeconfig string Path to kubeconfig associated with airshipctl configuration. (default "$HOME/.airship/kubeconfig")
```
### SEE ALSO

@ -42,7 +42,6 @@ airshipctl config set-context \
```
--airshipconf string Path to file for airshipctl configuration. (default "$HOME/.airship/config")
--debug enable verbose output
--kubeconfig string Path to kubeconfig associated with airshipctl configuration. (default "$HOME/.airship/kubeconfig")
```
### SEE ALSO

@ -44,7 +44,6 @@ airshipctl config set-encryption-config exampleConfig \
```
--airshipconf string Path to file for airshipctl configuration. (default "$HOME/.airship/config")
--debug enable verbose output
--kubeconfig string Path to kubeconfig associated with airshipctl configuration. (default "$HOME/.airship/kubeconfig")
```
### SEE ALSO

@ -24,7 +24,6 @@ airshipctl config set-management-config NAME [flags]
```
--airshipconf string Path to file for airshipctl configuration. (default "$HOME/.airship/config")
--debug enable verbose output
--kubeconfig string Path to kubeconfig associated with airshipctl configuration. (default "$HOME/.airship/kubeconfig")
```
### SEE ALSO

@ -60,7 +60,6 @@ airshipctl config set-manifest e2e \
```
--airshipconf string Path to file for airshipctl configuration. (default "$HOME/.airship/config")
--debug enable verbose output
--kubeconfig string Path to kubeconfig associated with airshipctl configuration. (default "$HOME/.airship/kubeconfig")
```
### SEE ALSO

@ -32,7 +32,6 @@ airshipctl config use-context exampleContext
```
--airshipconf string Path to file for airshipctl configuration. (default "$HOME/.airship/config")
--debug enable verbose output
--kubeconfig string Path to kubeconfig associated with airshipctl configuration. (default "$HOME/.airship/kubeconfig")
```
### SEE ALSO

@ -17,7 +17,6 @@ Manage deployment documents
```
--airshipconf string Path to file for airshipctl configuration. (default "$HOME/.airship/config")
--debug enable verbose output
--kubeconfig string Path to kubeconfig associated with airshipctl configuration. (default "$HOME/.airship/kubeconfig")
```
### SEE ALSO

@ -55,7 +55,6 @@ airshipctl document plugin /tmp/replacement.yaml
```
--airshipconf string Path to file for airshipctl configuration. (default "$HOME/.airship/config")
--debug enable verbose output
--kubeconfig string Path to kubeconfig associated with airshipctl configuration. (default "$HOME/.airship/kubeconfig")
```
### SEE ALSO

@ -22,7 +22,6 @@ airshipctl document pull [flags]
```
--airshipconf string Path to file for airshipctl configuration. (default "$HOME/.airship/config")
--debug enable verbose output
--kubeconfig string Path to kubeconfig associated with airshipctl configuration. (default "$HOME/.airship/kubeconfig")
```
### SEE ALSO

@ -17,7 +17,6 @@ Manage ISO image creation
```
--airshipconf string Path to file for airshipctl configuration. (default "$HOME/.airship/config")
--debug enable verbose output
--kubeconfig string Path to kubeconfig associated with airshipctl configuration. (default "$HOME/.airship/kubeconfig")
```
### SEE ALSO

@ -22,7 +22,6 @@ airshipctl image build [flags]
```
--airshipconf string Path to file for airshipctl configuration. (default "$HOME/.airship/config")
--debug enable verbose output
--kubeconfig string Path to kubeconfig associated with airshipctl configuration. (default "$HOME/.airship/kubeconfig")
```
### SEE ALSO

@ -19,7 +19,6 @@ such as getting list and applying specific one.
```
--airshipconf string Path to file for airshipctl configuration. (default "$HOME/.airship/config")
--debug enable verbose output
--kubeconfig string Path to kubeconfig associated with airshipctl configuration. (default "$HOME/.airship/kubeconfig")
```
### SEE ALSO

@ -24,7 +24,6 @@ airshipctl phase plan [flags]
```
--airshipconf string Path to file for airshipctl configuration. (default "$HOME/.airship/config")
--debug enable verbose output
--kubeconfig string Path to kubeconfig associated with airshipctl configuration. (default "$HOME/.airship/kubeconfig")
```
### SEE ALSO

@ -39,7 +39,6 @@ airshipctl phase render initinfra -l app=helm,service=tiller -k Deployment
```
--airshipconf string Path to file for airshipctl configuration. (default "$HOME/.airship/config")
--debug enable verbose output
--kubeconfig string Path to kubeconfig associated with airshipctl configuration. (default "$HOME/.airship/kubeconfig")
```
### SEE ALSO

@ -24,6 +24,7 @@ airshipctl phase run ephemeral-control-plane
```
--dry-run simulate phase execution
-h, --help help for run
--kubeconfig string Path to kubeconfig associated with site being managed
--wait-timeout duration wait timeout
```
@ -32,7 +33,6 @@ airshipctl phase run ephemeral-control-plane
```
--airshipconf string Path to file for airshipctl configuration. (default "$HOME/.airship/config")
--debug enable verbose output
--kubeconfig string Path to kubeconfig associated with airshipctl configuration. (default "$HOME/.airship/kubeconfig")
```
### SEE ALSO

@ -17,7 +17,6 @@ Manage secrets
```
--airshipconf string Path to file for airshipctl configuration. (default "$HOME/.airship/config")
--debug enable verbose output
--kubeconfig string Path to kubeconfig associated with airshipctl configuration. (default "$HOME/.airship/kubeconfig")
```
### SEE ALSO

@ -17,7 +17,6 @@ Generate various secrets
```
--airshipconf string Path to file for airshipctl configuration. (default "$HOME/.airship/config")
--debug enable verbose output
--kubeconfig string Path to kubeconfig associated with airshipctl configuration. (default "$HOME/.airship/kubeconfig")
```
### SEE ALSO

@ -21,7 +21,6 @@ airshipctl secret generate masterpassphrase [flags]
```
--airshipconf string Path to file for airshipctl configuration. (default "$HOME/.airship/config")
--debug enable verbose output
--kubeconfig string Path to kubeconfig associated with airshipctl configuration. (default "$HOME/.airship/kubeconfig")
```
### SEE ALSO

@ -21,7 +21,6 @@ airshipctl version [flags]
```
--airshipconf string Path to file for airshipctl configuration. (default "$HOME/.airship/config")
--debug enable verbose output
--kubeconfig string Path to kubeconfig associated with airshipctl configuration. (default "$HOME/.airship/kubeconfig")
```
### SEE ALSO

@ -723,6 +723,7 @@ golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMx
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
golang.org/x/lint v0.0.0-20190301231843-5614ed5bae6f/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3 h1:XQyxROzUlZH+WIQwySDgnISgOivlhjIEwaQaJEJrrN0=
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE=
golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc=
@ -842,6 +843,7 @@ golang.org/x/tools v0.0.0-20190719005602-e377ae9d6386/go.mod h1:jcCCGcm9btYwXyDq
golang.org/x/tools v0.0.0-20190910044552-dd2b5c81c578/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20190920225731-5eefd052ad72/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20190930201159-7c411dea38b0/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191010075000-0337d82405ff h1:XdBG6es/oFDr1HwaxkxgVve7NB281QhxgK/i4voubFs=
golang.org/x/tools v0.0.0-20191010075000-0337d82405ff/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

@ -256,10 +256,9 @@ func TestVerifyInputs(t *testing.T) {
func TestGenerateBootstrapIso(t *testing.T) {
airshipConfigPath := "testdata/config/config"
kubeConfigPath := "testdata/config/kubeconfig"
t.Run("ContextEntryPointError", func(t *testing.T) {
cfg, err := config.CreateFactory(&airshipConfigPath, &kubeConfigPath)()
cfg, err := config.CreateFactory(&airshipConfigPath)()
require.NoError(t, err)
cfg.Manifests["default"].Repositories = make(map[string]*config.Repository)
settings := func() (*config.Config, error) {
@ -271,7 +270,7 @@ func TestGenerateBootstrapIso(t *testing.T) {
})
t.Run("NewBundleByPathError", func(t *testing.T) {
cfg, err := config.CreateFactory(&airshipConfigPath, &kubeConfigPath)()
cfg, err := config.CreateFactory(&airshipConfigPath)()
require.NoError(t, err)
cfg.Manifests["default"].TargetPath = "/nonexistent"
settings := func() (*config.Config, error) {
@ -283,7 +282,7 @@ func TestGenerateBootstrapIso(t *testing.T) {
})
t.Run("SelectOneError", func(t *testing.T) {
cfg, err := config.CreateFactory(&airshipConfigPath, &kubeConfigPath)()
cfg, err := config.CreateFactory(&airshipConfigPath)()
require.NoError(t, err)
cfg.Manifests["default"].SubPath = "missingkinddoc/site/test-site"
settings := func() (*config.Config, error) {
@ -296,7 +295,7 @@ func TestGenerateBootstrapIso(t *testing.T) {
})
t.Run("ToObjectError", func(t *testing.T) {
cfg, err := config.CreateFactory(&airshipConfigPath, &kubeConfigPath)()
cfg, err := config.CreateFactory(&airshipConfigPath)()
require.NoError(t, err)
cfg.Manifests["default"].SubPath = "missingmetadoc/site/test-site"
settings := func() (*config.Config, error) {
@ -304,11 +303,12 @@ func TestGenerateBootstrapIso(t *testing.T) {
}
expectedErrMessage := "missing metadata.name in object"
actualErr := GenerateBootstrapIso(settings, false)
require.NotNil(t, actualErr)
assert.Contains(t, actualErr.Error(), expectedErrMessage)
})
t.Run("verifyInputsError", func(t *testing.T) {
cfg, err := config.CreateFactory(&airshipConfigPath, &kubeConfigPath)()
cfg, err := config.CreateFactory(&airshipConfigPath)()
require.NoError(t, err)
cfg.Manifests["default"].SubPath = "missingvoldoc/site/test-site"
settings := func() (*config.Config, error) {

@ -43,18 +43,19 @@ type StatusOptions interface {
}
type statusOptions struct {
ConfigFactory config.Factory
CfgFactory config.Factory
ClientFactory client.Factory
Kubeconfig string
}
// NewStatusOptions constructs a new StatusOptions interface based on inner struct
func NewStatusOptions(cfgFactory config.Factory, clientFactory client.Factory) StatusOptions {
return &statusOptions{ConfigFactory: cfgFactory, ClientFactory: clientFactory}
func NewStatusOptions(cfgFactory config.Factory, clientFactory client.Factory, kubeconfig string) StatusOptions {
return &statusOptions{CfgFactory: cfgFactory, ClientFactory: clientFactory, Kubeconfig: kubeconfig}
}
// GetStatusMapDocs returns status map within all the documents in the bundle
func (o *statusOptions) GetStatusMapDocs() (*StatusMap, []document.Document, error) {
conf, err := o.ConfigFactory()
conf, err := o.CfgFactory()
if err != nil {
return nil, nil, err
}
@ -74,7 +75,7 @@ func (o *statusOptions) GetStatusMapDocs() (*StatusMap, []document.Document, err
return nil, nil, err
}
client, err := o.ClientFactory(conf)
client, err := o.ClientFactory(conf.LoadedConfigPath(), o.Kubeconfig)
if err != nil {
return nil, nil, err
}

@ -63,11 +63,12 @@ func TestGetStatusMapDocs(t *testing.T) {
fakeClient := fake.NewClient(
fake.WithDynamicObjects(tt.resources...),
fake.WithCRDs(tt.CRDs...))
clientFactory := func(_ string, _ string) (client.Interface, error) {
return fakeClient, nil
}
statusOptions := cluster.NewStatusOptions(func() (*config.Config, error) {
return settings, nil
}, func(_ *config.Config) (client.Interface, error) {
return fakeClient, nil
})
}, clientFactory, "")
expectedSM, err := cluster.NewStatusMap(fakeClient)
require.NoError(t, err)

@ -32,7 +32,7 @@ type Command struct {
}
// NewCommand returns instance of Command
func NewCommand(cfgFactory config.Factory) (*Command, error) {
func NewCommand(cfgFactory config.Factory, kubeconfig string) (*Command, error) {
cfg, err := cfgFactory()
if err != nil {
return nil, err
@ -53,10 +53,9 @@ func NewCommand(cfgFactory config.Factory) (*Command, error) {
if err != nil {
return nil, err
}
kubeConfigPath := cfg.KubeConfigPath()
return &Command{
kubeconfigPath: kubeConfigPath,
kubeconfigPath: kubeconfig,
documentRoot: root,
client: client,
options: options,

@ -31,8 +31,7 @@ const (
// TODO (kkalynovskyi) expand test cases
func TestNewCommand(t *testing.T) {
airshipConfigPath := "testdata/airshipconfig.yaml"
kubeConfigPath := "testdata/kubeconfig.yaml"
cfg, err := config.CreateFactory(&airshipConfigPath, &kubeConfigPath)()
cfg, err := config.CreateFactory(&airshipConfigPath)()
require.NoError(t, err)
tests := []struct {
@ -120,7 +119,7 @@ func TestNewCommand(t *testing.T) {
cfg.CurrentContext = context
command, err := NewCommand(func() (*config.Config, error) {
return cfg, nil
})
}, "")
if expectErr {
assert.Error(t, err)
assert.Nil(t, command)

@ -15,7 +15,6 @@ limitations under the License.
package config
import (
"errors"
"fmt"
"io/ioutil"
"os"
@ -23,8 +22,6 @@ import (
"path/filepath"
"sort"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
"sigs.k8s.io/yaml"
"opendev.org/airship/airshipctl/pkg/log"
@ -36,8 +33,7 @@ import (
// Any truly optional piece of config is allowed to be omitted.
// Config holds the information required by airshipctl commands
// It is somewhat a superset of what a kubeconfig looks like, we allow for this overlaps by providing
// a mechanism to consume or produce a kubeconfig into / from the airship config.
// It is somewhat a superset of what a kubeconfig looks like
type Config struct {
// +optional
Kind string `json:"kind,omitempty"`
@ -67,14 +63,6 @@ type Config struct {
// file from which this config was loaded
// +not persisted in file
loadedConfigPath string
// kubeConfigPath is the full path to the the location of the
// kubeconfig file associated with this airship config instance
// +not persisted in file
kubeConfigPath string
// Private instance of Kube Config content as an object
kubeConfig *clientcmdapi.Config
}
// Permissions has the permissions for file and directory
@ -87,21 +75,17 @@ type Permissions struct {
type Factory func() (*Config, error)
// CreateFactory returns function which creates ready to use Config object
func CreateFactory(airshipConfigPath *string, kubeConfigPath *string) Factory {
func CreateFactory(airshipConfigPath *string) Factory {
return func() (*Config, error) {
cfg := NewConfig()
cfg.kubeConfig = NewKubeConfig()
var acp, kcp string
var acp string
if airshipConfigPath != nil {
acp = *airshipConfigPath
}
if kubeConfigPath != nil {
kcp = *kubeConfigPath
}
cfg.initConfigPath(acp, kcp)
err := cfg.LoadConfig(cfg.loadedConfigPath, cfg.kubeConfigPath, false)
cfg.initConfigPath(acp)
err := cfg.LoadConfig()
if err != nil {
// Should stop airshipctl
log.Fatal("Failed to load or initialize config: ", err)
@ -112,15 +96,14 @@ func CreateFactory(airshipConfigPath *string, kubeConfigPath *string) Factory {
}
// CreateConfig saves default config to specified paths
func CreateConfig(airshipConfigPath string, kubeConfigPath string) error {
func CreateConfig(airshipConfigPath string) error {
cfg := NewConfig()
cfg.kubeConfig = NewKubeConfig()
cfg.initConfigPath(airshipConfigPath, kubeConfigPath)
return cfg.PersistConfig(true)
cfg.initConfigPath(airshipConfigPath)
return cfg.PersistConfig()
}
// initConfigPath - Initializes loadedConfigPath and kubeConfigPath variable for Config object
func (c *Config) initConfigPath(airshipConfigPath string, kubeConfigPath string) {
// initConfigPath - Initializes loadedConfigPath variable for Config object
func (c *Config) initConfigPath(airshipConfigPath string) {
switch {
case airshipConfigPath != "":
// The loadedConfigPath may already have been received as a command line argument
@ -132,77 +115,22 @@ func (c *Config) initConfigPath(airshipConfigPath string, kubeConfigPath string)
// Otherwise, we'll try putting it in the home directory
c.loadedConfigPath = filepath.Join(util.UserHomeDir(), AirshipConfigDir, AirshipConfig)
}
switch {
case kubeConfigPath != "":
// The kubeConfigPath may already have been received as a command line argument
c.kubeConfigPath = kubeConfigPath
case os.Getenv(AirshipKubeConfigEnv) != "":
// Otherwise, we can check if we got the path via ENVIRONMENT variable
c.kubeConfigPath = os.Getenv(AirshipKubeConfigEnv)
default:
// Otherwise, we'll try putting it in the home directory
c.kubeConfigPath = filepath.Join(util.UserHomeDir(), AirshipConfigDir, AirshipKubeConfig)
}
}
// LoadConfig populates the Config object using the files found at
// airshipConfigPath and kubeConfigPath
func (c *Config) LoadConfig(airshipConfigPath, kubeConfigPath string, create bool) error {
err := c.loadFromAirConfig(airshipConfigPath, create)
if err != nil {
return err
}
err = c.loadKubeConfig(kubeConfigPath, create)
if err != nil {
return err
}
return nil
}
// loadFromAirConfig populates the Config from the file found at airshipConfigPath.
// LoadConfig populates the Config from the file found at airshipConfigPath.
// If there is no file at airshipConfigPath, this function does nothing.
// An error is returned if:
// * airshipConfigPath is the empty string
// * the file at airshipConfigPath is inaccessible
// * the file at airshipConfigPath cannot be marshaled into Config
func (c *Config) loadFromAirConfig(airshipConfigPath string, create bool) error {
if airshipConfigPath == "" {
return errors.New("configuration file location was not provided")
}
// Remember where I loaded the Config from
c.loadedConfigPath = airshipConfigPath
func (c *Config) LoadConfig() error {
// If I can read from the file, load from it
// throw an error otherwise
if _, err := os.Stat(airshipConfigPath); os.IsNotExist(err) && create {
return nil
} else if err != nil {
if _, err := os.Stat(c.loadedConfigPath); err != nil {
return err
}
return util.ReadYAMLFile(airshipConfigPath, c)
}
func (c *Config) loadKubeConfig(kubeConfigPath string, create bool) error {
// Will need this for persisting the changes
c.kubeConfigPath = kubeConfigPath
// If I can read from the file, load from it
var err error
if _, err = os.Stat(kubeConfigPath); os.IsNotExist(err) && create {
// Default kubeconfig matching Airship target cluster
c.kubeConfig = NewKubeConfig()
return nil
} else if err != nil {
return err
}
c.kubeConfig, err = clientcmd.LoadFromFile(kubeConfigPath)
return err
return util.ReadYAMLFile(c.loadedConfigPath, c)
}
// EnsureComplete verifies that a Config object is ready to use.
@ -247,11 +175,11 @@ func (c *Config) EnsureComplete() error {
return nil
}
// PersistConfig updates the airshipctl config and kubeconfig files to match
// the current Config and KubeConfig objects.
// If either file did not previously exist, the file will be created.
// PersistConfig updates the airshipctl config file to match
// the current Config object.
// If file did not previously exist, the file will be created.
// Otherwise, the file will be overwritten
func (c *Config) PersistConfig(persistKubeConfig bool) error {
func (c *Config) PersistConfig() error {
airshipConfigYaml, err := c.ToYaml()
if err != nil {
return err
@ -282,13 +210,6 @@ func (c *Config) PersistConfig(persistKubeConfig bool) error {
return err
}
if persistKubeConfig {
// Persist the kubeconfig file referenced
if err := clientcmd.WriteToFile(*c.kubeConfig, c.kubeConfigPath); err != nil {
return err
}
}
return nil
}
@ -319,29 +240,6 @@ func (c *Config) SetLoadedConfigPath(lcp string) {
c.loadedConfigPath = lcp
}
// KubeConfigPath returns the file path of the kube config
// from Config object
func (c *Config) KubeConfigPath() string {
return c.kubeConfigPath
}
// SetKubeConfigPath updates the file path of the kubeconfig
// in Config object
func (c *Config) SetKubeConfigPath(kubeConfigPath string) {
c.kubeConfigPath = kubeConfigPath
}
// KubeConfig returns kube config object from the
// context of current Config object
func (c *Config) KubeConfig() *clientcmdapi.Config {
return c.kubeConfig
}
// SetKubeConfig updates kube config in Config object
func (c *Config) SetKubeConfig(kubeConfig *clientcmdapi.Config) {
c.kubeConfig = kubeConfig
}
// GetContext returns a context instance
func (c *Config) GetContext(cName string) (*Context, error) {
context, exists := c.Contexts[cName]

@ -60,7 +60,7 @@ func RunSetContext(o *ContextOptions, airconfig *Config, writeToStorage bool) (b
}
// Update configuration file just in time persistence approach
if writeToStorage {
if err := airconfig.PersistConfig(true); err != nil {
if err := airconfig.PersistConfig(); err != nil {
// Error that it didnt persist the changes
return modified, ErrConfigFailed{}
}
@ -77,7 +77,7 @@ func RunUseContext(desiredContext string, airconfig *Config) error {
if airconfig.CurrentContext != desiredContext {
airconfig.CurrentContext = desiredContext
if err := airconfig.PersistConfig(false); err != nil {
if err := airconfig.PersistConfig(); err != nil {
return err
}
}
@ -107,7 +107,7 @@ func RunSetManifest(o *ManifestOptions, airconfig *Config, writeToStorage bool)
}
// Update configuration file just in time persistence approach
if writeToStorage {
if err := airconfig.PersistConfig(true); err != nil {
if err := airconfig.PersistConfig(); err != nil {
// Error that it didnt persist the changes
return modified, ErrConfigFailed{}
}
@ -138,7 +138,7 @@ func RunSetEncryptionConfig(o *EncryptionConfigOptions, airconfig *Config, write
}
// Update configuration file just in time persistence approach
if writeToStorage {
if err := airconfig.PersistConfig(false); err != nil {
if err := airconfig.PersistConfig(); err != nil {
// Error that it didnt persist the changes
return modified, ErrConfigFailed{}
}

@ -98,16 +98,12 @@ func TestPersistConfig(t *testing.T) {
defer cleanup(t)
conf.SetLoadedConfigPath(conf.LoadedConfigPath() + ".new")
conf.SetKubeConfigPath(conf.KubeConfigPath() + ".new")
err := conf.PersistConfig(true)
err := conf.PersistConfig()
require.NoError(t, err)
// Check that the files were created
assert.FileExists(t, conf.LoadedConfigPath())
assert.FileExists(t, conf.KubeConfigPath())
// Check that the invalid name was changed to a valid one
assert.Contains(t, conf.KubeConfig().Clusters, "def_ephemeral")
}
func TestEnsureComplete(t *testing.T) {
@ -208,7 +204,7 @@ func TestPurge(t *testing.T) {
defer cleanup(t)
// Store it
err := conf.PersistConfig(true)
err := conf.PersistConfig()
assert.NoErrorf(t, err, "Unable to persist configuration expected at %v", conf.LoadedConfigPath())
// Verify that the file is there
@ -236,17 +232,6 @@ func TestSetLoadedConfigPath(t *testing.T) {
assert.Equal(t, testPath, conf.LoadedConfigPath())
}
func TestSetKubeConfigPath(t *testing.T) {
conf, cleanup := testutil.InitConfig(t)
defer cleanup(t)
testPath := "/tmp/kubeconfig"
assert.NotEqual(t, testPath, conf.KubeConfigPath())
conf.SetKubeConfigPath(testPath)
assert.Equal(t, testPath, conf.KubeConfigPath())
}
func TestGetContexts(t *testing.T) {
conf, cleanup := testutil.InitConfig(t)
defer cleanup(t)
@ -297,14 +282,10 @@ func TestGetCurrentContext(t *testing.T) {
conf, cleanup := testutil.InitConfig(t)
defer cleanup(t)
context, err := conf.GetCurrentContext()
require.Error(t, err)
assert.Nil(t, context)
conf.CurrentContext = currentContextName
conf.Contexts[currentContextName].Manifest = defaultString
context, err = conf.GetCurrentContext()
context, err := conf.GetCurrentContext()
require.NoError(t, err)
assert.Equal(t, conf.Contexts[currentContextName], context)
})
@ -314,14 +295,10 @@ func TestCurrentContextManifest(t *testing.T) {
conf, cleanup := testutil.InitConfig(t)
defer cleanup(t)
manifest, err := conf.CurrentContextManifest()
require.Error(t, err)
assert.Nil(t, manifest)
conf.CurrentContext = currentContextName
conf.Contexts[currentContextName].Manifest = defaultString
manifest, err = conf.CurrentContextManifest()
manifest, err := conf.CurrentContextManifest()
require.NoError(t, err)
assert.Equal(t, conf.Manifests[defaultString], manifest)
}
@ -330,10 +307,6 @@ func TestCurrentTargetPath(t *testing.T) {
conf, cleanup := testutil.InitConfig(t)
defer cleanup(t)
manifest, err := conf.CurrentContextManifest()
require.Error(t, err)
assert.Nil(t, manifest)
conf.CurrentContext = currentContextName
conf.Contexts[currentContextName].Manifest = defaultString
@ -364,10 +337,6 @@ func TestCurrentContextClusterType(t *testing.T) {