-
Notifications
You must be signed in to change notification settings - Fork 9
/
pod_utils.go
57 lines (51 loc) · 1.7 KB
/
pod_utils.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
package kubernetes
// This file includes utility functions for operating `Pod`
// resources in kubernetes.
import (
corev1 "k8s.io/api/core/v1"
)
// GetPodContainerByName takes a PodSpec reference and a string and returns a reference to the container in the PodSpec
// with that name, if any exists.
func GetPodContainerByName(podSpec *corev1.PodSpec, name string) *corev1.Container {
for i, container := range podSpec.Containers {
if container.Name == name {
return &podSpec.Containers[i]
}
}
return nil
}
// SetPodContainer appends a container to the list of containers if it does not exists,
// or it overwrites the existing container, in case it exists.
func SetPodContainer(podSpec *corev1.PodSpec, container *corev1.Container) {
var found bool
for i, c := range podSpec.Containers {
if c.Name == container.Name {
podSpec.Containers[i] = *container
found = true
break
}
}
if !found {
podSpec.Containers = append(podSpec.Containers, *container)
}
}
// GetPodVolumeByName gets the pointer of volume with given name.
// if the volume with given name does not exist in the pod, it returns `nil`.
func GetPodVolumeByName(podSpec *corev1.PodSpec, name string) *corev1.Volume {
for i, volume := range podSpec.Volumes {
if volume.Name == name {
return &podSpec.Volumes[i]
}
}
return nil
}
// GetContainerVolumeMountByPath gets the volume mounted to given path in container.
// if the mount path does not exist, it returns `nil`.
func GetContainerVolumeMountByMountPath(container *corev1.Container, mountPath string) *corev1.VolumeMount {
for i, volumeMount := range container.VolumeMounts {
if volumeMount.MountPath == mountPath {
return &container.VolumeMounts[i]
}
}
return nil
}