rsc minikube start subcommand

dir pkg has many libs and functions for the whole project.

pkg/minikube/cluster

cluster pkg provides functions to interact with the cluster:

  • start cluster
  • stop cluster
  • remove cluster
  • get cluster status
  • get driver ip

below functions are important:

  • createHost
  • GetVMHostIP
  • CreateSSHShell
1
2
3
4
5
6
7
8
9
10
11
// DeleteHost deletes the host VM.
func DeleteHost(api libmachine.API) error {
host, err := api.Load(cfg.GetMachineName())
if err != nil {
return errors.Wrapf(err, "Error deleting host: %s", cfg.GetMachineName())
}
m := util.MultiError{}
m.Collect(host.Driver.Remove())
m.Collect(api.Remove(cfg.GetMachineName()))
return m.ToError()
}

when remove cluster, it will produce many errors, so util pkg wraps a struct MultiError to handle it.

runStart

runStart is the main func to start minikube cluster.It mainly does following things(Note: I try to record detailed things):

get basic options from flags

1
2
3
shouldCacheImages := viper.GetBool(cacheImages)
k8sVersion := viper.GetString(kubernetesVersion)
clusterBootstrapper := viper.GetString(cmdcfg.Bootstrapper)

new api client

1
api, err := machine.NewAPIClient()

then check if default machine name has existed.

1
exists, err := api.Exists(cfg.GetMachineName())

new machine config

check diskSize and k8sVersion, then new a machine config:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
config := cluster.MachineConfig{
MinikubeISO: viper.GetString(isoURL),
Memory: viper.GetInt(memory),
CPUs: viper.GetInt(cpus),
DiskSize: diskSizeMB,
VMDriver: viper.GetString(vmDriver),
XhyveDiskDriver: viper.GetString(xhyveDiskDriver),
DockerEnv: dockerEnv,
DockerOpt: dockerOpt,
InsecureRegistry: insecureRegistry,
RegistryMirror: registryMirror,
HostOnlyCIDR: viper.GetString(hostOnlyCIDR),
HypervVirtualSwitch: viper.GetString(hypervVirtualSwitch),
KvmNetwork: viper.GetString(kvmNetwork),
Downloader: pkgutil.DefaultDownloader{},
DisableDriverMounts: viper.GetBool(disableDriverMounts),
}

start cluster machine

1
2
3
4
5
6
7
8
start := func() (err error) {
host, err = cluster.StartHost(api, config)
if err != nil {
glog.Errorf("Error starting host: %s.\n\n Retrying.\n", err)
}
return err
}
err = pkgutil.RetryAfter(5, start, 2*time.Second)

load and write cluster profile config

load confg

1
cc, err := loadConfigFromFile(viper.GetString(cfg.MachineProfile))

setup kubernetesConfig

1
2
3
4
5
6
7
8
9
10
11
12
kubernetesConfig := bootstrapper.KubernetesConfig{
KubernetesVersion: selectedKubernetesVersion,
NodeIP: ip,
NodeName: cfg.GetMachineName(),
APIServerName: viper.GetString(apiServerName),
DNSDomain: viper.GetString(dnsDomain),
FeatureGates: viper.GetString(featureGates),
ContainerRuntime: viper.GetString(containerRuntime),
NetworkPlugin: viper.GetString(networkPlugin),
ExtraOptions: extraOptions,
ShouldLoadCachedImages: shouldCacheImages,
}

setup cluster config

1
2
3
4
clusterConfig := cluster.Config{
MachineConfig: config,
KubernetesConfig: kubernetesConfig,
}

save cluster config

1
2
3
if err := saveConfig(clusterConfig); err != nil {
glog.Errorln("Error saving profile cluster configuration: ", err)
}

mount path

1
2
mountCmd := exec.Command(path, "mount", fmt.Sprintf("--v=%d", mountDebugVal), viper.GetString(mountString))
mountCmd.Env = append(os.Environ(), constants.IsMinikubeChildProcess+"=true")

demo output

output of minikube start:

1
2
3
4
5
6
7
8
9
10
➜  k8swp minikube start
Starting local Kubernetes v1.7.5 cluster...
Starting VM...
Getting VM IP address...
Moving files into cluster...
Setting up certs...
Connecting to cluster...
Setting up kubeconfig...
Starting cluster components...
Kubectl is now configured to use the cluster.

Conclusion

There are many pkgs should learn:

  • k8s.io/minikube/pkg/minikube
  • k8s.io/minikube/pkg/util
  • k8s.io/minikube/pkg/version
  • github.com/docker/machine

start subcommand uses many pkgs, so it does not have many codes. Reader should read more about the pkgs start uses.

AdolphLWQ wechat
subscribe to my blog by scanning my public wechat account
Offer me coffe