Skip to content

Commit 26e1137

Browse files
authored
Merge pull request #5602 from twz123/win-containerd-endpoint
Use correct containerd endpoints on Windows
2 parents bd3bbd7 + 83115aa commit 26e1137

File tree

3 files changed

+40
-27
lines changed

3 files changed

+40
-27
lines changed

cmd/ctr/ctr.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ package ctr
1818

1919
import (
2020
"os"
21-
"path"
2221

2322
"github.com/k0sproject/k0s/internal/pkg/dir"
23+
"github.com/k0sproject/k0s/pkg/component/worker"
2424
"github.com/k0sproject/k0s/pkg/config"
2525

2626
"github.com/containerd/containerd/cmd/ctr/app"
@@ -58,7 +58,7 @@ func setDefaultValues(runDir string, flags []cli.Flag) {
5858
for i, flag := range flags {
5959
if f, ok := flag.(cli.StringFlag); ok {
6060
if f.Name == "address, a" {
61-
f.Value = path.Join(runDir, "containerd.sock")
61+
f.Value = worker.GetContainerdAddress(runDir)
6262
flags[i] = f
6363
} else if f.Name == "namespace, n" {
6464
f.Value = "k8s.io"

pkg/component/worker/cri_runtime.go

+19-7
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,27 @@ type RuntimeEndpoint = url.URL
3030
// Parses the CRI runtime flag and returns the parsed values.
3131
// If the flag is empty, provide k0s's defaults.
3232
func GetContainerRuntimeEndpoint(criSocketFlag, k0sRunDir string) (*RuntimeEndpoint, error) {
33-
switch {
34-
case criSocketFlag != "":
33+
if criSocketFlag != "" {
3534
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
4135
}
36+
37+
return getContainerRuntimeEndpoint(k0sRunDir), nil
38+
}
39+
40+
func getContainerRuntimeEndpoint(k0sRunDir string) *RuntimeEndpoint {
41+
if runtime.GOOS == "windows" {
42+
return &url.URL{Scheme: "npipe", Path: "//./pipe/containerd-containerd"}
43+
}
44+
45+
return &url.URL{Scheme: "unix", Path: filepath.ToSlash(GetContainerdAddress(k0sRunDir))}
46+
}
47+
48+
func GetContainerdAddress(k0sRunDir string) string {
49+
if runtime.GOOS == "windows" {
50+
return getContainerRuntimeEndpoint(k0sRunDir).String()
51+
}
52+
53+
return filepath.Join(k0sRunDir, "containerd.sock")
4254
}
4355

4456
func parseCRISocketFlag(criSocketFlag string) (*RuntimeEndpoint, error) {

pkg/component/worker/ocibundle.go

+19-18
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,13 @@ const (
4949

5050
// OCIBundleReconciler tries to import OCI bundle into the running containerd instance
5151
type OCIBundleReconciler struct {
52-
k0sVars *config.CfgVars
53-
log *logrus.Entry
54-
alreadyImported map[string]time.Time
55-
mtx sync.Mutex
56-
cancel context.CancelFunc
57-
end chan struct{}
52+
ociBundleDir string
53+
containerdAddress string
54+
log *logrus.Entry
55+
alreadyImported map[string]time.Time
56+
mtx sync.Mutex
57+
cancel context.CancelFunc
58+
end chan struct{}
5859
*prober.EventEmitter
5960
}
6061

@@ -63,25 +64,25 @@ var _ manager.Component = (*OCIBundleReconciler)(nil)
6364
// NewOCIBundleReconciler builds new reconciler
6465
func NewOCIBundleReconciler(vars *config.CfgVars) *OCIBundleReconciler {
6566
return &OCIBundleReconciler{
66-
k0sVars: vars,
67-
log: logrus.WithField("component", "OCIBundleReconciler"),
68-
EventEmitter: prober.NewEventEmitter(),
69-
alreadyImported: map[string]time.Time{},
70-
end: make(chan struct{}),
67+
ociBundleDir: vars.OCIBundleDir,
68+
containerdAddress: GetContainerdAddress(vars.RunDir),
69+
log: logrus.WithField("component", "OCIBundleReconciler"),
70+
EventEmitter: prober.NewEventEmitter(),
71+
alreadyImported: map[string]time.Time{},
72+
end: make(chan struct{}),
7173
}
7274
}
7375

7476
func (a *OCIBundleReconciler) Init(_ context.Context) error {
75-
return dir.Init(a.k0sVars.OCIBundleDir, constant.ManifestsDirMode)
77+
return dir.Init(a.ociBundleDir, constant.ManifestsDirMode)
7678
}
7779

7880
// containerdClient returns a connected containerd client.
7981
func (a *OCIBundleReconciler) containerdClient(ctx context.Context) (*containerd.Client, error) {
8082
var client *containerd.Client
81-
sock := filepath.Join(a.k0sVars.RunDir, "containerd.sock")
8283
if err := retry.Do(func() (err error) {
8384
client, err = containerd.New(
84-
sock,
85+
a.containerdAddress,
8586
containerd.WithDefaultNamespace("k8s.io"),
8687
containerd.WithDefaultPlatform(
8788
platforms.Only(platforms.DefaultSpec()),
@@ -126,14 +127,14 @@ func (a *OCIBundleReconciler) loadAll(ctx context.Context) {
126127
defer a.mtx.Unlock()
127128

128129
a.log.Info("Loading OCI bundles directory")
129-
files, err := os.ReadDir(a.k0sVars.OCIBundleDir)
130+
files, err := os.ReadDir(a.ociBundleDir)
130131
if err != nil {
131132
a.log.WithError(err).Errorf("Failed to read bundles directory")
132133
return
133134
}
134135
a.EmitWithPayload("importing OCI bundles", files)
135136
for _, file := range files {
136-
fpath := filepath.Join(a.k0sVars.OCIBundleDir, file.Name())
137+
fpath := filepath.Join(a.ociBundleDir, file.Name())
137138
finfo, err := os.Stat(fpath)
138139
if err != nil {
139140
a.log.WithError(err).Errorf("failed to stat %s", fpath)
@@ -237,7 +238,7 @@ func (a *OCIBundleReconciler) installWatcher(ctx context.Context) error {
237238
return fmt.Errorf("failed to create watcher: %w", err)
238239
}
239240

240-
if err := watcher.Add(a.k0sVars.OCIBundleDir); err != nil {
241+
if err := watcher.Add(a.ociBundleDir); err != nil {
241242
return fmt.Errorf("failed to add watcher: %w", err)
242243
}
243244

@@ -261,7 +262,7 @@ func (a *OCIBundleReconciler) installWatcher(ctx context.Context) error {
261262

262263
go func() {
263264
defer close(a.end)
264-
a.log.Infof("Started to watch events on %s", a.k0sVars.OCIBundleDir)
265+
a.log.Infof("Started to watch events on %s", a.ociBundleDir)
265266
_ = debouncer.Run(ctx)
266267
if err := watcher.Close(); err != nil {
267268
a.log.Errorf("Failed to close watcher: %s", err)

0 commit comments

Comments
 (0)