-
Notifications
You must be signed in to change notification settings - Fork 32
/
list.go
184 lines (135 loc) · 4.79 KB
/
list.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
184
package pod
import (
"context"
"fmt"
"strings"
"time"
"github.com/golang/glog"
"github.com/openshift-kni/eco-goinfra/pkg/clients"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// List returns pod inventory in the given namespace.
func List(apiClient *clients.Settings, nsname string, options ...metav1.ListOptions) ([]*Builder, error) {
if nsname == "" {
glog.V(100).Infof("pod 'nsname' parameter can not be empty")
return nil, fmt.Errorf("failed to list pods, 'nsname' parameter is empty")
}
logMessage := fmt.Sprintf("Listing pods in the nsname %s", nsname)
passedOptions := metav1.ListOptions{}
if len(options) == 1 {
passedOptions = options[0]
logMessage += fmt.Sprintf(" with the options %v", passedOptions)
} else if len(options) > 1 {
glog.V(100).Infof("'options' parameter must be empty or single-valued")
return nil, fmt.Errorf("error: more than one ListOptions was passed")
}
glog.V(100).Infof(logMessage)
podList, err := apiClient.Pods(nsname).List(context.TODO(), passedOptions)
if err != nil {
glog.V(100).Infof("Failed to list pods in the nsname %s due to %s", nsname, err.Error())
return nil, err
}
var podObjects []*Builder
for _, runningPod := range podList.Items {
copiedPod := runningPod
podBuilder := &Builder{
apiClient: apiClient,
Object: &copiedPod,
Definition: &copiedPod,
}
podObjects = append(podObjects, podBuilder)
}
return podObjects, nil
}
// ListInAllNamespaces returns a cluster-wide pod inventory.
func ListInAllNamespaces(apiClient *clients.Settings, options ...metav1.ListOptions) ([]*Builder, error) {
logMessage := "Listing all pods in all namespaces"
passedOptions := metav1.ListOptions{}
if len(options) > 1 {
glog.V(100).Infof("'options' parameter must be empty or single-valued")
return nil, fmt.Errorf("error: more than one ListOptions was passed")
}
if len(options) == 1 {
passedOptions = options[0]
logMessage += fmt.Sprintf(" with the options %v", passedOptions)
}
glog.V(100).Infof(logMessage)
podList, err := apiClient.Pods("").List(context.TODO(), passedOptions)
if err != nil {
glog.V(100).Infof("Failed to list all pods due to %s", err.Error())
return nil, err
}
var podObjects []*Builder
for _, runningPod := range podList.Items {
copiedPod := runningPod
podBuilder := &Builder{
apiClient: apiClient,
Object: &copiedPod,
Definition: &copiedPod,
}
podObjects = append(podObjects, podBuilder)
}
return podObjects, nil
}
// ListByNamePattern returns pod inventory in the given namespace filtered by name pattern.
func ListByNamePattern(apiClient *clients.Settings, namePattern, nsname string) ([]*Builder, error) {
glog.V(100).Infof("Listing pods in the nsname %s filtered by the name pattern %s", nsname, namePattern)
if nsname == "" {
glog.V(100).Infof("pod 'nsname' parameter can not be empty")
return nil, fmt.Errorf("failed to list pods, 'nsname' parameter is empty")
}
podList, err := apiClient.Pods(nsname).List(context.TODO(), metav1.ListOptions{})
if err != nil {
glog.V(100).Infof("Failed to list pods filtered by the name pattern %s in the nsname %s due to %s",
namePattern, nsname, err.Error())
return nil, err
}
var podObjects []*Builder
for _, runningPod := range podList.Items {
if strings.Contains(runningPod.Name, namePattern) {
copiedPod := runningPod
podBuilder := &Builder{
apiClient: apiClient,
Object: &copiedPod,
Definition: &copiedPod,
}
podObjects = append(podObjects, podBuilder)
}
}
return podObjects, nil
}
// WaitForAllPodsInNamespaceRunning wait until all pods in namespace that match options are in running state.
func WaitForAllPodsInNamespaceRunning(
apiClient *clients.Settings,
nsname string,
timeout time.Duration,
options ...metav1.ListOptions) (bool, error) {
if nsname == "" {
glog.V(100).Infof("'nsname' parameter can not be empty")
return false, fmt.Errorf("failed to list pods, 'nsname' parameter is empty")
}
logMessage := fmt.Sprintf("Waiting for all pods in %s namespace", nsname)
passedOptions := metav1.ListOptions{}
if len(options) > 1 {
glog.V(100).Infof("'options' parameter must be empty or single-valued")
return false, fmt.Errorf("error: more than one ListOptions was passed")
}
if len(options) == 1 {
passedOptions = options[0]
logMessage += fmt.Sprintf(" with the options %v", passedOptions)
}
glog.V(100).Infof(logMessage + " are in running state")
podList, err := List(apiClient, nsname, passedOptions)
if err != nil {
glog.V(100).Infof("Failed to list all pods due to %s", err.Error())
return false, err
}
for _, podObj := range podList {
err = podObj.WaitUntilRunning(timeout)
if err != nil {
glog.V(100).Infof("Timout was reached while waiting for all pods in running state: %s", err.Error())
return false, err
}
}
return true, nil
}