Skip to content

Commit

Permalink
Merge pull request #6287 from afbjorklund/cgroup-driver
Browse files Browse the repository at this point in the history
Use CGroupDriver function from cruntime for kubelet
  • Loading branch information
medyagh authored Jan 15, 2020
2 parents cdafd16 + 708bf5c commit 6c45d66
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 2 deletions.
5 changes: 5 additions & 0 deletions pkg/minikube/bootstrapper/bsutil/kubelet.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ func NewKubeletConfig(k8s config.KubernetesConfig, r cruntime.Manager) ([]byte,
return nil, errors.Wrap(err, "generating extra configuration for kubelet")
}

cgroupDriver, err := r.CGroupDriver()
if err == nil {
extraOpts["cgroup-driver"] = cgroupDriver
}

for k, v := range r.KubeletOptions() {
extraOpts[k] = v
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/minikube/bootstrapper/bsutil/kubelet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"testing"

"github.com/pmezard/go-difflib/difflib"
"k8s.io/minikube/pkg/minikube/command"
"k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/cruntime"
Expand Down Expand Up @@ -135,7 +136,8 @@ ExecStart=/var/lib/minikube/binaries/v1.17.0/kubelet --authorization-mode=Webhoo

for _, tc := range tests {
t.Run(tc.description, func(t *testing.T) {
runtime, err := cruntime.New(cruntime.Config{Type: tc.cfg.ContainerRuntime})
runtime, err := cruntime.New(cruntime.Config{Type: tc.cfg.ContainerRuntime,
Runner: command.NewFakeCommandRunner()})
if err != nil {
t.Fatalf("runtime: %v", err)
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/minikube/bootstrapper/kubeadm/kubeadm.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,8 @@ func (k *Bootstrapper) UpdateCluster(cfg config.MachineConfig) error {
out.FailureT("Unable to load cached images: {{.error}}", out.V{"error": err})
}
}
r, err := cruntime.New(cruntime.Config{Type: cfg.ContainerRuntime, Socket: cfg.KubernetesConfig.CRISocket})
r, err := cruntime.New(cruntime.Config{Type: cfg.ContainerRuntime,
Runner: k.c, Socket: cfg.KubernetesConfig.CRISocket})
if err != nil {
return errors.Wrap(err, "runtime")
}
Expand Down
23 changes: 23 additions & 0 deletions pkg/minikube/cruntime/containerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,29 @@ func (r *Containerd) LoadImage(path string) error {
return nil
}

// CGroupDriver returns cgroup driver ("cgroupfs" or "systemd")
func (r *Containerd) CGroupDriver() (string, error) {
info, err := getCRIInfo(r.Runner)
if err != nil {
return "", err
}
if info["config"] == nil {
return "", errors.Wrapf(err, "missing config")
}
config, ok := info["config"].(map[string]interface{})
if !ok {
return "", errors.Wrapf(err, "config not map")
}
cgroupManager := "cgroupfs" // default
switch config["systemdCgroup"] {
case false:
cgroupManager = "cgroupfs"
case true:
cgroupManager = "systemd"
}
return cgroupManager, nil
}

// KubeletOptions returns kubelet options for a containerd
func (r *Containerd) KubeletOptions() map[string]string {
return map[string]string{
Expand Down
18 changes: 18 additions & 0 deletions pkg/minikube/cruntime/cri.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package cruntime
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"html/template"
"os/exec"
Expand Down Expand Up @@ -420,6 +421,23 @@ image-endpoint: unix://{{.Socket}}
return nil
}

// getCRIInfo returns current information
func getCRIInfo(cr CommandRunner) (map[string]interface{}, error) {
args := []string{"crictl", "info"}
c := exec.Command("sudo", args...)
rr, err := cr.RunCmd(c)
if err != nil {
return nil, errors.Wrap(err, "get cri info")
}
info := rr.Stdout.String()
jsonMap := make(map[string]interface{})
err = json.Unmarshal([]byte(info), &jsonMap)
if err != nil {
return nil, err
}
return jsonMap, nil
}

// generateCRIOConfig sets up /etc/crio/crio.conf
func generateCRIOConfig(cr CommandRunner, imageRepository string) error {
cPath := crioConfigFile
Expand Down
20 changes: 20 additions & 0 deletions pkg/minikube/cruntime/crio.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,26 @@ func (r *CRIO) LoadImage(path string) error {
return nil
}

// CGroupDriver returns cgroup driver ("cgroupfs" or "systemd")
func (r *CRIO) CGroupDriver() (string, error) {
c := exec.Command("crio", "config")
rr, err := r.Runner.RunCmd(c)
if err != nil {
return "", err
}
cgroupManager := "cgroupfs" // default
for _, line := range strings.Split(rr.Stdout.String(), "\n") {
if strings.HasPrefix(line, "cgroup_manager") {
// cgroup_manager = "cgroupfs"
f := strings.Split(strings.TrimSpace(line), " = ")
if len(f) == 2 {
cgroupManager = strings.Trim(f[1], "\"")
}
}
}
return cgroupManager, nil
}

// KubeletOptions returns kubelet options for a runtime.
func (r *CRIO) KubeletOptions() map[string]string {
return map[string]string{
Expand Down
2 changes: 2 additions & 0 deletions pkg/minikube/cruntime/cruntime.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ type Manager interface {
// Style is an associated StyleEnum for Name()
Style() out.StyleEnum

// CGroupDriver returns cgroup driver ("cgroupfs" or "systemd")
CGroupDriver() (string, error)
// KubeletOptions returns kubelet options for a runtime.
KubeletOptions() map[string]string
// SocketPath returns the path to the socket file for a given runtime
Expand Down
45 changes: 45 additions & 0 deletions pkg/minikube/cruntime/cruntime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,33 @@ func TestName(t *testing.T) {
}
}

func TestCGroupDriver(t *testing.T) {
var tests = []struct {
runtime string
want string
}{
{"docker", "cgroupfs"},
{"crio", "cgroupfs"},
{"containerd", "cgroupfs"},
}
for _, tc := range tests {
t.Run(tc.runtime, func(t *testing.T) {
r, err := New(Config{Type: tc.runtime, Runner: NewFakeRunner(t)})
if err != nil {
t.Fatalf("New(%s): %v", tc.runtime, err)
}

got, err := r.CGroupDriver()
if err != nil {
t.Fatalf("CGroupDriver(): %v", err)
}
if diff := cmp.Diff(tc.want, got); diff != "" {
t.Errorf("CGroupDriver(%s) returned diff (-want +got):\n%s", tc.runtime, diff)
}
})
}
}

func TestKubeletOptions(t *testing.T) {
var tests = []struct {
runtime string
Expand Down Expand Up @@ -201,6 +228,12 @@ func (f *FakeRunner) docker(args []string, _ bool) (string, error) {
if args[1] == "--format" && args[2] == "'{{.Server.Version}}'" {
return "18.06.2-ce", nil
}

case "info":

if args[1] == "--format" && args[2] == "'{{.CgroupDriver}}'" {
return "cgroupfs", nil
}
}
return "", nil
}
Expand All @@ -210,6 +243,9 @@ func (f *FakeRunner) crio(args []string, _ bool) (string, error) { //nolint (res
if args[0] == "--version" {
return "crio version 1.13.0", nil
}
if args[0] == "config" {
return "# Cgroup management implementation used for the runtime.\ncgroup_manager = \"cgroupfs\"\n", nil
}
return "", nil
}

Expand All @@ -227,6 +263,15 @@ func (f *FakeRunner) containerd(args []string, _ bool) (string, error) {
// crictl is a fake implementation of crictl
func (f *FakeRunner) crictl(args []string, _ bool) (string, error) {
switch cmd := args[0]; cmd {
case "info":
return `{
"status": {
},
"config": {
"systemdCgroup": false
},
"golang": "go1.11.13"
}`, nil
case "ps":
// crictl ps -a --name=apiserver --state=Running --quiet
if args[1] == "-a" && strings.HasPrefix(args[2], "--name") {
Expand Down
11 changes: 11 additions & 0 deletions pkg/minikube/cruntime/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,17 @@ func (r *Docker) LoadImage(path string) error {

}

// CGroupDriver returns cgroup driver ("cgroupfs" or "systemd")
func (r *Docker) CGroupDriver() (string, error) {
// Note: the server daemon has to be running, for this call to return successfully
c := exec.Command("docker", "info", "--format", "'{{.CgroupDriver}}'")
rr, err := r.Runner.RunCmd(c)
if err != nil {
return "", err
}
return strings.Split(rr.Stdout.String(), "\n")[0], nil
}

// KubeletOptions returns kubelet options for a runtime.
func (r *Docker) KubeletOptions() map[string]string {
return map[string]string{
Expand Down

0 comments on commit 6c45d66

Please sign in to comment.