Skip to content

Commit 2474b44

Browse files
committed
Option to set kernel capabilities on service update
1 parent d3b448a commit 2474b44

File tree

6 files changed

+28
-2
lines changed

6 files changed

+28
-2
lines changed

.github/workflows/e2e.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ jobs:
3434
echo ::set-output name=build_tag::swarm-cronjob:local
3535
echo ::set-output name=service_name::swarm-cronjob
3636
echo ::set-output name=running_timeout::120
37-
echo ::set-output name=running_log_check::Number of cronjob tasks: 7
37+
echo ::set-output name=running_log_check::Number of cronjob tasks: 8
3838
-
3939
name: Set up QEMU
4040
uses: docker/setup-qemu-action@v2
@@ -61,6 +61,7 @@ jobs:
6161
docker stack deploy global -c test/global.yml
6262
docker stack deploy more_replicas -c test/more_replicas.yml
6363
docker stack deploy query -c test/query.yml
64+
docker stack deploy cap -c test/cap.yml
6465
-
6566
name: Create service
6667
run: |

docs/usage/docker-labels.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ You can configure your service using swarm-cronjob through Docker labels:
1010
| `swarm.cronjob.replicas` | `1` | Number of replicas to set on schedule in `replicated` mode. |
1111
| `swarm.cronjob.registry-auth` | `false` | Send registry authentication details to Swarm agents. |
1212
| `swarm.cronjob.query-registry` | | Indicates whether the service update requires contacting a registry |
13+
| `swarm.cronjob.capabilities` | | Comma separated list of kernel capabilities to add to the default set when service is updated |

internal/app/app.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ package app
33
import (
44
"context"
55
"strconv"
6+
"strings"
67

78
"github.com/crazy-max/swarm-cronjob/internal/docker"
89
"github.com/crazy-max/swarm-cronjob/internal/model"
910
"github.com/crazy-max/swarm-cronjob/internal/worker"
1011
"github.com/docker/docker/api/types"
1112
"github.com/docker/docker/api/types/filters"
1213
"github.com/mitchellh/mapstructure"
13-
"github.com/robfig/cron/v3"
14+
cron "github.com/robfig/cron/v3"
1415
"github.com/rs/zerolog/log"
1516
)
1617

@@ -157,6 +158,8 @@ func (sc *SwarmCronjob) crudJob(serviceName string) (bool, error) {
157158
log.Error().Str("service", service.Name).Err(err).Msgf("Cannot parse %s value of label %s", labelValue, labelKey)
158159
}
159160
wc.Job.QueryRegistry = &queryRegistry
161+
case "swarm.cronjob.capabilities":
162+
wc.Job.Capabilities = strings.Split(labelValue, ",")
160163
case "swarm.cronjob.scaledown":
161164
if labelValue == "true" {
162165
log.Debug().Str("service", service.Name).Msg("Scale down detected. Skipping cronjob")

internal/model/worker.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ type Job struct {
88
SkipRunning bool
99
RegistryAuth bool
1010
QueryRegistry *bool
11+
Capabilities []string
1112
Replicas uint64
1213
}

internal/worker/worker.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ func (c *Client) Run() {
6666
// Set ForceUpdate with Version to ensure update
6767
serviceUp.Spec.TaskTemplate.ForceUpdate = serviceUp.Version.Index
6868

69+
// Add capabilities
70+
if len(c.Job.Capabilities) > 0 {
71+
log.Debug().Str("service", c.Job.Name).Strs("capabilities", c.Job.Capabilities).Msg("Set capabilities")
72+
serviceUp.Spec.TaskTemplate.ContainerSpec.CapabilityAdd = c.Job.Capabilities
73+
}
74+
6975
// Update options
7076
updateOpts := types.ServiceUpdateOptions{}
7177
if c.Job.RegistryAuth {

test/cap.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
version: "3.8"
2+
3+
services:
4+
test:
5+
image: alpine:edge
6+
command: >
7+
/bin/sh -c "apk add libcap-utils && capsh --print | grep Current: | cut -d' ' -f2"
8+
deploy:
9+
replicas: 0
10+
labels:
11+
- "swarm.cronjob.enable=true"
12+
- "swarm.cronjob.schedule=*/5 * * * * *"
13+
- "swarm.cronjob.skip-running=true"
14+
- "swarm.cronjob.capabilities=NET_ADMIN"

0 commit comments

Comments
 (0)