Skip to content

Commit 6f6f35b

Browse files
committed
Add Windows Support
Signed-off-by: Olli Janatuinen <[email protected]>
1 parent 8e8f5c5 commit 6f6f35b

File tree

5 files changed

+47
-19
lines changed

5 files changed

+47
-19
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.vagrant/
22
/containerd-driver
3+
/containerd-driver.exe

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ clean:
1616
.PHONY: build
1717
build:
1818
$(GOLANG) build -o $(BINARY) .
19+
GOOS=windows $(GOLANG) build -o $(BINARY).exe .
1920

2021
.PHONY: test
2122
test:

containerd/driver.go

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"time"
2525

2626
"github.com/containerd/containerd"
27+
"github.com/containerd/containerd/defaults"
2728
"github.com/containerd/containerd/namespaces"
2829
"github.com/hashicorp/consul-template/signals"
2930
"github.com/hashicorp/go-hclog"
@@ -37,7 +38,6 @@ import (
3738
"github.com/hashicorp/nomad/plugins/drivers"
3839
"github.com/hashicorp/nomad/plugins/shared/hclspec"
3940
"github.com/hashicorp/nomad/plugins/shared/structs"
40-
"github.com/opencontainers/runc/libcontainer/cgroups"
4141
)
4242

4343
const (
@@ -260,21 +260,12 @@ func NewPlugin(logger log.Logger) drivers.DriverPlugin {
260260

261261
// This will create a new containerd client which will talk to
262262
// default containerd socket path.
263-
client, err := containerd.New("/run/containerd/containerd.sock")
263+
client, err := containerd.New(defaults.DefaultAddress)
264264
if err != nil {
265265
logger.Error("Error in creating containerd client", "err", err)
266266
return nil
267267
}
268-
269-
// Calls to containerd API are namespaced.
270-
// "nomad" is the namespace that will be used for all nomad-driver-containerd
271-
// related containerd API calls.
272-
namespace := "nomad"
273-
// Unless we are operating in cgroups.v2 mode, in which case we use the
274-
// name "nomad.slice", which ends up being the cgroup parent.
275-
if cgroups.IsCgroup2UnifiedMode() {
276-
namespace = "nomad.slice"
277-
}
268+
namespace := getNamespaceName()
278269
ctxContainerd := namespaces.WithNamespace(context.Background(), namespace)
279270

280271
return &Driver{
@@ -445,13 +436,7 @@ func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *drive
445436
handle := drivers.NewTaskHandle(taskHandleVersion)
446437
handle.Config = cfg
447438

448-
// Use Nomad's docker naming convention for the container name
449-
// https://www.nomadproject.io/docs/drivers/docker#container-name
450-
containerName := cfg.Name + "-" + cfg.AllocID
451-
if cgroups.IsCgroup2UnifiedMode() {
452-
// In cgroup.v2 mode, the name is slightly different.
453-
containerName = fmt.Sprintf("%s.%s.scope", cfg.AllocID, cfg.Name)
454-
}
439+
containerName := getContainerName(cfg.Name, cfg.AllocID)
455440
containerConfig.ContainerName = containerName
456441

457442
var err error

containerd/driver_linux.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package containerd
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/opencontainers/runc/libcontainer/cgroups"
7+
)
8+
9+
func getNamespaceName() string {
10+
// Calls to containerd API are namespaced.
11+
// "nomad" is the namespace that will be used for all nomad-driver-containerd
12+
// related containerd API calls.
13+
namespace := "nomad"
14+
// Unless we are operating in cgroups.v2 mode, in which case we use the
15+
// name "nomad.slice", which ends up being the cgroup parent.
16+
if cgroups.IsCgroup2UnifiedMode() {
17+
namespace = "nomad.slice"
18+
}
19+
return namespace
20+
}
21+
22+
func getContainerName(name, allocID string) string {
23+
// Use Nomad's docker naming convention for the container name
24+
// https://www.nomadproject.io/docs/drivers/docker#container-name
25+
containerName := name + "-" + allocID
26+
if cgroups.IsCgroup2UnifiedMode() {
27+
// In cgroup.v2 mode, the name is slightly different.
28+
containerName = fmt.Sprintf("%s.%s.scope", allocID, name)
29+
}
30+
return containerName
31+
}

containerd/driver_windows.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package containerd
2+
3+
func getNamespaceName() string {
4+
return "nomad"
5+
}
6+
7+
func getContainerName(name, allocID string) string {
8+
containerName := name + "-" + allocID
9+
return containerName
10+
}

0 commit comments

Comments
 (0)