airshipctl/pkg/environment/settings.go
Ian Howell 32ef58435d Refactor management of config file paths
This change replaces the usage of the clientcmd's PathOptions with a
simpler filepath approach. The handling of the kube config file
associated with airshipctl is now entirely managed by airshipctl.

This also introduces the environment variable AIRSHIP_KUBECONFIG, which
can be used to override the default location for airship's kube config.
It can in turn be overridden by the command line argument.

Prior to this change, the kube config object was created by creating a
kubernetes client, then stripping off that client's config object. As a
side effect, this change removes the middleman, and creates the kube
config object directly from file.

Change-Id: I99ba88d50a0f45c40597a58fe4b3fdfeb7d1467d
2020-02-07 10:57:50 -06:00

127 lines
3.6 KiB
Go

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"
)
// AirshipCTLSettings is a container for all of the settings needed by airshipctl
type AirshipCTLSettings struct {
// Debug is used for verbose output
Debug bool
airshipConfigPath string
kubeConfigPath string
config *config.Config
}
// InitFlags adds the default settings flags to cmd
func (a *AirshipCTLSettings) InitFlags(cmd *cobra.Command) {
flags := cmd.PersistentFlags()
flags.BoolVar(&a.Debug, "debug", false, "enable verbose output")
flags.StringVar(&a.airshipConfigPath, config.FlagConfigFilePath,
filepath.Join(HomePlaceholder, config.AirshipConfigDir, config.AirshipConfig),
"Path to file for airshipctl configuration.")
flags.StringVar(&a.kubeConfigPath, clientcmd.RecommendedConfigPathFlag,
filepath.Join(HomePlaceholder, config.AirshipConfigDir, config.AirshipKubeConfig),
"Path to kubeconfig associated with airshipctl configuration.")
}
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.initAirshipConfigPath()
a.initKubeConfigPath()
err := a.Config().LoadConfig(a.AirshipConfigPath(), a.KubeConfigPath())
if err != nil {
// Should stop airshipctl
log.Fatal(err)
}
}
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 := userHomeDir()
a.airshipConfigPath = filepath.Join(homeDir, config.AirshipConfigDir, config.AirshipConfig)
}
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 := userHomeDir()
a.kubeConfigPath = filepath.Join(homeDir, config.AirshipConfigDir, config.AirshipKubeConfig)
}
// userHomeDir is a utility function that wraps os.UserHomeDir and returns no
// errors. If the user has no home directory, the returned value will be the
// empty string
func userHomeDir() string {
homeDir, err := os.UserHomeDir()
if err != nil {
homeDir = ""
}
return homeDir
}