Skip to content

Commit 491ab43

Browse files
authored
Merge pull request #4251 from twz123/main
Remove Docker container runtime support
2 parents b898a16 + 20b869d commit 491ab43

File tree

9 files changed

+155
-145
lines changed

9 files changed

+155
-145
lines changed

docs/experimental-windows.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ During the first run, the calico install script is created as `C:\bootstrap.ps1`
1515
Install Mirantis Container Runtime on the Windows node(s), as it is required for the initial Calico set up).
1616

1717
```shell
18-
k0s worker --cri-socket=docker:tcp://127.0.0.1:2375 --cidr-range=<cidr_range> --cluster-dns=<clusterdns> --api-server=<k0s api> <token>
18+
k0s worker --cri-socket=remote:npipe:////./pipe/containerd-containerd --cidr-range=<cidr_range> --cluster-dns=<clusterdns> --api-server=<k0s api> <token>
1919
```
2020

2121
You must initiate the Cluster control with the correct config.
@@ -76,4 +76,4 @@ spec:
7676
- name: iis
7777
image: mcr.microsoft.com/windows/servercore/iis
7878
imagePullPolicy: IfNotPresent
79-
```
79+
```

docs/runtime.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ EOF
189189
190190
**Warning**: You can use your own CRI runtime with k0s (for example, `docker`). However, k0s will not start or manage the runtime, and configuration is solely your responsibility.
191191
192-
Use the option `--cri-socket` to run a k0s worker with a custom CRI runtime. the option takes input in the form of `<type>:<socket_path>` (for `type`, use `docker` for a pure Docker setup and `remote` for anything else).
192+
Use the option `--cri-socket` to run a k0s worker with a custom CRI runtime. the option takes input in the form of `<type>:<url>` (the only supported type is `remote`).
193193
194194
### Using Docker as the container runtime
195195
@@ -216,25 +216,25 @@ need to be taken:
216216
located at `/var/run/cri-dockerd.sock`. For instance, the commands to start a
217217
node would be as follows:
218218
219-
k0s worker --cri-socket=docker:unix:///var/run/cri-dockerd.sock
219+
k0s worker --cri-socket=remote:unix:///var/run/cri-dockerd.sock
220220
221221
or, respectively
222222
223223
```console
224-
k0s controller --enable-worker --cri-socket=docker:unix:///var/run/cri-dockerd.sock
224+
k0s controller --enable-worker --cri-socket=remote:unix:///var/run/cri-dockerd.sock
225225
```
226226
227227
When running k0s [as a service](cli/k0s_install.md), consider reinstalling the
228228
service with the appropriate flags:
229229
230230
```console
231-
sudo k0s install --force worker --cri-socket=docker:unix:///var/run/cri-dockerd.sock
231+
sudo k0s install --force worker --cri-socket=remote:unix:///var/run/cri-dockerd.sock
232232
```
233233
234234
or, respectively
235235
236236
```console
237-
sudo k0s install --force controller --enable-worker --cri-socket=docker:unix:///var/run/cri-dockerd.sock
237+
sudo k0s install --force controller --enable-worker --cri-socket=remote:unix:///var/run/cri-dockerd.sock
238238
```
239239
240240
In scenarios where Docker is managed via systemd, it is crucial that the

pkg/cleanup/cleanup_unix.go

+8-13
Original file line numberDiff line numberDiff line change
@@ -43,31 +43,26 @@ type containerdConfig struct {
4343
socketPath string
4444
}
4545

46-
func NewConfig(k0sVars *config.CfgVars, cfgFile string, criSocketPath string) (*Config, error) {
46+
func NewConfig(k0sVars *config.CfgVars, cfgFile string, criSocketFlag string) (*Config, error) {
4747
runDir := "/run/k0s" // https://github.com/k0sproject/k0s/pull/591/commits/c3f932de85a0b209908ad39b817750efc4987395
4848

49-
var err error
5049
var containerdCfg *containerdConfig
51-
var runtimeType string
5250

53-
if criSocketPath == "" {
54-
criSocketPath = fmt.Sprintf("unix://%s/containerd.sock", runDir)
51+
runtimeEndpoint, err := worker.GetContainerRuntimeEndpoint(criSocketFlag, runDir)
52+
if err != nil {
53+
return nil, err
54+
}
55+
if criSocketFlag == "" {
5556
containerdCfg = &containerdConfig{
5657
binPath: fmt.Sprintf("%s/%s", k0sVars.DataDir, "bin/containerd"),
57-
socketPath: fmt.Sprintf("%s/containerd.sock", runDir),
58-
}
59-
runtimeType = "cri"
60-
} else {
61-
runtimeType, criSocketPath, err = worker.SplitRuntimeConfig(criSocketPath)
62-
if err != nil {
63-
return nil, err
58+
socketPath: runtimeEndpoint.Path,
6459
}
6560
}
6661

6762
return &Config{
6863
cfgFile: cfgFile,
6964
containerd: containerdCfg,
70-
containerRuntime: runtime.NewContainerRuntime(runtimeType, criSocketPath),
65+
containerRuntime: runtime.NewContainerRuntime(runtimeEndpoint),
7166
dataDir: k0sVars.DataDir,
7267
runDir: runDir,
7368
k0sVars: k0sVars,

pkg/component/worker/cri_runtime.go

+30-11
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,42 @@ limitations under the License.
1717
package worker
1818

1919
import (
20+
"errors"
2021
"fmt"
22+
"net/url"
23+
"path/filepath"
24+
"runtime"
2125
"strings"
2226
)
2327

24-
type RuntimeType = string
25-
type RuntimeSocket = string
28+
type RuntimeEndpoint = url.URL
29+
30+
// Parses the CRI runtime flag and returns the parsed values.
31+
// If the flag is empty, provide k0s's defaults.
32+
func GetContainerRuntimeEndpoint(criSocketFlag, k0sRunDir string) (*RuntimeEndpoint, error) {
33+
switch {
34+
case criSocketFlag != "":
35+
return parseCRISocketFlag(criSocketFlag)
36+
case runtime.GOOS == "windows":
37+
return &url.URL{Scheme: "npipe", Path: "//./pipe/containerd-containerd"}, nil
38+
default:
39+
socketPath := filepath.Join(k0sRunDir, "containerd.sock")
40+
return &url.URL{Scheme: "unix", Path: filepath.ToSlash(socketPath)}, nil
41+
}
42+
}
2643

27-
func SplitRuntimeConfig(rtConfig string) (RuntimeType, RuntimeSocket, error) {
28-
runtimeConfig := strings.SplitN(rtConfig, ":", 2)
29-
if len(runtimeConfig) != 2 {
30-
return "", "", fmt.Errorf("cannot parse CRI socket path")
44+
func parseCRISocketFlag(criSocketFlag string) (*RuntimeEndpoint, error) {
45+
runtimeType, runtimeEndpoint, ok := strings.Cut(criSocketFlag, ":")
46+
if !ok {
47+
return nil, errors.New("CRI socket flag must be of the form <type>:<url>")
48+
}
49+
if runtimeType != "remote" {
50+
return nil, fmt.Errorf(`unknown runtime type %q, only "remote" is supported`, runtimeType)
3151
}
32-
runtimeType := runtimeConfig[0]
33-
runtimeSocket := runtimeConfig[1]
34-
if runtimeType != "docker" && runtimeType != "remote" {
35-
return "", "", fmt.Errorf("unknown runtime type %s, must be either of remote or docker", runtimeType)
52+
parsedRuntimeEndpoint, err := url.Parse(runtimeEndpoint)
53+
if err != nil {
54+
return nil, fmt.Errorf("failed to parse runtime endpoint: %w", err)
3655
}
3756

38-
return runtimeType, runtimeSocket, nil
57+
return parsedRuntimeEndpoint, nil
3958
}
+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
Copyright 2024 k0s authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package worker_test
18+
19+
import (
20+
"runtime"
21+
"testing"
22+
23+
"github.com/k0sproject/k0s/pkg/component/worker"
24+
"github.com/stretchr/testify/assert"
25+
)
26+
27+
func TestGetContainerRuntimeEndpoint_Defaults(t *testing.T) {
28+
runtimeEndpoint, err := worker.GetContainerRuntimeEndpoint("", "/run/user/999")
29+
assert.NoError(t, err)
30+
if assert.NotNil(t, runtimeEndpoint) {
31+
if runtime.GOOS == "windows" {
32+
assert.Equal(t, "npipe:////./pipe/containerd-containerd", runtimeEndpoint.String())
33+
assert.Equal(t, "//./pipe/containerd-containerd", runtimeEndpoint.Path)
34+
} else {
35+
assert.Equal(t, "unix:///run/user/999/containerd.sock", runtimeEndpoint.String())
36+
assert.Equal(t, "/run/user/999/containerd.sock", runtimeEndpoint.Path)
37+
}
38+
}
39+
}
40+
41+
func TestGetContainerRuntimeEndpoint_Flag(t *testing.T) {
42+
cases := []struct {
43+
name string
44+
flag string
45+
expEndpoint string
46+
expPath string
47+
err string
48+
}{
49+
{
50+
name: "containerd-unix",
51+
flag: "remote:unix:///var/run/mke/containerd.sock",
52+
expEndpoint: "unix:///var/run/mke/containerd.sock",
53+
expPath: "/var/run/mke/containerd.sock",
54+
err: "",
55+
},
56+
{
57+
name: "containerd-windows",
58+
flag: "remote:npipe:////./pipe/containerd-containerd",
59+
expEndpoint: "npipe:////./pipe/containerd-containerd",
60+
expPath: "//./pipe/containerd-containerd",
61+
err: "",
62+
},
63+
{
64+
name: "no-colon-in-flag",
65+
flag: "no-colon-in-flag",
66+
expEndpoint: "",
67+
expPath: "",
68+
err: "CRI socket flag must be of the form <type>:<url>",
69+
},
70+
{
71+
name: "invalid-url",
72+
flag: "remote:u<nix:///foo",
73+
expEndpoint: "",
74+
expPath: "",
75+
err: "failed to parse runtime endpoint: ",
76+
},
77+
{
78+
name: "unknown-type",
79+
flag: "foobar:unix:///var/run/mke/containerd.sock",
80+
expEndpoint: "",
81+
expPath: "",
82+
err: `unknown runtime type "foobar", only "remote" is supported`,
83+
},
84+
}
85+
86+
for _, tc := range cases {
87+
t.Run(tc.name, func(t *testing.T) {
88+
endpoint, err := worker.GetContainerRuntimeEndpoint(tc.flag, "y u use me?")
89+
if tc.err != "" {
90+
assert.ErrorContains(t, err, tc.err)
91+
assert.Nil(t, endpoint)
92+
} else {
93+
assert.NoError(t, err)
94+
if assert.NotNil(t, endpoint) {
95+
assert.Equal(t, tc.expEndpoint, endpoint.String())
96+
assert.Equal(t, tc.expPath, endpoint.Path)
97+
}
98+
}
99+
})
100+
}
101+
102+
}

pkg/component/worker/kubelet.go

+4-11
Original file line numberDiff line numberDiff line change
@@ -240,18 +240,11 @@ func (k *Kubelet) prepareLocalKubeletConfig(kubeletConfigData kubeletConfig) (st
240240
preparedConfig.CgroupsPerQOS = ptr.To(kubeletConfigData.CgroupsPerQOS)
241241
preparedConfig.StaticPodURL = kubeletConfigData.StaticPodURL
242242

243-
if k.CRISocket == "" && runtime.GOOS != "windows" {
244-
socketPath := filepath.Join(k.K0sVars.RunDir, "containerd.sock")
245-
preparedConfig.ContainerRuntimeEndpoint = "unix://" + filepath.ToSlash(socketPath)
246-
} else if k.CRISocket == "" && runtime.GOOS == "windows" {
247-
preparedConfig.ContainerRuntimeEndpoint = "npipe:////./pipe/containerd-containerd"
248-
} else {
249-
_, runtimeEndpoint, err := SplitRuntimeConfig(k.CRISocket)
250-
if err != nil {
251-
return "", err
252-
}
253-
preparedConfig.ContainerRuntimeEndpoint = runtimeEndpoint
243+
containerRuntimeEndpoint, err := GetContainerRuntimeEndpoint(k.CRISocket, k.K0sVars.RunDir)
244+
if err != nil {
245+
return "", err
254246
}
247+
preparedConfig.ContainerRuntimeEndpoint = containerRuntimeEndpoint.String()
255248

256249
if len(k.Taints) > 0 {
257250
var taints []corev1.Taint

pkg/component/worker/kubelet_test.go

-45
Original file line numberDiff line numberDiff line change
@@ -27,51 +27,6 @@ import (
2727
"github.com/stretchr/testify/require"
2828
)
2929

30-
func TestCRISocketParsing(t *testing.T) {
31-
cases := []struct {
32-
name string
33-
input string
34-
expType string
35-
expSocket string
36-
err bool
37-
}{
38-
{
39-
name: "docker",
40-
input: "docker:unix:///var/run/docker.sock",
41-
expType: "docker",
42-
expSocket: "unix:///var/run/docker.sock",
43-
err: false,
44-
},
45-
{
46-
name: "containerd",
47-
input: "remote:unix:///var/run/mke/containerd.sock",
48-
expType: "remote",
49-
expSocket: "unix:///var/run/mke/containerd.sock",
50-
err: false,
51-
},
52-
{
53-
name: "unknown-type",
54-
input: "foobar:unix:///var/run/mke/containerd.sock",
55-
expType: "remote",
56-
expSocket: "unix:///var/run/mke/containerd.sock",
57-
err: true,
58-
},
59-
}
60-
61-
for _, tc := range cases {
62-
t.Run(tc.name, func(t *testing.T) {
63-
criType, sock, err := SplitRuntimeConfig(tc.input)
64-
if tc.err {
65-
require.Error(t, err)
66-
} else {
67-
require.Equal(t, tc.expType, criType)
68-
require.Equal(t, tc.expSocket, sock)
69-
}
70-
})
71-
}
72-
73-
}
74-
7530
func TestParseTaints(t *testing.T) {
7631
cases := []struct {
7732
name string

pkg/container/runtime/docker.go

-53
This file was deleted.

0 commit comments

Comments
 (0)