dir pkg
has many libs and functions for the whole project.
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
// 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 is the main func to start minikube cluster.It mainly does following things(Note: I try to record detailed things):
shouldCacheImages := viper.GetBool(cacheImages)
k8sVersion := viper.GetString(kubernetesVersion)
clusterBootstrapper := viper.GetString(cmdcfg.Bootstrapper)
api, err := machine.NewAPIClient()
then check if default machine name has existed.
exists, err := api.Exists(cfg.GetMachineName())
check diskSize and k8sVersion, then new a machine config:
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 := 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 confg
cc, err := loadConfigFromFile(viper.GetString(cfg.MachineProfile))
setup kubernetesConfig
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
clusterConfig := cluster.Config{
MachineConfig: config,
KubernetesConfig: kubernetesConfig,
}
save cluster config
if err := saveConfig(clusterConfig); err != nil {
glog.Errorln("Error saving profile cluster configuration: ", err)
}
mountCmd := exec.Command(path, "mount", fmt.Sprintf("--v=%d", mountDebugVal), viper.GetString(mountString))
mountCmd.Env = append(os.Environ(), constants.IsMinikubeChildProcess+"=true")
output of minikube start
:
➜ 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.
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.