2020-10-21 14:19:46 +00:00
|
|
|
package jobspec2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/api"
|
|
|
|
)
|
|
|
|
|
2020-11-09 21:23:09 +00:00
|
|
|
func normalizeJob(jc *jobConfig) {
|
|
|
|
j := jc.Job
|
2020-10-21 14:19:46 +00:00
|
|
|
if j.Name == nil {
|
2020-11-09 21:23:09 +00:00
|
|
|
j.Name = &jc.JobID
|
2020-10-21 14:19:46 +00:00
|
|
|
}
|
|
|
|
if j.ID == nil {
|
2020-11-09 21:23:09 +00:00
|
|
|
j.ID = &jc.JobID
|
2020-10-21 14:19:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if j.Periodic != nil && j.Periodic.Spec != nil {
|
|
|
|
v := "cron"
|
|
|
|
j.Periodic.SpecType = &v
|
|
|
|
}
|
|
|
|
|
2020-11-09 21:23:09 +00:00
|
|
|
normalizeVault(jc.Vault)
|
2020-10-21 14:19:46 +00:00
|
|
|
|
2020-11-09 21:23:09 +00:00
|
|
|
if len(jc.Tasks) != 0 {
|
|
|
|
alone := make([]*api.TaskGroup, 0, len(jc.Tasks))
|
|
|
|
for _, t := range jc.Tasks {
|
2020-10-21 14:19:46 +00:00
|
|
|
alone = append(alone, &api.TaskGroup{
|
|
|
|
Name: &t.Name,
|
|
|
|
Tasks: []*api.Task{t},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
alone = append(alone, j.TaskGroups...)
|
|
|
|
j.TaskGroups = alone
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tg := range j.TaskGroups {
|
|
|
|
normalizeNetworkPorts(tg.Networks)
|
|
|
|
for _, t := range tg.Tasks {
|
|
|
|
if t.Resources != nil {
|
|
|
|
normalizeNetworkPorts(t.Resources.Networks)
|
|
|
|
}
|
|
|
|
|
|
|
|
normalizeTemplates(t.Templates)
|
|
|
|
|
|
|
|
// normalize Vault
|
|
|
|
normalizeVault(t.Vault)
|
|
|
|
|
|
|
|
if t.Vault == nil {
|
2020-11-09 21:23:09 +00:00
|
|
|
t.Vault = jc.Vault
|
2020-10-21 14:19:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func normalizeVault(v *api.Vault) {
|
|
|
|
if v == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.Env == nil {
|
|
|
|
v.Env = boolToPtr(true)
|
|
|
|
}
|
|
|
|
if v.ChangeMode == nil {
|
|
|
|
v.ChangeMode = stringToPtr("restart")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func normalizeNetworkPorts(networks []*api.NetworkResource) {
|
|
|
|
if networks == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, n := range networks {
|
|
|
|
if len(n.DynamicPorts) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
dynamic := make([]api.Port, 0, len(n.DynamicPorts))
|
|
|
|
var reserved []api.Port
|
|
|
|
|
|
|
|
for _, p := range n.DynamicPorts {
|
|
|
|
if p.Value > 0 {
|
|
|
|
reserved = append(reserved, p)
|
|
|
|
} else {
|
|
|
|
dynamic = append(dynamic, p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(dynamic) == 0 {
|
|
|
|
dynamic = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
n.DynamicPorts = dynamic
|
|
|
|
n.ReservedPorts = reserved
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func normalizeTemplates(templates []*api.Template) {
|
|
|
|
if len(templates) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, t := range templates {
|
|
|
|
if t.ChangeMode == nil {
|
|
|
|
t.ChangeMode = stringToPtr("restart")
|
|
|
|
}
|
|
|
|
if t.Perms == nil {
|
|
|
|
t.Perms = stringToPtr("0644")
|
|
|
|
}
|
2022-08-02 20:15:38 +00:00
|
|
|
if t.Uid == nil {
|
2022-08-04 15:26:08 +00:00
|
|
|
t.Uid = intToPtr(-1)
|
2022-08-02 20:15:38 +00:00
|
|
|
}
|
|
|
|
if t.Gid == nil {
|
2022-08-04 15:26:08 +00:00
|
|
|
t.Gid = intToPtr(-1)
|
2022-08-02 20:15:38 +00:00
|
|
|
}
|
2020-10-21 14:19:46 +00:00
|
|
|
if t.Splay == nil {
|
|
|
|
t.Splay = durationToPtr(5 * time.Second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func int8ToPtr(v int8) *int8 {
|
|
|
|
return &v
|
|
|
|
}
|
|
|
|
|
|
|
|
func boolToPtr(v bool) *bool {
|
|
|
|
return &v
|
|
|
|
}
|
|
|
|
|
2022-08-02 20:15:38 +00:00
|
|
|
func intToPtr(v int) *int {
|
|
|
|
return &v
|
|
|
|
}
|
|
|
|
|
2020-10-21 14:19:46 +00:00
|
|
|
func stringToPtr(v string) *string {
|
|
|
|
return &v
|
|
|
|
}
|
|
|
|
|
|
|
|
func durationToPtr(v time.Duration) *time.Duration {
|
|
|
|
return &v
|
|
|
|
}
|