This repository has been archived by the owner on Oct 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 183
/
helpers.go
131 lines (111 loc) · 3.38 KB
/
helpers.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
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
package k8s
import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// NewTrue returns a pointer to a boolean variable set to true
func NewTrue() *bool {
b := true
return &b
}
// NewFalse returns a pointer to a boolean variable set to false
func NewFalse() *bool {
return new(bool)
}
func GetContainers(resource Resource) []*ContainerV1 {
podSpec := GetPodSpec(resource)
if podSpec == nil {
return nil
}
var containers []*ContainerV1
for i := range podSpec.Containers {
containers = append(containers, &podSpec.Containers[i])
}
if len(podSpec.InitContainers) > 0 {
containers = append(containers, GetInitContainers(resource)...)
}
return containers
}
func GetInitContainers(resource Resource) []*ContainerV1 {
podSpec := GetPodSpec(resource)
if podSpec == nil {
return nil
}
containers := make([]*ContainerV1, len(podSpec.InitContainers))
for i := range podSpec.InitContainers {
containers[i] = &podSpec.InitContainers[i]
}
return containers
}
// GetAnnotations returns the annotations at the pod level. If the resource does not have pods, then it returns
// the least-nested annotations
func GetAnnotations(resource Resource) map[string]string {
objectMeta := GetPodObjectMeta(resource)
if objectMeta != nil {
return objectMeta.GetAnnotations()
}
return nil
}
// GetLabels returns the labels at the pod level. If the resource does not have pods, then it returns the
// least-nested labels
func GetLabels(resource Resource) map[string]string {
objectMeta := GetPodObjectMeta(resource)
if objectMeta != nil {
return objectMeta.GetLabels()
}
return nil
}
// GetObjectMeta returns the highest-level ObjectMeta
func GetObjectMeta(resource Resource) metav1.Object {
obj, _ := resource.(metav1.ObjectMetaAccessor)
if obj != nil {
return obj.GetObjectMeta()
}
return nil
}
// GetPodObjectMeta returns the ObjectMeta at the pod level. If the resource does not have pods, then it returns
// the highest-level ObjectMeta
func GetPodObjectMeta(resource Resource) metav1.Object {
podTemplateSpec := GetPodTemplateSpec(resource)
if podTemplateSpec != nil {
return &podTemplateSpec.ObjectMeta
}
return GetObjectMeta(resource)
}
// GetPodSpec gets the PodSpec for a resource. Avoid using this function if you need support for Namespace or
// ServiceAccount resources, and write a helper functions in this package instead
func GetPodSpec(resource Resource) *PodSpecV1 {
podTemplateSpec := GetPodTemplateSpec(resource)
if podTemplateSpec != nil {
return &podTemplateSpec.Spec
}
switch kubeType := resource.(type) {
case *PodV1:
return &kubeType.Spec
case *NamespaceV1, *ServiceAccountV1:
return nil
}
return nil
}
// GetPodTemplateSpec gets the PodTemplateSpec for a resource. Avoid using this function if you need support for
// Pod, Namespace, or ServiceAccount resources, and write a helper functions in this package instead
func GetPodTemplateSpec(resource Resource) *PodTemplateSpecV1 {
switch kubeType := resource.(type) {
case *CronJobV1Beta1:
return &kubeType.Spec.JobTemplate.Spec.Template
case *DaemonSetV1:
return &kubeType.Spec.Template
case *DeploymentV1:
return &kubeType.Spec.Template
case *JobV1:
return &kubeType.Spec.Template
case *PodTemplateV1:
return &kubeType.Template
case *ReplicationControllerV1:
return kubeType.Spec.Template
case *StatefulSetV1:
return &kubeType.Spec.Template
case *PodV1, *NamespaceV1:
return nil
}
return nil
}