Skip to content

Commit

Permalink
Option to set kernel capabilities on service update
Browse files Browse the repository at this point in the history
  • Loading branch information
crazy-max committed Jul 17, 2022
1 parent d3b448a commit decfd1b
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
echo ::set-output name=build_tag::swarm-cronjob:local
echo ::set-output name=service_name::swarm-cronjob
echo ::set-output name=running_timeout::120
echo ::set-output name=running_log_check::Number of cronjob tasks: 7
echo ::set-output name=running_log_check::Number of cronjob tasks: 8
-
name: Set up QEMU
uses: docker/setup-qemu-action@v2
Expand All @@ -61,6 +61,7 @@ jobs:
docker stack deploy global -c test/global.yml
docker stack deploy more_replicas -c test/more_replicas.yml
docker stack deploy query -c test/query.yml
docker stack deploy cap -c test/cap.yml
-
name: Create service
run: |
Expand Down
1 change: 1 addition & 0 deletions docs/usage/docker-labels.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ You can configure your service using swarm-cronjob through Docker labels:
| `swarm.cronjob.replicas` | `1` | Number of replicas to set on schedule in `replicated` mode. |
| `swarm.cronjob.registry-auth` | `false` | Send registry authentication details to Swarm agents. |
| `swarm.cronjob.query-registry` | | Indicates whether the service update requires contacting a registry |
| `swarm.cronjob.capabilities` | | Comma separated list of kernel capabilities to add to the default set when service is updated |
5 changes: 4 additions & 1 deletion internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package app
import (
"context"
"strconv"
"strings"

"github.com/crazy-max/swarm-cronjob/internal/docker"
"github.com/crazy-max/swarm-cronjob/internal/model"
"github.com/crazy-max/swarm-cronjob/internal/worker"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/mitchellh/mapstructure"
"github.com/robfig/cron/v3"
cron "github.com/robfig/cron/v3"
"github.com/rs/zerolog/log"
)

Expand Down Expand Up @@ -157,6 +158,8 @@ func (sc *SwarmCronjob) crudJob(serviceName string) (bool, error) {
log.Error().Str("service", service.Name).Err(err).Msgf("Cannot parse %s value of label %s", labelValue, labelKey)
}
wc.Job.QueryRegistry = &queryRegistry
case "swarm.cronjob.capabilities":
wc.Job.Capabilities = strings.Split(labelValue, ",")
case "swarm.cronjob.scaledown":
if labelValue == "true" {
log.Debug().Str("service", service.Name).Msg("Scale down detected. Skipping cronjob")
Expand Down
1 change: 1 addition & 0 deletions internal/model/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ type Job struct {
SkipRunning bool
RegistryAuth bool
QueryRegistry *bool
Capabilities []string
Replicas uint64
}
3 changes: 3 additions & 0 deletions internal/worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ func (c *Client) Run() {
// Set ForceUpdate with Version to ensure update
serviceUp.Spec.TaskTemplate.ForceUpdate = serviceUp.Version.Index

// Add capabilities
serviceUp.Spec.TaskTemplate.ContainerSpec.CapabilityAdd = c.Job.Capabilities

// Update options
updateOpts := types.ServiceUpdateOptions{}
if c.Job.RegistryAuth {
Expand Down
16 changes: 16 additions & 0 deletions test/cap.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
version: "3.8"

services:
test:
image: alpine:edge
command: >
/bin/sh -c "apk add libcap-utils && capsh --print | grep Current: | cut -d' ' -f2"
deploy:
replicas: 0
labels:
- "swarm.cronjob.enable=true"
- "swarm.cronjob.schedule=*/5 * * * * *"
- "swarm.cronjob.skip-running=true"
- "swarm.cronjob.capabilities=NET_ADMIN"
cap_add:
- NET_ADMIN

0 comments on commit decfd1b

Please sign in to comment.