Remove getters/setters of AirshipCTLSettings

This change removes the getters and setters for the following attributes
of pkg/environment.AirshipCTLSettings:

* airshipConfigPath
* kubeConfigPath
* config

The above attributes have been exported to account for the removal of
these methods.

Change-Id: I4e7d38bf6771f7521179a2fe2990475b936b5d95
This commit is contained in:
Ian Howell 2020-03-23 14:41:08 -05:00
parent a4001d255f
commit 5efc26d9f0
29 changed files with 127 additions and 171 deletions

View File

@ -9,9 +9,10 @@ import (
) )
func TestNewClusterCommandReturn(t *testing.T) { func TestNewClusterCommandReturn(t *testing.T) {
fakeRootSettings := &environment.AirshipCTLSettings{} fakeRootSettings := &environment.AirshipCTLSettings{
fakeRootSettings.SetAirshipConfigPath("../../testdata/k8s/config.yaml") AirshipConfigPath: "../../testdata/k8s/config.yaml",
fakeRootSettings.SetKubeConfigPath("../../testdata/k8s/kubeconfig.yaml") KubeConfigPath: "../../testdata/k8s/kubeconfig.yaml",
}
fakeRootSettings.InitConfig() fakeRootSettings.InitConfig()
tests := []*testutil.CmdTest{ tests := []*testutil.CmdTest{

View File

@ -46,7 +46,7 @@ func NewCmdConfigGetAuthInfo(rootSettings *environment.AirshipCTLSettings) *cobr
if len(args) == 1 { if len(args) == 1 {
o.Name = args[0] o.Name = args[0]
} }
return config.RunGetAuthInfo(o, cmd.OutOrStdout(), rootSettings.Config()) return config.RunGetAuthInfo(o, cmd.OutOrStdout(), rootSettings.Config)
}, },
} }

View File

@ -36,17 +36,16 @@ const (
) )
func TestGetAuthInfoCmd(t *testing.T) { func TestGetAuthInfoCmd(t *testing.T) {
conf := &config.Config{ settings := &environment.AirshipCTLSettings{
AuthInfos: map[string]*config.AuthInfo{ Config: &config.Config{
fooAuthInfo: getTestAuthInfo(), AuthInfos: map[string]*config.AuthInfo{
barAuthInfo: getTestAuthInfo(), fooAuthInfo: getTestAuthInfo(),
bazAuthInfo: getTestAuthInfo(), barAuthInfo: getTestAuthInfo(),
bazAuthInfo: getTestAuthInfo(),
},
}, },
} }
settings := &environment.AirshipCTLSettings{}
settings.SetConfig(conf)
cmdTests := []*testutil.CmdTest{ cmdTests := []*testutil.CmdTest{
{ {
Name: "get-credentials", Name: "get-credentials",
@ -80,8 +79,7 @@ func TestGetAuthInfoCmd(t *testing.T) {
} }
func TestNoAuthInfosGetAuthInfoCmd(t *testing.T) { func TestNoAuthInfosGetAuthInfoCmd(t *testing.T) {
settings := &environment.AirshipCTLSettings{} settings := &environment.AirshipCTLSettings{Config: new(config.Config)}
settings.SetConfig(&config.Config{})
cmdTest := &testutil.CmdTest{ cmdTest := &testutil.CmdTest{
Name: "no-credentials", Name: "no-credentials",
CmdLine: "", CmdLine: "",

View File

@ -53,7 +53,7 @@ func NewCmdConfigGetCluster(rootSettings *environment.AirshipCTLSettings) *cobra
return err return err
} }
return config.RunGetCluster(o, cmd.OutOrStdout(), rootSettings.Config()) return config.RunGetCluster(o, cmd.OutOrStdout(), rootSettings.Config)
}, },
} }

View File

@ -39,32 +39,31 @@ const (
) )
func TestGetClusterCmd(t *testing.T) { func TestGetClusterCmd(t *testing.T) {
conf := &config.Config{ settings := &environment.AirshipCTLSettings{
Clusters: map[string]*config.ClusterPurpose{ Config: &config.Config{
fooCluster: { Clusters: map[string]*config.ClusterPurpose{
ClusterTypes: map[string]*config.Cluster{ fooCluster: {
config.Ephemeral: getNamedTestCluster(fooCluster, config.Ephemeral), ClusterTypes: map[string]*config.Cluster{
config.Target: getNamedTestCluster(fooCluster, config.Target), config.Ephemeral: getNamedTestCluster(fooCluster, config.Ephemeral),
config.Target: getNamedTestCluster(fooCluster, config.Target),
},
}, },
}, barCluster: {
barCluster: { ClusterTypes: map[string]*config.Cluster{
ClusterTypes: map[string]*config.Cluster{ config.Ephemeral: getNamedTestCluster(barCluster, config.Ephemeral),
config.Ephemeral: getNamedTestCluster(barCluster, config.Ephemeral), config.Target: getNamedTestCluster(barCluster, config.Target),
config.Target: getNamedTestCluster(barCluster, config.Target), },
}, },
}, bazCluster: {
bazCluster: { ClusterTypes: map[string]*config.Cluster{
ClusterTypes: map[string]*config.Cluster{ config.Ephemeral: getNamedTestCluster(bazCluster, config.Ephemeral),
config.Ephemeral: getNamedTestCluster(bazCluster, config.Ephemeral), config.Target: getNamedTestCluster(bazCluster, config.Target),
config.Target: getNamedTestCluster(bazCluster, config.Target), },
}, },
}, },
}, },
} }
settings := &environment.AirshipCTLSettings{}
settings.SetConfig(conf)
cmdTests := []*testutil.CmdTest{ cmdTests := []*testutil.CmdTest{
{ {
Name: "get-ephemeral", Name: "get-ephemeral",
@ -121,8 +120,7 @@ func TestGetClusterCmd(t *testing.T) {
} }
func TestNoClustersGetClusterCmd(t *testing.T) { func TestNoClustersGetClusterCmd(t *testing.T) {
settings := &environment.AirshipCTLSettings{} settings := &environment.AirshipCTLSettings{Config: new(config.Config)}
settings.SetConfig(&config.Config{})
cmdTest := &testutil.CmdTest{ cmdTest := &testutil.CmdTest{
Name: "no-clusters", Name: "no-clusters",
CmdLine: "", CmdLine: "",

View File

@ -54,7 +54,7 @@ func NewCmdConfigGetContext(rootSettings *environment.AirshipCTLSettings) *cobra
if len(args) == 1 { if len(args) == 1 {
o.Name = args[0] o.Name = args[0]
} }
return config.RunGetContext(o, cmd.OutOrStdout(), rootSettings.Config()) return config.RunGetContext(o, cmd.OutOrStdout(), rootSettings.Config)
}, },
} }

View File

@ -38,18 +38,17 @@ const (
) )
func TestGetContextCmd(t *testing.T) { func TestGetContextCmd(t *testing.T) {
conf := &config.Config{ settings := &environment.AirshipCTLSettings{
Contexts: map[string]*config.Context{ Config: &config.Config{
fooContext: getNamedTestContext(fooContext), Contexts: map[string]*config.Context{
barContext: getNamedTestContext(barContext), fooContext: getNamedTestContext(fooContext),
bazContext: getNamedTestContext(bazContext), barContext: getNamedTestContext(barContext),
bazContext: getNamedTestContext(bazContext),
},
CurrentContext: bazContext,
}, },
CurrentContext: bazContext,
} }
settings := &environment.AirshipCTLSettings{}
settings.SetConfig(conf)
cmdTests := []*testutil.CmdTest{ cmdTests := []*testutil.CmdTest{
{ {
Name: "get-context", Name: "get-context",
@ -88,8 +87,7 @@ found in the configuration.`, missingContext),
} }
func TestNoContextsGetContextCmd(t *testing.T) { func TestNoContextsGetContextCmd(t *testing.T) {
settings := &environment.AirshipCTLSettings{} settings := &environment.AirshipCTLSettings{Config: new(config.Config)}
settings.SetConfig(&config.Config{})
cmdTest := &testutil.CmdTest{ cmdTest := &testutil.CmdTest{
Name: "no-contexts", Name: "no-contexts",
CmdLine: "", CmdLine: "",

View File

@ -32,14 +32,7 @@ func NewCmdConfigInit(rootSettings *environment.AirshipCTLSettings) *cobra.Comma
Use: "init", Use: "init",
Short: configInitLong, Short: configInitLong,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
c := rootSettings.Config() return rootSettings.Config.PersistConfig()
err := c.PersistConfig()
if err != nil {
return err
}
return nil
}, },
} }

View File

@ -61,7 +61,7 @@ func NewCmdConfigSetAuthInfo(rootSettings *environment.AirshipCTLSettings) *cobr
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
o.Name = args[0] o.Name = args[0]
modified, err := config.RunSetAuthInfo(o, rootSettings.Config(), true) modified, err := config.RunSetAuthInfo(o, rootSettings.Config, true)
if err != nil { if err != nil {
return err return err
} }

View File

@ -88,12 +88,11 @@ func initInputConfig(t *testing.T) (given *config.Config, cleanup func(*testing.
} }
func (test setAuthInfoTest) run(t *testing.T) { func (test setAuthInfoTest) run(t *testing.T) {
settings := &environment.AirshipCTLSettings{} settings := &environment.AirshipCTLSettings{Config: test.inputConfig}
settings.SetConfig(test.inputConfig)
test.cmdTest.Cmd = cmd.NewCmdConfigSetAuthInfo(settings) test.cmdTest.Cmd = cmd.NewCmdConfigSetAuthInfo(settings)
testutil.RunTest(t, test.cmdTest) testutil.RunTest(t, test.cmdTest)
afterRunConf := settings.Config() afterRunConf := settings.Config
// Find the AuthInfo Created or Modified // Find the AuthInfo Created or Modified
afterRunAuthInfo, err := afterRunConf.GetAuthInfo(test.userName) afterRunAuthInfo, err := afterRunConf.GetAuthInfo(test.userName)
require.NoError(t, err) require.NoError(t, err)

View File

@ -66,7 +66,7 @@ func NewCmdConfigSetCluster(rootSettings *environment.AirshipCTLSettings) *cobra
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
o.Name = args[0] o.Name = args[0]
modified, err := config.RunSetCluster(o, rootSettings.Config(), true) modified, err := config.RunSetCluster(o, rootSettings.Config, true)
if err != nil { if err != nil {
return err return err
} }

View File

@ -201,10 +201,7 @@ func TestModifyCluster(t *testing.T) {
} }
func (test setClusterTest) run(t *testing.T) { func (test setClusterTest) run(t *testing.T) {
// Get the Environment settings := &environment.AirshipCTLSettings{Config: test.givenConfig}
settings := &environment.AirshipCTLSettings{}
settings.SetConfig(test.givenConfig)
buf := bytes.NewBuffer([]byte{}) buf := bytes.NewBuffer([]byte{})
cmd := cmd.NewCmdConfigSetCluster(settings) cmd := cmd.NewCmdConfigSetCluster(settings)
@ -219,7 +216,7 @@ func (test setClusterTest) run(t *testing.T) {
require.NoErrorf(t, err, "unexpected error executing command: %v, args: %v, flags: %v", err, test.args, test.flags) require.NoErrorf(t, err, "unexpected error executing command: %v, args: %v, flags: %v", err, test.args, test.flags)
// Loads the Config File that was updated // Loads the Config File that was updated
afterRunConf := settings.Config() afterRunConf := settings.Config
// Get ClusterType // Get ClusterType
tctypeFlag := cmd.Flag(config.FlagClusterType) tctypeFlag := cmd.Flag(config.FlagClusterType)
require.NotNil(t, tctypeFlag) require.NotNil(t, tctypeFlag)

View File

@ -70,7 +70,7 @@ func NewCmdConfigSetContext(rootSettings *environment.AirshipCTLSettings) *cobra
return nil return nil
} }
modified, err := config.RunSetContext(o, rootSettings.Config(), true) modified, err := config.RunSetContext(o, rootSettings.Config, true)
if err != nil { if err != nil {
return err return err

View File

@ -127,13 +127,12 @@ func TestSetContext(t *testing.T) {
func (test setContextTest) run(t *testing.T) { func (test setContextTest) run(t *testing.T) {
// Get the Environment // Get the Environment
settings := &environment.AirshipCTLSettings{} settings := &environment.AirshipCTLSettings{Config: test.givenConfig}
settings.SetConfig(test.givenConfig)
test.cmdTest.Cmd = cmd.NewCmdConfigSetContext(settings) test.cmdTest.Cmd = cmd.NewCmdConfigSetContext(settings)
testutil.RunTest(t, test.cmdTest) testutil.RunTest(t, test.cmdTest)
afterRunConf := settings.Config() afterRunConf := settings.Config
// Find the Context Created or Modified // Find the Context Created or Modified
afterRunContext, err := afterRunConf.GetContext(test.contextName) afterRunContext, err := afterRunConf.GetContext(test.contextName)

View File

@ -44,7 +44,7 @@ func NewCmdConfigUseContext(rootSettings *environment.AirshipCTLSettings) *cobra
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
contextName := args[0] contextName := args[0]
err := config.RunUseContext(contextName, rootSettings.Config()) err := config.RunUseContext(contextName, rootSettings.Config)
if err != nil { if err != nil {
return err return err
} }

View File

@ -26,11 +26,7 @@ import (
) )
func TestConfigUseContext(t *testing.T) { func TestConfigUseContext(t *testing.T) {
conf := testutil.DummyConfig() settings := &environment.AirshipCTLSettings{Config: testutil.DummyConfig()}
settings := &environment.AirshipCTLSettings{}
settings.SetConfig(conf)
cmdTests := []*testutil.CmdTest{ cmdTests := []*testutil.CmdTest{
{ {
Name: "config-use-context", Name: "config-use-context",

View File

@ -13,27 +13,26 @@ import (
) )
func getDummyAirshipSettings(t *testing.T) *environment.AirshipCTLSettings { func getDummyAirshipSettings(t *testing.T) *environment.AirshipCTLSettings {
settings := new(environment.AirshipCTLSettings) settings := &environment.AirshipCTLSettings{Config: testutil.DummyConfig()}
conf := testutil.DummyConfig()
mfst := conf.Manifests["dummy_manifest"]
err := fixtures.Init() err := fixtures.Init()
require.NoError(t, err) require.NoError(t, err)
fx := fixtures.Basic().One() fx := fixtures.Basic().One()
mfst.Repositories = map[string]*config.Repository{"primary": { mfst := settings.Config.Manifests["dummy_manifest"]
URLString: fx.DotGit().Root(), mfst.Repositories = map[string]*config.Repository{
CheckoutOptions: &config.RepoCheckout{ "primary": {
Branch: "master", URLString: fx.DotGit().Root(),
ForceCheckout: false, CheckoutOptions: &config.RepoCheckout{
Branch: "master",
ForceCheckout: false,
},
Auth: &config.RepoAuth{
Type: "http-basic",
},
}, },
Auth: &config.RepoAuth{
Type: "http-basic",
},
},
} }
settings.SetConfig(conf)
return settings return settings
} }

View File

@ -68,7 +68,7 @@ func TestFlagLoading(t *testing.T) {
err = rootCmd.Execute() err = rootCmd.Execute()
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, settings.AirshipConfigPath(), tt.expected) assert.Equal(t, settings.AirshipConfigPath, tt.expected)
}) })
} }
} }

View File

@ -24,7 +24,7 @@ const (
func GenerateBootstrapIso(settings *environment.AirshipCTLSettings) error { func GenerateBootstrapIso(settings *environment.AirshipCTLSettings) error {
ctx := context.Background() ctx := context.Background()
globalConf := settings.Config() globalConf := settings.Config
if err := globalConf.EnsureComplete(); err != nil { if err := globalConf.EnsureComplete(); err != nil {
return err return err
} }

View File

@ -50,7 +50,7 @@ func (infra *Infra) Deploy() error {
ao.SetPrune(document.InitInfraSelector) ao.SetPrune(document.InitInfraSelector)
} }
globalConf := infra.RootSettings.Config() globalConf := infra.RootSettings.Config
if err = globalConf.EnsureComplete(); err != nil { if err = globalConf.EnsureComplete(); err != nil {
return err return err
} }

View File

@ -86,10 +86,9 @@ func TestDeploy(t *testing.T) {
} }
} }
// MakeNewFakeRootSettings takes kubeconfig path and directory path to fixture dir as argument. // makeNewFakeRootSettings takes kubeconfig path and directory path to fixture dir as argument.
func makeNewFakeRootSettings(t *testing.T, kp string, dir string) *environment.AirshipCTLSettings { func makeNewFakeRootSettings(t *testing.T, kp string, dir string) *environment.AirshipCTLSettings {
t.Helper() t.Helper()
rs := &environment.AirshipCTLSettings{}
akp, err := filepath.Abs(kp) akp, err := filepath.Abs(kp)
require.NoError(t, err) require.NoError(t, err)
@ -97,9 +96,11 @@ func makeNewFakeRootSettings(t *testing.T, kp string, dir string) *environment.A
adir, err := filepath.Abs(dir) adir, err := filepath.Abs(dir)
require.NoError(t, err) require.NoError(t, err)
rs.SetAirshipConfigPath(adir) settings := &environment.AirshipCTLSettings{
rs.SetKubeConfigPath(akp) AirshipConfigPath: adir,
KubeConfigPath: akp,
}
rs.InitConfig() settings.InitConfig()
return rs return settings
} }

View File

@ -20,7 +20,7 @@ func (s *Settings) Pull() error {
func (s *Settings) cloneRepositories() error { func (s *Settings) cloneRepositories() error {
// Clone main repository // Clone main repository
currentManifest, err := s.Config().CurrentContextManifest() currentManifest, err := s.Config.CurrentContextManifest()
if err != nil { if err != nil {
return err return err
} }

View File

@ -20,12 +20,11 @@ import (
) )
func getDummyPullSettings() *Settings { func getDummyPullSettings() *Settings {
mockPullSettings := &Settings{ return &Settings{
AirshipCTLSettings: new(environment.AirshipCTLSettings), AirshipCTLSettings: &environment.AirshipCTLSettings{
Config: testutil.DummyConfig(),
},
} }
mockConf := testutil.DummyConfig()
mockPullSettings.AirshipCTLSettings.SetConfig(mockConf)
return mockPullSettings
} }
func TestPull(t *testing.T) { func TestPull(t *testing.T) {
@ -34,7 +33,7 @@ func TestPull(t *testing.T) {
t.Run("cloneRepositories", func(t *testing.T) { t.Run("cloneRepositories", func(t *testing.T) {
dummyPullSettings := getDummyPullSettings() dummyPullSettings := getDummyPullSettings()
currentManifest, err := dummyPullSettings.Config().CurrentContextManifest() currentManifest, err := dummyPullSettings.Config.CurrentContextManifest()
require.NoError(err) require.NoError(err)
err = fixtures.Init() err = fixtures.Init()
@ -75,7 +74,7 @@ func TestPull(t *testing.T) {
t.Run("Pull", func(t *testing.T) { t.Run("Pull", func(t *testing.T) {
dummyPullSettings := getDummyPullSettings() dummyPullSettings := getDummyPullSettings()
conf := dummyPullSettings.AirshipCTLSettings.Config() conf := dummyPullSettings.AirshipCTLSettings.Config
err := fixtures.Init() err := fixtures.Init()
require.NoError(err) require.NoError(err)
@ -95,7 +94,7 @@ func TestPull(t *testing.T) {
}, },
}, },
} }
dummyPullSettings.SetConfig(conf) dummyPullSettings.Config = conf
tmpDir, cleanup := testutil.TempDir(t, "airshipctlPullTest-") tmpDir, cleanup := testutil.TempDir(t, "airshipctlPullTest-")
defer cleanup(t) defer cleanup(t)

View File

@ -16,9 +16,9 @@ import (
type AirshipCTLSettings struct { type AirshipCTLSettings struct {
// Debug is used for verbose output // Debug is used for verbose output
Debug bool Debug bool
airshipConfigPath string AirshipConfigPath string
kubeConfigPath string KubeConfigPath string
config *config.Config Config *config.Config
} }
// InitFlags adds the default settings flags to cmd // InitFlags adds the default settings flags to cmd
@ -34,51 +34,27 @@ func (a *AirshipCTLSettings) InitFlags(cmd *cobra.Command) {
defaultAirshipConfigPath := filepath.Join(defaultAirshipConfigDir, config.AirshipConfig) defaultAirshipConfigPath := filepath.Join(defaultAirshipConfigDir, config.AirshipConfig)
flags.StringVar( flags.StringVar(
&a.airshipConfigPath, &a.AirshipConfigPath,
config.FlagConfigFilePath, config.FlagConfigFilePath,
"", "",
`Path to file for airshipctl configuration. (default "`+defaultAirshipConfigPath+`")`) `Path to file for airshipctl configuration. (default "`+defaultAirshipConfigPath+`")`)
defaultKubeConfigPath := filepath.Join(defaultAirshipConfigDir, config.AirshipKubeConfig) defaultKubeConfigPath := filepath.Join(defaultAirshipConfigDir, config.AirshipKubeConfig)
flags.StringVar( flags.StringVar(
&a.kubeConfigPath, &a.KubeConfigPath,
clientcmd.RecommendedConfigPathFlag, clientcmd.RecommendedConfigPathFlag,
"", "",
`Path to kubeconfig associated with airshipctl configuration. (default "`+defaultKubeConfigPath+`")`) `Path to kubeconfig associated with airshipctl configuration. (default "`+defaultKubeConfigPath+`")`)
} }
func (a *AirshipCTLSettings) Config() *config.Config {
return a.config
}
func (a *AirshipCTLSettings) SetConfig(conf *config.Config) {
a.config = conf
}
func (a *AirshipCTLSettings) AirshipConfigPath() string {
return a.airshipConfigPath
}
func (a *AirshipCTLSettings) SetAirshipConfigPath(acp string) {
a.airshipConfigPath = acp
}
func (a *AirshipCTLSettings) KubeConfigPath() string {
return a.kubeConfigPath
}
func (a *AirshipCTLSettings) SetKubeConfigPath(kcp string) {
a.kubeConfigPath = kcp
}
// InitConfig - Initializes and loads Config it exists. // InitConfig - Initializes and loads Config it exists.
func (a *AirshipCTLSettings) InitConfig() { func (a *AirshipCTLSettings) InitConfig() {
a.SetConfig(config.NewConfig()) a.Config = config.NewConfig()
a.initAirshipConfigPath() a.initAirshipConfigPath()
a.initKubeConfigPath() a.initKubeConfigPath()
err := a.Config().LoadConfig(a.AirshipConfigPath(), a.KubeConfigPath()) err := a.Config.LoadConfig(a.AirshipConfigPath, a.KubeConfigPath)
if err != nil { if err != nil {
// Should stop airshipctl // Should stop airshipctl
log.Fatal(err) log.Fatal(err)
@ -87,19 +63,19 @@ func (a *AirshipCTLSettings) InitConfig() {
func (a *AirshipCTLSettings) initAirshipConfigPath() { func (a *AirshipCTLSettings) initAirshipConfigPath() {
// The airshipConfigPath may already have been received as a command line argument // The airshipConfigPath may already have been received as a command line argument
if a.airshipConfigPath != "" { if a.AirshipConfigPath != "" {
return return
} }
// Otherwise, we can check if we got the path via ENVIRONMENT variable // Otherwise, we can check if we got the path via ENVIRONMENT variable
a.airshipConfigPath = os.Getenv(config.AirshipConfigEnv) a.AirshipConfigPath = os.Getenv(config.AirshipConfigEnv)
if a.airshipConfigPath != "" { if a.AirshipConfigPath != "" {
return return
} }
// Otherwise, we'll try putting it in the home directory // Otherwise, we'll try putting it in the home directory
homeDir := userHomeDir() homeDir := userHomeDir()
a.airshipConfigPath = filepath.Join(homeDir, config.AirshipConfigDir, config.AirshipConfig) a.AirshipConfigPath = filepath.Join(homeDir, config.AirshipConfigDir, config.AirshipConfig)
} }
func (a *AirshipCTLSettings) initKubeConfigPath() { func (a *AirshipCTLSettings) initKubeConfigPath() {
@ -111,19 +87,19 @@ func (a *AirshipCTLSettings) initKubeConfigPath() {
// explicitly want airshipctl to use it. // explicitly want airshipctl to use it.
// The kubeConfigPath may already have been received as a command line argument // The kubeConfigPath may already have been received as a command line argument
if a.kubeConfigPath != "" { if a.KubeConfigPath != "" {
return return
} }
// Otherwise, we can check if we got the path via ENVIRONMENT variable // Otherwise, we can check if we got the path via ENVIRONMENT variable
a.kubeConfigPath = os.Getenv(config.AirshipKubeConfigEnv) a.KubeConfigPath = os.Getenv(config.AirshipKubeConfigEnv)
if a.kubeConfigPath != "" { if a.KubeConfigPath != "" {
return return
} }
// Otherwise, we'll try putting it in the home directory // Otherwise, we'll try putting it in the home directory
homeDir := userHomeDir() homeDir := userHomeDir()
a.kubeConfigPath = filepath.Join(homeDir, config.AirshipConfigDir, config.AirshipKubeConfig) a.KubeConfigPath = filepath.Join(homeDir, config.AirshipConfigDir, config.AirshipKubeConfig)
} }
// userHomeDir is a utility function that wraps os.UserHomeDir and returns no // userHomeDir is a utility function that wraps os.UserHomeDir and returns no

View File

@ -48,8 +48,8 @@ func TestInitConfig(t *testing.T) {
expectedKubeConfig := filepath.Join(testDir, config.AirshipConfigDir, config.AirshipKubeConfig) expectedKubeConfig := filepath.Join(testDir, config.AirshipConfigDir, config.AirshipKubeConfig)
testSettings.InitConfig() testSettings.InitConfig()
assert.Equal(t, expectedAirshipConfig, testSettings.AirshipConfigPath()) assert.Equal(t, expectedAirshipConfig, testSettings.AirshipConfigPath)
assert.Equal(t, expectedKubeConfig, testSettings.KubeConfigPath()) assert.Equal(t, expectedKubeConfig, testSettings.KubeConfigPath)
}) })
t.Run("PreferEnvToDefault", func(subTest *testing.T) { t.Run("PreferEnvToDefault", func(subTest *testing.T) {
@ -68,8 +68,8 @@ func TestInitConfig(t *testing.T) {
defer os.Unsetenv(config.AirshipKubeConfigEnv) defer os.Unsetenv(config.AirshipKubeConfigEnv)
testSettings.InitConfig() testSettings.InitConfig()
assert.Equal(t, expectedAirshipConfig, testSettings.AirshipConfigPath()) assert.Equal(t, expectedAirshipConfig, testSettings.AirshipConfigPath)
assert.Equal(t, expectedKubeConfig, testSettings.KubeConfigPath()) assert.Equal(t, expectedKubeConfig, testSettings.KubeConfigPath)
}) })
t.Run("PreferCmdLineArgToDefault", func(subTest *testing.T) { t.Run("PreferCmdLineArgToDefault", func(subTest *testing.T) {
@ -78,17 +78,18 @@ func TestInitConfig(t *testing.T) {
defer cleanup(t) defer cleanup(t)
defer setHome(testDir)() defer setHome(testDir)()
var testSettings AirshipCTLSettings
expectedAirshipConfig := filepath.Join(testDir, "airshipCmdLine") expectedAirshipConfig := filepath.Join(testDir, "airshipCmdLine")
expectedKubeConfig := filepath.Join(testDir, "kubeCmdLine") expectedKubeConfig := filepath.Join(testDir, "kubeCmdLine")
testSettings.SetAirshipConfigPath(expectedAirshipConfig) testSettings := AirshipCTLSettings{
testSettings.SetKubeConfigPath(expectedKubeConfig) AirshipConfigPath: expectedAirshipConfig,
KubeConfigPath: expectedKubeConfig,
}
// InitConfig should not change any values // InitConfig should not change any values
testSettings.InitConfig() testSettings.InitConfig()
assert.Equal(t, expectedAirshipConfig, testSettings.AirshipConfigPath()) assert.Equal(t, expectedAirshipConfig, testSettings.AirshipConfigPath)
assert.Equal(t, expectedKubeConfig, testSettings.KubeConfigPath()) assert.Equal(t, expectedKubeConfig, testSettings.KubeConfigPath)
}) })
t.Run("PreferCmdLineArgToEnv", func(subTest *testing.T) { t.Run("PreferCmdLineArgToEnv", func(subTest *testing.T) {
@ -97,7 +98,6 @@ func TestInitConfig(t *testing.T) {
defer cleanup(t) defer cleanup(t)
defer setHome(testDir)() defer setHome(testDir)()
var testSettings AirshipCTLSettings
expectedAirshipConfig := filepath.Join(testDir, "airshipCmdLine") expectedAirshipConfig := filepath.Join(testDir, "airshipCmdLine")
expectedKubeConfig := filepath.Join(testDir, "kubeCmdLine") expectedKubeConfig := filepath.Join(testDir, "kubeCmdLine")
@ -111,12 +111,14 @@ func TestInitConfig(t *testing.T) {
defer os.Unsetenv(config.AirshipConfigEnv) defer os.Unsetenv(config.AirshipConfigEnv)
defer os.Unsetenv(config.AirshipKubeConfigEnv) defer os.Unsetenv(config.AirshipKubeConfigEnv)
testSettings.SetAirshipConfigPath(expectedAirshipConfig) testSettings := AirshipCTLSettings{
testSettings.SetKubeConfigPath(expectedKubeConfig) AirshipConfigPath: expectedAirshipConfig,
KubeConfigPath: expectedKubeConfig,
}
testSettings.InitConfig() testSettings.InitConfig()
assert.Equal(t, expectedAirshipConfig, testSettings.AirshipConfigPath()) assert.Equal(t, expectedAirshipConfig, testSettings.AirshipConfigPath)
assert.Equal(t, expectedKubeConfig, testSettings.KubeConfigPath()) assert.Equal(t, expectedKubeConfig, testSettings.KubeConfigPath)
}) })
} }

View File

@ -41,9 +41,9 @@ func NewClient(settings *environment.AirshipCTLSettings) (Interface, error) {
client := new(Client) client := new(Client)
var err error var err error
f := k8sutils.FactoryFromKubeConfigPath(settings.KubeConfigPath()) f := k8sutils.FactoryFromKubeConfigPath(settings.KubeConfigPath)
pathToBufferDir := filepath.Dir(settings.AirshipConfigPath()) pathToBufferDir := filepath.Dir(settings.AirshipConfigPath)
client.kubectl = kubectl.NewKubectl(f).WithBufferDir(pathToBufferDir) client.kubectl = kubectl.NewKubectl(f).WithBufferDir(pathToBufferDir)
client.clientSet, err = f.KubernetesClientSet() client.clientSet, err = f.KubernetesClientSet()

View File

@ -18,10 +18,8 @@ const (
) )
func TestNewClient(t *testing.T) { func TestNewClient(t *testing.T) {
settings := &environment.AirshipCTLSettings{}
conf, cleanup := testutil.InitConfig(t) conf, cleanup := testutil.InitConfig(t)
defer cleanup(t) defer cleanup(t)
settings.SetConfig(conf)
akp, err := filepath.Abs(kubeconfigPath) akp, err := filepath.Abs(kubeconfigPath)
require.NoError(t, err) require.NoError(t, err)
@ -29,8 +27,11 @@ func TestNewClient(t *testing.T) {
adir, err := filepath.Abs(airshipConfigDir) adir, err := filepath.Abs(airshipConfigDir)
require.NoError(t, err) require.NoError(t, err)
settings.SetAirshipConfigPath(adir) settings := &environment.AirshipCTLSettings{
settings.SetKubeConfigPath(akp) Config: conf,
AirshipConfigPath: adir,
KubeConfigPath: akp,
}
client, err := client.NewClient(settings) client, err := client.NewClient(settings)
assert.NoError(t, err) assert.NoError(t, err)

View File

@ -91,7 +91,7 @@ func (a *Adapter) configureClient(remoteURL string) error {
// initializeAdapter retrieves the remote direct configuration defined in the Airship configuration file. // initializeAdapter retrieves the remote direct configuration defined in the Airship configuration file.
func (a *Adapter) initializeAdapter(settings *environment.AirshipCTLSettings) error { func (a *Adapter) initializeAdapter(settings *environment.AirshipCTLSettings) error {
cfg := settings.Config() cfg := settings.Config
bootstrapSettings, err := cfg.CurrentContextBootstrapInfo() bootstrapSettings, err := cfg.CurrentContextBootstrapInfo()
if err != nil { if err != nil {
return err return err

View File

@ -21,12 +21,11 @@ const (
func initSettings(t *testing.T, rd *config.RemoteDirect, testdata string) *environment.AirshipCTLSettings { func initSettings(t *testing.T, rd *config.RemoteDirect, testdata string) *environment.AirshipCTLSettings {
t.Helper() t.Helper()
settings := &environment.AirshipCTLSettings{} settings := &environment.AirshipCTLSettings{Config: testutil.DummyConfig()}
settings.SetConfig(testutil.DummyConfig()) bi, err := settings.Config.CurrentContextBootstrapInfo()
bi, err := settings.Config().CurrentContextBootstrapInfo()
require.NoError(t, err) require.NoError(t, err)
bi.RemoteDirect = rd bi.RemoteDirect = rd
cm, err := settings.Config().CurrentContextManifest() cm, err := settings.Config.CurrentContextManifest()
require.NoError(t, err) require.NoError(t, err)
cm.TargetPath = "testdata/" + testdata cm.TargetPath = "testdata/" + testdata
return settings return settings