Skip to content

Commit

Permalink
Write /etc/crictl.yaml when starting
Browse files Browse the repository at this point in the history
  • Loading branch information
afbjorklund committed Nov 21, 2018
1 parent dd2ef87 commit a997fcc
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 6 deletions.
41 changes: 35 additions & 6 deletions cmd/minikube/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,22 @@ assumes you have already installed one of the VM drivers: virtualbox/vmwarefusio
Run: runStart,
}

// SetContainerRuntime possibly sets the container runtime
func SetContainerRuntime(cfg map[string]string, runtime string) map[string]string {
switch runtime {
case "crio", "cri-o":
cfg["runtime-endpoint"] = "unix:///var/run/crio/crio.sock"
cfg["image-endpoint"] = "unix:///var/run/crio/crio.sock"
case "containerd":
cfg["runtime-endpoint"] = "unix:///run/containerd/containerd.sock"
cfg["image-endpoint"] = "unix:///run/containerd/containerd.sock"
default:
return nil
}

return cfg
}

func runStart(cmd *cobra.Command, args []string) {
if glog.V(8) {
glog.Infoln("Viper configuration:")
Expand Down Expand Up @@ -183,6 +199,20 @@ func runStart(cmd *cobra.Command, args []string) {
cmdutil.MaybeReportErrorAndExit(err)
}

// common config (currently none)
var cricfg = map[string]string{}
selectedContainerRuntime := viper.GetString(containerRuntime)
if cricfg := SetContainerRuntime(cricfg, selectedContainerRuntime); cricfg != nil {
var command string
fmt.Println("Writing crictl config...")
if command, err = cmdutil.GetCrictlConfigCommand(cricfg); err == nil {
_, err = host.RunSSHCommand(command)
}
if err != nil {
glog.Errorln("Error writing crictl config: ", err)
}
}

selectedKubernetesVersion := viper.GetString(kubernetesVersion)
if strings.Compare(selectedKubernetesVersion, "") == 0 {
selectedKubernetesVersion = constants.DefaultKubernetesVersion
Expand Down Expand Up @@ -220,7 +250,7 @@ func runStart(cmd *cobra.Command, args []string) {
APIServerIPs: apiServerIPs,
DNSDomain: viper.GetString(dnsDomain),
FeatureGates: viper.GetString(featureGates),
ContainerRuntime: viper.GetString(containerRuntime),
ContainerRuntime: selectedContainerRuntime,
CRISocket: viper.GetString(criSocket),
NetworkPlugin: viper.GetString(networkPlugin),
ServiceCIDR: pkgutil.DefaultServiceCIDR,
Expand Down Expand Up @@ -294,21 +324,20 @@ func runStart(cmd *cobra.Command, args []string) {

fmt.Println("Stopping extra container runtimes...")

containerRuntime := viper.GetString(containerRuntime)
if config.VMDriver != constants.DriverNone && containerRuntime != "" {
if config.VMDriver != constants.DriverNone && selectedContainerRuntime != "" {
if _, err := host.RunSSHCommand("sudo systemctl stop docker"); err == nil {
_, err = host.RunSSHCommand("sudo systemctl stop docker.socket")
}
if err != nil {
glog.Errorf("Error stopping docker: %v", err)
}
}
if config.VMDriver != constants.DriverNone && (containerRuntime != constants.CrioRuntime && containerRuntime != constants.Cri_oRuntime) {
if config.VMDriver != constants.DriverNone && (selectedContainerRuntime != constants.CrioRuntime && selectedContainerRuntime != constants.Cri_oRuntime) {
if _, err := host.RunSSHCommand("sudo systemctl stop crio"); err != nil {
glog.Errorf("Error stopping crio: %v", err)
}
}
if config.VMDriver != constants.DriverNone && containerRuntime != constants.RktRuntime {
if config.VMDriver != constants.DriverNone && selectedContainerRuntime != constants.RktRuntime {
if _, err := host.RunSSHCommand("sudo systemctl stop rkt-api"); err == nil {
_, err = host.RunSSHCommand("sudo systemctl stop rkt-metadata")
}
Expand All @@ -317,7 +346,7 @@ func runStart(cmd *cobra.Command, args []string) {
}
}

if config.VMDriver != constants.DriverNone && containerRuntime == constants.ContainerdRuntime {
if config.VMDriver != constants.DriverNone && selectedContainerRuntime == constants.ContainerdRuntime {
fmt.Println("Restarting containerd runtime...")
// restart containerd so that it can install all plugins
if _, err := host.RunSSHCommand("sudo systemctl restart containerd"); err != nil {
Expand Down
29 changes: 29 additions & 0 deletions cmd/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ import (
"net/http"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"strings"
"text/template"
"time"

"strconv"
Expand Down Expand Up @@ -220,6 +222,33 @@ minikube config set WantKubectlDownloadMsg false
}
}

// Return a command to run, that will generate the crictl config file
func GetCrictlConfigCommand(cfg map[string]string) (string, error) {
var (
crictlYamlTmpl = `runtime-endpoint: {{.RuntimeEndpoint}}
image-endpoint: {{.ImageEndpoint}}
`
crictlYamlPath = "/etc/crictl.yaml"
)
t, err := template.New("crictlYaml").Parse(crictlYamlTmpl)
if err != nil {
return "", err
}
opts := struct {
RuntimeEndpoint string
ImageEndpoint string
}{
RuntimeEndpoint: cfg["runtime-endpoint"],
ImageEndpoint: cfg["image-endpoint"],
}
var crictlYamlBuf bytes.Buffer
if err := t.Execute(&crictlYamlBuf, opts); err != nil {
return "", err
}

return fmt.Sprintf("sudo mkdir -p %s && printf %%s \"%s\" | sudo tee %s", path.Dir(crictlYamlPath), crictlYamlBuf.String(), crictlYamlPath), nil
}

// Ask the kernel for a free open port that is ready to use
func GetPort() (string, error) {
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
Expand Down
2 changes: 2 additions & 0 deletions docs/alternative_runtimes.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Or you can use the extended version:
```shell
$ minikube start \
--network-plugin=cni \
--container-runtime=cri-o \
--cri-socket=/var/run/crio/crio.sock \
--extra-config=kubelet.container-runtime=remote \
--extra-config=kubelet.container-runtime-endpoint=unix:///var/run/crio/crio.sock \
Expand All @@ -45,6 +46,7 @@ Or you can use the extended version:
```shell
$ minikube start \
--network-plugin=cni \
--container-runtime=containerd \
--cri-socket=/run/containerd/containerd.sock \
--extra-config=kubelet.container-runtime=remote \
--extra-config=kubelet.container-runtime-endpoint=unix:///run/containerd/containerd.sock \
Expand Down

0 comments on commit a997fcc

Please sign in to comment.