diff --git a/pkg/cleanup/cleanup_unix.go b/pkg/cleanup/cleanup_unix.go index 50c92195eadd..1679bdaf623e 100644 --- a/pkg/cleanup/cleanup_unix.go +++ b/pkg/cleanup/cleanup_unix.go @@ -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, @@ -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, diff --git a/pkg/component/worker/cri_runtime.go b/pkg/component/worker/cri_runtime.go index 2ef6caeb527a..161651f2ee20 100644 --- a/pkg/component/worker/cri_runtime.go +++ b/pkg/component/worker/cri_runtime.go @@ -17,6 +17,7 @@ limitations under the License. package worker import ( + "errors" "fmt" "net/url" "path/filepath" @@ -24,38 +25,34 @@ import ( "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 :") } - 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 } diff --git a/pkg/component/worker/cri_runtime_test.go b/pkg/component/worker/cri_runtime_test.go index 5962a4f06e74..c3f8d822551a 100644 --- a/pkg/component/worker/cri_runtime_test.go +++ b/pkg/component/worker/cri_runtime_test.go @@ -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()) @@ -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: "", @@ -74,15 +63,13 @@ 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 :", }, { name: "invalid-url", flag: "remote:u