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

fix: enforce only one instance of an in-pod cronjob running at once #192

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ toolchain go1.23.3
require (
dario.cat/mergo v1.0.1
github.com/PaesslerAG/gval v1.2.4
github.com/alessio/shellescape v1.4.1
github.com/amazeeio/dbaas-operator v0.3.0
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883
github.com/compose-spec/compose-go v1.2.7
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuy
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
github.com/alessio/shellescape v1.4.1 h1:V7yhSDDn8LP4lc4jS8pFkt0zCnzVJlG5JXy9BVKJUX0=
github.com/alessio/shellescape v1.4.1/go.mod h1:PZAiSCk0LJaZkiCSkPv8qIobYglO3FPpyFjDCtHLS30=
github.com/amazeeio/dbaas-operator v0.3.0 h1:+yd0RHp+TSAUMUo6o7f5/ReqIEpVJHqoRDzfsibtSzQ=
github.com/amazeeio/dbaas-operator v0.3.0/go.mod h1:fbZuWO1a4JhEJZLrSdOg/+YEzGL6yZcGpfHiIqn72dc=
Expand Down
12 changes: 11 additions & 1 deletion internal/generator/services.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package generator

import (
"crypto/sha256"
"fmt"
"reflect"
"regexp"
"strconv"
"strings"

"github.com/alessio/shellescape"
composetypes "github.com/compose-spec/compose-go/types"
"github.com/uselagoon/build-deploy-tool/internal/helpers"
"github.com/uselagoon/build-deploy-tool/internal/lagoon"
Expand Down Expand Up @@ -478,7 +480,7 @@ func composeToServiceValues(
return nil, err
}
if !buildValues.CronjobsDisabled {
for _, cronjob := range buildValues.LagoonYAML.Environments[buildValues.Branch].Cronjobs {
for idx, cronjob := range buildValues.LagoonYAML.Environments[buildValues.Branch].Cronjobs {
// if this cronjob is meant for this service, add it
if cronjob.Service == composeService {
var err error
Expand All @@ -492,6 +494,14 @@ func composeToServiceValues(
}
// if the cronjob is inpod, or the cronjob has an inpod flag override
if inpod || (cronjob.InPod != nil && *cronjob.InPod) {
cmd := cronjob.Command
// Lagoon enforces that only a single instance of a cronjob can run at any one time.
// https://man7.org/linux/man-pages/man1/flock.1.html
// https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Shell-Parameter-Expansion
sha := sha256.New()
sha.Write([]byte(fmt.Sprintf("%d %s", idx, cmd)))
cmdSha := sha.Sum(nil)
cronjob.Command = fmt.Sprintf("flock -n /tmp/cron.lock.%x -c %s", cmdSha, shellescape.Quote(cmd))
inpodcronjobs = append(inpodcronjobs, cronjob)
} else {
// make the cronjob name kubernetes compliant
Expand Down
2 changes: 1 addition & 1 deletion internal/generator/services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -943,7 +943,7 @@ func Test_composeToServiceValues(t *testing.T) {
Name: "My Cronjob2",
Service: "cli",
Schedule: "3,8,13,18,23,28,33,38,43,48,53,58 * * * *",
Command: "drush cron",
Command: "flock -n /tmp/cron.lock.f6d199ad6c0075de8176b5c0a14a5d571a473001ced482bc9289182da3d90083 -c 'drush cron'",
},
},
NativeCronjobs: []lagoon.Cronjob{
Expand Down
2 changes: 0 additions & 2 deletions internal/generator/volumes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
)

func Test_flagDefaultVolumeCreation(t *testing.T) {
type args struct {
}
tests := []struct {
name string
buildValues *BuildValues
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ spec:
value: abcdefg123456
- name: CRONJOBS
value: |
3,18,33,48 * * * * drush cron
18,48 * * * * drush cron
3,18,33,48 * * * * flock -n /tmp/cron.lock.932b8586d96eb88e1574cb8a1223a0b964763c7d0ce90d9aff64d2d92e60fd8d -c 'drush cron'
18,48 * * * * flock -n /tmp/cron.lock.f6d199ad6c0075de8176b5c0a14a5d571a473001ced482bc9289182da3d90083 -c 'drush cron'
- name: SERVICE_NAME
value: node
envFrom:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ spec:
value: abcdefg123456
- name: CRONJOBS
value: |
3,18,33,48 0 * * * drush cron
3,18,33,48 0 * * * flock -n /tmp/cron.lock.932b8586d96eb88e1574cb8a1223a0b964763c7d0ce90d9aff64d2d92e60fd8d -c 'drush cron'
- name: SERVICE_NAME
value: node
envFrom:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ spec:
value: "0000000000000000000000000000000000000000"
- name: CRONJOBS
value: |
3,18,33,48 * * * * drush cron
3,18,33,48 * * * * flock -n /tmp/cron.lock.932b8586d96eb88e1574cb8a1223a0b964763c7d0ce90d9aff64d2d92e60fd8d -c 'drush cron'
- name: SERVICE_NAME
value: cli
envFrom:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ spec:
value: "0000000000000000000000000000000000000000"
- name: CRONJOBS
value: |
3,18,33,48 * * * * drush cron
3,18,33,48 * * * * flock -n /tmp/cron.lock.932b8586d96eb88e1574cb8a1223a0b964763c7d0ce90d9aff64d2d92e60fd8d -c 'drush cron'
- name: SERVICE_NAME
value: cli
envFrom:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ spec:
value: "0000000000000000000000000000000000000000"
- name: CRONJOBS
value: |
3,18,33,48 * * * * drush cron
3,18,33,48 * * * * flock -n /tmp/cron.lock.932b8586d96eb88e1574cb8a1223a0b964763c7d0ce90d9aff64d2d92e60fd8d -c 'drush cron'
- name: SERVICE_NAME
value: cli
envFrom:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ spec:
value: "0000000000000000000000000000000000000000"
- name: CRONJOBS
value: |
3,18,33,48 * * * * drush cron
3,18,33,48 * * * * flock -n /tmp/cron.lock.932b8586d96eb88e1574cb8a1223a0b964763c7d0ce90d9aff64d2d92e60fd8d -c 'drush cron'
- name: SERVICE_NAME
value: cli
envFrom:
Expand Down