diff --git a/pkg/minikube/bootstrapper/kubeadm/kubeadm_test.go b/pkg/minikube/bootstrapper/kubeadm/kubeadm_test.go index f2aa743380b8..cc61f0197985 100644 --- a/pkg/minikube/bootstrapper/kubeadm/kubeadm_test.go +++ b/pkg/minikube/bootstrapper/kubeadm/kubeadm_test.go @@ -25,6 +25,96 @@ import ( "k8s.io/minikube/pkg/util" ) +func TestGenerateKubeletConfig(t *testing.T) { + tests := []struct { + description string + cfg config.KubernetesConfig + expectedCfg string + shouldErr bool + }{ + { + description: "docker runtime", + cfg: config.KubernetesConfig{ + NodeIP: "192.168.1.100", + KubernetesVersion: "v1.1.0", + NodeName: "minikube", + ContainerRuntime: "docker", + }, + expectedCfg: ` +[Unit] +Wants=docker.socket + +[Service] +ExecStart= +ExecStart=/usr/bin/kubelet --allow-privileged=true --authorization-mode=Webhook --bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.conf --cadvisor-port=0 --cgroup-driver=cgroupfs --client-ca-file=/var/lib/minikube/certs/ca.crt --cluster-dns=10.96.0.10 --cluster-domain=cluster.local --container-runtime=docker --hostname-override=minikube --kubeconfig=/etc/kubernetes/kubelet.conf --pod-manifest-path=/etc/kubernetes/manifests --require-kubeconfig=true + +[Install] +`, + }, + { + description: "cri runtime", + cfg: config.KubernetesConfig{ + NodeIP: "192.168.1.100", + KubernetesVersion: "v1.1.0", + NodeName: "minikube", + ContainerRuntime: "cri-o", + }, + expectedCfg: ` +[Unit] +Wants=crio.service + +[Service] +ExecStart= +ExecStart=/usr/bin/kubelet --allow-privileged=true --authorization-mode=Webhook --bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.conf --cadvisor-port=0 --cgroup-driver=cgroupfs --client-ca-file=/var/lib/minikube/certs/ca.crt --cluster-dns=10.96.0.10 --cluster-domain=cluster.local --container-runtime=remote --container-runtime-endpoint=/var/run/crio/crio.sock --hostname-override=minikube --image-service-endpoint=/var/run/crio/crio.sock --kubeconfig=/etc/kubernetes/kubelet.conf --pod-manifest-path=/etc/kubernetes/manifests --require-kubeconfig=true --runtime-request-timeout=15m + +[Install] +`, + }, + { + description: "docker runtime with custom image repository", + cfg: config.KubernetesConfig{ + NodeIP: "192.168.1.100", + KubernetesVersion: "v1.1.0", + NodeName: "minikube", + ContainerRuntime: "docker", + ImageRepository: "docker-proxy-image.io/google_containers", + }, + expectedCfg: ` +[Unit] +Wants=docker.socket + +[Service] +ExecStart= +ExecStart=/usr/bin/kubelet --allow-privileged=true --authorization-mode=Webhook --bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.conf --cadvisor-port=0 --cgroup-driver=cgroupfs --client-ca-file=/var/lib/minikube/certs/ca.crt --cluster-dns=10.96.0.10 --cluster-domain=cluster.local --container-runtime=docker --hostname-override=minikube --kubeconfig=/etc/kubernetes/kubelet.conf --pod-infra-container-image=docker-proxy-image.io/google_containers//pause-amd64:3.0 --pod-manifest-path=/etc/kubernetes/manifests --require-kubeconfig=true + +[Install] +`, + }, + } + + for _, test := range tests { + t.Run(test.description, func(t *testing.T) { + runtime, err := cruntime.New(cruntime.Config{Type: test.cfg.ContainerRuntime}) + if err != nil { + t.Fatalf("runtime: %v", err) + } + + actualCfg, err := NewKubeletConfig(test.cfg, runtime) + if err != nil && !test.shouldErr { + t.Errorf("got unexpected error generating config: %v", err) + return + } + if err == nil && test.shouldErr { + t.Errorf("expected error but got none, config: %s", actualCfg) + return + } + if diff := cmp.Diff(test.expectedCfg, actualCfg); diff != "" { + t.Errorf("actual config does not match expected. (-want +got)\n%s", diff) + } + }) + } +} + func TestGenerateConfig(t *testing.T) { tests := []struct { description string diff --git a/pkg/minikube/bootstrapper/kubeadm/versions.go b/pkg/minikube/bootstrapper/kubeadm/versions.go index fc2201cab2b6..ea7603143831 100644 --- a/pkg/minikube/bootstrapper/kubeadm/versions.go +++ b/pkg/minikube/bootstrapper/kubeadm/versions.go @@ -164,8 +164,13 @@ func ParseKubernetesVersion(version string) (semver.Version, error) { func convertToFlags(opts map[string]string) string { var flags []string - for k, v := range opts { - flags = append(flags, fmt.Sprintf("--%s=%s", k, v)) + var keys []string + for k := range opts { + keys = append(keys, k) + } + sort.Strings(keys) + for _, k := range keys { + flags = append(flags, fmt.Sprintf("--%s=%s", k, opts[k])) } return strings.Join(flags, " ") }