forked from jenkins-x/jx
/
containers.go
67 lines (56 loc) · 1.46 KB
/
containers.go
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
package kube
import (
corev1 "k8s.io/api/core/v1"
)
// GetEnvVar returns the env var if its defined for the given name
func GetEnvVar(container *corev1.Container, name string) *corev1.EnvVar {
if container == nil {
return nil
}
return GetSliceEnvVar(container.Env, name)
}
func GetVolumeMount(volumenMounts *[]corev1.VolumeMount, name string) *corev1.VolumeMount {
if volumenMounts != nil {
for idx, v := range *volumenMounts {
if v.Name == name {
return &(*volumenMounts)[idx]
}
}
}
return nil
}
func GetVolume(volumes *[]corev1.Volume, name string) *corev1.Volume {
if volumes != nil {
for idx, v := range *volumes {
if v.Name == name {
return &(*volumes)[idx]
}
}
}
return nil
}
func entrypointIndex(args []string) *int {
for i, a := range args {
if a == "-entrypoint" {
return &i
}
}
return nil
}
// GetCommandAndArgs extracts the command and arguments for the container, taking into account the
// entrypoint invocation if it's not an init container
func GetCommandAndArgs(container *corev1.Container, isInit bool) ([]string, []string) {
if isInit {
return container.Command, container.Args
}
idx := entrypointIndex(container.Args)
if idx == nil {
// TODO: Logging here probably
return nil, nil
} else if *idx >= len(container.Args) {
// TODO: Logging here probably
return nil, nil
}
// Args ends up as [..., "-entrypoint", COMMAND, "--", ARGS...]
return []string{container.Args[*idx+1]}, container.Args[*idx+3:]
}