Skip to content

Commit

Permalink
Merge pull request #10114 from daehyeok/machinename
Browse files Browse the repository at this point in the history
refactor: migrate MachineName function to config package
  • Loading branch information
medyagh authored Jan 9, 2021
2 parents 0d56927 + 36d94a2 commit f17951c
Show file tree
Hide file tree
Showing 38 changed files with 122 additions and 125 deletions.
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 @@ -92,7 +92,7 @@ func profileStatus(p *config.Profile, api libmachine.API) string {
exit.Error(reason.GuestCpConfig, "error getting primary control plane", err)
}

host, err := machine.LoadHost(api, driver.MachineName(*p.Config, cp))
host, err := machine.LoadHost(api, config.MachineName(*p.Config, cp))
if err != nil {
klog.Warningf("error loading profiles: %v", err)
return "Unknown"
Expand Down
8 changes: 4 additions & 4 deletions cmd/minikube/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ func deleteProfile(profile *config.Profile) error {
if driver.IsKIC(profile.Config.Driver) {
out.Step(style.DeletingHost, `Deleting "{{.profile_name}}" in {{.driver_name}} ...`, out.V{"profile_name": profile.Name, "driver_name": profile.Config.Driver})
for _, n := range profile.Config.Nodes {
machineName := driver.MachineName(*profile.Config, n)
machineName := config.MachineName(*profile.Config, n)
deletePossibleKicLeftOver(machineName, profile.Config.Driver)
}
}
Expand Down Expand Up @@ -347,7 +347,7 @@ func deleteHosts(api libmachine.API, cc *config.ClusterConfig) {

if cc != nil {
for _, n := range cc.Nodes {
machineName := driver.MachineName(*cc, n)
machineName := config.MachineName(*cc, n)
if err := machine.DeleteHost(api, machineName); err != nil {
switch errors.Cause(err).(type) {
case mcnerror.ErrHostDoesNotExist:
Expand Down Expand Up @@ -412,7 +412,7 @@ func profileDeletionErr(cname string, additionalInfo string) error {

func uninstallKubernetes(api libmachine.API, cc config.ClusterConfig, n config.Node, bsName string) error {
out.Step(style.Resetting, "Uninstalling Kubernetes {{.kubernetes_version}} using {{.bootstrapper_name}} ...", out.V{"kubernetes_version": cc.KubernetesConfig.KubernetesVersion, "bootstrapper_name": bsName})
host, err := machine.LoadHost(api, driver.MachineName(cc, n))
host, err := machine.LoadHost(api, config.MachineName(cc, n))
if err != nil {
return DeletionError{Err: fmt.Errorf("unable to load host: %v", err), Errtype: MissingCluster}
}
Expand Down Expand Up @@ -500,7 +500,7 @@ func deleteProfileDirectory(profile string) {
func deleteMachineDirectories(cc *config.ClusterConfig) {
if cc != nil {
for _, n := range cc.Nodes {
machineName := driver.MachineName(*cc, n)
machineName := config.MachineName(*cc, n)
deleteProfileDirectory(machineName)
}
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/minikube/cmd/node_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package cmd

import (
"github.com/spf13/cobra"
"k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/driver"
"k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/mustload"
Expand Down Expand Up @@ -46,7 +47,7 @@ var nodeDeleteCmd = &cobra.Command{
}

if driver.IsKIC(co.Config.Driver) {
machineName := driver.MachineName(*co.Config, *n)
machineName := config.MachineName(*co.Config, *n)
deletePossibleKicLeftOver(machineName, co.Config.Driver)
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/minikube/cmd/node_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

"github.com/spf13/cobra"
"k8s.io/klog/v2"
"k8s.io/minikube/pkg/minikube/driver"
"k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/mustload"
"k8s.io/minikube/pkg/minikube/reason"
Expand All @@ -47,7 +47,7 @@ var nodeListCmd = &cobra.Command{
}

for _, n := range cc.Nodes {
machineName := driver.MachineName(*cc, n)
machineName := config.MachineName(*cc, n)
fmt.Printf("%s\t%s\n", machineName, n.IP)
}
os.Exit(0)
Expand Down
4 changes: 2 additions & 2 deletions cmd/minikube/cmd/node_start.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (

"github.com/spf13/cobra"
"github.com/spf13/viper"
"k8s.io/minikube/pkg/minikube/driver"
"k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/machine"
"k8s.io/minikube/pkg/minikube/mustload"
Expand Down Expand Up @@ -49,7 +49,7 @@ var nodeStartCmd = &cobra.Command{
exit.Error(reason.GuestNodeRetrieve, "retrieving node", err)
}

machineName := driver.MachineName(*cc, *n)
machineName := config.MachineName(*cc, *n)
if machine.IsRunning(api, machineName) {
out.Step(style.Check, "{{.name}} is already running", out.V{"name": name})
os.Exit(0)
Expand Down
4 changes: 2 additions & 2 deletions cmd/minikube/cmd/node_stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package cmd

import (
"github.com/spf13/cobra"
"k8s.io/minikube/pkg/minikube/driver"
"k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/machine"
"k8s.io/minikube/pkg/minikube/mustload"
Expand All @@ -45,7 +45,7 @@ var nodeStopCmd = &cobra.Command{
exit.Error(reason.GuestNodeRetrieve, "retrieving node", err)
}

machineName := driver.MachineName(*cc, *n)
machineName := config.MachineName(*cc, *n)

err = machine.StopHost(api, machineName)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions cmd/minikube/cmd/pause.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import (

"k8s.io/klog/v2"
"k8s.io/minikube/pkg/minikube/cluster"
"k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/cruntime"
"k8s.io/minikube/pkg/minikube/driver"
"k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/localpath"
"k8s.io/minikube/pkg/minikube/machine"
Expand Down Expand Up @@ -73,7 +73,7 @@ func runPause(cmd *cobra.Command, args []string) {

out.Step(style.Pause, "Pausing node {{.name}} ... ", out.V{"name": name})

host, err := machine.LoadHost(co.API, driver.MachineName(*co.Config, n))
host, err := machine.LoadHost(co.API, config.MachineName(*co.Config, n))
if err != nil {
exit.Error(reason.GuestLoadHost, "Error getting host", err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/minikube/cmd/ssh-key.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"path/filepath"

"github.com/spf13/cobra"
"k8s.io/minikube/pkg/minikube/driver"
"k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/localpath"
"k8s.io/minikube/pkg/minikube/mustload"
Expand All @@ -41,7 +41,7 @@ var sshKeyCmd = &cobra.Command{
exit.Error(reason.GuestNodeRetrieve, "retrieving node", err)
}

out.Ln(filepath.Join(localpath.MiniPath(), "machines", driver.MachineName(*cc, *n), "id_rsa"))
out.Ln(filepath.Join(localpath.MiniPath(), "machines", config.MachineName(*cc, *n), "id_rsa"))
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/minikube/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ func hostDriver(existing *config.ClusterConfig) string {
klog.Warningf("Unable to get control plane from existing config: %v", err)
return existing.Driver
}
machineName := driver.MachineName(*existing, cp)
machineName := config.MachineName(*existing, cp)
h, err := api.Load(machineName)
if err != nil {
klog.Warningf("api.Load failed for %s: %v", machineName, err)
Expand Down
4 changes: 2 additions & 2 deletions cmd/minikube/cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func writeStatusesAtInterval(duration time.Duration, api libmachine.API, cc *con
statuses = append(statuses, st)
} else {
for _, n := range cc.Nodes {
machineName := driver.MachineName(*cc, n)
machineName := config.MachineName(*cc, n)
klog.Infof("checking status of %s ...", machineName)
st, err := nodeStatus(api, *cc, n)
klog.Infof("%s status: %+v", machineName, st)
Expand Down Expand Up @@ -301,7 +301,7 @@ func exitCode(statuses []*Status) int {
// nodeStatus looks up the status of a node
func nodeStatus(api libmachine.API, cc config.ClusterConfig, n config.Node) (*Status, error) {
controlPlane := n.ControlPlane
name := driver.MachineName(cc, n)
name := config.MachineName(cc, n)

st := &Status{
Name: name,
Expand Down
3 changes: 1 addition & 2 deletions cmd/minikube/cmd/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"github.com/spf13/viper"
"k8s.io/klog/v2"
"k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/driver"
"k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/kubeconfig"
"k8s.io/minikube/pkg/minikube/localpath"
Expand Down Expand Up @@ -138,7 +137,7 @@ func stopProfile(profile string) int {
defer api.Close()

for _, n := range cc.Nodes {
machineName := driver.MachineName(*cc, n)
machineName := config.MachineName(*cc, n)

nonexistent := stop(api, machineName)
if !nonexistent {
Expand Down
4 changes: 2 additions & 2 deletions cmd/minikube/cmd/unpause.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import (

"k8s.io/klog/v2"
"k8s.io/minikube/pkg/minikube/cluster"
"k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/cruntime"
"k8s.io/minikube/pkg/minikube/driver"
"k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/localpath"
"k8s.io/minikube/pkg/minikube/machine"
Expand Down Expand Up @@ -71,7 +71,7 @@ var unpauseCmd = &cobra.Command{

out.Step(style.Pause, "Unpausing node {{.name}} ... ", out.V{"name": name})

machineName := driver.MachineName(*co.Config, n)
machineName := config.MachineName(*co.Config, n)
host, err := machine.LoadHost(co.API, machineName)
if err != nil {
exit.Error(reason.GuestLoadHost, "Error getting host", err)
Expand Down
6 changes: 3 additions & 3 deletions pkg/addons/addons.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ https://github.com/kubernetes/minikube/issues/7332`, out.V{"driver_name": cc.Dri
exit.Error(reason.GuestCpConfig, "Error getting primary control plane", err)
}

mName := driver.MachineName(*cc, cp)
mName := config.MachineName(*cc, cp)
host, err := machine.LoadHost(api, mName)
if err != nil || !machine.IsRunning(api, mName) {
klog.Warningf("%q is not running, setting %s=%v and skipping enablement (err=%v)", mName, addon.Name(), enable, err)
Expand Down Expand Up @@ -296,8 +296,8 @@ func enableOrDisableStorageClasses(cc *config.ClusterConfig, name string, val st
if err != nil {
return errors.Wrap(err, "getting control plane")
}
if !machine.IsRunning(api, driver.MachineName(*cc, cp)) {
klog.Warningf("%q is not running, writing %s=%v to disk and skipping enablement", driver.MachineName(*cc, cp), name, val)
if !machine.IsRunning(api, config.MachineName(*cc, cp)) {
klog.Warningf("%q is not running, writing %s=%v to disk and skipping enablement", config.MachineName(*cc, cp), name, val)
return enableOrDisableAddon(cc, name, val)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/minikube/bootstrapper/bsutil/kubelet.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,5 @@ func KubeNodeName(cc config.ClusterConfig, n config.Node) string {
hostname, _ := os.Hostname()
return hostname
}
return driver.MachineName(cc, n)
return config.MachineName(cc, n)
}
2 changes: 1 addition & 1 deletion pkg/minikube/bootstrapper/kubeadm/kubeadm.go
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ func (k *Bootstrapper) JoinCluster(cc config.ClusterConfig, n config.Node, joinC
}()

// Join the master by specifying its token
joinCmd = fmt.Sprintf("%s --node-name=%s", joinCmd, driver.MachineName(cc, n))
joinCmd = fmt.Sprintf("%s --node-name=%s", joinCmd, config.MachineName(cc, n))

join := func() error {
// reset first to clear any possibly existing state
Expand Down
3 changes: 1 addition & 2 deletions pkg/minikube/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"k8s.io/minikube/pkg/minikube/bootstrapper/kubeadm"
"k8s.io/minikube/pkg/minikube/command"
"k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/driver"
"k8s.io/minikube/pkg/minikube/machine"
)

Expand Down Expand Up @@ -61,7 +60,7 @@ func ControlPlaneBootstrapper(mAPI libmachine.API, cc *config.ClusterConfig, boo
if err != nil {
return nil, nil, errors.Wrap(err, "getting primary control plane")
}
h, err := machine.LoadHost(mAPI, driver.MachineName(*cc, cp))
h, err := machine.LoadHost(mAPI, config.MachineName(*cc, cp))
if err != nil {
return nil, nil, errors.Wrap(err, "getting control plane host")
}
Expand Down
54 changes: 54 additions & 0 deletions pkg/minikube/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,57 @@ func TestEncode(t *testing.T) {
b.Reset()
}
}

func TestMachineName(t *testing.T) {
testsCases := []struct {
ClusterConfig ClusterConfig
Want string
}{
{
ClusterConfig: ClusterConfig{Name: "minikube",
Nodes: []Node{
{
Name: "",
IP: "172.17.0.3",
Port: 8443,
KubernetesVersion: "v1.19.2",
ControlPlane: true,
Worker: true,
},
},
},
Want: "minikube",
},

{
ClusterConfig: ClusterConfig{Name: "p2",
Nodes: []Node{
{
Name: "",
IP: "172.17.0.3",
Port: 8443,
KubernetesVersion: "v1.19.2",
ControlPlane: true,
Worker: true,
},
{
Name: "m2",
IP: "172.17.0.4",
Port: 0,
KubernetesVersion: "v1.19.2",
ControlPlane: false,
Worker: true,
},
},
},
Want: "p2-m2",
},
}

for _, tc := range testsCases {
got := MachineName(tc.ClusterConfig, tc.ClusterConfig.Nodes[len(tc.ClusterConfig.Nodes)-1])
if got != tc.Want {
t.Errorf("Expected MachineName to be %q but got %q", tc.Want, got)
}
}
}
10 changes: 10 additions & 0 deletions pkg/minikube/config/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package config

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -286,3 +287,12 @@ func ProfileFolderPath(profile string, miniHome ...string) string {
}
return filepath.Join(miniPath, "profiles", profile)
}

// MachineName returns the name of the machine, as seen by the hypervisor given the cluster and node names
func MachineName(cc ClusterConfig, n Node) string {
// For single node cluster, default to back to old naming
if len(cc.Nodes) == 1 || n.ControlPlane {
return cc.Name
}
return fmt.Sprintf("%s-%s", cc.Name, n.Name)
}
10 changes: 0 additions & 10 deletions pkg/minikube/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (

"k8s.io/klog/v2"
"k8s.io/minikube/pkg/drivers/kic/oci"
"k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/registry"
)

Expand Down Expand Up @@ -305,15 +304,6 @@ func SetLibvirtURI(v string) {

}

// MachineName returns the name of the machine, as seen by the hypervisor given the cluster and node names
func MachineName(cc config.ClusterConfig, n config.Node) string {
// For single node cluster, default to back to old naming
if len(cc.Nodes) == 1 || n.ControlPlane {
return cc.Name
}
return fmt.Sprintf("%s-%s", cc.Name, n.Name)
}

// IndexFromMachineName returns the order of the container based on it is name
func IndexFromMachineName(machineName string) int {
// minikube-m02
Expand Down
Loading

0 comments on commit f17951c

Please sign in to comment.