-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathtemplate.go
More file actions
409 lines (365 loc) · 11.8 KB
/
template.go
File metadata and controls
409 lines (365 loc) · 11.8 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
package mustgather
import (
"fmt"
"math"
"strconv"
"time"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/openshift/must-gather-operator/api/v1alpha1"
"github.com/operator-framework/operator-lib/proxy"
)
const (
infraNodeLabelKey = "node-role.kubernetes.io/infra"
outputVolumeName = "must-gather-output"
uploadVolumeName = "must-gather-upload"
trustedCAVolumeName = "trusted-ca"
volumeMountPath = "/must-gather"
volumeUploadMountPath = "/must-gather-upload"
trustedCAMountPath = "/etc/pki/tls/certs"
gatherCommandBinaryAudit = "gather_audit_logs"
gatherCommandBinaryNoAudit = "gather"
gatherCommand = "timeout %v bash -x -c -- '/usr/bin/%v' 2>&1 | tee /must-gather/must-gather.log\n\nstatus=$?\nif [[ $status -eq 124 || $status -eq 137 ]]; then\n echo \"Gather timed out.\"\n exit 0\nfi | tee -a /must-gather/must-gather.log"
gatherContainerName = "gather"
// Environment variables for time-based log filtering
gatherEnvSince = "MUST_GATHER_SINCE"
gatherEnvSinceTime = "MUST_GATHER_SINCE_TIME"
backoffLimit = 3
uploadContainerName = "upload"
uploadEnvUsername = "username"
uploadEnvPassword = "password"
uploadEnvCaseId = "caseid"
uploadEnvHost = "host"
uploadEnvInternalUser = "internal_user"
uploadEnvHttpProxy = "http_proxy"
uploadEnvHttpsProxy = "https_proxy"
uploadEnvNoProxy = "no_proxy"
uploadEnvMustGatherOutput = "must_gather_output"
uploadEnvMustGatherUpload = "must_gather_upload"
uploadCommand = "count=0\nuntil [ $count -gt 4 ]\ndo\n while `pgrep -a gather > /dev/null`\n do\n echo \"waiting for gathers to complete ...\"\n sleep 120\n count=0\n done\n echo \"no gather is running ($count / 4)\"\n ((count++))\n sleep 30\ndone\n/usr/local/bin/upload"
// SSH directory and known hosts file
sshDir = "/tmp/must-gather-operator/.ssh"
knownHostsFile = "/tmp/must-gather-operator/.ssh/known_hosts"
)
// timeNow exists to allow deterministic unit testing of time-based behavior.
var timeNow = time.Now
// GatherTimeFilter holds the time-based filtering options for log collection
type GatherTimeFilter struct {
// Since is a relative duration (e.g., "2h", "30m")
Since time.Duration
// SinceTime is an absolute timestamp
SinceTime *time.Time
}
func getJobTemplate(image string, operatorImage string, mustGather v1alpha1.MustGather, trustedCAConfigMapName string, clusterCreationTime *time.Time) *batchv1.Job {
job := initializeJobTemplate(mustGather.Name, mustGather.Namespace, mustGather.Spec.ServiceAccountName, mustGather.Spec.Storage, trustedCAConfigMapName)
var httpProxy, httpsProxy, noProxy string
// Use operator's environment proxy variables
envVars := proxy.ReadProxyVarsFromEnv()
// the below loop should implicitly handle len(envVars) > 0
for _, envVar := range envVars {
switch envVar.Name {
case "HTTP_PROXY":
httpProxy = envVar.Value
case "HTTPS_PROXY":
httpsProxy = envVar.Value
case "NO_PROXY":
noProxy = envVar.Value
}
}
var audit bool
if mustGather.Spec.GatherSpec != nil {
audit = mustGather.Spec.GatherSpec.Audit
}
timeout := time.Duration(0)
if mustGather.Spec.MustGatherTimeout != nil {
timeout = mustGather.Spec.MustGatherTimeout.Duration
}
// Build time filter from spec
var timeFilter *GatherTimeFilter
var command, args []string
if mustGather.Spec.GatherSpec != nil {
command = mustGather.Spec.GatherSpec.Command
args = mustGather.Spec.GatherSpec.Args
if mustGather.Spec.GatherSpec.Since != nil || mustGather.Spec.GatherSpec.SinceTime != nil {
timeFilter = &GatherTimeFilter{}
if mustGather.Spec.GatherSpec.Since != nil {
timeFilter.Since = mustGather.Spec.GatherSpec.Since.Duration
}
if mustGather.Spec.GatherSpec.SinceTime != nil {
t := mustGather.Spec.GatherSpec.SinceTime.Time
timeFilter.SinceTime = &t
}
}
}
job.Spec.Template.Spec.Containers = append(
job.Spec.Template.Spec.Containers,
getGatherContainer(image, audit, timeout, mustGather.Spec.Storage, trustedCAConfigMapName, timeFilter, clusterCreationTime, command, args),
)
// Add the upload container only if the upload target is specified
if mustGather.Spec.UploadTarget != nil && mustGather.Spec.UploadTarget.Type == v1alpha1.UploadTypeSFTP {
s := mustGather.Spec.UploadTarget.SFTP
if s != nil && s.CaseID != "" && s.CaseManagementAccountSecretRef.Name != "" {
job.Spec.Template.Spec.Containers = append(
job.Spec.Template.Spec.Containers,
getUploadContainer(
operatorImage,
s.CaseID,
s.Host,
s.InternalUser,
httpProxy,
httpsProxy,
noProxy,
s.CaseManagementAccountSecretRef,
trustedCAConfigMapName != "",
),
)
}
}
return job
}
func initializeJobTemplate(name string, namespace string, serviceAccountRef string, storage *v1alpha1.Storage, trustedCAConfigMapName string) *batchv1.Job {
outputVolume := corev1.Volume{
Name: outputVolumeName,
VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}},
}
if storage != nil && storage.Type == v1alpha1.StorageTypePersistentVolume {
outputVolume.VolumeSource = corev1.VolumeSource{
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
ClaimName: storage.PersistentVolume.Claim.Name,
},
}
}
volumes := []corev1.Volume{
outputVolume,
{
Name: uploadVolumeName,
VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}},
},
}
// Add trusted CA volume if configmap name is provided
if trustedCAConfigMapName != "" {
volumes = append(volumes, corev1.Volume{
Name: trustedCAVolumeName,
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: trustedCAConfigMapName,
},
},
},
})
}
return &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Spec: batchv1.JobSpec{
BackoffLimit: ToPtr(int32(backoffLimit)),
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Affinity: &corev1.Affinity{
NodeAffinity: &corev1.NodeAffinity{
PreferredDuringSchedulingIgnoredDuringExecution: []corev1.PreferredSchedulingTerm{
{
Preference: corev1.NodeSelectorTerm{
MatchExpressions: []corev1.NodeSelectorRequirement{
{
Key: infraNodeLabelKey,
Operator: corev1.NodeSelectorOpExists,
},
},
},
Weight: 1,
},
},
},
},
Tolerations: []corev1.Toleration{
{
Effect: corev1.TaintEffectNoSchedule,
Key: infraNodeLabelKey,
Operator: corev1.TolerationOpExists,
},
},
RestartPolicy: corev1.RestartPolicyNever,
ShareProcessNamespace: ToPtr(true),
Volumes: volumes,
ServiceAccountName: serviceAccountRef,
},
},
},
}
}
func getGatherContainer(image string, audit bool, timeout time.Duration, storage *v1alpha1.Storage, trustedCAConfigMapName string, timeFilter *GatherTimeFilter, clusterCreationTime *time.Time, command []string, args []string) corev1.Container {
var commandBinary string
if audit {
commandBinary = gatherCommandBinaryAudit
} else {
commandBinary = gatherCommandBinaryNoAudit
}
volumeMount := corev1.VolumeMount{
MountPath: volumeMountPath,
Name: outputVolumeName,
}
if storage != nil && storage.Type == v1alpha1.StorageTypePersistentVolume && storage.PersistentVolume.SubPath != "" {
volumeMount.SubPath = storage.PersistentVolume.SubPath
}
volumeMounts := []corev1.VolumeMount{volumeMount}
// Add trusted CA mount if configmap name is provided
if trustedCAConfigMapName != "" {
volumeMounts = append(volumeMounts, corev1.VolumeMount{
Name: trustedCAVolumeName,
MountPath: trustedCAMountPath,
ReadOnly: true,
})
}
container := corev1.Container{
Image: image,
Name: gatherContainerName,
VolumeMounts: volumeMounts,
}
if len(command) > 0 {
container.Command = command
} else {
container.Command = []string{
"/bin/bash",
"-c",
fmt.Sprintf(gatherCommand, math.Ceil(timeout.Seconds()), commandBinary),
}
}
if len(args) > 0 {
container.Args = args
}
// Add time filter environment variables if specified
if timeFilter != nil {
// Clamp the time filter so it never precedes cluster creation time.
// - For Since (duration): ensure (now - since) >= clusterCreationTime by reducing Since to clusterAge.
// - For SinceTime (absolute): ensure sinceTime >= clusterCreationTime by bumping it up.
effectiveSince := timeFilter.Since
effectiveSinceTime := timeFilter.SinceTime
if clusterCreationTime != nil && !clusterCreationTime.IsZero() {
now := timeNow()
if effectiveSince > 0 {
clusterAge := now.Sub(*clusterCreationTime)
if clusterAge < 0 {
clusterAge = 0
}
if effectiveSince > clusterAge {
effectiveSince = clusterAge
}
}
if effectiveSinceTime != nil && effectiveSinceTime.Before(*clusterCreationTime) {
t := *clusterCreationTime
effectiveSinceTime = &t
}
}
if effectiveSince > 0 {
container.Env = append(container.Env, corev1.EnvVar{
Name: gatherEnvSince,
Value: effectiveSince.String(),
})
}
if effectiveSinceTime != nil {
container.Env = append(container.Env, corev1.EnvVar{
Name: gatherEnvSinceTime,
Value: effectiveSinceTime.Format(time.RFC3339),
})
}
}
return container
}
func getUploadContainer(
operatorImage string,
caseId string,
host string,
internalUser bool,
httpProxy string,
httpsProxy string,
noProxy string,
secretKeyRefName corev1.LocalObjectReference,
shouldMountTrustedCAConfigMap bool,
) corev1.Container {
// Create the modified upload command that includes SSH setup
uploadCommandWithSSH := fmt.Sprintf("mkdir -p %s; touch %s; chmod 700 %s; chmod 600 %s; %s",
sshDir, knownHostsFile, sshDir, knownHostsFile, uploadCommand)
volumeMounts := []corev1.VolumeMount{
{
MountPath: volumeMountPath,
Name: outputVolumeName,
},
{
MountPath: volumeUploadMountPath,
Name: uploadVolumeName,
},
}
if shouldMountTrustedCAConfigMap {
volumeMounts = append(volumeMounts, corev1.VolumeMount{
Name: trustedCAVolumeName,
MountPath: trustedCAMountPath,
ReadOnly: true,
})
}
container := corev1.Container{
Command: []string{
"/bin/bash",
"-c",
uploadCommandWithSSH,
},
Image: operatorImage,
Name: uploadContainerName,
VolumeMounts: volumeMounts,
Env: []corev1.EnvVar{
{
Name: uploadEnvUsername,
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
Key: uploadEnvUsername,
LocalObjectReference: secretKeyRefName,
},
},
},
{
Name: uploadEnvPassword,
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
Key: uploadEnvPassword,
LocalObjectReference: secretKeyRefName,
},
},
},
{
Name: uploadEnvCaseId,
Value: caseId,
},
{
Name: uploadEnvHost,
Value: host,
},
{
Name: uploadEnvMustGatherOutput,
Value: volumeMountPath,
},
{
Name: uploadEnvMustGatherUpload,
Value: volumeUploadMountPath,
},
{
Name: uploadEnvInternalUser,
Value: strconv.FormatBool(internalUser),
},
},
}
if httpProxy != "" {
container.Env = append(container.Env, corev1.EnvVar{Name: uploadEnvHttpProxy, Value: httpProxy})
}
if httpsProxy != "" {
container.Env = append(container.Env, corev1.EnvVar{Name: uploadEnvHttpsProxy, Value: httpsProxy})
}
if noProxy != "" {
container.Env = append(container.Env, corev1.EnvVar{Name: uploadEnvNoProxy, Value: noProxy})
}
return container
}
func ToPtr[T any](t T) *T { return &t }