github.com/jenkins-x/jx/v2@v2.1.155/pkg/kube/containers.go (about) 1 package kube 2 3 import ( 4 corev1 "k8s.io/api/core/v1" 5 ) 6 7 // GetEnvVar returns the env var if its defined for the given name 8 func GetEnvVar(container *corev1.Container, name string) *corev1.EnvVar { 9 if container == nil { 10 return nil 11 } 12 return GetSliceEnvVar(container.Env, name) 13 } 14 15 func GetVolumeMount(volumenMounts *[]corev1.VolumeMount, name string) *corev1.VolumeMount { 16 if volumenMounts != nil { 17 for idx, v := range *volumenMounts { 18 if v.Name == name { 19 return &(*volumenMounts)[idx] 20 } 21 } 22 } 23 return nil 24 } 25 26 func GetVolume(volumes *[]corev1.Volume, name string) *corev1.Volume { 27 if volumes != nil { 28 for idx, v := range *volumes { 29 if v.Name == name { 30 return &(*volumes)[idx] 31 } 32 } 33 } 34 return nil 35 36 } 37 38 func entrypointIndex(args []string) *int { 39 for i, a := range args { 40 if a == "-entrypoint" { 41 return &i 42 } 43 } 44 45 return nil 46 } 47 48 // GetCommandAndArgs extracts the command and arguments for the container, taking into account the 49 // entrypoint invocation if it's not an init container 50 func GetCommandAndArgs(container *corev1.Container, isInit bool) ([]string, []string) { 51 if isInit { 52 return container.Command, container.Args 53 } 54 55 idx := entrypointIndex(container.Args) 56 57 if idx == nil { 58 // TODO: Logging here probably 59 return nil, nil 60 } else if *idx >= len(container.Args) { 61 // TODO: Logging here probably 62 return nil, nil 63 } 64 65 // Args ends up as [..., "-entrypoint", COMMAND, "--", ARGS...] 66 return []string{container.Args[*idx+1]}, container.Args[*idx+3:] 67 }