Skip to content

Commit

Permalink
Refactor config.Config to prepare for multinode (#5889)
Browse files Browse the repository at this point in the history
* Refactor config.Config to prepare for multinode

* merge conflict
  • Loading branch information
sharifelgamal authored Nov 12, 2019
1 parent fb5430f commit c03aee8
Show file tree
Hide file tree
Showing 28 changed files with 185 additions and 190 deletions.
2 changes: 1 addition & 1 deletion cmd/minikube/cmd/config/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ var ProfileCmd = &cobra.Command{
out.ErrT(out.Sad, `Error loading profile config: {{.error}}`, out.V{"error": err})
}
if err == nil {
if cc.MachineConfig.KeepContext {
if cc.KeepContext {
out.SuccessT("Skipped switching kubectl context for {{.profile_name}} because --keep-context was set.", out.V{"profile_name": profile})
out.SuccessT("To connect to this cluster, use: kubectl --context={{.profile_name}}", out.V{"profile_name": profile})
} else {
Expand Down
2 changes: 1 addition & 1 deletion cmd/minikube/cmd/config/profile_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ var printProfilesTable = func() {
exit.UsageT("No minikube profile was found. You can create one using `minikube start`.")
}
for _, p := range validProfiles {
validData = append(validData, []string{p.Name, p.Config.MachineConfig.VMDriver, p.Config.KubernetesConfig.NodeIP, strconv.Itoa(p.Config.KubernetesConfig.NodePort), p.Config.KubernetesConfig.KubernetesVersion})
validData = append(validData, []string{p.Name, p.Config[0].VMDriver, p.Config[0].KubernetesConfig.NodeIP, strconv.Itoa(p.Config[0].KubernetesConfig.NodePort), p.Config[0].KubernetesConfig.KubernetesVersion})
}

table.AppendBulk(validData)
Expand Down
2 changes: 1 addition & 1 deletion cmd/minikube/cmd/config/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func EnableOrDisableAddon(name string, val string) error {
exit.WithCodeT(exit.Data, "Unable to load config: {{.error}}", out.V{"error": err})
}

host, err := cluster.CheckIfHostExistsAndLoad(api, cfg.MachineConfig.Name)
host, err := cluster.CheckIfHostExistsAndLoad(api, cfg.Name)
if err != nil {
return errors.Wrap(err, "getting host")
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/minikube/cmd/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ var dashboardCmd = &cobra.Command{
exit.WithError("Error getting client", err)
}

if _, err = api.Load(cc.MachineConfig.Name); err != nil {
if _, err = api.Load(cc.Name); err != nil {
switch err := errors.Cause(err).(type) {
case mcnerror.ErrHostDoesNotExist:
exit.WithCodeT(exit.Unavailable, "{{.name}} cluster does not exist", out.V{"name": cc.MachineConfig.Name})
exit.WithCodeT(exit.Unavailable, "{{.name}} cluster does not exist", out.V{"name": cc.Name})
default:
exit.WithError("Error getting cluster", err)
}
Expand Down Expand Up @@ -119,7 +119,7 @@ var dashboardCmd = &cobra.Command{
}

out.ErrT(out.Launch, "Launching proxy ...")
p, hostPort, err := kubectlProxy(kubectl, cc.MachineConfig.Name)
p, hostPort, err := kubectlProxy(kubectl, cc.Name)
if err != nil {
exit.WithError("kubectl proxy", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/minikube/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func deleteProfile(profile *pkg_config.Profile) error {
return DeletionError{Err: delErr, Errtype: MissingProfile}
}

if err == nil && driver.BareMetal(cc.MachineConfig.VMDriver) {
if err == nil && driver.BareMetal(cc.VMDriver) {
if err := uninstallKubernetes(api, cc.KubernetesConfig, viper.GetString(cmdcfg.Bootstrapper)); err != nil {
deletionError, ok := err.(DeletionError)
if ok {
Expand Down
4 changes: 2 additions & 2 deletions cmd/minikube/cmd/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,14 +344,14 @@ var dockerEnvCmd = &cobra.Command{
if err != nil {
exit.WithError("Error getting config", err)
}
host, err := cluster.CheckIfHostExistsAndLoad(api, cc.MachineConfig.Name)
host, err := cluster.CheckIfHostExistsAndLoad(api, cc.Name)
if err != nil {
exit.WithError("Error getting host", err)
}
if host.Driver.DriverName() == driver.None {
exit.UsageT(`'none' driver does not support 'minikube docker-env' command`)
}
hostSt, err := cluster.GetHostStatus(api, cc.MachineConfig.Name)
hostSt, err := cluster.GetHostStatus(api, cc.Name)
if err != nil {
exit.WithError("Error getting host status", err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/minikube/cmd/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ var ipCmd = &cobra.Command{
if err != nil {
exit.WithError("Error getting config", err)
}
host, err := api.Load(cc.MachineConfig.Name)
host, err := api.Load(cc.Name)
if err != nil {
switch err := errors.Cause(err).(type) {
case mcnerror.ErrHostDoesNotExist:
exit.WithCodeT(exit.NoInput, `"{{.profile_name}}" host does not exist, unable to show an IP`, out.V{"profile_name": cc.MachineConfig.Name})
exit.WithCodeT(exit.NoInput, `"{{.profile_name}}" host does not exist, unable to show an IP`, out.V{"profile_name": cc.Name})
default:
exit.WithError("Error getting host", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/minikube/cmd/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ var logsCmd = &cobra.Command{
}
defer api.Close()

h, err := api.Load(cfg.MachineConfig.Name)
h, err := api.Load(cfg.Name)
if err != nil {
exit.WithError("api load", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/minikube/cmd/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ var mountCmd = &cobra.Command{
if err != nil {
exit.WithError("Error getting config", err)
}
host, err := api.Load(cc.MachineConfig.Name)
host, err := api.Load(cc.Name)

if err != nil {
exit.WithError("Error loading api", err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/minikube/cmd/ssh-key.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ var sshKeyCmd = &cobra.Command{
if err != nil {
exit.WithError("Getting machine config failed", err)
}
out.Ln(filepath.Join(localpath.MiniPath(), "machines", cc.MachineConfig.Name, "id_rsa"))
out.Ln(filepath.Join(localpath.MiniPath(), "machines", cc.Name, "id_rsa"))
},
}
2 changes: 1 addition & 1 deletion cmd/minikube/cmd/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var sshCmd = &cobra.Command{
if err != nil {
exit.WithError("Error getting config", err)
}
host, err := cluster.CheckIfHostExistsAndLoad(api, cc.MachineConfig.Name)
host, err := cluster.CheckIfHostExistsAndLoad(api, cc.Name)
if err != nil {
exit.WithError("Error getting host", err)
}
Expand Down
101 changes: 49 additions & 52 deletions cmd/minikube/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ func runStart(cmd *cobra.Command, args []string) {
}

if !driver.BareMetal(driverName) {
if err := cluster.CacheISO(config.MachineConfig); err != nil {
if err := cluster.CacheISO(config); err != nil {
exit.WithError("Failed to cache ISO", err)
}
}
Expand Down Expand Up @@ -342,7 +342,7 @@ func runStart(cmd *cobra.Command, args []string) {
waitCacheImages(&cacheGroup)

// Must be written before bootstrap, otherwise health checks may flake due to stale IP
kubeconfig, err := setupKubeconfig(host, &config)
kubeconfig, err := setupKubeconfig(host, &config, config.Name)
if err != nil {
exit.WithError("Failed to setup kubeconfig", err)
}
Expand All @@ -366,7 +366,7 @@ func runStart(cmd *cobra.Command, args []string) {
prepareNone()
}
waitCluster(bs, config)
if err := showKubectlInfo(kubeconfig, k8sVersion, config.MachineConfig.Name); err != nil {
if err := showKubectlInfo(kubeconfig, k8sVersion, config.Name); err != nil {
glog.Errorf("kubectl info: %v", err)
}
}
Expand All @@ -389,7 +389,7 @@ func enableAddons() {
}
}

func waitCluster(bs bootstrapper.Bootstrapper, config cfg.Config) {
func waitCluster(bs bootstrapper.Bootstrapper, config cfg.MachineConfig) {
var podsToWaitFor []string

if !viper.GetBool(waitUntilHealthy) {
Expand Down Expand Up @@ -427,7 +427,7 @@ func displayEnviron(env []string) {
}
}

func setupKubeconfig(h *host.Host, c *cfg.Config) (*kubeconfig.Settings, error) {
func setupKubeconfig(h *host.Host, c *cfg.MachineConfig, clusterName string) (*kubeconfig.Settings, error) {
addr, err := h.Driver.GetURL()
if err != nil {
exit.WithError("Failed to get driver URL", err)
Expand All @@ -439,7 +439,7 @@ func setupKubeconfig(h *host.Host, c *cfg.Config) (*kubeconfig.Settings, error)
}

kcs := &kubeconfig.Settings{
ClusterName: c.MachineConfig.Name,
ClusterName: clusterName,
ClusterServerAddress: addr,
ClientCertificate: localpath.MakeMiniPath("client.crt"),
ClientKey: localpath.MakeMiniPath("client.key"),
Expand Down Expand Up @@ -472,12 +472,12 @@ func handleDownloadOnly(cacheGroup *errgroup.Group, k8sVersion string) {

}

func startMachine(config *cfg.Config) (runner command.Runner, preExists bool, machineAPI libmachine.API, host *host.Host) {
func startMachine(config *cfg.MachineConfig) (runner command.Runner, preExists bool, machineAPI libmachine.API, host *host.Host) {
m, err := machine.NewAPIClient()
if err != nil {
exit.WithError("Failed to get machine client", err)
}
host, preExists = startHost(m, config.MachineConfig)
host, preExists = startHost(m, *config)
runner, err = machine.CommandRunner(host)
if err != nil {
exit.WithError("Failed to get command runner", err)
Expand Down Expand Up @@ -553,8 +553,7 @@ func showKubectlInfo(kcs *kubeconfig.Settings, k8sVersion string, machineName st
return nil
}

// selectDriver returns which driver to choose based on flags, existing configs, and hypervisor detection
func selectDriver(existing *cfg.Config) string {
func selectDriver(existing *cfg.MachineConfig) string {
name := viper.GetString("vm-driver")
glog.Infof("selectDriver: flag=%q, old=%v", name, existing)
options := driver.Choices()
Expand All @@ -567,8 +566,8 @@ func selectDriver(existing *cfg.Config) string {

// By default, the driver is whatever we used last time
if existing != nil {
pick, alts := driver.Choose(existing.MachineConfig.VMDriver, options)
out.T(out.Sparkle, `Selecting '{{.driver}}' driver from existing profile (alternates: {{.alternates}})`, out.V{"driver": existing.MachineConfig.VMDriver, "alternates": alts})
pick, alts := driver.Choose(existing.VMDriver, options)
out.T(out.Sparkle, `Selecting '{{.driver}}' driver from existing profile (alternates: {{.alternates}})`, out.V{"driver": existing.VMDriver, "alternates": alts})
return pick.Name
}

Expand All @@ -585,7 +584,7 @@ func selectDriver(existing *cfg.Config) string {
}

// validateDriver validates that the selected driver appears sane, exits if not
func validateDriver(name string, existing *cfg.Config) {
func validateDriver(name string, existing *cfg.MachineConfig) {
glog.Infof("validating driver %q against %+v", name, existing)
if !driver.Supported(name) {
exit.WithCodeT(exit.Unavailable, "The driver '{{.driver}}' is not supported on {{.os}}", out.V{"driver": name, "os": runtime.GOOS})
Expand All @@ -605,7 +604,7 @@ func validateDriver(name string, existing *cfg.Config) {
out.ErrLn("")

if !st.Installed && !viper.GetBool(force) {
if existing != nil && name == existing.MachineConfig.VMDriver {
if existing != nil && name == existing.VMDriver {
exit.WithCodeT(exit.Unavailable, "{{.driver}} does not appear to be installed, but is specified by an existing profile. Please run 'minikube delete' or install {{.driver}}", out.V{"driver": name})
}
exit.WithCodeT(exit.Unavailable, "{{.driver}} does not appear to be installed", out.V{"driver": name})
Expand Down Expand Up @@ -845,10 +844,10 @@ func waitCacheImages(g *errgroup.Group) {
}

// generateCfgFromFlags generates cfg.Config based on flags and supplied arguments
func generateCfgFromFlags(cmd *cobra.Command, k8sVersion string, drvName string) (cfg.Config, error) {
func generateCfgFromFlags(cmd *cobra.Command, k8sVersion string, drvName string) (cfg.MachineConfig, error) {
r, err := cruntime.New(cruntime.Config{Type: viper.GetString(containerRuntime)})
if err != nil {
return cfg.Config{}, err
return cfg.MachineConfig{}, err
}

// Pick good default values for --network-plugin and --enable-default-cni based on runtime.
Expand Down Expand Up @@ -889,38 +888,36 @@ func generateCfgFromFlags(cmd *cobra.Command, k8sVersion string, drvName string)
out.T(out.SuccessType, "Using image repository {{.name}}", out.V{"name": repository})
}

cfg := cfg.Config{
MachineConfig: cfg.MachineConfig{
Name: viper.GetString(cfg.MachineProfile),
KeepContext: viper.GetBool(keepContext),
EmbedCerts: viper.GetBool(embedCerts),
MinikubeISO: viper.GetString(isoURL),
Memory: pkgutil.CalculateSizeInMB(viper.GetString(memory)),
CPUs: viper.GetInt(cpus),
DiskSize: pkgutil.CalculateSizeInMB(viper.GetString(humanReadableDiskSize)),
VMDriver: drvName,
ContainerRuntime: viper.GetString(containerRuntime),
HyperkitVpnKitSock: viper.GetString(vpnkitSock),
HyperkitVSockPorts: viper.GetStringSlice(vsockPorts),
NFSShare: viper.GetStringSlice(nfsShare),
NFSSharesRoot: viper.GetString(nfsSharesRoot),
DockerEnv: dockerEnv,
DockerOpt: dockerOpt,
InsecureRegistry: insecureRegistry,
RegistryMirror: registryMirror,
HostOnlyCIDR: viper.GetString(hostOnlyCIDR),
HypervVirtualSwitch: viper.GetString(hypervVirtualSwitch),
KVMNetwork: viper.GetString(kvmNetwork),
KVMQemuURI: viper.GetString(kvmQemuURI),
KVMGPU: viper.GetBool(kvmGPU),
KVMHidden: viper.GetBool(kvmHidden),
Downloader: pkgutil.DefaultDownloader{},
DisableDriverMounts: viper.GetBool(disableDriverMounts),
UUID: viper.GetString(uuid),
NoVTXCheck: viper.GetBool(noVTXCheck),
DNSProxy: viper.GetBool(dnsProxy),
HostDNSResolver: viper.GetBool(hostDNSResolver),
},
cfg := cfg.MachineConfig{
Name: viper.GetString(cfg.MachineProfile),
KeepContext: viper.GetBool(keepContext),
EmbedCerts: viper.GetBool(embedCerts),
MinikubeISO: viper.GetString(isoURL),
Memory: pkgutil.CalculateSizeInMB(viper.GetString(memory)),
CPUs: viper.GetInt(cpus),
DiskSize: pkgutil.CalculateSizeInMB(viper.GetString(humanReadableDiskSize)),
VMDriver: drvName,
ContainerRuntime: viper.GetString(containerRuntime),
HyperkitVpnKitSock: viper.GetString(vpnkitSock),
HyperkitVSockPorts: viper.GetStringSlice(vsockPorts),
NFSShare: viper.GetStringSlice(nfsShare),
NFSSharesRoot: viper.GetString(nfsSharesRoot),
DockerEnv: dockerEnv,
DockerOpt: dockerOpt,
InsecureRegistry: insecureRegistry,
RegistryMirror: registryMirror,
HostOnlyCIDR: viper.GetString(hostOnlyCIDR),
HypervVirtualSwitch: viper.GetString(hypervVirtualSwitch),
KVMNetwork: viper.GetString(kvmNetwork),
KVMQemuURI: viper.GetString(kvmQemuURI),
KVMGPU: viper.GetBool(kvmGPU),
KVMHidden: viper.GetBool(kvmHidden),
Downloader: pkgutil.DefaultDownloader{},
DisableDriverMounts: viper.GetBool(disableDriverMounts),
UUID: viper.GetString(uuid),
NoVTXCheck: viper.GetBool(noVTXCheck),
DNSProxy: viper.GetBool(dnsProxy),
HostDNSResolver: viper.GetBool(hostDNSResolver),
KubernetesConfig: cfg.KubernetesConfig{
KubernetesVersion: k8sVersion,
NodePort: viper.GetInt(apiServerPort),
Expand Down Expand Up @@ -1112,7 +1109,7 @@ func tryRegistry(r command.Runner) {
}

// getKubernetesVersion ensures that the requested version is reasonable
func getKubernetesVersion(old *cfg.Config) (string, bool) {
func getKubernetesVersion(old *cfg.MachineConfig) (string, bool) {
rawVersion := viper.GetString(kubernetesVersion)
isUpgrade := false
if rawVersion == "" {
Expand Down Expand Up @@ -1151,8 +1148,8 @@ func getKubernetesVersion(old *cfg.Config) (string, bool) {
if nvs.LT(ovs) {
nv = version.VersionPrefix + ovs.String()
profileArg := ""
if old.MachineConfig.Name != constants.DefaultMachineName {
profileArg = fmt.Sprintf("-p %s", old.MachineConfig.Name)
if old.Name != constants.DefaultMachineName {
profileArg = fmt.Sprintf("-p %s", old.Name)
}
exit.WithCodeT(exit.Config, `Error: You have selected Kubernetes v{{.new}}, but the existing cluster for your profile is running Kubernetes v{{.old}}. Non-destructive downgrades are not supported, but you can proceed by performing one of the following options:
Expand Down Expand Up @@ -1260,6 +1257,6 @@ func configureMounts() {
}

// saveConfig saves profile cluster configuration in $MINIKUBE_HOME/profiles/<profilename>/config.json
func saveConfig(clusterCfg *cfg.Config) error {
func saveConfig(clusterCfg *cfg.MachineConfig) error {
return cfg.CreateProfile(viper.GetString(cfg.MachineProfile), clusterCfg)
}
2 changes: 1 addition & 1 deletion cmd/minikube/cmd/start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TestGenerateCfgFromFlagsHTTPProxyHandling(t *testing.T) {
t.Fatalf("Got unexpected error %v during config generation", err)
}
// ignored proxy should not be in config
for _, v := range config.MachineConfig.DockerEnv {
for _, v := range config.DockerEnv {
if v == test.proxy && test.proxyIgnored {
t.Fatalf("Value %v not expected in dockerEnv but occurred", v)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/minikube/cmd/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ var tunnelCmd = &cobra.Command{
if err != nil {
exit.WithError("Error getting config", err)
}
done, err := manager.StartTunnel(ctx, cc.MachineConfig.Name, api, config.DefaultLoader, clientset.CoreV1())
done, err := manager.StartTunnel(ctx, cc.Name, api, config.DefaultLoader, clientset.CoreV1())
if err != nil {
exit.WithError("error starting tunnel", err)
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/minikube/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,23 +124,23 @@ func encode(w io.Writer, m MinikubeConfig) error {
}

// Load loads the kubernetes and machine config for the current machine
func Load() (*Config, error) {
func Load() (*MachineConfig, error) {
machine := viper.GetString(MachineProfile)
return DefaultLoader.LoadConfigFromFile(machine)
}

// Loader loads the kubernetes and machine config based on the machine profile name
type Loader interface {
LoadConfigFromFile(profile string, miniHome ...string) (*Config, error)
LoadConfigFromFile(profile string, miniHome ...string) (*MachineConfig, error)
}

type simpleConfigLoader struct{}

// DefaultLoader is the default config loader
var DefaultLoader Loader = &simpleConfigLoader{}

func (c *simpleConfigLoader) LoadConfigFromFile(profileName string, miniHome ...string) (*Config, error) {
var cc Config
func (c *simpleConfigLoader) LoadConfigFromFile(profileName string, miniHome ...string) (*MachineConfig, error) {
var cc MachineConfig
// Move to profile package
path := profileFilePath(profileName, miniHome...)

Expand Down
Loading

0 comments on commit c03aee8

Please sign in to comment.