From 5efc26d9f085de64c388ef70e5b47bb3d3940aea Mon Sep 17 00:00:00 2001 From: Ian Howell Date: Mon, 23 Mar 2020 14:41:08 -0500 Subject: [PATCH] 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 --- cmd/cluster/cluster_test.go | 7 ++-- cmd/config/get_authinfo.go | 2 +- cmd/config/get_authinfo_test.go | 18 +++++----- cmd/config/get_cluster.go | 2 +- cmd/config/get_cluster_test.go | 40 ++++++++++----------- cmd/config/get_context.go | 2 +- cmd/config/get_context_test.go | 20 +++++------ cmd/config/init.go | 9 +---- cmd/config/set_authinfo.go | 2 +- cmd/config/set_authinfo_test.go | 5 ++- cmd/config/set_cluster.go | 2 +- cmd/config/set_cluster_test.go | 7 ++-- cmd/config/set_context.go | 2 +- cmd/config/set_context_test.go | 5 ++- cmd/config/use_context.go | 2 +- cmd/config/use_context_test.go | 6 +--- cmd/document/pull_test.go | 25 +++++++------ cmd/root_test.go | 2 +- pkg/bootstrap/isogen/command.go | 2 +- pkg/cluster/initinfra/infra.go | 2 +- pkg/cluster/initinfra/infra_test.go | 13 +++---- pkg/document/pull/pull.go | 2 +- pkg/document/pull/pull_test.go | 15 ++++---- pkg/environment/settings.go | 54 ++++++++--------------------- pkg/environment/settings_test.go | 30 ++++++++-------- pkg/k8s/client/client.go | 4 +-- pkg/k8s/client/client_test.go | 9 ++--- pkg/remote/remote_direct.go | 2 +- pkg/remote/remote_direct_test.go | 7 ++-- 29 files changed, 127 insertions(+), 171 deletions(-) diff --git a/cmd/cluster/cluster_test.go b/cmd/cluster/cluster_test.go index 12a9e0878..b5b3fc22d 100644 --- a/cmd/cluster/cluster_test.go +++ b/cmd/cluster/cluster_test.go @@ -9,9 +9,10 @@ import ( ) func TestNewClusterCommandReturn(t *testing.T) { - fakeRootSettings := &environment.AirshipCTLSettings{} - fakeRootSettings.SetAirshipConfigPath("../../testdata/k8s/config.yaml") - fakeRootSettings.SetKubeConfigPath("../../testdata/k8s/kubeconfig.yaml") + fakeRootSettings := &environment.AirshipCTLSettings{ + AirshipConfigPath: "../../testdata/k8s/config.yaml", + KubeConfigPath: "../../testdata/k8s/kubeconfig.yaml", + } fakeRootSettings.InitConfig() tests := []*testutil.CmdTest{ diff --git a/cmd/config/get_authinfo.go b/cmd/config/get_authinfo.go index 234d4c791..b95b2175e 100644 --- a/cmd/config/get_authinfo.go +++ b/cmd/config/get_authinfo.go @@ -46,7 +46,7 @@ func NewCmdConfigGetAuthInfo(rootSettings *environment.AirshipCTLSettings) *cobr if len(args) == 1 { o.Name = args[0] } - return config.RunGetAuthInfo(o, cmd.OutOrStdout(), rootSettings.Config()) + return config.RunGetAuthInfo(o, cmd.OutOrStdout(), rootSettings.Config) }, } diff --git a/cmd/config/get_authinfo_test.go b/cmd/config/get_authinfo_test.go index a1bed9294..3486e3e2b 100644 --- a/cmd/config/get_authinfo_test.go +++ b/cmd/config/get_authinfo_test.go @@ -36,17 +36,16 @@ const ( ) func TestGetAuthInfoCmd(t *testing.T) { - conf := &config.Config{ - AuthInfos: map[string]*config.AuthInfo{ - fooAuthInfo: getTestAuthInfo(), - barAuthInfo: getTestAuthInfo(), - bazAuthInfo: getTestAuthInfo(), + settings := &environment.AirshipCTLSettings{ + Config: &config.Config{ + AuthInfos: map[string]*config.AuthInfo{ + fooAuthInfo: getTestAuthInfo(), + barAuthInfo: getTestAuthInfo(), + bazAuthInfo: getTestAuthInfo(), + }, }, } - settings := &environment.AirshipCTLSettings{} - settings.SetConfig(conf) - cmdTests := []*testutil.CmdTest{ { Name: "get-credentials", @@ -80,8 +79,7 @@ func TestGetAuthInfoCmd(t *testing.T) { } func TestNoAuthInfosGetAuthInfoCmd(t *testing.T) { - settings := &environment.AirshipCTLSettings{} - settings.SetConfig(&config.Config{}) + settings := &environment.AirshipCTLSettings{Config: new(config.Config)} cmdTest := &testutil.CmdTest{ Name: "no-credentials", CmdLine: "", diff --git a/cmd/config/get_cluster.go b/cmd/config/get_cluster.go index f284b049d..84575e8e6 100644 --- a/cmd/config/get_cluster.go +++ b/cmd/config/get_cluster.go @@ -53,7 +53,7 @@ func NewCmdConfigGetCluster(rootSettings *environment.AirshipCTLSettings) *cobra return err } - return config.RunGetCluster(o, cmd.OutOrStdout(), rootSettings.Config()) + return config.RunGetCluster(o, cmd.OutOrStdout(), rootSettings.Config) }, } diff --git a/cmd/config/get_cluster_test.go b/cmd/config/get_cluster_test.go index d531bb67c..cb878e2aa 100644 --- a/cmd/config/get_cluster_test.go +++ b/cmd/config/get_cluster_test.go @@ -39,32 +39,31 @@ const ( ) func TestGetClusterCmd(t *testing.T) { - conf := &config.Config{ - Clusters: map[string]*config.ClusterPurpose{ - fooCluster: { - ClusterTypes: map[string]*config.Cluster{ - config.Ephemeral: getNamedTestCluster(fooCluster, config.Ephemeral), - config.Target: getNamedTestCluster(fooCluster, config.Target), + settings := &environment.AirshipCTLSettings{ + Config: &config.Config{ + Clusters: map[string]*config.ClusterPurpose{ + fooCluster: { + ClusterTypes: map[string]*config.Cluster{ + config.Ephemeral: getNamedTestCluster(fooCluster, config.Ephemeral), + config.Target: getNamedTestCluster(fooCluster, config.Target), + }, }, - }, - barCluster: { - ClusterTypes: map[string]*config.Cluster{ - config.Ephemeral: getNamedTestCluster(barCluster, config.Ephemeral), - config.Target: getNamedTestCluster(barCluster, config.Target), + barCluster: { + ClusterTypes: map[string]*config.Cluster{ + config.Ephemeral: getNamedTestCluster(barCluster, config.Ephemeral), + config.Target: getNamedTestCluster(barCluster, config.Target), + }, }, - }, - bazCluster: { - ClusterTypes: map[string]*config.Cluster{ - config.Ephemeral: getNamedTestCluster(bazCluster, config.Ephemeral), - config.Target: getNamedTestCluster(bazCluster, config.Target), + bazCluster: { + ClusterTypes: map[string]*config.Cluster{ + config.Ephemeral: getNamedTestCluster(bazCluster, config.Ephemeral), + config.Target: getNamedTestCluster(bazCluster, config.Target), + }, }, }, }, } - settings := &environment.AirshipCTLSettings{} - settings.SetConfig(conf) - cmdTests := []*testutil.CmdTest{ { Name: "get-ephemeral", @@ -121,8 +120,7 @@ func TestGetClusterCmd(t *testing.T) { } func TestNoClustersGetClusterCmd(t *testing.T) { - settings := &environment.AirshipCTLSettings{} - settings.SetConfig(&config.Config{}) + settings := &environment.AirshipCTLSettings{Config: new(config.Config)} cmdTest := &testutil.CmdTest{ Name: "no-clusters", CmdLine: "", diff --git a/cmd/config/get_context.go b/cmd/config/get_context.go index 0cfb4ea7f..b578a1c83 100644 --- a/cmd/config/get_context.go +++ b/cmd/config/get_context.go @@ -54,7 +54,7 @@ func NewCmdConfigGetContext(rootSettings *environment.AirshipCTLSettings) *cobra if len(args) == 1 { o.Name = args[0] } - return config.RunGetContext(o, cmd.OutOrStdout(), rootSettings.Config()) + return config.RunGetContext(o, cmd.OutOrStdout(), rootSettings.Config) }, } diff --git a/cmd/config/get_context_test.go b/cmd/config/get_context_test.go index 0ba951c78..026565207 100644 --- a/cmd/config/get_context_test.go +++ b/cmd/config/get_context_test.go @@ -38,18 +38,17 @@ const ( ) func TestGetContextCmd(t *testing.T) { - conf := &config.Config{ - Contexts: map[string]*config.Context{ - fooContext: getNamedTestContext(fooContext), - barContext: getNamedTestContext(barContext), - bazContext: getNamedTestContext(bazContext), + settings := &environment.AirshipCTLSettings{ + Config: &config.Config{ + Contexts: map[string]*config.Context{ + fooContext: getNamedTestContext(fooContext), + barContext: getNamedTestContext(barContext), + bazContext: getNamedTestContext(bazContext), + }, + CurrentContext: bazContext, }, - CurrentContext: bazContext, } - settings := &environment.AirshipCTLSettings{} - settings.SetConfig(conf) - cmdTests := []*testutil.CmdTest{ { Name: "get-context", @@ -88,8 +87,7 @@ found in the configuration.`, missingContext), } func TestNoContextsGetContextCmd(t *testing.T) { - settings := &environment.AirshipCTLSettings{} - settings.SetConfig(&config.Config{}) + settings := &environment.AirshipCTLSettings{Config: new(config.Config)} cmdTest := &testutil.CmdTest{ Name: "no-contexts", CmdLine: "", diff --git a/cmd/config/init.go b/cmd/config/init.go index 31f5c8b70..93b483625 100644 --- a/cmd/config/init.go +++ b/cmd/config/init.go @@ -32,14 +32,7 @@ func NewCmdConfigInit(rootSettings *environment.AirshipCTLSettings) *cobra.Comma Use: "init", Short: configInitLong, RunE: func(cmd *cobra.Command, args []string) error { - c := rootSettings.Config() - - err := c.PersistConfig() - if err != nil { - return err - } - - return nil + return rootSettings.Config.PersistConfig() }, } diff --git a/cmd/config/set_authinfo.go b/cmd/config/set_authinfo.go index ef381d97e..514692624 100644 --- a/cmd/config/set_authinfo.go +++ b/cmd/config/set_authinfo.go @@ -61,7 +61,7 @@ func NewCmdConfigSetAuthInfo(rootSettings *environment.AirshipCTLSettings) *cobr Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { o.Name = args[0] - modified, err := config.RunSetAuthInfo(o, rootSettings.Config(), true) + modified, err := config.RunSetAuthInfo(o, rootSettings.Config, true) if err != nil { return err } diff --git a/cmd/config/set_authinfo_test.go b/cmd/config/set_authinfo_test.go index 71989811a..a1af0421b 100644 --- a/cmd/config/set_authinfo_test.go +++ b/cmd/config/set_authinfo_test.go @@ -88,12 +88,11 @@ func initInputConfig(t *testing.T) (given *config.Config, cleanup func(*testing. } func (test setAuthInfoTest) run(t *testing.T) { - settings := &environment.AirshipCTLSettings{} - settings.SetConfig(test.inputConfig) + settings := &environment.AirshipCTLSettings{Config: test.inputConfig} test.cmdTest.Cmd = cmd.NewCmdConfigSetAuthInfo(settings) testutil.RunTest(t, test.cmdTest) - afterRunConf := settings.Config() + afterRunConf := settings.Config // Find the AuthInfo Created or Modified afterRunAuthInfo, err := afterRunConf.GetAuthInfo(test.userName) require.NoError(t, err) diff --git a/cmd/config/set_cluster.go b/cmd/config/set_cluster.go index 795058acd..4799ba374 100644 --- a/cmd/config/set_cluster.go +++ b/cmd/config/set_cluster.go @@ -66,7 +66,7 @@ func NewCmdConfigSetCluster(rootSettings *environment.AirshipCTLSettings) *cobra Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { o.Name = args[0] - modified, err := config.RunSetCluster(o, rootSettings.Config(), true) + modified, err := config.RunSetCluster(o, rootSettings.Config, true) if err != nil { return err } diff --git a/cmd/config/set_cluster_test.go b/cmd/config/set_cluster_test.go index 36f2fa07a..e574d6d4a 100644 --- a/cmd/config/set_cluster_test.go +++ b/cmd/config/set_cluster_test.go @@ -201,10 +201,7 @@ func TestModifyCluster(t *testing.T) { } func (test setClusterTest) run(t *testing.T) { - // Get the Environment - settings := &environment.AirshipCTLSettings{} - settings.SetConfig(test.givenConfig) - + settings := &environment.AirshipCTLSettings{Config: test.givenConfig} buf := bytes.NewBuffer([]byte{}) 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) // Loads the Config File that was updated - afterRunConf := settings.Config() + afterRunConf := settings.Config // Get ClusterType tctypeFlag := cmd.Flag(config.FlagClusterType) require.NotNil(t, tctypeFlag) diff --git a/cmd/config/set_context.go b/cmd/config/set_context.go index 3107aa72e..13d6ab7eb 100644 --- a/cmd/config/set_context.go +++ b/cmd/config/set_context.go @@ -70,7 +70,7 @@ func NewCmdConfigSetContext(rootSettings *environment.AirshipCTLSettings) *cobra return nil } - modified, err := config.RunSetContext(o, rootSettings.Config(), true) + modified, err := config.RunSetContext(o, rootSettings.Config, true) if err != nil { return err diff --git a/cmd/config/set_context_test.go b/cmd/config/set_context_test.go index b1e2f52dd..f5c2b7f8f 100644 --- a/cmd/config/set_context_test.go +++ b/cmd/config/set_context_test.go @@ -127,13 +127,12 @@ func TestSetContext(t *testing.T) { func (test setContextTest) run(t *testing.T) { // Get the Environment - settings := &environment.AirshipCTLSettings{} - settings.SetConfig(test.givenConfig) + settings := &environment.AirshipCTLSettings{Config: test.givenConfig} test.cmdTest.Cmd = cmd.NewCmdConfigSetContext(settings) testutil.RunTest(t, test.cmdTest) - afterRunConf := settings.Config() + afterRunConf := settings.Config // Find the Context Created or Modified afterRunContext, err := afterRunConf.GetContext(test.contextName) diff --git a/cmd/config/use_context.go b/cmd/config/use_context.go index cc6ced789..748505bdc 100644 --- a/cmd/config/use_context.go +++ b/cmd/config/use_context.go @@ -44,7 +44,7 @@ func NewCmdConfigUseContext(rootSettings *environment.AirshipCTLSettings) *cobra Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { contextName := args[0] - err := config.RunUseContext(contextName, rootSettings.Config()) + err := config.RunUseContext(contextName, rootSettings.Config) if err != nil { return err } diff --git a/cmd/config/use_context_test.go b/cmd/config/use_context_test.go index 46f9f61b4..186c52106 100644 --- a/cmd/config/use_context_test.go +++ b/cmd/config/use_context_test.go @@ -26,11 +26,7 @@ import ( ) func TestConfigUseContext(t *testing.T) { - conf := testutil.DummyConfig() - - settings := &environment.AirshipCTLSettings{} - settings.SetConfig(conf) - + settings := &environment.AirshipCTLSettings{Config: testutil.DummyConfig()} cmdTests := []*testutil.CmdTest{ { Name: "config-use-context", diff --git a/cmd/document/pull_test.go b/cmd/document/pull_test.go index e811b5480..2a3d49fbc 100644 --- a/cmd/document/pull_test.go +++ b/cmd/document/pull_test.go @@ -13,27 +13,26 @@ import ( ) func getDummyAirshipSettings(t *testing.T) *environment.AirshipCTLSettings { - settings := new(environment.AirshipCTLSettings) - conf := testutil.DummyConfig() - mfst := conf.Manifests["dummy_manifest"] + settings := &environment.AirshipCTLSettings{Config: testutil.DummyConfig()} err := fixtures.Init() require.NoError(t, err) fx := fixtures.Basic().One() - mfst.Repositories = map[string]*config.Repository{"primary": { - URLString: fx.DotGit().Root(), - CheckoutOptions: &config.RepoCheckout{ - Branch: "master", - ForceCheckout: false, + mfst := settings.Config.Manifests["dummy_manifest"] + mfst.Repositories = map[string]*config.Repository{ + "primary": { + URLString: fx.DotGit().Root(), + CheckoutOptions: &config.RepoCheckout{ + Branch: "master", + ForceCheckout: false, + }, + Auth: &config.RepoAuth{ + Type: "http-basic", + }, }, - Auth: &config.RepoAuth{ - Type: "http-basic", - }, - }, } - settings.SetConfig(conf) return settings } diff --git a/cmd/root_test.go b/cmd/root_test.go index 6d2edf871..f890acdfc 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -68,7 +68,7 @@ func TestFlagLoading(t *testing.T) { err = rootCmd.Execute() require.NoError(t, err) - assert.Equal(t, settings.AirshipConfigPath(), tt.expected) + assert.Equal(t, settings.AirshipConfigPath, tt.expected) }) } } diff --git a/pkg/bootstrap/isogen/command.go b/pkg/bootstrap/isogen/command.go index d633adeb5..4663afdd3 100644 --- a/pkg/bootstrap/isogen/command.go +++ b/pkg/bootstrap/isogen/command.go @@ -24,7 +24,7 @@ const ( func GenerateBootstrapIso(settings *environment.AirshipCTLSettings) error { ctx := context.Background() - globalConf := settings.Config() + globalConf := settings.Config if err := globalConf.EnsureComplete(); err != nil { return err } diff --git a/pkg/cluster/initinfra/infra.go b/pkg/cluster/initinfra/infra.go index 0f90b7311..2d88928c2 100644 --- a/pkg/cluster/initinfra/infra.go +++ b/pkg/cluster/initinfra/infra.go @@ -50,7 +50,7 @@ func (infra *Infra) Deploy() error { ao.SetPrune(document.InitInfraSelector) } - globalConf := infra.RootSettings.Config() + globalConf := infra.RootSettings.Config if err = globalConf.EnsureComplete(); err != nil { return err } diff --git a/pkg/cluster/initinfra/infra_test.go b/pkg/cluster/initinfra/infra_test.go index bdd1ccb4f..b673ee501 100644 --- a/pkg/cluster/initinfra/infra_test.go +++ b/pkg/cluster/initinfra/infra_test.go @@ -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 { t.Helper() - rs := &environment.AirshipCTLSettings{} akp, err := filepath.Abs(kp) require.NoError(t, err) @@ -97,9 +96,11 @@ func makeNewFakeRootSettings(t *testing.T, kp string, dir string) *environment.A adir, err := filepath.Abs(dir) require.NoError(t, err) - rs.SetAirshipConfigPath(adir) - rs.SetKubeConfigPath(akp) + settings := &environment.AirshipCTLSettings{ + AirshipConfigPath: adir, + KubeConfigPath: akp, + } - rs.InitConfig() - return rs + settings.InitConfig() + return settings } diff --git a/pkg/document/pull/pull.go b/pkg/document/pull/pull.go index 8a48e1178..7079619eb 100644 --- a/pkg/document/pull/pull.go +++ b/pkg/document/pull/pull.go @@ -20,7 +20,7 @@ func (s *Settings) Pull() error { func (s *Settings) cloneRepositories() error { // Clone main repository - currentManifest, err := s.Config().CurrentContextManifest() + currentManifest, err := s.Config.CurrentContextManifest() if err != nil { return err } diff --git a/pkg/document/pull/pull_test.go b/pkg/document/pull/pull_test.go index 9aa0af38a..57a6a41fd 100644 --- a/pkg/document/pull/pull_test.go +++ b/pkg/document/pull/pull_test.go @@ -20,12 +20,11 @@ import ( ) func getDummyPullSettings() *Settings { - mockPullSettings := &Settings{ - AirshipCTLSettings: new(environment.AirshipCTLSettings), + return &Settings{ + AirshipCTLSettings: &environment.AirshipCTLSettings{ + Config: testutil.DummyConfig(), + }, } - mockConf := testutil.DummyConfig() - mockPullSettings.AirshipCTLSettings.SetConfig(mockConf) - return mockPullSettings } func TestPull(t *testing.T) { @@ -34,7 +33,7 @@ func TestPull(t *testing.T) { t.Run("cloneRepositories", func(t *testing.T) { dummyPullSettings := getDummyPullSettings() - currentManifest, err := dummyPullSettings.Config().CurrentContextManifest() + currentManifest, err := dummyPullSettings.Config.CurrentContextManifest() require.NoError(err) err = fixtures.Init() @@ -75,7 +74,7 @@ func TestPull(t *testing.T) { t.Run("Pull", func(t *testing.T) { dummyPullSettings := getDummyPullSettings() - conf := dummyPullSettings.AirshipCTLSettings.Config() + conf := dummyPullSettings.AirshipCTLSettings.Config err := fixtures.Init() require.NoError(err) @@ -95,7 +94,7 @@ func TestPull(t *testing.T) { }, }, } - dummyPullSettings.SetConfig(conf) + dummyPullSettings.Config = conf tmpDir, cleanup := testutil.TempDir(t, "airshipctlPullTest-") defer cleanup(t) diff --git a/pkg/environment/settings.go b/pkg/environment/settings.go index a2a6bffe3..964929953 100644 --- a/pkg/environment/settings.go +++ b/pkg/environment/settings.go @@ -16,9 +16,9 @@ import ( type AirshipCTLSettings struct { // Debug is used for verbose output Debug bool - airshipConfigPath string - kubeConfigPath string - config *config.Config + AirshipConfigPath string + KubeConfigPath string + Config *config.Config } // 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) flags.StringVar( - &a.airshipConfigPath, + &a.AirshipConfigPath, config.FlagConfigFilePath, "", `Path to file for airshipctl configuration. (default "`+defaultAirshipConfigPath+`")`) defaultKubeConfigPath := filepath.Join(defaultAirshipConfigDir, config.AirshipKubeConfig) flags.StringVar( - &a.kubeConfigPath, + &a.KubeConfigPath, clientcmd.RecommendedConfigPathFlag, "", `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. func (a *AirshipCTLSettings) InitConfig() { - a.SetConfig(config.NewConfig()) + a.Config = config.NewConfig() a.initAirshipConfigPath() a.initKubeConfigPath() - err := a.Config().LoadConfig(a.AirshipConfigPath(), a.KubeConfigPath()) + err := a.Config.LoadConfig(a.AirshipConfigPath, a.KubeConfigPath) if err != nil { // Should stop airshipctl log.Fatal(err) @@ -87,19 +63,19 @@ func (a *AirshipCTLSettings) InitConfig() { func (a *AirshipCTLSettings) initAirshipConfigPath() { // The airshipConfigPath may already have been received as a command line argument - if a.airshipConfigPath != "" { + if a.AirshipConfigPath != "" { return } // Otherwise, we can check if we got the path via ENVIRONMENT variable - a.airshipConfigPath = os.Getenv(config.AirshipConfigEnv) - if a.airshipConfigPath != "" { + a.AirshipConfigPath = os.Getenv(config.AirshipConfigEnv) + if a.AirshipConfigPath != "" { return } // Otherwise, we'll try putting it in the home directory homeDir := userHomeDir() - a.airshipConfigPath = filepath.Join(homeDir, config.AirshipConfigDir, config.AirshipConfig) + a.AirshipConfigPath = filepath.Join(homeDir, config.AirshipConfigDir, config.AirshipConfig) } func (a *AirshipCTLSettings) initKubeConfigPath() { @@ -111,19 +87,19 @@ func (a *AirshipCTLSettings) initKubeConfigPath() { // explicitly want airshipctl to use it. // The kubeConfigPath may already have been received as a command line argument - if a.kubeConfigPath != "" { + if a.KubeConfigPath != "" { return } // Otherwise, we can check if we got the path via ENVIRONMENT variable - a.kubeConfigPath = os.Getenv(config.AirshipKubeConfigEnv) - if a.kubeConfigPath != "" { + a.KubeConfigPath = os.Getenv(config.AirshipKubeConfigEnv) + if a.KubeConfigPath != "" { return } // Otherwise, we'll try putting it in the home directory 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 diff --git a/pkg/environment/settings_test.go b/pkg/environment/settings_test.go index ad9a0d8be..e8c327962 100644 --- a/pkg/environment/settings_test.go +++ b/pkg/environment/settings_test.go @@ -48,8 +48,8 @@ func TestInitConfig(t *testing.T) { expectedKubeConfig := filepath.Join(testDir, config.AirshipConfigDir, config.AirshipKubeConfig) testSettings.InitConfig() - assert.Equal(t, expectedAirshipConfig, testSettings.AirshipConfigPath()) - assert.Equal(t, expectedKubeConfig, testSettings.KubeConfigPath()) + assert.Equal(t, expectedAirshipConfig, testSettings.AirshipConfigPath) + assert.Equal(t, expectedKubeConfig, testSettings.KubeConfigPath) }) t.Run("PreferEnvToDefault", func(subTest *testing.T) { @@ -68,8 +68,8 @@ func TestInitConfig(t *testing.T) { defer os.Unsetenv(config.AirshipKubeConfigEnv) testSettings.InitConfig() - assert.Equal(t, expectedAirshipConfig, testSettings.AirshipConfigPath()) - assert.Equal(t, expectedKubeConfig, testSettings.KubeConfigPath()) + assert.Equal(t, expectedAirshipConfig, testSettings.AirshipConfigPath) + assert.Equal(t, expectedKubeConfig, testSettings.KubeConfigPath) }) t.Run("PreferCmdLineArgToDefault", func(subTest *testing.T) { @@ -78,17 +78,18 @@ func TestInitConfig(t *testing.T) { defer cleanup(t) defer setHome(testDir)() - var testSettings AirshipCTLSettings expectedAirshipConfig := filepath.Join(testDir, "airshipCmdLine") expectedKubeConfig := filepath.Join(testDir, "kubeCmdLine") - testSettings.SetAirshipConfigPath(expectedAirshipConfig) - testSettings.SetKubeConfigPath(expectedKubeConfig) + testSettings := AirshipCTLSettings{ + AirshipConfigPath: expectedAirshipConfig, + KubeConfigPath: expectedKubeConfig, + } // InitConfig should not change any values testSettings.InitConfig() - assert.Equal(t, expectedAirshipConfig, testSettings.AirshipConfigPath()) - assert.Equal(t, expectedKubeConfig, testSettings.KubeConfigPath()) + assert.Equal(t, expectedAirshipConfig, testSettings.AirshipConfigPath) + assert.Equal(t, expectedKubeConfig, testSettings.KubeConfigPath) }) t.Run("PreferCmdLineArgToEnv", func(subTest *testing.T) { @@ -97,7 +98,6 @@ func TestInitConfig(t *testing.T) { defer cleanup(t) defer setHome(testDir)() - var testSettings AirshipCTLSettings expectedAirshipConfig := filepath.Join(testDir, "airshipCmdLine") expectedKubeConfig := filepath.Join(testDir, "kubeCmdLine") @@ -111,12 +111,14 @@ func TestInitConfig(t *testing.T) { defer os.Unsetenv(config.AirshipConfigEnv) defer os.Unsetenv(config.AirshipKubeConfigEnv) - testSettings.SetAirshipConfigPath(expectedAirshipConfig) - testSettings.SetKubeConfigPath(expectedKubeConfig) + testSettings := AirshipCTLSettings{ + AirshipConfigPath: expectedAirshipConfig, + KubeConfigPath: expectedKubeConfig, + } testSettings.InitConfig() - assert.Equal(t, expectedAirshipConfig, testSettings.AirshipConfigPath()) - assert.Equal(t, expectedKubeConfig, testSettings.KubeConfigPath()) + assert.Equal(t, expectedAirshipConfig, testSettings.AirshipConfigPath) + assert.Equal(t, expectedKubeConfig, testSettings.KubeConfigPath) }) } diff --git a/pkg/k8s/client/client.go b/pkg/k8s/client/client.go index f5338609d..16123929c 100644 --- a/pkg/k8s/client/client.go +++ b/pkg/k8s/client/client.go @@ -41,9 +41,9 @@ func NewClient(settings *environment.AirshipCTLSettings) (Interface, error) { client := new(Client) 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.clientSet, err = f.KubernetesClientSet() diff --git a/pkg/k8s/client/client_test.go b/pkg/k8s/client/client_test.go index ffd330255..dbb92694a 100644 --- a/pkg/k8s/client/client_test.go +++ b/pkg/k8s/client/client_test.go @@ -18,10 +18,8 @@ const ( ) func TestNewClient(t *testing.T) { - settings := &environment.AirshipCTLSettings{} conf, cleanup := testutil.InitConfig(t) defer cleanup(t) - settings.SetConfig(conf) akp, err := filepath.Abs(kubeconfigPath) require.NoError(t, err) @@ -29,8 +27,11 @@ func TestNewClient(t *testing.T) { adir, err := filepath.Abs(airshipConfigDir) require.NoError(t, err) - settings.SetAirshipConfigPath(adir) - settings.SetKubeConfigPath(akp) + settings := &environment.AirshipCTLSettings{ + Config: conf, + AirshipConfigPath: adir, + KubeConfigPath: akp, + } client, err := client.NewClient(settings) assert.NoError(t, err) diff --git a/pkg/remote/remote_direct.go b/pkg/remote/remote_direct.go index 5b6a015b2..e604ef1f4 100644 --- a/pkg/remote/remote_direct.go +++ b/pkg/remote/remote_direct.go @@ -91,7 +91,7 @@ func (a *Adapter) configureClient(remoteURL string) error { // initializeAdapter retrieves the remote direct configuration defined in the Airship configuration file. func (a *Adapter) initializeAdapter(settings *environment.AirshipCTLSettings) error { - cfg := settings.Config() + cfg := settings.Config bootstrapSettings, err := cfg.CurrentContextBootstrapInfo() if err != nil { return err diff --git a/pkg/remote/remote_direct_test.go b/pkg/remote/remote_direct_test.go index 0df7a2989..4aa88d621 100644 --- a/pkg/remote/remote_direct_test.go +++ b/pkg/remote/remote_direct_test.go @@ -21,12 +21,11 @@ const ( func initSettings(t *testing.T, rd *config.RemoteDirect, testdata string) *environment.AirshipCTLSettings { t.Helper() - settings := &environment.AirshipCTLSettings{} - settings.SetConfig(testutil.DummyConfig()) - bi, err := settings.Config().CurrentContextBootstrapInfo() + settings := &environment.AirshipCTLSettings{Config: testutil.DummyConfig()} + bi, err := settings.Config.CurrentContextBootstrapInfo() require.NoError(t, err) bi.RemoteDirect = rd - cm, err := settings.Config().CurrentContextManifest() + cm, err := settings.Config.CurrentContextManifest() require.NoError(t, err) cm.TargetPath = "testdata/" + testdata return settings