github.com/hernad/nomad@v1.6.112/jobspec2/parse_job.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package jobspec2 5 6 import ( 7 "time" 8 9 "github.com/hernad/nomad/api" 10 "github.com/hernad/nomad/helper/pointer" 11 ) 12 13 func normalizeJob(jc *jobConfig) { 14 j := jc.Job 15 if j.Name == nil { 16 j.Name = &jc.JobID 17 } 18 if j.ID == nil { 19 j.ID = &jc.JobID 20 } 21 22 if j.Periodic != nil && j.Periodic.Spec != nil { 23 v := "cron" 24 j.Periodic.SpecType = &v 25 } 26 27 normalizeVault(jc.Vault) 28 29 if len(jc.Tasks) != 0 { 30 alone := make([]*api.TaskGroup, 0, len(jc.Tasks)) 31 for _, t := range jc.Tasks { 32 alone = append(alone, &api.TaskGroup{ 33 Name: &t.Name, 34 Tasks: []*api.Task{t}, 35 }) 36 } 37 alone = append(alone, j.TaskGroups...) 38 j.TaskGroups = alone 39 } 40 41 for _, tg := range j.TaskGroups { 42 normalizeNetworkPorts(tg.Networks) 43 for _, t := range tg.Tasks { 44 if t.Resources != nil { 45 normalizeNetworkPorts(t.Resources.Networks) 46 } 47 48 normalizeTemplates(t.Templates) 49 50 // normalize Vault 51 normalizeVault(t.Vault) 52 53 if t.Vault == nil { 54 t.Vault = jc.Vault 55 } 56 } 57 } 58 } 59 60 func normalizeVault(v *api.Vault) { 61 if v == nil { 62 return 63 } 64 65 if v.Env == nil { 66 v.Env = pointer.Of(true) 67 } 68 if v.DisableFile == nil { 69 v.DisableFile = pointer.Of(false) 70 } 71 if v.ChangeMode == nil { 72 v.ChangeMode = pointer.Of("restart") 73 } 74 } 75 76 func normalizeNetworkPorts(networks []*api.NetworkResource) { 77 if networks == nil { 78 return 79 } 80 for _, n := range networks { 81 if len(n.DynamicPorts) == 0 { 82 continue 83 } 84 85 dynamic := make([]api.Port, 0, len(n.DynamicPorts)) 86 var reserved []api.Port 87 88 for _, p := range n.DynamicPorts { 89 if p.Value > 0 { 90 reserved = append(reserved, p) 91 } else { 92 dynamic = append(dynamic, p) 93 } 94 } 95 if len(dynamic) == 0 { 96 dynamic = nil 97 } 98 99 n.DynamicPorts = dynamic 100 n.ReservedPorts = reserved 101 } 102 103 } 104 105 func normalizeTemplates(templates []*api.Template) { 106 if len(templates) == 0 { 107 return 108 } 109 110 for _, t := range templates { 111 if t.ChangeMode == nil { 112 t.ChangeMode = pointer.Of("restart") 113 } 114 if t.Perms == nil { 115 t.Perms = pointer.Of("0644") 116 } 117 if t.Splay == nil { 118 t.Splay = pointer.Of(5 * time.Second) 119 } 120 if t.ErrMissingKey == nil { 121 t.ErrMissingKey = pointer.Of(false) 122 } 123 normalizeChangeScript(t.ChangeScript) 124 } 125 } 126 127 func normalizeChangeScript(ch *api.ChangeScript) { 128 if ch == nil { 129 return 130 } 131 132 if ch.Args == nil { 133 ch.Args = []string{} 134 } 135 136 if ch.Timeout == nil { 137 ch.Timeout = pointer.Of(5 * time.Second) 138 } 139 140 if ch.FailOnError == nil { 141 ch.FailOnError = pointer.Of(false) 142 } 143 }