-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathawait.go
195 lines (148 loc) · 6.1 KB
/
await.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
185
186
187
188
189
190
191
192
193
194
195
package await
import (
"context"
"fmt"
"strings"
"time"
"github.com/openshift-kni/eco-goinfra/pkg/kmm"
"github.com/openshift-kni/eco-goinfra/pkg/nodes"
"github.com/openshift-kni/eco-goinfra/pkg/pod"
"github.com/openshift-kni/eco-gotests/tests/hw-accel/kmm/internal/get"
"github.com/openshift-kni/eco-gotests/tests/hw-accel/kmm/internal/kmmparams"
"github.com/golang/glog"
"github.com/openshift-kni/eco-goinfra/pkg/clients"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/wait"
)
var buildPod = make(map[string]string)
// BuildPodCompleted awaits kmm build pods to finish build.
func BuildPodCompleted(apiClient *clients.Settings, nsname string, timeout time.Duration) error {
return wait.PollUntilContextTimeout(
context.TODO(), 5*time.Second, timeout, true, func(ctx context.Context) (bool, error) {
var err error
if buildPod[nsname] == "" {
pods, err := pod.List(apiClient, nsname, metav1.ListOptions{FieldSelector: "status.phase=Running"})
if err != nil {
glog.V(kmmparams.KmmLogLevel).Infof("build list error: %s", err)
}
for _, podObj := range pods {
if strings.Contains(podObj.Object.Name, "-build") {
buildPod[nsname] = podObj.Object.Name
glog.V(kmmparams.KmmLogLevel).Infof("Build podObj '%s' is Running\n", podObj.Object.Name)
}
}
}
if buildPod[nsname] != "" {
fieldSelector := fmt.Sprintf("metadata.name=%s", buildPod[nsname])
pods, _ := pod.List(apiClient, nsname, metav1.ListOptions{FieldSelector: fieldSelector})
if len(pods) == 0 {
glog.V(kmmparams.KmmLogLevel).Infof("BuildPod %s no longer in namespace", buildPod)
buildPod[nsname] = ""
return true, nil
}
for _, podObj := range pods {
if strings.Contains(string(podObj.Object.Status.Phase), "Failed") {
err = fmt.Errorf("BuildPod %s has failed", podObj.Object.Name)
glog.V(kmmparams.KmmLogLevel).Info(err)
return false, err
}
if strings.Contains(string(podObj.Object.Status.Phase), "Succeeded") {
glog.V(kmmparams.KmmLogLevel).Infof("BuildPod %s is in phase Succeeded",
podObj.Object.Name)
buildPod[nsname] = ""
return true, nil
}
}
}
return false, err
})
}
// ModuleDeployment awaits module to de deployed.
func ModuleDeployment(apiClient *clients.Settings, moduleName, nsname string,
timeout time.Duration, selector map[string]string) error {
label := fmt.Sprintf(kmmparams.ModuleNodeLabelTemplate, nsname, moduleName)
return deploymentPerLabel(apiClient, moduleName, label, timeout, selector)
}
// DeviceDriverDeployment awaits device driver pods to de deployed.
func DeviceDriverDeployment(apiClient *clients.Settings, moduleName, nsname string,
timeout time.Duration, selector map[string]string) error {
label := fmt.Sprintf(kmmparams.DevicePluginNodeLabelTemplate, nsname, moduleName)
return deploymentPerLabel(apiClient, moduleName, label, timeout, selector)
}
// ModuleUndeployed awaits module pods to be undeployed.
func ModuleUndeployed(apiClient *clients.Settings, nsName string, timeout time.Duration) error {
return wait.PollUntilContextTimeout(
context.TODO(), time.Second, timeout, true, func(ctx context.Context) (bool, error) {
pods, err := pod.List(apiClient, nsName, metav1.ListOptions{})
if err != nil {
glog.V(kmmparams.KmmLogLevel).Infof("pod list error: %s\n", err)
return false, err
}
glog.V(kmmparams.KmmLogLevel).Infof("current number of pods: %v\n", len(pods))
return len(pods) == 0, nil
})
}
// ModuleObjectDeleted awaits module object to be deleted.
// required from KMM 2.0 so that NMC has time to unload the modules.
func ModuleObjectDeleted(apiClient *clients.Settings, moduleName, nsName string, timeout time.Duration) error {
return wait.PollUntilContextTimeout(
context.TODO(), time.Second, timeout, true, func(ctx context.Context) (bool, error) {
_, err := kmm.Pull(apiClient, moduleName, nsName)
if err != nil {
glog.V(kmmparams.KmmLogLevel).Infof("error while pulling the module; most likely it is deleted")
}
return err != nil, nil
})
}
// PreflightStageDone awaits preflightvalidationocp to be in stage Done.
func PreflightStageDone(apiClinet *clients.Settings, preflight, module, nsname string,
timeout time.Duration) error {
return wait.PollUntilContextTimeout(
context.TODO(), 5*time.Second, timeout, true, func(ctx context.Context) (bool, error) {
pre, err := kmm.PullPreflightValidationOCP(apiClinet, preflight,
nsname)
if err != nil {
glog.V(kmmparams.KmmLogLevel).Infof("error pulling preflightvalidationocp")
}
preflightValidationOCP, err := pre.Get()
if err == nil {
status := preflightValidationOCP.Status.CRStatuses[module].VerificationStage
glog.V(kmmparams.KmmLogLevel).Infof("Stage: %s", status)
return status == "Done", nil
}
return false, err
})
}
func deploymentPerLabel(apiClient *clients.Settings, moduleName, label string,
timeout time.Duration, selector map[string]string) error {
return wait.PollUntilContextTimeout(
context.TODO(), 5*time.Second, timeout, true, func(ctx context.Context) (bool, error) {
var err error
nodeBuilder, err := nodes.List(apiClient, metav1.ListOptions{LabelSelector: labels.Set(selector).String()})
if err != nil {
glog.V(kmmparams.KmmLogLevel).Infof("could not discover %v nodes", selector)
}
nodesForSelector, err := get.NumberOfNodesForSelector(apiClient, selector)
if err != nil {
glog.V(kmmparams.KmmLogLevel).Infof("nodes list error: %s", err)
return false, err
}
foundLabels := 0
for _, node := range nodeBuilder {
glog.V(kmmparams.KmmLogLevel).Infof("%v", node.Object.Labels)
_, ok := node.Object.Labels[label]
if ok {
glog.V(kmmparams.KmmLogLevel).Infof("Found label %v that contains %v on node %v",
label, moduleName, node.Object.Name)
foundLabels++
glog.V(kmmparams.KmmLogLevel).Infof("Number of nodes: %v, Number of nodes with '%v' label pods: %v\n",
nodesForSelector, label, foundLabels)
if foundLabels == len(nodeBuilder) {
return true, nil
}
}
}
return false, err
})
}