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 Oct 4, 2018
1 parent 55afd08 commit b3053e3
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
32 changes: 31 additions & 1 deletion cmd/minikube/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,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 @@ -182,6 +198,20 @@ func runStart(cmd *cobra.Command, args []string) {
cmdutil.MaybeReportErrorAndExit(err)
}

// common config (currently none)
var cricfg = map[string]string{}
containerRuntime := viper.GetString(containerRuntime)
if cricfg := SetContainerRuntime(cricfg, containerRuntime); 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 @@ -219,7 +249,7 @@ func runStart(cmd *cobra.Command, args []string) {
APIServerIPs: apiServerIPs,
DNSDomain: viper.GetString(dnsDomain),
FeatureGates: viper.GetString(featureGates),
ContainerRuntime: viper.GetString(containerRuntime),
ContainerRuntime: containerRuntime,
NetworkPlugin: viper.GetString(networkPlugin),
ServiceCIDR: pkgutil.DefaultServiceCIDR,
ExtraOptions: extraOptions,
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 @@ -25,6 +25,7 @@ Or you can use the extended version:
```shell
$ minikube start \
--network-plugin=cni \
--container-runtime=cri-o \
--extra-config=kubelet.container-runtime=remote \
--extra-config=kubelet.container-runtime-endpoint=/var/run/crio/crio.sock \
--extra-config=kubelet.image-service-endpoint=/var/run/crio/crio.sock \
Expand All @@ -47,6 +48,7 @@ Or you can use the extended version:
```shell
$ minikube start \
--network-plugin=cni \
--container-runtime=containerd \
--extra-config=kubelet.container-runtime=remote \
--extra-config=kubelet.container-runtime-endpoint=unix:///run/containerd/containerd.sock \
--extra-config=kubelet.image-service-endpoint=unix:///run/containerd/containerd.sock \
Expand Down

0 comments on commit b3053e3

Please sign in to comment.