• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kubescape / k8s-interface / 6625443749

24 Oct 2023 10:14AM UTC coverage: 29.311% (+0.2%) from 29.1%
6625443749

push

github

web-flow
Merge pull request #78 from kubescape/volumes_functions

workload volume functions

16 of 16 new or added lines in 1 file covered. (100.0%)

1450 of 4947 relevant lines covered (29.31%)

4.76 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

49.02
/workloadinterface/workloadmethods.go
1
package workloadinterface
2

3
import (
4
        "encoding/json"
5
        "errors"
6
        "fmt"
7
        "strconv"
8
        "strings"
9

10
        "github.com/armosec/armoapi-go/apis"
11
        "github.com/armosec/utils-k8s-go/armometadata"
12
        wlidpkg "github.com/armosec/utils-k8s-go/wlid"
13
        corev1 "k8s.io/api/core/v1"
14
        v1 "k8s.io/api/core/v1"
15
        metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
16
        "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
17
        "k8s.io/utils/strings/slices"
18
)
19

20
const TypeWorkloadObject ObjectType = "workload"
21

22
type Workload struct {
23
        workload map[string]interface{}
24
}
25

26
func NewWorkload(bWorkload []byte) (*Workload, error) {
45✔
27
        workload := make(map[string]interface{})
45✔
28
        if bWorkload != nil {
90✔
29
                if err := json.Unmarshal(bWorkload, &workload); err != nil {
45✔
30
                        return nil, err
×
31
                }
×
32
        }
33
        // if !IsTypeWorkload(workload) {
34
        //         return nil, fmt.Errorf("invalid workload - expected k8s workload")
35
        // }
36
        return &Workload{
45✔
37
                workload: workload,
45✔
38
        }, nil
45✔
39
}
40

41
func NewWorkloadObj(workload map[string]interface{}) *Workload {
2✔
42
        return &Workload{
2✔
43
                workload: workload,
2✔
44
        }
2✔
45
}
2✔
46

47
func (w *Workload) GetObjectType() ObjectType {
×
48
        return TypeWorkloadObject
×
49
}
×
50

51
func (w *Workload) Json() string {
×
52
        return w.ToString()
×
53
}
×
54
func (w *Workload) ToString() string {
×
55
        if w.GetWorkload() == nil {
×
56
                return ""
×
57
        }
×
58
        bWorkload, err := json.Marshal(w.GetWorkload())
×
59
        if err != nil {
×
60
                return err.Error()
×
61
        }
×
62
        return string(bWorkload)
×
63
}
64

65
func (workload *Workload) DeepCopy(w map[string]interface{}) {
×
66
        workload.workload = make(map[string]interface{})
×
67
        byt, _ := json.Marshal(w)
×
68
        json.Unmarshal(byt, &workload.workload)
×
69
}
×
70

71
func (w *Workload) ToUnstructured() (*unstructured.Unstructured, error) {
×
72
        obj := &unstructured.Unstructured{}
×
73
        if w.workload == nil {
×
74
                return obj, nil
×
75
        }
×
76
        bWorkload, err := json.Marshal(w.workload)
×
77
        if err != nil {
×
78
                return obj, err
×
79
        }
×
80
        if err := json.Unmarshal(bWorkload, obj); err != nil {
×
81
                return obj, err
×
82

×
83
        }
×
84

85
        return obj, nil
×
86
}
87

88
// ======================================= DELETE ========================================
89

90
func (w *Workload) RemoveJobID() {
×
91
        w.RemovePodAnnotation(armometadata.ArmoJobIDPath)
×
92
        w.RemovePodAnnotation(armometadata.ArmoJobParentPath)
×
93
        w.RemovePodAnnotation(armometadata.ArmoJobActionPath)
×
94

×
95
        w.RemoveAnnotation(armometadata.ArmoJobIDPath)
×
96
        w.RemoveAnnotation(armometadata.ArmoJobParentPath)
×
97
        w.RemoveAnnotation(armometadata.ArmoJobActionPath)
×
98
}
×
99

100
func (w *Workload) RemoveSecretData() {
×
101
        w.RemoveAnnotation("kubectl.kubernetes.io/last-applied-configuration")
×
102
        delete(w.workload, "data")
×
103
}
×
104

105
func (w *Workload) RemovePodStatus() {
×
106
        delete(w.workload, "status")
×
107
}
×
108

109
func (w *Workload) RemoveResourceVersion() {
×
110
        if _, ok := w.workload["metadata"]; !ok {
×
111
                return
×
112
        }
×
113
        meta, _ := w.workload["metadata"].(map[string]interface{})
×
114
        delete(meta, "resourceVersion")
×
115
}
116

117
func (w *Workload) RemoveLabel(key string) {
1✔
118
        w.RemoveMetadata([]string{"metadata"}, "labels", key)
1✔
119
}
1✔
120

121
func (w *Workload) RemoveAnnotation(key string) {
1✔
122
        w.RemoveMetadata([]string{"metadata"}, "annotations", key)
1✔
123
}
1✔
124

125
func (w *Workload) RemovePodAnnotation(key string) {
×
126
        w.RemoveMetadata(PodMetadata(w.GetKind()), "annotations", key)
×
127
}
×
128

129
func (w *Workload) RemovePodLabel(key string) {
1✔
130
        w.RemoveMetadata(PodMetadata(w.GetKind()), "labels", key)
1✔
131
}
1✔
132

133
func (w *Workload) RemoveMetadata(scope []string, metadata, key string) {
3✔
134

3✔
135
        workload := w.workload
3✔
136
        for i := range scope {
8✔
137
                if _, ok := workload[scope[i]]; !ok {
5✔
138
                        return
×
139
                }
×
140
                workload, _ = workload[scope[i]].(map[string]interface{})
5✔
141
        }
142

143
        if _, ok := workload[metadata]; !ok {
3✔
144
                return
×
145
        }
×
146

147
        labels, _ := workload[metadata].(map[string]interface{})
3✔
148
        delete(labels, key)
3✔
149

150
}
151

152
// ========================================= SET =========================================
153

154
func (w *Workload) SetWorkload(workload map[string]interface{}) {
×
155
        w.SetObject(workload)
×
156
}
×
157

158
func (w *Workload) SetObject(workload map[string]interface{}) {
×
159
        w.workload = workload
×
160
}
×
161

162
func (w *Workload) SetApiVersion(apiVersion string) {
×
163
        w.workload["apiVersion"] = apiVersion
×
164
}
×
165

166
func (w *Workload) SetKind(kind string) {
×
167
        w.workload["kind"] = kind
×
168
}
×
169

170
func (w *Workload) SetJobID(jobTracking apis.JobTracking) {
×
171
        w.SetPodAnnotation(armometadata.ArmoJobIDPath, jobTracking.JobID)
×
172
        w.SetPodAnnotation(armometadata.ArmoJobParentPath, jobTracking.ParentID)
×
173
        w.SetPodAnnotation(armometadata.ArmoJobActionPath, fmt.Sprintf("%d", jobTracking.LastActionNumber))
×
174
}
×
175

176
func (w *Workload) SetNamespace(namespace string) {
1✔
177
        SetInMap(w.workload, []string{"metadata"}, "namespace", namespace)
1✔
178
}
1✔
179

180
func (w *Workload) SetName(name string) {
×
181
        SetInMap(w.workload, []string{"metadata"}, "name", name)
×
182
}
×
183

184
func (w *Workload) SetLabel(key, value string) {
1✔
185
        SetInMap(w.workload, []string{"metadata", "labels"}, key, value)
1✔
186
}
1✔
187

188
func (w *Workload) SetPodLabel(key, value string) {
1✔
189
        SetInMap(w.workload, append(PodMetadata(w.GetKind()), "labels"), key, value)
1✔
190
}
1✔
191
func (w *Workload) SetAnnotation(key, value string) {
1✔
192
        SetInMap(w.workload, []string{"metadata", "annotations"}, key, value)
1✔
193
}
1✔
194
func (w *Workload) SetPodAnnotation(key, value string) {
×
195
        SetInMap(w.workload, append(PodMetadata(w.GetKind()), "annotations"), key, value)
×
196
}
×
197

198
// ========================================= GET =========================================
199
func (w *Workload) GetWorkload() map[string]interface{} {
×
200
        return w.GetObject()
×
201
}
×
202
func (w *Workload) GetObject() map[string]interface{} {
3✔
203
        return w.workload
3✔
204
}
3✔
205
func (w *Workload) GetNamespace() string {
4✔
206
        if v, ok := InspectWorkload(w.workload, "metadata", "namespace"); ok {
7✔
207
                return v.(string)
3✔
208
        }
3✔
209
        return ""
1✔
210
}
211
func (w *Workload) GetID() string {
2✔
212
        return fmt.Sprintf("%s/%s/%s/%s/%s", w.GetGroup(), w.GetVersion(), w.GetNamespace(), w.GetKind(), w.GetName())
2✔
213

2✔
214
        // TODO - return like selfLink - e.g. /apis/apps/v1/namespaces/monitoring/statefulsets/alertmanager-prometheus-
2✔
215
        // return fmt.Sprintf("apps/%s/%s/%s/%s", w.GetApiVersion(), w.GetNamespace(), w.GetKind(), w.GetName())
2✔
216
}
2✔
217
func (w *Workload) GetName() string {
5✔
218
        if v, ok := InspectWorkload(w.workload, "metadata", "name"); ok {
9✔
219
                return v.(string)
4✔
220
        }
4✔
221
        return ""
1✔
222
}
223

224
func (w *Workload) GetData() map[string]interface{} {
×
225
        if v, ok := InspectWorkload(w.workload, "data"); ok {
×
226
                return v.(map[string]interface{})
×
227
        }
×
228
        return nil
×
229
}
230

231
func (w *Workload) GetApiVersion() string {
4✔
232
        if v, ok := InspectWorkload(w.workload, "apiVersion"); ok {
8✔
233
                return v.(string)
4✔
234
        }
4✔
235
        return ""
×
236
}
237

238
func (w *Workload) GetVersion() string {
2✔
239
        apiVersion := w.GetApiVersion()
2✔
240
        splitted := strings.Split(apiVersion, "/")
2✔
241
        if len(splitted) == 1 {
3✔
242
                return splitted[0]
1✔
243
        } else if len(splitted) == 2 {
3✔
244
                return splitted[1]
1✔
245
        }
1✔
246
        return ""
×
247
}
248

249
func (w *Workload) GetGroup() string {
2✔
250
        apiVersion := w.GetApiVersion()
2✔
251
        splitted := strings.Split(apiVersion, "/")
2✔
252
        if len(splitted) == 2 {
3✔
253
                return splitted[0]
1✔
254
        }
1✔
255
        return ""
1✔
256
}
257

258
func (w *Workload) GetGenerateName() string {
×
259
        if v, ok := InspectWorkload(w.workload, "metadata", "generateName"); ok {
×
260
                return v.(string)
×
261
        }
×
262
        return ""
×
263
}
264

265
func (w *Workload) GetReplicas() int {
1✔
266
        if v, ok := InspectWorkload(w.workload, "spec", "replicas"); ok {
2✔
267
                switch n := v.(type) {
1✔
268
                case float64:
×
269
                        return int(n)
×
270
                case int64:
×
271
                        return int(n)
×
272
                case float32:
×
273
                        return int(n)
×
274
                case int32:
×
275
                        return int(n)
×
276
                case int16:
×
277
                        return int(n)
×
278
                case int:
1✔
279
                        return n
1✔
280
                }
281
        }
282
        return 1
×
283
}
284

285
func (w *Workload) GetKind() string {
64✔
286
        if v, ok := InspectWorkload(w.workload, "kind"); ok {
128✔
287
                return v.(string)
64✔
288
        }
64✔
289
        return ""
×
290
}
291
func (w *Workload) GetSelector() (*metav1.LabelSelector, error) {
1✔
292
        selector := &metav1.LabelSelector{}
1✔
293
        if matchLabels, ok := InspectWorkload(w.workload, "spec", "selector", "matchLabels"); ok && matchLabels != nil {
2✔
294
                if m, ok := matchLabels.(map[string]interface{}); ok {
2✔
295
                        selector.MatchLabels = make(map[string]string, len(m))
1✔
296
                        for k, v := range m {
2✔
297
                                selector.MatchLabels[k] = v.(string)
1✔
298
                        }
1✔
299
                }
300
        }
301
        if matchExpressions, ok := InspectWorkload(w.workload, "spec", "selector", "matchExpressions"); ok && matchExpressions != nil {
1✔
302
                b, err := json.Marshal(matchExpressions)
×
303
                if err != nil {
×
304
                        return selector, err
×
305
                }
×
306
                if err := json.Unmarshal(b, &selector.MatchExpressions); err != nil {
×
307
                        return selector, nil
×
308
                }
×
309
        }
310
        return selector, nil
1✔
311
}
312

313
func (w *Workload) GetAnnotation(annotation string) (string, bool) {
2✔
314
        if v, ok := InspectWorkload(w.workload, "metadata", "annotations", annotation); ok {
3✔
315
                return v.(string), ok
1✔
316
        }
1✔
317
        return "", false
1✔
318
}
319
func (w *Workload) GetLabel(label string) (string, bool) {
2✔
320
        if v, ok := InspectWorkload(w.workload, "metadata", "labels", label); ok {
3✔
321
                return v.(string), ok
1✔
322
        }
1✔
323
        return "", false
1✔
324
}
325

326
func (w *Workload) GetPodLabel(label string) (string, bool) {
2✔
327
        if v, ok := InspectWorkload(w.workload, append(PodMetadata(w.GetKind()), "labels", label)...); ok && v != nil {
3✔
328
                return v.(string), ok
1✔
329
        }
1✔
330
        return "", false
1✔
331
}
332

333
func (w *Workload) GetLabels() map[string]string {
1✔
334
        if v, ok := InspectWorkload(w.workload, "metadata", "labels"); ok && v != nil {
2✔
335
                labels := make(map[string]string)
1✔
336
                for k, i := range v.(map[string]interface{}) {
5✔
337
                        // null labels will be ignored
4✔
338
                        if i == nil {
6✔
339
                                continue
2✔
340
                        }
341

342
                        labels[k] = i.(string)
2✔
343
                }
344
                return labels
1✔
345
        }
346
        return nil
×
347
}
348

349
// GetInnerLabels - DEPRECATED
350
func (w *Workload) GetInnerLabels() map[string]string {
×
351
        return w.GetPodLabels()
×
352
}
×
353

354
func (w *Workload) GetPodLabels() map[string]string {
×
355
        if v, ok := InspectWorkload(w.workload, append(PodMetadata(w.GetKind()), "labels")...); ok && v != nil {
×
356
                labels := make(map[string]string)
×
357
                for k, i := range v.(map[string]interface{}) {
×
358
                        labels[k] = i.(string)
×
359
                }
×
360
                return labels
×
361
        }
362
        return nil
×
363
}
364

365
// GetInnerAnnotations - DEPRECATED
366
func (w *Workload) GetInnerAnnotations() map[string]string {
×
367
        return w.GetPodAnnotations()
×
368
}
×
369

370
// GetPodAnnotations
371
func (w *Workload) GetPodAnnotations() map[string]string {
×
372
        if v, ok := InspectWorkload(w.workload, append(PodMetadata(w.GetKind()), "annotations")...); ok && v != nil {
×
373
                annotations := make(map[string]string)
×
374
                for k, i := range v.(map[string]interface{}) {
×
375
                        annotations[k] = fmt.Sprintf("%v", i)
×
376
                }
×
377
                return annotations
×
378
        }
379
        return nil
×
380
}
381

382
// GetInnerAnnotation DEPRECATED
383
func (w *Workload) GetInnerAnnotation(annotation string) (string, bool) {
×
384
        return w.GetPodAnnotation(annotation)
×
385
}
×
386

387
func (w *Workload) GetPodAnnotation(annotation string) (string, bool) {
×
388
        if v, ok := InspectWorkload(w.workload, append(PodMetadata(w.GetKind()), "annotations", annotation)...); ok && v != nil {
×
389
                return v.(string), ok
×
390
        }
×
391
        return "", false
×
392
}
393

394
func (w *Workload) GetAnnotations() map[string]string {
×
395
        if v, ok := InspectWorkload(w.workload, "metadata", "annotations"); ok && v != nil {
×
396
                annotations := make(map[string]string)
×
397
                for k, i := range v.(map[string]interface{}) {
×
398
                        annotations[k] = fmt.Sprintf("%v", i)
×
399
                }
×
400
                return annotations
×
401
        }
402
        return nil
×
403
}
404

405
func (w *Workload) GetServiceAccountName() string {
×
406

×
407
        if v, ok := InspectWorkload(w.workload, append(PodSpec(w.GetKind()), "serviceAccountName")...); ok && v != nil {
×
408
                return v.(string)
×
409
        }
×
410
        return ""
×
411
}
412

413
func (w *Workload) GetPodSpec() (*corev1.PodSpec, error) {
25✔
414
        podSpec := &corev1.PodSpec{}
25✔
415
        podSepcRaw, _ := InspectWorkload(w.workload, PodSpec(w.GetKind())...)
25✔
416
        if podSepcRaw == nil {
25✔
417
                return podSpec, fmt.Errorf("no PodSpec for workload: %v", w)
×
418
        }
×
419
        b, err := json.Marshal(podSepcRaw)
25✔
420
        if err != nil {
25✔
421
                return podSpec, err
×
422
        }
×
423
        err = json.Unmarshal(b, podSpec)
25✔
424

25✔
425
        return podSpec, err
25✔
426
}
427

428
func (w *Workload) GetImagePullSecret() ([]corev1.LocalObjectReference, error) {
×
429
        imgPullSecrets := []corev1.LocalObjectReference{}
×
430

×
431
        iImgPullSecrets, _ := InspectWorkload(w.workload, append(PodSpec(w.GetKind()), "imagePullSecrets")...)
×
432
        b, err := json.Marshal(iImgPullSecrets)
×
433
        if err != nil {
×
434
                return imgPullSecrets, err
×
435
        }
×
436
        err = json.Unmarshal(b, &imgPullSecrets)
×
437

×
438
        return imgPullSecrets, err
×
439
}
440

441
// GetContainers -
442
func (w *Workload) GetContainers() ([]corev1.Container, error) {
27✔
443
        containers := []corev1.Container{}
27✔
444

27✔
445
        interContainers, _ := InspectWorkload(w.workload, append(PodSpec(w.GetKind()), "containers")...)
27✔
446
        if interContainers == nil {
28✔
447
                return containers, nil
1✔
448
        }
1✔
449
        containersBytes, err := json.Marshal(interContainers)
26✔
450
        if err != nil {
26✔
451
                return containers, err
×
452
        }
×
453
        err = json.Unmarshal(containersBytes, &containers)
26✔
454

26✔
455
        return containers, err
26✔
456
}
457

458
// GetInitContainers -
459
func (w *Workload) GetInitContainers() ([]corev1.Container, error) {
×
460
        containers := []corev1.Container{}
×
461

×
462
        interContainers, _ := InspectWorkload(w.workload, append(PodSpec(w.GetKind()), "initContainers")...)
×
463
        if interContainers == nil {
×
464
                return containers, nil
×
465
        }
×
466
        containersBytes, err := json.Marshal(interContainers)
×
467
        if err != nil {
×
468
                return containers, err
×
469
        }
×
470
        err = json.Unmarshal(containersBytes, &containers)
×
471

×
472
        return containers, err
×
473
}
474

475
// GetOwnerReferences -
476
func (w *Workload) GetOwnerReferences() ([]metav1.OwnerReference, error) {
×
477
        ownerReferences := []metav1.OwnerReference{}
×
478
        interOwnerReferences, ok := InspectWorkload(w.workload, "metadata", "ownerReferences")
×
479
        if !ok {
×
480
                return ownerReferences, nil
×
481
        }
×
482

483
        ownerReferencesBytes, err := json.Marshal(interOwnerReferences)
×
484
        if err != nil {
×
485
                return ownerReferences, err
×
486
        }
×
487
        err = json.Unmarshal(ownerReferencesBytes, &ownerReferences)
×
488
        if err != nil {
×
489
                return ownerReferences, err
×
490

×
491
        }
×
492
        return ownerReferences, nil
×
493
}
494
func (w *Workload) GetResourceVersion() string {
1✔
495
        if v, ok := InspectWorkload(w.workload, "metadata", "resourceVersion"); ok {
2✔
496
                return v.(string)
1✔
497
        }
1✔
498
        return ""
×
499
}
500
func (w *Workload) GetUID() string {
1✔
501
        if v, ok := InspectWorkload(w.workload, "metadata", "uid"); ok {
2✔
502
                return v.(string)
1✔
503
        }
1✔
504
        return ""
×
505
}
506
func (w *Workload) GetWlid() string {
×
507
        if wlid, ok := w.GetAnnotation(armometadata.ArmoWlid); ok {
×
508
                return wlid
×
509
        }
×
510
        return ""
×
511
}
512

513
func (w *Workload) GenerateWlid(clusterName string) string {
×
514
        return wlidpkg.GetK8sWLID(clusterName, w.GetNamespace(), w.GetKind(), w.GetName())
×
515
}
×
516

517
func (w *Workload) GetJobID() *apis.JobTracking {
×
518
        jobTracking := apis.JobTracking{}
×
519
        if job, ok := w.GetPodAnnotation(armometadata.ArmoJobIDPath); ok {
×
520
                jobTracking.JobID = job
×
521
        }
×
522
        if parent, ok := w.GetPodAnnotation(armometadata.ArmoJobParentPath); ok {
×
523
                jobTracking.ParentID = parent
×
524
        }
×
525
        if action, ok := w.GetPodAnnotation(armometadata.ArmoJobActionPath); ok {
×
526
                if i, err := strconv.Atoi(action); err == nil {
×
527
                        jobTracking.LastActionNumber = i
×
528
                }
×
529
        }
530
        if jobTracking.LastActionNumber == 0 { // start the counter at 1
×
531
                jobTracking.LastActionNumber = 1
×
532
        }
×
533
        return &jobTracking
×
534
}
535

536
// Returns map of container name to container's secrets
537
func (w *Workload) GetSecretsOfContainer() (map[string][]string, error) {
12✔
538
        mapMountToSecretName := make(map[string]string)
12✔
539
        volumes, err := w.GetVolumes()
12✔
540
        if err != nil {
12✔
541
                return nil, err
×
542
        }
×
543
        for _, volume := range volumes {
55✔
544
                if volume.Secret != nil {
70✔
545
                        mapMountToSecretName[volume.Name] = volume.Secret.SecretName
27✔
546
                }
27✔
547
        }
548

549
        containers, err := w.GetContainers()
12✔
550
        if err != nil {
12✔
551
                return nil, err
×
552
        }
×
553

554
        secretsOfContainer := make(map[string][]string)
12✔
555
        for _, container := range containers {
36✔
556
                secretsOfContainer[container.Name] = []string{}
24✔
557
                for _, volumeMount := range container.VolumeMounts {
76✔
558
                        if secretName, ok := mapMountToSecretName[volumeMount.Name]; ok {
80✔
559
                                if !slices.Contains(secretsOfContainer[container.Name], secretName) {
56✔
560
                                        secretsOfContainer[container.Name] = append(secretsOfContainer[container.Name], secretName)
28✔
561
                                }
28✔
562
                        }
563
                }
564

565
                for _, envFrom := range container.EnvFrom {
26✔
566
                        if envFrom.SecretRef != nil {
4✔
567
                                if !slices.Contains(secretsOfContainer[container.Name], envFrom.SecretRef.Name) {
4✔
568
                                        secretsOfContainer[container.Name] = append(secretsOfContainer[container.Name], envFrom.SecretRef.Name)
2✔
569
                                }
2✔
570
                        }
571
                }
572

573
                for _, env := range container.Env {
28✔
574
                        if env.ValueFrom != nil && env.ValueFrom.SecretKeyRef != nil {
8✔
575
                                if !slices.Contains(secretsOfContainer[container.Name], env.ValueFrom.SecretKeyRef.Name) {
6✔
576
                                        secretsOfContainer[container.Name] = append(secretsOfContainer[container.Name], env.ValueFrom.SecretKeyRef.Name)
2✔
577
                                }
2✔
578
                        }
579
                }
580

581
        }
582
        return secretsOfContainer, nil
12✔
583
}
584

585
// Returns map of container name to container's configmaps
586
func (w *Workload) GetConfigMapsOfContainer() (map[string][]string, error) {
12✔
587
        mapMountToConfigMapName := make(map[string]string)
12✔
588
        volumes, err := w.GetVolumes()
12✔
589
        if err != nil {
12✔
590
                return nil, err
×
591
        }
×
592
        for _, volume := range volumes {
54✔
593
                if volume.ConfigMap != nil {
61✔
594
                        mapMountToConfigMapName[volume.Name] = volume.ConfigMap.Name
19✔
595
                }
19✔
596
        }
597
        containers, err := w.GetContainers()
12✔
598
        if err != nil {
12✔
599
                return nil, err
×
600
        }
×
601

602
        configMapsOfContainer := make(map[string][]string)
12✔
603
        for _, container := range containers {
36✔
604
                configMapsOfContainer[container.Name] = []string{}
24✔
605
                for _, volumeMount := range container.VolumeMounts {
74✔
606
                        if configMapName, ok := mapMountToConfigMapName[volumeMount.Name]; ok {
70✔
607
                                if !slices.Contains(configMapsOfContainer[container.Name], configMapName) {
40✔
608
                                        configMapsOfContainer[container.Name] = append(configMapsOfContainer[container.Name], configMapName)
20✔
609
                                }
20✔
610
                        }
611
                }
612

613
                for _, envFrom := range container.EnvFrom {
26✔
614
                        if envFrom.ConfigMapRef != nil {
4✔
615
                                if !slices.Contains(configMapsOfContainer[container.Name], envFrom.ConfigMapRef.Name) {
4✔
616
                                        configMapsOfContainer[container.Name] = append(configMapsOfContainer[container.Name], envFrom.ConfigMapRef.Name)
2✔
617
                                }
2✔
618
                        }
619
                }
620

621
                for _, env := range container.Env {
28✔
622
                        if env.ValueFrom != nil && env.ValueFrom.ConfigMapKeyRef != nil {
8✔
623
                                if !slices.Contains(configMapsOfContainer[container.Name], env.ValueFrom.ConfigMapKeyRef.Name) {
6✔
624
                                        configMapsOfContainer[container.Name] = append(configMapsOfContainer[container.Name], env.ValueFrom.ConfigMapKeyRef.Name)
2✔
625
                                }
2✔
626
                        }
627
                }
628

629
        }
630
        return configMapsOfContainer, nil
12✔
631
}
632

633
func (w *Workload) GetSecrets() ([]string, error) {
5✔
634
        secretsOfContainer, err := w.GetSecretsOfContainer()
5✔
635
        if err != nil {
5✔
636
                return nil, err
×
637
        }
×
638
        secrets := []string{}
5✔
639
        for _, scrts := range secretsOfContainer {
14✔
640
                for _, scrt := range scrts {
19✔
641
                        if !slices.Contains(secrets, scrt) {
19✔
642
                                secrets = append(secrets, scrt)
9✔
643
                        }
9✔
644
                }
645
        }
646
        return secrets, nil
5✔
647
}
648

649
func (w *Workload) GetConfigMaps() ([]string, error) {
5✔
650
        cfgMapsOfContainer, err := w.GetConfigMapsOfContainer()
5✔
651
        if err != nil {
5✔
652
                return nil, err
×
653
        }
×
654
        configMaps := []string{}
5✔
655
        for _, cfgMaps := range cfgMapsOfContainer {
14✔
656
                for _, cfgMap := range cfgMaps {
17✔
657
                        if !slices.Contains(configMaps, cfgMap) {
15✔
658
                                configMaps = append(configMaps, cfgMap)
7✔
659
                        }
7✔
660
                }
661
        }
662
        return configMaps, nil
5✔
663
}
664

665
func (w *Workload) GetPodStatus() (*corev1.PodStatus, error) {
2✔
666
        status := corev1.PodStatus{}
2✔
667
        if v, ok := InspectWorkload(w.workload, "status"); ok && v != nil {
3✔
668
                vBytes, err := json.Marshal(v)
1✔
669
                if err != nil {
1✔
670
                        return nil, err
×
671
                }
×
672
                err = json.Unmarshal(vBytes, &status)
1✔
673
                if err != nil {
1✔
674
                        return nil, err
×
675
                }
×
676
        }
677
        return &status, nil
2✔
678
}
679

680
// GetHostVolumes returns all host volumes of the workload
681
func (w *Workload) GetVolumes() ([]v1.Volume, error) {
25✔
682
        podSpec, err := w.GetPodSpec()
25✔
683
        if err != nil {
25✔
684
                return nil, err
×
685
        }
×
686

687
        return podSpec.Volumes, nil
25✔
688
}
689

690
// GetSpecPathPrefix returns the path prefix of the workload spec
691
func (w *Workload) GetSpecPath() (string, error) {
3✔
692
        switch w.GetKind() {
3✔
693
        case "Pod":
1✔
694
                return "spec", nil
1✔
695
        case "Deployment", "ReplicaSet", "DaemonSet", "StatefulSet", "Job":
1✔
696
                return "spec.template.spec", nil
1✔
697
        case "CronJob":
1✔
698
                return "spec.jobTemplate.spec.template.spec", nil
1✔
699
        default:
×
700
                return "", errors.New("unsupported workload kind")
×
701
        }
702
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc