Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use CGroupDriver function from cruntime for kubelet #6287

Merged
merged 4 commits into from
Jan 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a nit but could put it in a const ?

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 @@ -406,6 +407,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 @@ -199,6 +226,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 @@ -208,6 +241,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 @@ -225,6 +261,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