-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathparse_job.go
More file actions
169 lines (142 loc) · 3.11 KB
/
parse_job.go
File metadata and controls
169 lines (142 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright IBM Corp. 2015, 2025
// SPDX-License-Identifier: MPL-2.0
package jobspec2
import (
"slices"
"time"
"github.com/hashicorp/nomad/api"
)
func normalizeJob(jc *jobConfig) {
j := jc.Job
if j.Name == nil {
j.Name = &jc.JobID
}
if j.ID == nil {
j.ID = &jc.JobID
}
if j.Periodic != nil && (j.Periodic.Spec != nil || j.Periodic.Specs != nil) {
v := "cron"
j.Periodic.SpecType = &v
}
normalizeVault(jc.Vault)
if len(jc.Tasks) != 0 {
alone := make([]*api.TaskGroup, 0, len(jc.Tasks))
for _, t := range jc.Tasks {
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 {
t.Vault = jc.Vault
}
if len(t.Secrets) == 0 {
t.Secrets = jc.Secrets
} else {
t.Secrets = append(t.Secrets, jc.Secrets...)
}
//COMPAT To preserve compatibility with pre-1.7 agents, move the default
// identity to Task.Identity.
defaultIdx := -1
for i, wid := range t.Identities {
if wid.Name == "" || wid.Name == "default" {
t.Identity = wid
defaultIdx = i
break
}
}
// If the default identity was found in Identities above, remove it from the
// slice.
if defaultIdx >= 0 {
t.Identities = slices.Delete(t.Identities, defaultIdx, defaultIdx+1)
}
}
}
}
func normalizeVault(v *api.Vault) {
if v == nil {
return
}
if v.Env == nil {
v.Env = pointerOf(true)
}
if v.DisableFile == nil {
v.DisableFile = pointerOf(false)
}
if v.ChangeMode == nil {
v.ChangeMode = pointerOf("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 = pointerOf("restart")
}
if t.Perms == nil {
t.Perms = pointerOf("0644")
}
if t.Splay == nil {
t.Splay = pointerOf(5 * time.Second)
}
if t.ErrMissingKey == nil {
t.ErrMissingKey = pointerOf(false)
}
normalizeChangeScript(t.ChangeScript)
}
}
func normalizeChangeScript(ch *api.ChangeScript) {
if ch == nil {
return
}
if ch.Args == nil {
ch.Args = []string{}
}
if ch.Timeout == nil {
ch.Timeout = pointerOf(5 * time.Second)
}
if ch.FailOnError == nil {
ch.FailOnError = pointerOf(false)
}
if ch.RunOnFirstRender == nil {
ch.RunOnFirstRender = pointerOf(false)
}
}