Make it possible to use multiline script in crons
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
|||||||
"katenary/logger"
|
"katenary/logger"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
|
"github.com/alessio/shellescape"
|
||||||
"github.com/compose-spec/compose-go/types"
|
"github.com/compose-spec/compose-go/types"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
@@ -14,11 +15,11 @@ const (
|
|||||||
cronMulti = `pods=$(kubectl get pods --selector=%s/component=%s,%s/resource=deployment -o jsonpath='{.items[*].metadata.name}')`
|
cronMulti = `pods=$(kubectl get pods --selector=%s/component=%s,%s/resource=deployment -o jsonpath='{.items[*].metadata.name}')`
|
||||||
cronMultiCmd = `
|
cronMultiCmd = `
|
||||||
for pod in $pods; do
|
for pod in $pods; do
|
||||||
kubectl exec -i $pod -c %s -- sh -c '%s'
|
kubectl exec -i $pod -c %s -- sh -c %s
|
||||||
done`
|
done`
|
||||||
cronSingle = `pod=$(kubectl get pods --selector=%s/component=%s,%s/resource=deployment -o jsonpath='{.items[0].metadata.name}')`
|
cronSingle = `pod=$(kubectl get pods --selector=%s/component=%s,%s/resource=deployment -o jsonpath='{.items[0].metadata.name}')`
|
||||||
cronCmd = `
|
cronCmd = `
|
||||||
kubectl exec -i $pod -c %s -- sh -c '%s'`
|
kubectl exec -i $pod -c %s -- sh -c %s`
|
||||||
)
|
)
|
||||||
|
|
||||||
type CronDef struct {
|
type CronDef struct {
|
||||||
@@ -65,6 +66,7 @@ func buildCrontab(deployName string, deployment *helm.Deployment, s *types.Servi
|
|||||||
|
|
||||||
// create crontabs
|
// create crontabs
|
||||||
for _, cron := range crons {
|
for _, cron := range crons {
|
||||||
|
escaped := shellescape.Quote(cron.Command)
|
||||||
var cmd, podget string
|
var cmd, podget string
|
||||||
if cron.Multi {
|
if cron.Multi {
|
||||||
podget = cronMulti
|
podget = cronMulti
|
||||||
@@ -74,7 +76,7 @@ func buildCrontab(deployName string, deployment *helm.Deployment, s *types.Servi
|
|||||||
cmd = cronCmd
|
cmd = cronCmd
|
||||||
}
|
}
|
||||||
podget = fmt.Sprintf(podget, helm.K, deployName, helm.K)
|
podget = fmt.Sprintf(podget, helm.K, deployName, helm.K)
|
||||||
cmd = fmt.Sprintf(cmd, s.Name, cron.Command)
|
cmd = fmt.Sprintf(cmd, s.Name, escaped)
|
||||||
cmd = podget + cmd
|
cmd = podget + cmd
|
||||||
|
|
||||||
if cron.Image == "" {
|
if cron.Image == "" {
|
||||||
|
1
go.mod
1
go.mod
@@ -3,6 +3,7 @@ module katenary
|
|||||||
go 1.16
|
go 1.16
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/alessio/shellescape v1.4.1
|
||||||
github.com/compose-spec/compose-go v1.2.5
|
github.com/compose-spec/compose-go v1.2.5
|
||||||
github.com/distribution/distribution/v3 v3.0.0-20220505155552-985711c1f414 // indirect
|
github.com/distribution/distribution/v3 v3.0.0-20220505155552-985711c1f414 // indirect
|
||||||
github.com/kr/pretty v0.2.0 // indirect
|
github.com/kr/pretty v0.2.0 // indirect
|
||||||
|
2
go.sum
2
go.sum
@@ -16,6 +16,8 @@ github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAE
|
|||||||
github.com/Shopify/logrus-bugsnag v0.0.0-20171204204709-577dee27f20d/go.mod h1:HI8ITrYtUY+O+ZhtlqUnD8+KwNPOyugEhfP9fdUIaEQ=
|
github.com/Shopify/logrus-bugsnag v0.0.0-20171204204709-577dee27f20d/go.mod h1:HI8ITrYtUY+O+ZhtlqUnD8+KwNPOyugEhfP9fdUIaEQ=
|
||||||
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
|
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
|
||||||
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
|
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
|
||||||
|
github.com/alessio/shellescape v1.4.1 h1:V7yhSDDn8LP4lc4jS8pFkt0zCnzVJlG5JXy9BVKJUX0=
|
||||||
|
github.com/alessio/shellescape v1.4.1/go.mod h1:PZAiSCk0LJaZkiCSkPv8qIobYglO3FPpyFjDCtHLS30=
|
||||||
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
|
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
|
||||||
github.com/aws/aws-sdk-go v1.34.9/go.mod h1:5zCpMtNQVjRREroY7sYe8lOMRSxkhG6MZveU8YkpAk0=
|
github.com/aws/aws-sdk-go v1.34.9/go.mod h1:5zCpMtNQVjRREroY7sYe8lOMRSxkhG6MZveU8YkpAk0=
|
||||||
github.com/aws/aws-sdk-go v1.43.16/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4oIKwKHZo=
|
github.com/aws/aws-sdk-go v1.43.16/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4oIKwKHZo=
|
||||||
|
Reference in New Issue
Block a user