airshipctl/pkg/environment/settings.go
jezogwza b2af034e57 airshipctl config (replace 686508)
This implementation creates named references between an airship
config file , and a user specified or system  default kubeconfig file

airshipconfig location can be specified via an envirnment variable
or via
--airshipconf string   Path to file for airshipctl configuration.
                       (default ".airship/config")

kubeconfig has to be explicitly stated using the argument below
--kubeconfig string    Path to kubeconfig associated with airshipctl
                       configuration. (default ".airship/kubeconfig")

if the argument is not specified a default empty kubeconfig will be
used using the default ".airship/kubeconfig"

All subcommands exposed via airshipctl config will update airship
config and airship related kubeconfig
when appropriate.

This patchset adds :

- Config Struct (type)
- config cmd and pkg
- get_cluster : List a specific name cluster or
                List all clusters if no name is provided.
- set-cluster : Create or Modify an existing cluster.

Review comment fixes as of Pathset 19
- Moved core functionality from cmd to pkg
- Encapsulate cmd needs in pck in nw files cmds, cmds_types and cmds_test .
  Expectation is that other functions will need func an structs there.
- added test for GetCluster
- Added GetCluster method to config object to be used by get_cluster command
- Change ClusterNames func as per review suggestion
- Change TestEmpty Cluster to avoid pointing to non test kubecnfig by default
- Change constant AirshipConfigFilePath to AirshipConfigDir
- Renamed config_utils to utils
- Added config cmd output tests
- Changes to settings_test.go to clean after itself.
- Created new pkg/config/testdata/GoldenString for struct data comparison values to avoid confusion
- Fix small get_cluster no name issue when empty config
- Fix issue when reconciling a cluster info that only exists in airship config and not in kubeconfig

Increased coverage to: SUCCESS: Test coverage is at 84.2%,
Started to move all testdata to a single place under pkg/config for now.

Change-Id: I7aae1f15afaebc99407f7fabccecf86ab0923bc3
2019-11-05 15:42:42 +00:00

130 lines
4.1 KiB
Go

package environment
import (
"os"
"path/filepath"
"strings"
"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() {
// Raw - Empty Config object
a.SetConfig(config.NewConfig())
a.setAirshipConfigPath()
//Pass the airshipConfigPath and kubeConfig object
err := a.Config().LoadConfig(a.AirshipConfigPath(), a.setKubePathOptions())
if err != nil {
// Should stop airshipctl
log.Fatal(err)
}
}
func (a *AirshipCTLSettings) setAirshipConfigPath() {
// (1) If the airshipConfigPath was received as an argument its aleady set
if a.airshipConfigPath == "" {
// (2) If not , we can check if we got the Path via ENVIRONMNT variable,
// set the appropriate fields
a.setAirshipConfigPathFromEnv()
}
// (3) Check if the a.airshipConfigPath is empty still at this point , use the defaults
acp, home := a.replaceHomePlaceholder(a.airshipConfigPath)
a.airshipConfigPath = acp
if a.airshipConfigPath == "" {
a.airshipConfigPath = filepath.Join(home, config.AirshipConfigDir, config.AirshipConfig)
}
}
// setAirshipConfigPathFromEnv Get AIRSHIP CONFIG from an environment variable if set
func (a *AirshipCTLSettings) setAirshipConfigPathFromEnv() {
// Check if AIRSHIPCONF env variable was set
// I have the path and name for the airship config file
a.airshipConfigPath = os.Getenv(config.AirshipConfigEnv)
}
func (a *AirshipCTLSettings) setKubePathOptions() *clientcmd.PathOptions {
// USe default expectations for Kubeconfig
kubePathOptions := clientcmd.NewDefaultPathOptions()
// No need to check the Environment , since we are relying on the kubeconfig defaults
// If we did not get an explicit kubeconfig definition on airshipctl
// as far as airshipctkl is concerned will use the default expectations for the kubeconfig
// file location . This avoids messing up someones kubeconfig if they didnt explicitly want
// airshipctl to use it.
kcp, home := a.replaceHomePlaceholder(a.kubeConfigPath)
a.kubeConfigPath = kcp
if a.kubeConfigPath == "" {
a.kubeConfigPath = filepath.Join(home, config.AirshipConfigDir, config.AirshipKubeConfig)
}
// We will always rely on tha airshipctl cli args or default for where to find kubeconfig
kubePathOptions.GlobalFile = a.kubeConfigPath
kubePathOptions.GlobalFileSubpath = a.kubeConfigPath
return kubePathOptions
}
func (a *AirshipCTLSettings) replaceHomePlaceholder(configPath string) (string, string) {
home, err := os.UserHomeDir()
if err != nil {
// Use defaults under current directory
home = ""
}
if configPath == "" {
return configPath, home
}
return strings.Replace(configPath, HomePlaceholder, home, 1), home
}