-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathrancluster.go
246 lines (192 loc) · 6.76 KB
/
rancluster.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package rancluster
import (
"context"
"fmt"
"time"
"github.com/golang/glog"
"github.com/openshift-kni/eco-goinfra/pkg/bmc"
"github.com/openshift-kni/eco-goinfra/pkg/clients"
"github.com/openshift-kni/eco-goinfra/pkg/nodes"
"github.com/openshift-kni/eco-goinfra/pkg/ocm"
. "github.com/openshift-kni/eco-gotests/tests/cnf/ran/internal/raninittools"
"github.com/openshift-kni/eco-gotests/tests/cnf/ran/internal/ranparam"
"github.com/openshift-kni/eco-gotests/tests/system-tests/diskencryption/tsparams"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/wait"
)
// AreClustersPresent checks all of the provided clusters and returns false if any are nil.
func AreClustersPresent(clusters []*clients.Settings) bool {
for _, cluster := range clusters {
if cluster == nil {
return false
}
}
return true
}
// GetPlusOneWorkerName gets the name of the one worker in a SNO+1 cluster.
func GetPlusOneWorkerName(client *clients.Settings) (string, error) {
workers, err := ListNodesByLabel(client, RANConfig.WorkerLabelMap)
if err != nil {
return "", err
}
for _, worker := range workers {
if !IsNodeControlPlane(worker) {
return worker.Definition.Name, nil
}
}
return "", fmt.Errorf("could not find a worker node for cluster")
}
// IsClusterStable checks if the provided cluster does not have any unschedulable nodes.
func IsClusterStable(client *clients.Settings) (bool, error) {
nodeList, err := nodes.List(client)
if err != nil {
return false, err
}
for _, node := range nodeList {
if node.Definition.Spec.Unschedulable {
return false, nil
}
}
return true, nil
}
// IsNodeControlPlane checks whether the provided node is a control plane node.
func IsNodeControlPlane(node *nodes.Builder) bool {
_, exists := node.Definition.Labels[RANConfig.ControlPlaneLabel]
return exists
}
// IsSnoPlusOne checks if the specified cluster has one control plane and one worker node.
func IsSnoPlusOne(client *clients.Settings) (bool, error) {
controlPlanes, err := ListNodesByLabel(client, RANConfig.ControlPlaneLabelMap)
if err != nil {
return false, err
}
if len(controlPlanes) != 1 {
return false, nil
}
glog.V(tsparams.LogLevel).Info("Exactly one control plane node found")
workers, err := ListNodesByLabel(client, RANConfig.WorkerLabelMap)
if err != nil {
return false, err
}
trueWorkers := 0
for _, worker := range workers {
if !IsNodeControlPlane(worker) {
trueWorkers++
}
}
if trueWorkers != 1 {
return false, nil
}
glog.V(tsparams.LogLevel).Info("Exactly one worker node found")
return true, nil
}
// ListNodesByLabel returns a list of nodes that have the specified label.
func ListNodesByLabel(client *clients.Settings, labelMap map[string]string) ([]*nodes.Builder, error) {
return nodes.List(client, metav1.ListOptions{
LabelSelector: labels.Set(labelMap).String(),
})
}
// WaitForNumberOfNodes waits up to timeout until the number of nodes on the cluster matches the expected.
func WaitForNumberOfNodes(client *clients.Settings, expected int, timeout time.Duration) error {
return wait.PollUntilContextTimeout(
context.TODO(), 5*time.Second, timeout, true, func(ctx context.Context) (bool, error) {
nodeList, err := nodes.List(client)
if err != nil {
return false, err
}
if len(nodeList) == expected {
return true, nil
}
glog.V(tsparams.LogLevel).Infof("Expected %d nodes but found %d nodes", expected, len(nodeList))
return false, nil
})
}
// CheckSpokeClusterType checks and returns a spoke cluster type based on number of control plane nodes.
func CheckSpokeClusterType(client *clients.Settings) (ranparam.ClusterType, error) {
controlPlaneNodesList, err := ListNodesByLabel(client, RANConfig.ControlPlaneLabelMap)
if err != nil {
return "", err
}
if len(controlPlaneNodesList) == 1 {
return ranparam.SNOCluster, nil
}
return ranparam.HighlyAvailableCluster, fmt.Errorf("could not determine spoke cluster type")
}
// DoesClusterLabelExist looks for a label on a managed cluster and returns true if it exists.
func DoesClusterLabelExist(client *clients.Settings, clusterName string, label string) (bool, error) {
managedCluster, err := ocm.PullManagedCluster(client, clusterName)
if err != nil {
return false, err
}
_, exists := managedCluster.Object.Labels[label]
return exists, nil
}
// PowerOffAndWait will trigger a power off and poll every 30 seconds for up to 3 minutes until the system is off.
func PowerOffAndWait(bmcClient *bmc.BMC) error {
err := bmcClient.SystemPowerOff()
if err != nil {
glog.V(ranparam.LogLevel).Infof("Failed to trigger system power off: %v", err)
return err
}
return wait.PollUntilContextTimeout(
context.TODO(), 30*time.Second, 3*time.Minute, true, func(ctx context.Context) (bool, error) {
powerState, err := bmcClient.SystemPowerState()
if err != nil {
glog.V(ranparam.LogLevel).Infof("Failed to get system power state: %v", err)
return false, err
}
if powerState != "Off" {
glog.V(ranparam.LogLevel).Infof("System power state is not Off: %s", powerState)
return false, nil
}
return true, nil
})
}
// PowerOffWithRetries will attempt to power off and wait until the system is powered off, trying up to retries times if
// the system does not power off.
func PowerOffWithRetries(bmcClient *bmc.BMC, retries uint) error {
var err error
for retry := range retries {
err = PowerOffAndWait(bmcClient)
if err == nil {
return nil
}
glog.V(ranparam.LogLevel).Infof("Powering off failed with %d retries left: %v", retries-retry-1, err)
}
return err
}
// PowerOnAndWait will trigger a power on and poll every 30 seconds for up to 3 minutes until the system is on.
func PowerOnAndWait(bmcClient *bmc.BMC) error {
err := bmcClient.SystemPowerOn()
if err != nil {
glog.V(ranparam.LogLevel).Infof("Failed to trigger system power off: %v", err)
return err
}
return wait.PollUntilContextTimeout(
context.TODO(), 30*time.Second, 3*time.Minute, true, func(ctx context.Context) (bool, error) {
powerState, err := bmcClient.SystemPowerState()
if err != nil {
glog.V(ranparam.LogLevel).Infof("Failed to get system power state: %v", err)
return false, err
}
if powerState != "On" {
glog.V(ranparam.LogLevel).Infof("System power state is not On: %s", powerState)
return false, nil
}
return true, nil
})
}
// PowerOnWithRetries will attempt to power on and wait until the system is powered on, trying up to retries times if
// the system does not power on.
func PowerOnWithRetries(bmcClient *bmc.BMC, retries uint) error {
var err error
for retry := range retries {
err = PowerOnAndWait(bmcClient)
if err == nil {
return nil
}
glog.V(ranparam.LogLevel).Infof("Powering on failed with %d retries left: %v", retries-retry-1, err)
}
return err
}