/
pod.go
183 lines (159 loc) · 4.14 KB
/
pod.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright © 2018-2019 The OpenEBS Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package v1alpha1
import (
corev1 "k8s.io/api/core/v1"
)
// Pod holds the api's pod objects
type Pod struct {
object *corev1.Pod
}
// PodList holds the list of API pod instances
type PodList struct {
items []*Pod
}
// PredicateList holds a list of predicate
type PredicateList []Predicate
// Predicate defines an abstraction
// to determine conditional checks
// against the provided pod instance
type Predicate func(*Pod) bool
// ToAPIList converts PodList to API PodList
func (pl *PodList) ToAPIList() *corev1.PodList {
plist := &corev1.PodList{}
for _, pod := range pl.items {
plist.Items = append(plist.Items, *pod.object)
}
return plist
}
type podBuildOption func(*Pod)
// NewForAPIObject returns a new instance of Pod
func NewForAPIObject(obj *corev1.Pod, opts ...podBuildOption) *Pod {
p := &Pod{object: obj}
for _, o := range opts {
o(p)
}
return p
}
// Len returns the number of items present in the PodList
func (pl *PodList) Len() int {
return len(pl.items)
}
// all returns true if all the predicates
// succeed against the provided pod
// instance
func (l PredicateList) all(p *Pod) bool {
for _, pred := range l {
if !pred(p) {
return false
}
}
return true
}
// IsRunning retuns true if the pod is in running
// state
func (p *Pod) IsRunning() bool {
return p.object.Status.Phase == "Running"
}
// IsRunning is a predicate to filter out pods
// which in running state
func IsRunning() Predicate {
return func(p *Pod) bool {
return p.IsRunning()
}
}
// IsCompleted retuns true if the pod is in completed
// state
func (p *Pod) IsCompleted() bool {
return p.object.Status.Phase == "Succeeded"
}
// IsCompleted is a predicate to filter out pods
// which in completed state
func IsCompleted() Predicate {
return func(p *Pod) bool {
return p.IsCompleted()
}
}
// HasLabels returns true if provided labels
// map[key]value are present in the provided PodList
// instance
func HasLabels(keyValuePair map[string]string) Predicate {
return func(p *Pod) bool {
// objKeyValues := p.object.GetLabels()
for key, value := range keyValuePair {
if !p.HasLabel(key, value) {
return false
}
}
return true
}
}
// HasLabel return true if provided lable
// key and value are present in the the provided PodList
// instance
func (p *Pod) HasLabel(key, value string) bool {
val, ok := p.object.GetLabels()[key]
if ok {
return val == value
}
return false
}
// HasLabel is predicate to filter out labeled
// pod instances
func HasLabel(key, value string) Predicate {
return func(p *Pod) bool {
return p.HasLabel(key, value)
}
}
// IsNil returns true if the pod instance
// is nil
func (p *Pod) IsNil() bool {
return p.object == nil
}
// IsNil is predicate to filter out nil pod
// instances
func IsNil() Predicate {
return func(p *Pod) bool {
return p.IsNil()
}
}
// GetAPIObject returns a API's Pod
func (p *Pod) GetAPIObject() *corev1.Pod {
return p.object
}
// FromList created a PodList with provided api podlist
func FromList(pods *corev1.PodList) *PodList {
pl := ListBuilderForAPIList(pods).
List()
return pl
}
// GetScheduledNodes returns the nodes on which pods are scheduled
func (pl *PodList) GetScheduledNodes() map[string]int {
nodeNames := make(map[string]int)
for _, p := range pl.items {
p := p // pin it
nodeNames[p.object.Spec.NodeName]++
}
return nodeNames
}
// IsMatchNodeAny checks the PodList is running on the provided nodes
func (pl *PodList) IsMatchNodeAny(nodes map[string]int) bool {
for _, p := range pl.items {
p := p // pin it
if nodes[p.object.Spec.NodeName] == 0 {
return false
}
}
return true
}