Feat cronjob (#23)
Make possible to declare cronTabs inside docker-compose file. ⇒ Also, add multiple compose file injection with `-c` arguments ⇒ Also, fixes “ignore depends on” for same pod ⇒ Also fixes * fix [Be able to specify compose.yml files and its override #21](https://github.com/metal3d/katenary/issues/21) * fix [Be able to ignore ports to expose in a katenary.io/ports list #16](https://github.com/metal3d/katenary/issues/16) And more fixes… (later, we will use branches in a better way, that was a hard, long fix process)
This commit is contained in:
110
generator/crontabs.go
Normal file
110
generator/crontabs.go
Normal file
@@ -0,0 +1,110 @@
|
||||
package generator
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"katenary/helm"
|
||||
"katenary/logger"
|
||||
"log"
|
||||
|
||||
"github.com/alessio/shellescape"
|
||||
"github.com/compose-spec/compose-go/types"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
const (
|
||||
cronMulti = `pods=$(kubectl get pods --selector=%s/component=%s,%s/resource=deployment -o jsonpath='{.items[*].metadata.name}')`
|
||||
cronMultiCmd = `
|
||||
for pod in $pods; do
|
||||
kubectl exec -i $pod -c %s -- sh -c %s
|
||||
done`
|
||||
cronSingle = `pod=$(kubectl get pods --selector=%s/component=%s,%s/resource=deployment -o jsonpath='{.items[0].metadata.name}')`
|
||||
cronCmd = `
|
||||
kubectl exec -i $pod -c %s -- sh -c %s`
|
||||
)
|
||||
|
||||
type CronDef struct {
|
||||
Command string `yaml:"command"`
|
||||
Schedule string `yaml:"schedule"`
|
||||
Image string `yaml:"image"`
|
||||
Multi bool `yaml:"allPods,omitempty"`
|
||||
}
|
||||
|
||||
func buildCrontab(deployName string, deployment *helm.Deployment, s *types.ServiceConfig, fileGeneratorChan HelmFileGenerator) {
|
||||
// get the cron label from the service
|
||||
var crondef string
|
||||
var ok bool
|
||||
if crondef, ok = s.Labels[helm.LABEL_CRON]; !ok {
|
||||
return
|
||||
}
|
||||
|
||||
// parse yaml
|
||||
crons := []CronDef{}
|
||||
err := yaml.Unmarshal([]byte(crondef), &crons)
|
||||
if err != nil {
|
||||
log.Fatalf("error: %v", err)
|
||||
}
|
||||
|
||||
if len(crons) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
// create a serviceAccount
|
||||
sa := helm.NewServiceAccount(deployName)
|
||||
// create a role
|
||||
role := helm.NewCronRole(deployName)
|
||||
|
||||
// create a roleBinding
|
||||
roleBinding := helm.NewRoleBinding(deployName, sa, role)
|
||||
|
||||
// make generation
|
||||
logger.Magenta(ICON_RBAC, "Generating ServiceAccount, Role and RoleBinding for cron jobs", deployName)
|
||||
fileGeneratorChan <- sa
|
||||
fileGeneratorChan <- role
|
||||
fileGeneratorChan <- roleBinding
|
||||
|
||||
numcron := len(crons) - 1
|
||||
index := 1
|
||||
|
||||
// create crontabs
|
||||
for _, cron := range crons {
|
||||
escaped := shellescape.Quote(cron.Command)
|
||||
var cmd, podget string
|
||||
if cron.Multi {
|
||||
podget = cronMulti
|
||||
cmd = cronMultiCmd
|
||||
} else {
|
||||
podget = cronSingle
|
||||
cmd = cronCmd
|
||||
}
|
||||
podget = fmt.Sprintf(podget, helm.K, deployName, helm.K)
|
||||
cmd = fmt.Sprintf(cmd, s.Name, escaped)
|
||||
cmd = podget + cmd
|
||||
|
||||
if cron.Image == "" {
|
||||
cron.Image = `bitnami/kubectl:{{ printf "%s.%s" .Capabilities.KubeVersion.Major .Capabilities.KubeVersion.Minor }}`
|
||||
}
|
||||
|
||||
name := deployName
|
||||
if numcron > 0 {
|
||||
name = fmt.Sprintf("%s-%d", deployName, index)
|
||||
}
|
||||
|
||||
// add crontab
|
||||
suffix := ""
|
||||
if numcron > 0 {
|
||||
suffix = fmt.Sprintf("%d", index)
|
||||
}
|
||||
cronTab := helm.NewCrontab(
|
||||
name,
|
||||
cron.Image,
|
||||
cmd,
|
||||
cron.Schedule,
|
||||
sa,
|
||||
)
|
||||
logger.Magenta(ICON_CRON, "Generating crontab", deployName, suffix)
|
||||
fileGeneratorChan <- cronTab
|
||||
index++
|
||||
}
|
||||
|
||||
return
|
||||
}
|
Reference in New Issue
Block a user