This module is no longer needed since all config preloading actions were moved to config module and global flags are stored within root level cmd. Change-Id: I411f6717e5b3d2998706c35a82f1e7f1b2aef3a8 Signed-off-by: Ruslan Aliev <raliev@mirantis.com> Relates-To: #327changes/27/748827/8
parent
fba618225a
commit
233bbda0e0
@ -1,18 +0,0 @@
|
||||
/*
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
https://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package environment
|
||||
|
||||
// HomeEnvVar holds value of HOME directory from env
|
||||
const HomeEnvVar = "$HOME"
|
@ -1,119 +0,0 @@
|
||||
/*
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
https://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package environment
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"k8s.io/client-go/tools/clientcmd"
|
||||
|
||||
"opendev.org/airship/airshipctl/pkg/config"
|
||||
"opendev.org/airship/airshipctl/pkg/log"
|
||||
"opendev.org/airship/airshipctl/pkg/util"
|
||||
)
|
||||
|
||||
// AirshipCTLSettings is a container for all of the settings needed by airshipctl
|
||||
type AirshipCTLSettings struct {
|
||||
AirshipConfigPath string
|
||||
KubeConfigPath string
|
||||
Create bool
|
||||
Config *config.Config
|
||||
}
|
||||
|
||||
// InitFlags adds the default settings flags to cmd
|
||||
func (a *AirshipCTLSettings) InitFlags(cmd *cobra.Command) {
|
||||
flags := cmd.PersistentFlags()
|
||||
|
||||
defaultAirshipConfigDir := filepath.Join(HomeEnvVar, config.AirshipConfigDir)
|
||||
|
||||
defaultAirshipConfigPath := filepath.Join(defaultAirshipConfigDir, config.AirshipConfig)
|
||||
flags.StringVar(
|
||||
&a.AirshipConfigPath,
|
||||
"airshipconf",
|
||||
"",
|
||||
`Path to file for airshipctl configuration. (default "`+defaultAirshipConfigPath+`")`)
|
||||
|
||||
defaultKubeConfigPath := filepath.Join(defaultAirshipConfigDir, config.AirshipKubeConfig)
|
||||
flags.StringVar(
|
||||
&a.KubeConfigPath,
|
||||
clientcmd.RecommendedConfigPathFlag,
|
||||
"",
|
||||
`Path to kubeconfig associated with airshipctl configuration. (default "`+defaultKubeConfigPath+`")`)
|
||||
|
||||
a.Create = false
|
||||
}
|
||||
|
||||
// InitConfig - Initializes and loads Config if exists.
|
||||
// TODO (raliev) remove this function after completely switching to new approach of Config loading
|
||||
func (a *AirshipCTLSettings) InitConfig() {
|
||||
a.Config = config.NewConfig()
|
||||
|
||||
a.InitAirshipConfigPath()
|
||||
a.InitKubeConfigPath()
|
||||
|
||||
err := a.Config.LoadConfig(a.AirshipConfigPath, a.KubeConfigPath, a.Create)
|
||||
if err != nil {
|
||||
// Should stop airshipctl
|
||||
log.Fatal("Failed to load or initialize config: ", err)
|
||||
}
|
||||
}
|
||||
|
||||
// InitAirshipConfigPath - Initializes AirshipConfigPath variable for Config object
|
||||
// TODO (raliev) remove this function after completely switching to new approach of Config loading
|
||||
func (a *AirshipCTLSettings) InitAirshipConfigPath() {
|
||||
// The airshipConfigPath may already have been received as a command line argument
|
||||
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 != "" {
|
||||
return
|
||||
}
|
||||
|
||||
// Otherwise, we'll try putting it in the home directory
|
||||
homeDir := util.UserHomeDir()
|
||||
a.AirshipConfigPath = filepath.Join(homeDir, config.AirshipConfigDir, config.AirshipConfig)
|
||||
}
|
||||
|
||||
// InitKubeConfigPath - Initializes KubeConfigPath variable for Config object
|
||||
// TODO (raliev) remove this function after completely switching to new approach of Config loading
|
||||
func (a *AirshipCTLSettings) InitKubeConfigPath() {
|
||||
// NOTE(howell): This function will set the kubeConfigPath to the
|
||||
// default location under the airship directory unless the user
|
||||
// *explicitly* specifies a different location, either by setting the
|
||||
// ENVIRONMENT variable or by passing a command line argument.
|
||||
// This avoids messing up the user's kubeconfig if they didn't
|
||||
// explicitly want airshipctl to use it.
|
||||
|
||||
// The kubeConfigPath may already have been received as a command line argument
|
||||
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 != "" {
|
||||
return
|
||||
}
|
||||
|
||||
// Otherwise, we'll try putting it in the home directory
|
||||
homeDir := util.UserHomeDir()
|
||||
a.KubeConfigPath = filepath.Join(homeDir, config.AirshipConfigDir, config.AirshipKubeConfig)
|
||||
}
|
@ -1,138 +0,0 @@
|
||||
/*
|
||||
Copyright 2014 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package environment_test
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"opendev.org/airship/airshipctl/pkg/config"
|
||||
"opendev.org/airship/airshipctl/pkg/environment"
|
||||
"opendev.org/airship/airshipctl/testutil"
|
||||
)
|
||||
|
||||
func TestInitFlags(t *testing.T) {
|
||||
// Get the Environment
|
||||
settings := &environment.AirshipCTLSettings{}
|
||||
testCmd := &cobra.Command{}
|
||||
settings.InitFlags(testCmd)
|
||||
assert.True(t, testCmd.HasPersistentFlags())
|
||||
}
|
||||
|
||||
func TestInitConfig(t *testing.T) {
|
||||
t.Run("DefaultToHomeDirectory", func(subTest *testing.T) {
|
||||
// Set up a fake $HOME directory
|
||||
testDir, cleanup := testutil.TempDir(t, "test-home")
|
||||
defer cleanup(t)
|
||||
defer setHome(testDir)()
|
||||
|
||||
var testSettings environment.AirshipCTLSettings
|
||||
expectedAirshipConfig := filepath.Join(testDir, config.AirshipConfigDir, config.AirshipConfig)
|
||||
expectedKubeConfig := filepath.Join(testDir, config.AirshipConfigDir, config.AirshipKubeConfig)
|
||||
|
||||
testSettings.InitAirshipConfigPath()
|
||||
testSettings.InitKubeConfigPath()
|
||||
assert.Equal(t, expectedAirshipConfig, testSettings.AirshipConfigPath)
|
||||
assert.Equal(t, expectedKubeConfig, testSettings.KubeConfigPath)
|
||||
})
|
||||
|
||||
t.Run("PreferEnvToDefault", func(subTest *testing.T) {
|
||||
// Set up a fake $HOME directory
|
||||
testDir, cleanup := testutil.TempDir(t, "test-home")
|
||||
defer cleanup(t)
|
||||
defer setHome(testDir)()
|
||||
|
||||
var testSettings environment.AirshipCTLSettings
|
||||
expectedAirshipConfig := filepath.Join(testDir, "airshipEnv")
|
||||
expectedKubeConfig := filepath.Join(testDir, "kubeEnv")
|
||||
|
||||
os.Setenv(config.AirshipConfigEnv, expectedAirshipConfig)
|
||||
os.Setenv(config.AirshipKubeConfigEnv, expectedKubeConfig)
|
||||
defer os.Unsetenv(config.AirshipConfigEnv)
|
||||
defer os.Unsetenv(config.AirshipKubeConfigEnv)
|
||||
|
||||
testSettings.Create = true
|
||||
testSettings.InitConfig()
|
||||
|
||||
assert.Equal(t, expectedAirshipConfig, testSettings.AirshipConfigPath)
|
||||
assert.Equal(t, expectedKubeConfig, testSettings.KubeConfigPath)
|
||||
})
|
||||
|
||||
t.Run("PreferCmdLineArgToDefault", func(subTest *testing.T) {
|
||||
// Set up a fake $HOME directory
|
||||
testDir, cleanup := testutil.TempDir(t, "test-home")
|
||||
defer cleanup(t)
|
||||
defer setHome(testDir)()
|
||||
|
||||
expectedAirshipConfig := filepath.Join(testDir, "airshipCmdLine")
|
||||
expectedKubeConfig := filepath.Join(testDir, "kubeCmdLine")
|
||||
|
||||
testSettings := environment.AirshipCTLSettings{
|
||||
AirshipConfigPath: expectedAirshipConfig,
|
||||
KubeConfigPath: expectedKubeConfig,
|
||||
}
|
||||
|
||||
testSettings.InitAirshipConfigPath()
|
||||
testSettings.InitKubeConfigPath()
|
||||
assert.Equal(t, expectedAirshipConfig, testSettings.AirshipConfigPath)
|
||||
assert.Equal(t, expectedKubeConfig, testSettings.KubeConfigPath)
|
||||
})
|
||||
|
||||
t.Run("PreferCmdLineArgToEnv", func(subTest *testing.T) {
|
||||
// Set up a fake $HOME directory
|
||||
testDir, cleanup := testutil.TempDir(t, "test-home")
|
||||
defer cleanup(t)
|
||||
defer setHome(testDir)()
|
||||
|
||||
expectedAirshipConfig := filepath.Join(testDir, "airshipCmdLine")
|
||||
expectedKubeConfig := filepath.Join(testDir, "kubeCmdLine")
|
||||
|
||||
// set up "decoy" environment variables. These should be
|
||||
// ignored, since we're simulating passing in command line
|
||||
// arguments
|
||||
wrongAirshipConfig := filepath.Join(testDir, "wrongAirshipConfig")
|
||||
wrongKubeConfig := filepath.Join(testDir, "wrongKubeConfig")
|
||||
os.Setenv(config.AirshipConfigEnv, wrongAirshipConfig)
|
||||
os.Setenv(config.AirshipKubeConfigEnv, wrongKubeConfig)
|
||||
defer os.Unsetenv(config.AirshipConfigEnv)
|
||||
defer os.Unsetenv(config.AirshipKubeConfigEnv)
|
||||
|
||||
testSettings := environment.AirshipCTLSettings{
|
||||
AirshipConfigPath: expectedAirshipConfig,
|
||||
KubeConfigPath: expectedKubeConfig,
|
||||
}
|
||||
|
||||
testSettings.InitAirshipConfigPath()
|
||||
testSettings.InitKubeConfigPath()
|
||||
assert.Equal(t, expectedAirshipConfig, testSettings.AirshipConfigPath)
|
||||
assert.Equal(t, expectedKubeConfig, testSettings.KubeConfigPath)
|
||||
})
|
||||
}
|
||||
|
||||
// setHome sets the HOME environment variable to `path`, and returns a function
|
||||
// that can be used to reset HOME to its original value
|
||||
func setHome(path string) (resetHome func()) {
|
||||
oldHome := os.Getenv("HOME")
|
||||
os.Setenv("HOME", path)
|
||||
return func() {
|
||||
os.Setenv("HOME", oldHome)
|
||||
}
|
||||
}
|
Loading…
Reference in new issue