Repository: kubermatic/machine-controller
Pull request #882 (2021-01-25 14:04:17)

Fix kubelet systemd unit service dependency on the correct container runtime
**What this PR does / why we need it**:

**Which issue(s) this PR fixes** *(optional, in `fixes #<issue number>` format, will close the issue(s) when PR gets merged)*:
Fixes #

**Special notes for your reviewer**:

**Optional Release Note**:
<!--
Write your release note:
1. Enter your extended release note in the below block. If the PR requires additional action from users switching to the new release, include the string "action required".
2. If no release note is required, just write "NONE".
-->
```release-note
Fix kubelet systemd unit service dependency on the correct container runtime
```


kubermatic-bot: @kron4eg: Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our [release note process](https://git.k8s.io/community/contributors/guide/release-notes.md) to remove it.

<details>

Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md).  If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository.
</details>
kubermatic-bot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/kubermatic/machine-controller/pull/882#" title="Author self-approved">kron4eg</a>*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=kubermatic%2Fmachine-controller).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[OWNERS](https://github.com/kubermatic/machine-controller/blob/master/OWNERS)~~ [kron4eg]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->
kubermatic-bot: LGTM label has been added.  <details>Git tree hash: e13cae27af7ae2d059c9c38379f25dc128cfc764</details>

Patch:
diff --git a/pkg/userdata/centos/provider.go b/pkg/userdata/centos/provider.go
index cd9c13c1..0c7d8233 100644
--- a/pkg/userdata/centos/provider.go
+++ b/pkg/userdata/centos/provider.go
@@ -244,7 +244,7 @@ write_files:
 
 - path: "/etc/systemd/system/kubelet.service"
   content: |
-{{ kubeletSystemdUnit .KubeletVersion .CloudProviderName .MachineSpec.Name .DNSIPs .ExternalCloudProvider .PauseImage .MachineSpec.Taints .ExtraKubeletFlags | indent 4 }}
+{{ kubeletSystemdUnit .ContainerRuntime.String .KubeletVersion .CloudProviderName .MachineSpec.Name .DNSIPs .ExternalCloudProvider .PauseImage .MachineSpec.Taints .ExtraKubeletFlags | indent 4 }}
 
 - path: "/etc/kubernetes/cloud-config"
   permissions: "0600"
diff --git a/pkg/userdata/helper/kubelet.go b/pkg/userdata/helper/kubelet.go
index 0426b08b..66b2f60a 100644
--- a/pkg/userdata/helper/kubelet.go
+++ b/pkg/userdata/helper/kubelet.go
@@ -55,8 +55,8 @@ const (
 --node-ip ${KUBELET_NODE_IP}`
 
 	kubeletSystemdUnitTpl = `[Unit]
-After=docker.service
-Requires=docker.service
+After={{ .ContainerRuntime }}.service
+Requires={{ .ContainerRuntime }}.service
 
 Description=kubelet: The Kubernetes Node Agent
 Documentation=https://kubernetes.io/docs/home/
@@ -96,13 +96,14 @@ func CloudProviderFlags(cpName string, external bool) (string, error) {
 }
 
 // KubeletSystemdUnit returns the systemd unit for the kubelet
-func KubeletSystemdUnit(kubeletVersion, cloudProvider, hostname string, dnsIPs []net.IP, external bool, pauseImage string, initialTaints []corev1.Taint, extraKubeletFlags []string) (string, error) {
+func KubeletSystemdUnit(containerRuntime, kubeletVersion, cloudProvider, hostname string, dnsIPs []net.IP, external bool, pauseImage string, initialTaints []corev1.Taint, extraKubeletFlags []string) (string, error) {
 	tmpl, err := template.New("kubelet-systemd-unit").Funcs(TxtFuncMap()).Parse(kubeletSystemdUnitTpl)
 	if err != nil {
 		return "", fmt.Errorf("failed to parse kubelet-systemd-unit template: %v", err)
 	}
 
 	data := struct {
+		ContainerRuntime  string
 		KubeletVersion    string
 		CloudProvider     string
 		Hostname          string
@@ -112,6 +113,7 @@ func KubeletSystemdUnit(kubeletVersion, cloudProvider, hostname string, dnsIPs [
 		InitialTaints     []corev1.Taint
 		ExtraKubeletFlags []string
 	}{
+		ContainerRuntime:  containerRuntime,
 		KubeletVersion:    kubeletVersion,
 		CloudProvider:     cloudProvider,
 		Hostname:          hostname,
diff --git a/pkg/userdata/rhel/provider.go b/pkg/userdata/rhel/provider.go
index f97abf38..a71f888b 100644
--- a/pkg/userdata/rhel/provider.go
+++ b/pkg/userdata/rhel/provider.go
@@ -244,7 +244,7 @@ write_files:
 
 - path: "/etc/systemd/system/kubelet.service"
   content: |
-{{ kubeletSystemdUnit .KubeletVersion .CloudProviderName .MachineSpec.Name .DNSIPs .ExternalCloudProvider .PauseImage .MachineSpec.Taints .ExtraKubeletFlags | indent 4 }}
+{{ kubeletSystemdUnit .ContainerRuntime.String .KubeletVersion .CloudProviderName .MachineSpec.Name .DNSIPs .ExternalCloudProvider .PauseImage .MachineSpec.Taints .ExtraKubeletFlags | indent 4 }}
 
 - path: "/etc/kubernetes/cloud-config"
   permissions: "0600"
diff --git a/pkg/userdata/sles/provider.go b/pkg/userdata/sles/provider.go
index ff01677c..8e46874b 100644
--- a/pkg/userdata/sles/provider.go
+++ b/pkg/userdata/sles/provider.go
@@ -202,7 +202,7 @@ write_files:
 
 - path: "/etc/systemd/system/kubelet.service"
   content: |
-{{ kubeletSystemdUnit .KubeletVersion .CloudProviderName .MachineSpec.Name .DNSIPs .ExternalCloudProvider .PauseImage .MachineSpec.Taints .ExtraKubeletFlags | indent 4 }}
+{{ kubeletSystemdUnit .ContainerRuntime.String .KubeletVersion .CloudProviderName .MachineSpec.Name .DNSIPs .ExternalCloudProvider .PauseImage .MachineSpec.Taints .ExtraKubeletFlags | indent 4 }}
 
 - path: "/etc/systemd/system/kubelet.service.d/extras.conf"
   content: |
diff --git a/pkg/userdata/ubuntu/provider.go b/pkg/userdata/ubuntu/provider.go
index 3779cdcf..a4e021f3 100644
--- a/pkg/userdata/ubuntu/provider.go
+++ b/pkg/userdata/ubuntu/provider.go
@@ -246,7 +246,7 @@ write_files:
 
 - path: "/etc/systemd/system/kubelet.service"
   content: |
-{{ kubeletSystemdUnit .KubeletVersion .CloudProviderName .MachineSpec.Name .DNSIPs .ExternalCloudProvider .PauseImage .MachineSpec.Taints .ExtraKubeletFlags | indent 4 }}
+{{ kubeletSystemdUnit .ContainerRuntime.String .KubeletVersion .CloudProviderName .MachineSpec.Name .DNSIPs .ExternalCloudProvider .PauseImage .MachineSpec.Taints .ExtraKubeletFlags | indent 4 }}
 
 - path: "/etc/systemd/system/kubelet.service.d/extras.conf"
   content: |


Test patch:
diff --git a/pkg/userdata/helper/kubelet_test.go b/pkg/userdata/helper/kubelet_test.go
index b51c93a8..ec81b3db 100644
--- a/pkg/userdata/helper/kubelet_test.go
+++ b/pkg/userdata/helper/kubelet_test.go
@@ -29,15 +29,16 @@ import (
 )
 
 type kubeletFlagTestCase struct {
-	name          string
-	version       *semver.Version
-	dnsIPs        []net.IP
-	hostname      string
-	cloudProvider string
-	external      bool
-	pauseImage    string
-	initialTaints []corev1.Taint
-	extraFlags    []string
+	name             string
+	containerRuntime string
+	version          *semver.Version
+	dnsIPs           []net.IP
+	hostname         string
+	cloudProvider    string
+	external         bool
+	pauseImage       string
+	initialTaints    []corev1.Taint
+	extraFlags       []string
 }
 
 func TestKubeletSystemdUnit(t *testing.T) {
@@ -110,6 +111,7 @@ func TestKubeletSystemdUnit(t *testing.T) {
 		name := fmt.Sprintf("kublet_systemd_unit_%s", test.name)
 		t.Run(name, func(t *testing.T) {
 			out, err := KubeletSystemdUnit(
+				defaultTo(test.containerRuntime, "docker"),
 				test.version.String(),
 				test.cloudProvider,
 				test.hostname,
@@ -127,3 +129,11 @@ func TestKubeletSystemdUnit(t *testing.T) {
 		})
 	}
 }
+
+func defaultTo(in string, defaultValue string) string {
+	if in == "" {
+		return defaultValue
+	}
+
+	return in
+}
diff --git a/pkg/userdata/ubuntu/testdata/containerd.yaml b/pkg/userdata/ubuntu/testdata/containerd.yaml
index 945a240d..25026817 100644
--- a/pkg/userdata/ubuntu/testdata/containerd.yaml
+++ b/pkg/userdata/ubuntu/testdata/containerd.yaml
@@ -181,8 +181,8 @@ write_files:
 - path: "/etc/systemd/system/kubelet.service"
   content: |
     [Unit]
-    After=docker.service
-    Requires=docker.service
+    After=containerd.service
+    Requires=containerd.service
 
     Description=kubelet: The Kubernetes Node Agent
     Documentation=https://kubernetes.io/docs/home/