Skip to content

Commit

Permalink
Actually remove the container runtime implementation for Docker
Browse files Browse the repository at this point in the history
Signed-off-by: Tom Wieczorek <twieczorek@mirantis.com>
  • Loading branch information
twz123 committed Apr 5, 2024
1 parent a253696 commit 20b869d
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 96 deletions.
6 changes: 3 additions & 3 deletions pkg/cleanup/cleanup_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ func NewConfig(k0sVars *config.CfgVars, cfgFile string, criSocketFlag string) (*

var containerdCfg *containerdConfig

runtimeType, runtimeEndpoint, err := worker.GetContainerRuntimeEndpoint(criSocketFlag, runDir)
runtimeEndpoint, err := worker.GetContainerRuntimeEndpoint(criSocketFlag, runDir)
if err != nil {
return nil, err
}
if runtimeType == "" {
if criSocketFlag == "" {
containerdCfg = &containerdConfig{
binPath: fmt.Sprintf("%s/%s", k0sVars.DataDir, "bin/containerd"),
socketPath: runtimeEndpoint.Path,
Expand All @@ -62,7 +62,7 @@ func NewConfig(k0sVars *config.CfgVars, cfgFile string, criSocketFlag string) (*
return &Config{
cfgFile: cfgFile,
containerd: containerdCfg,
containerRuntime: runtime.NewContainerRuntime(runtimeType, runtimeEndpoint),
containerRuntime: runtime.NewContainerRuntime(runtimeEndpoint),
dataDir: k0sVars.DataDir,
runDir: runDir,
k0sVars: k0sVars,
Expand Down
27 changes: 12 additions & 15 deletions pkg/component/worker/cri_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,45 +17,42 @@ limitations under the License.
package worker

import (
"errors"
"fmt"
"net/url"
"path/filepath"
"runtime"
"strings"
)

type RuntimeType = string
type RuntimeEndpoint = url.URL

// Parses the CRI runtime flag and returns the parsed values.
// If the flag is empty, provide k0s's defaults.
func GetContainerRuntimeEndpoint(criSocketFlag, k0sRunDir string) (RuntimeType, *RuntimeEndpoint, error) {
func GetContainerRuntimeEndpoint(criSocketFlag, k0sRunDir string) (*RuntimeEndpoint, error) {
switch {
case criSocketFlag != "":
return parseCRISocketFlag(criSocketFlag)
case runtime.GOOS == "windows":
return "", &url.URL{Scheme: "npipe", Path: "//./pipe/containerd-containerd"}, nil
return &url.URL{Scheme: "npipe", Path: "//./pipe/containerd-containerd"}, nil
default:
socketPath := filepath.Join(k0sRunDir, "containerd.sock")
return "", &url.URL{Scheme: "unix", Path: filepath.ToSlash(socketPath)}, nil
return &url.URL{Scheme: "unix", Path: filepath.ToSlash(socketPath)}, nil
}
}

func parseCRISocketFlag(criSocketFlag string) (RuntimeType, *RuntimeEndpoint, error) {
runtimeConfig := strings.SplitN(criSocketFlag, ":", 2)
if len(runtimeConfig) != 2 {
return "", nil, fmt.Errorf("cannot parse CRI socket flag")
func parseCRISocketFlag(criSocketFlag string) (*RuntimeEndpoint, error) {
runtimeType, runtimeEndpoint, ok := strings.Cut(criSocketFlag, ":")
if !ok {
return nil, errors.New("CRI socket flag must be of the form <type>:<url>")
}
runtimeType := runtimeConfig[0]
runtimeEndpoint := runtimeConfig[1]
if runtimeType != "docker" && runtimeType != "remote" {
return "", nil, fmt.Errorf("unknown runtime type %s, must be either of remote or docker", runtimeType)
if runtimeType != "remote" {
return nil, fmt.Errorf(`unknown runtime type %q, only "remote" is supported`, runtimeType)
}

parsedRuntimeEndpoint, err := url.Parse(runtimeEndpoint)
if err != nil {
return "", nil, fmt.Errorf("failed to parse runtime endpoint: %w", err)
return nil, fmt.Errorf("failed to parse runtime endpoint: %w", err)
}

return runtimeType, parsedRuntimeEndpoint, nil
return parsedRuntimeEndpoint, nil
}
24 changes: 4 additions & 20 deletions pkg/component/worker/cri_runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ import (
)

func TestGetContainerRuntimeEndpoint_Defaults(t *testing.T) {
runtimeType, runtimeEndpoint, err := worker.GetContainerRuntimeEndpoint("", "/run/user/999")
runtimeEndpoint, err := worker.GetContainerRuntimeEndpoint("", "/run/user/999")
assert.NoError(t, err)
assert.Empty(t, runtimeType)
if assert.NotNil(t, runtimeEndpoint) {
if runtime.GOOS == "windows" {
assert.Equal(t, "npipe:////./pipe/containerd-containerd", runtimeEndpoint.String())
Expand All @@ -43,23 +42,13 @@ func TestGetContainerRuntimeEndpoint_Flag(t *testing.T) {
cases := []struct {
name string
flag string
expType string
expEndpoint string
expPath string
err string
}{
{
name: "docker",
flag: "docker:unix:///var/run/docker.sock",
expType: "docker",
expEndpoint: "unix:///var/run/docker.sock",
expPath: "/var/run/docker.sock",
err: "",
},
{
name: "containerd-unix",
flag: "remote:unix:///var/run/mke/containerd.sock",
expType: "remote",
expEndpoint: "unix:///var/run/mke/containerd.sock",
expPath: "/var/run/mke/containerd.sock",
err: "",
Expand All @@ -74,39 +63,34 @@ func TestGetContainerRuntimeEndpoint_Flag(t *testing.T) {
{
name: "no-colon-in-flag",
flag: "no-colon-in-flag",
expType: "",
expEndpoint: "",
expPath: "",
err: "cannot parse CRI socket flag",
err: "CRI socket flag must be of the form <type>:<url>",
},
{
name: "invalid-url",
flag: "remote:u<nix:///foo",
expType: "",
expEndpoint: "",
expPath: "",
err: "failed to parse runtime endpoint: ",
},
{
name: "unknown-type",
flag: "foobar:unix:///var/run/mke/containerd.sock",
expType: "",
expEndpoint: "",
expPath: "",
err: "unknown runtime type foobar, must be either of remote or docker",
err: `unknown runtime type "foobar", only "remote" is supported`,
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
criType, endpoint, err := worker.GetContainerRuntimeEndpoint(tc.flag, "y u use me?")
endpoint, err := worker.GetContainerRuntimeEndpoint(tc.flag, "y u use me?")
if tc.err != "" {
assert.ErrorContains(t, err, tc.err)
assert.Empty(t, criType)
assert.Nil(t, endpoint)
} else {
assert.NoError(t, err)
assert.Equal(t, tc.expType, criType)
if assert.NotNil(t, endpoint) {
assert.Equal(t, tc.expEndpoint, endpoint.String())
assert.Equal(t, tc.expPath, endpoint.Path)
Expand Down
2 changes: 1 addition & 1 deletion pkg/component/worker/kubelet.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func (k *Kubelet) prepareLocalKubeletConfig(kubeletConfigData kubeletConfig) (st
preparedConfig.CgroupsPerQOS = ptr.To(kubeletConfigData.CgroupsPerQOS)
preparedConfig.StaticPodURL = kubeletConfigData.StaticPodURL

_, containerRuntimeEndpoint, err := GetContainerRuntimeEndpoint(k.CRISocket, k.K0sVars.RunDir)
containerRuntimeEndpoint, err := GetContainerRuntimeEndpoint(k.CRISocket, k.K0sVars.RunDir)
if err != nil {
return "", err
}
Expand Down
53 changes: 0 additions & 53 deletions pkg/container/runtime/docker.go

This file was deleted.

5 changes: 1 addition & 4 deletions pkg/container/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ type ContainerRuntime interface {
StopContainer(id string) error
}

func NewContainerRuntime(runtimeType string, runtimeEndpoint *url.URL) ContainerRuntime {
if runtimeType == "docker" {
return &DockerRuntime{runtimeEndpoint.String()}
}
func NewContainerRuntime(runtimeEndpoint *url.URL) ContainerRuntime {
return &CRIRuntime{runtimeEndpoint.String()}
}

0 comments on commit 20b869d

Please sign in to comment.