Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Docker Swarm support #376

Merged
merged 26 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
8873aa3
add aio binary
s4ke Feb 7, 2023
325c905
add build tooling
s4ke Feb 7, 2023
2f8408b
change propagated mount path
s4ke Feb 7, 2023
22eef6b
rename docker image for driver
s4ke Feb 7, 2023
619fa5c
change staging/unstaging to noop to make things work on swarm
s4ke Feb 12, 2023
5c291d3
add step by step guide for resizing of volumes
s4ke Feb 12, 2023
2945a65
add step by step guide for resizing of volumes
s4ke Feb 12, 2023
ccf2b7b
add more details to resize steps
s4ke Feb 13, 2023
21bf9ed
add documentation on usage of docker plugin
s4ke Feb 13, 2023
b90ab4d
format config.json
s4ke Feb 13, 2023
4b817e4
clarify README.md for swarm, add step for creating read+write token
s4ke Feb 13, 2023
9e180be
add reference to ticket tracking volume resizing support in Docker Swarm
s4ke Feb 13, 2023
5604add
remove unnecessary call to hcloud api
s4ke Feb 13, 2023
0a9190f
add build step for docker plugin
s4ke Feb 13, 2023
5f8fa5c
change PLUGIN_NAME to hetznercloud/csi-driver
s4ke Feb 13, 2023
5883070
fix github flows as per review, change docker plugin image name
s4ke Feb 13, 2023
ea9686a
fix github flows as per review, change docker plugin image name
s4ke Feb 13, 2023
71a5b2b
fix image name for swarm in README.md
s4ke Feb 13, 2023
a5e7725
Update deploy/docker-swarm/pkg/Dockerfile
s4ke Feb 13, 2023
9f11113
add feature flag to force volume staging
s4ke Feb 13, 2023
73697b2
Merge branch 'main' of github.com:s4ke/csi-driver
s4ke Feb 13, 2023
3f3a03a
fix default plugin name in makefile
s4ke Feb 13, 2023
5075bcb
fix boolean logic for feature flag
s4ke Feb 13, 2023
a2263bf
Update deploy/docker-swarm/pkg/Makefile
s4ke Feb 13, 2023
5354d15
fix topology-required flags
s4ke Feb 14, 2023
13fb7cd
use forced tag in --alias
s4ke Feb 15, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/aio/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This contains an all in one binary (aio). This is required
for orchestrators such as Docker Swarm which need all endpoints in a single
API.
134 changes: 134 additions & 0 deletions cmd/aio/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package main

import (
"fmt"
"os"
"strconv"
"strings"

proto "github.com/container-storage-interface/spec/lib/go/csi"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/hetznercloud/csi-driver/api"
"github.com/hetznercloud/csi-driver/app"
"github.com/hetznercloud/csi-driver/driver"
"github.com/hetznercloud/csi-driver/volumes"
"github.com/hetznercloud/hcloud-go/hcloud/metadata"
)

var logger log.Logger

func main() {
logger = app.CreateLogger()

m := app.CreateMetrics(logger)

hcloudClient, err := app.CreateHcloudClient(m.Registry(), logger)
if err != nil {
level.Error(logger).Log(
"msg", "failed to initialize hcloud client",
"err", err,
)
os.Exit(1)
}

metadataClient := metadata.NewClient(metadata.WithInstrumentation(m.Registry()))

server, err := app.GetServer(logger, hcloudClient, metadataClient)
if err != nil {
level.Error(logger).Log(
"msg", "failed to fetch server",
"err", err,
)
os.Exit(1)
}

// node
serverID, err := metadataClient.InstanceID()
if err != nil {
level.Error(logger).Log("msg", "failed to fetch server ID from metadata service", "err", err)
os.Exit(1)
}

serverAZ, err := metadataClient.AvailabilityZone()
if err != nil {
level.Error(logger).Log("msg", "failed to fetch server availability-zone from metadata service", "err", err)
os.Exit(1)
}
parts := strings.Split(serverAZ, "-")
if len(parts) != 2 {
level.Error(logger).Log("msg", fmt.Sprintf("unexpected server availability zone: %s", serverAZ), "err", err)
os.Exit(1)
}
serverLocation := parts[0]

level.Info(logger).Log("msg", "Fetched data from metadata service", "id", serverID, "location", serverLocation)
apricote marked this conversation as resolved.
Show resolved Hide resolved

volumeMountService := volumes.NewLinuxMountService(
log.With(logger, "component", "linux-mount-service"),
)
volumeResizeService := volumes.NewLinuxResizeService(
log.With(logger, "component", "linux-resize-service"),
)
volumeStatsService := volumes.NewLinuxStatsService(
log.With(logger, "component", "linux-stats-service"),
)
nodeService := driver.NewNodeService(
log.With(logger, "component", "driver-node-service"),
strconv.Itoa(serverID),
serverLocation,
volumeMountService,
volumeResizeService,
volumeStatsService,
)

// controller
volumeService := volumes.NewIdempotentService(
log.With(logger, "component", "idempotent-volume-service"),
api.NewVolumeService(
log.With(logger, "component", "api-volume-service"),
hcloudClient,
),
)
controllerService := driver.NewControllerService(
log.With(logger, "component", "driver-controller-service"),
volumeService,
server.Datacenter.Location.Name,
)

// common
identityService := driver.NewIdentityService(
log.With(logger, "component", "driver-identity-service"),
)

// common
listener, err := app.CreateListener()
if err != nil {
level.Error(logger).Log(
"msg", "failed to create listener",
"err", err,
)
os.Exit(1)
}

grpcServer := app.CreateGRPCServer(logger, m.UnaryServerInterceptor())

// controller
proto.RegisterControllerServer(grpcServer, controllerService)
// common
proto.RegisterIdentityServer(grpcServer, identityService)
// node
proto.RegisterNodeServer(grpcServer, nodeService)

m.InitializeMetrics(grpcServer)

identityService.SetReady(true)

if err := grpcServer.Serve(listener); err != nil {
level.Error(logger).Log(
"msg", "grpc server failed",
"err", err,
)
os.Exit(1)
}
}
1 change: 1 addition & 0 deletions deploy/docker-swarm/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
plugin
97 changes: 97 additions & 0 deletions deploy/docker-swarm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# Docker Swarm Hetzner CSI plugin

Currently in Beta. Please consult the Docker Swarm documentation
apricote marked this conversation as resolved.
Show resolved Hide resolved
for cluster volumes (=CSI) support at https://github.com/moby/moby/blob/master/docs/cluster_volumes.md

## How to install the plugin

Run the following steps on all nodes (especially master nodes).
The simplest way to achieve this

1. Install the plugin

```bash
docker plugin install --disable --alias hetznercloud/csi-driver-docker --grant-all-permissions hetznercloud/csi-driver-docker
```

2. Set HCLOUD_TOKEN
s4ke marked this conversation as resolved.
Show resolved Hide resolved

```bash
docker plugin set hetznercloud/csi-driver-docker HCLOUD_TOKEN=<your token>
```

3. Enable plugin

```bash
docker plugin enable hetznercloud/csi-driver-docker
```

## How to create a volume

Example: Create a volume wih size 50G in Nuremberg:

```bash
docker volume create --driver hetznercloud/csi-driver-docker --required-bytes 50G --type mount --sharing onewriter --scope single hcloud-debug1 --topology-required nbg1
```

We can now use this in a service:

```bash
docker service create --name hcloud-debug-serv1 --mount type=cluster,src=hcloud-debug1,dst=/srv/www nginx:alpine
```

Note that only scope `single` is supported as Hetzner Cloud volumes can only be attached to one node at a time

We can however share the volume on multiple containers on the same host:

```bash
docker volume create --driver hetznercloud/csi-driver-docker --required-bytes 50G --type mount --sharing all --scope single hcloud-debug1 --topology-required nbg1
```

After creation we can now use this volume with `--sharing all` in more than one replica:

```bash
docker service create --name hcloud-debug-serv2 --mount type=cluster,src=hcloud-debug2,dst=/srv/www nginx:alpine
docker service scale hcloud-debug-serv2=2
```

## How to resize a docker swarm Hetzner CSI volume

Currently, the Docker Swarm CSI support does not come with support for volume resizing.
s4ke marked this conversation as resolved.
Show resolved Hide resolved
The following explains a step by step guide on how to do this manually instead.

Please test the following on a Swarm with the same version as your target cluster
as this strongly depends on the logic of `docker volume rm -f` not deleting the cloud volume.

### Steps

1. Drain Volume

```
docker volume update <volume-name> --availability drain
```

This way, we ensure that all services stop using the volume.

2. Force remove volume on cluster

```
docker volume rm -f <volume-name>
```

4. Resize Volume in Hetzner UI
5. Attach Volume to temporary server manually
6. Run resize2fs manually
7. Detach Volume from temporary server manually
8. Recreate Volume with new size to make it known to Swarm again

```
docker volume create --driver hetznercloud/csi-driver-docker:dev --required-bytes <new-size> --type mount --sharing onewriter --scope single <volume-name>
s4ke marked this conversation as resolved.
Show resolved Hide resolved
```

9. Verify that volume exists again:

```
docker volume ls --cluster
```

17 changes: 17 additions & 0 deletions deploy/docker-swarm/pkg/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM golang:1.19 as builder
WORKDIR /csi
ADD go.mod go.sum /csi/
RUN go mod download
ADD . /csi/
RUN ls -al
# `skaffold debug` sets SKAFFOLD_GO_GCFLAGS to disable compiler optimizations
ARG SKAFFOLD_GO_GCFLAGS
RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -gcflags="${SKAFFOLD_GO_GCFLAGS}" -o aio.bin github.com/hetznercloud/csi-driver/cmd/aio
s4ke marked this conversation as resolved.
Show resolved Hide resolved

FROM --platform=linux/amd64 alpine:3.15
RUN apk add --no-cache ca-certificates e2fsprogs xfsprogs blkid xfsprogs-extra e2fsprogs-extra btrfs-progs cryptsetup
ENV GOTRACEBACK=all
RUN mkdir -p /plugin
COPY --from=builder /csi/aio.bin /plugin

ENTRYPOINT [ "/plugin/aio.bin" ]
21 changes: 21 additions & 0 deletions deploy/docker-swarm/pkg/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 Leo Antunes (base packaging code from https://github.com/costela/docker-volume-hetzner)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
32 changes: 32 additions & 0 deletions deploy/docker-swarm/pkg/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# FIXME: change this
s4ke marked this conversation as resolved.
Show resolved Hide resolved
PLUGIN_NAME = hetznercloud/csi-driver-docker
s4ke marked this conversation as resolved.
Show resolved Hide resolved
PLUGIN_TAG ?= $(shell git describe --tags --exact-match 2> /dev/null || echo dev)

all: create

clean:
@rm -rf ./plugin
@docker container rm -vf tmp_plugin_build || true

rootfs: clean
docker image build -f Dockerfile -t ${PLUGIN_NAME}:rootfs ../../../
mkdir -p ./plugin/rootfs
docker container create --name tmp_plugin_build ${PLUGIN_NAME}:rootfs
docker container export tmp_plugin_build | tar -x -C ./plugin/rootfs
cp config.json ./plugin/
docker container rm -vf tmp_plugin_build

create: rootfs
docker plugin rm -f ${PLUGIN_NAME}:${PLUGIN_TAG} 2> /dev/null || true
docker plugin create ${PLUGIN_NAME}:${PLUGIN_TAG} ./plugin

enable: create
docker plugin enable ${PLUGIN_NAME}:${PLUGIN_TAG}

push: create
docker plugin push ${PLUGIN_NAME}:${PLUGIN_TAG}

push_latest: create
docker plugin push ${PLUGIN_NAME}:latest
s4ke marked this conversation as resolved.
Show resolved Hide resolved

.PHONY: clean rootfs create enable push
6 changes: 6 additions & 0 deletions deploy/docker-swarm/pkg/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
a lot in this directory comes from work originally done
by other awesome people.

Before CSI support, Docker Swarm volumes
were graciously supported by @costela over at:
https://github.com/costela/docker-volume-hetzner
62 changes: 62 additions & 0 deletions deploy/docker-swarm/pkg/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"description": "Hetzner csi-driver plugin for Docker",
"documentation": "https://github.com/hetznercloud/csi-driver",
"entrypoint": [
"/plugin/aio.bin"
apricote marked this conversation as resolved.
Show resolved Hide resolved
],
"env": [
{
"name": "HCLOUD_TOKEN",
"description": "authentication token to use when accessing the Hetzner Cloud API",
"settable": [
"value"
],
"value": ""
},
{
"name": "CSI_ENDPOINT",
"description": "the CSI endpoint to listen to internally",
"settable": [],
"value": "unix:///run/docker/plugins/hetzner-csi.sock"
},
{
"name": "LOG_LEVEL",
"description": "the log level to use",
"settable": [
"value"
],
"value": "debug"
}
],
"interface": {
"socket": "hetzner-csi.sock",
"types": [
"docker.csicontroller/1.0",
"docker.csinode/1.0"
]
},
"linux": {
"allowAllDevices": true,
"capabilities": [
"CAP_SYS_ADMIN",
"CAP_CHOWN"
]
},
"mounts": [
{
"description": "used to access the dynamically attached block devices",
"destination": "/dev",
"options": [
"rbind",
"rshared"
],
"name": "dev",
"source": "/dev/",
"type": "bind"
}
],
"network": {
"type": "host"
},
"propagatedmount": "/data/published"
}
Loading