/
check_nodes.go
276 lines (235 loc) · 7.94 KB
/
check_nodes.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package checks
import (
"context"
"fmt"
"strconv"
"strings"
"sync"
"time"
"github.com/openshift/vmware-vsphere-csi-driver-operator/pkg/operator/utils"
"github.com/vmware/govmomi/find"
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/vim25/mo"
"github.com/vmware/govmomi/vim25/types"
v1 "k8s.io/api/core/v1"
"k8s.io/klog/v2"
)
const (
nodeCheckTimeout = 5 * time.Minute
hardwareVersionPrefix = "vmx-"
minHardwareVersion = 15
minRequiredHostVersion = "6.7.3"
minUpgradeableHostVersion = "7.0.2"
workerCount = 10
)
var (
nodeProperties = []string{"config.extraConfig", "config.flags", "config.version", "runtime.host"}
)
type nodeChannelWorkData struct {
checkOpts CheckArgs
node *v1.Node
ctx context.Context
}
type NodeChecker struct {
hostESXIVersions map[string]bool
wg sync.WaitGroup
esxiVersionLock sync.RWMutex
results []ClusterCheckResult
resultLock sync.RWMutex
workChannel chan nodeChannelWorkData
}
var _ CheckInterface = &NodeChecker{}
func (n *NodeChecker) createPool(workerCount int) {
n.workChannel = make(chan nodeChannelWorkData, workerCount)
for i := 0; i < workerCount; i++ {
routineIndex := i
go n.runWorker(routineIndex)
n.wg.Add(1)
}
}
func (n *NodeChecker) waitForWorkFinish() []ClusterCheckResult {
n.wg.Wait()
// At this point no goroutine should be accessing results anyways
if n.getResultCount() > 0 {
return n.results
} else {
return []ClusterCheckResult{MakeClusterCheckResultPass()}
}
}
// add work to workqueue but if one or more of workers have already submitted a failed check result
// return false so as we can stop processing more nodes
func (n *NodeChecker) addWork(work nodeChannelWorkData) bool {
if n.getResultCount() > 0 {
return false
}
n.workChannel <- work
return true
}
func (n *NodeChecker) runWorker(workIndex int) {
for {
workData, ok := <-n.workChannel
if !ok {
break
}
response := n.checkOnNode(workData)
// if there was an error performing node check we can add result to worker results
// this will allow checks to fail quickly.
if response.CheckError != nil {
n.addResult(response)
break
}
}
n.wg.Done()
}
func (n *NodeChecker) addResult(res ClusterCheckResult) {
n.resultLock.Lock()
defer n.resultLock.Unlock()
n.results = append(n.results, res)
}
func (n *NodeChecker) getResultCount() int {
n.resultLock.RLock()
defer n.resultLock.RUnlock()
return len(n.results)
}
func (n *NodeChecker) checkOnNode(workInfo nodeChannelWorkData) ClusterCheckResult {
checkOpts := workInfo.checkOpts
node := workInfo.node
nodeCheckContext, cancel := context.WithTimeout(workInfo.ctx, nodeCheckTimeout)
defer cancel()
vm, err := getVM(nodeCheckContext, checkOpts, node)
if err != nil {
return makeDeprecatedEnvironmentError(CheckStatusVcenterAPIError, err)
}
hwVersion := vm.Config.Version
vmHWVersion := strings.Trim(hwVersion, hardwareVersionPrefix)
versionInt, err := strconv.ParseInt(vmHWVersion, 0, 64)
if err != nil {
klog.Errorf("error parsing hardware version %s: %v", hwVersion, err)
return makeDeprecatedEnvironmentError(CheckStatusVcenterAPIError, err)
}
if versionInt < minHardwareVersion {
reason := fmt.Errorf("node %s has hardware version %s, which is below the minimum required version %d", node.Name, hwVersion, minHardwareVersion)
return makeDeprecatedEnvironmentError(CheckStatusDeprecatedHWVersion, reason)
}
// check for esxi version
hostRef := vm.Runtime.Host
if hostRef == nil {
klog.Errorf("error getting ESXI host for node %s: vm.runtime.host is empty", node.Name)
return makeDeprecatedEnvironmentError(CheckStatusVcenterAPIError, err)
}
hostName := hostRef.Value
if beingProcessed := n.checkOrMarkHostForProcessing(hostName); beingProcessed {
return MakeClusterCheckResultPass()
}
hostSystem, err := n.getHost(nodeCheckContext, checkOpts, hostRef)
if err != nil {
klog.Errorf("error getting host for node %s: %v", node.Name, err)
return makeDeprecatedEnvironmentError(CheckStatusVcenterAPIError, err)
}
hostAPIVersion := hostSystem.Config.Product.ApiVersion
hasRequiredMinimum, err := isMinimumVersion(minRequiredHostVersion, hostAPIVersion)
if err != nil {
klog.Errorf("error parsing host version for node %s and host %s: %v", node.Name, hostName, err)
}
if !hasRequiredMinimum {
reason := fmt.Errorf("host %s is on ESXI version %s, which is below minimum required version %s", hostName, hostAPIVersion, minRequiredHostVersion)
return makeDeprecatedEnvironmentError(CheckStatusDeprecatedESXIVersion, reason)
}
hasUpgradeableMinimum, err := isMinimumVersion(minUpgradeableHostVersion, hostAPIVersion)
if err != nil {
klog.Errorf("error parsing host version for node %s and host %s: %v", node.Name, hostName, err)
}
if !hasUpgradeableMinimum {
reason := fmt.Errorf("host %s is on ESXI version %s, which is below minimum required version %s for cluster upgrade", hostName, hostAPIVersion, minUpgradeableHostVersion)
return MakeClusterUnupgradeableError(CheckStatusDeprecatedESXIVersion, reason)
}
return MakeClusterCheckResultPass()
}
func (n *NodeChecker) Check(ctx context.Context, checkOpts CheckArgs) []ClusterCheckResult {
nodes, err := checkOpts.apiClient.ListNodes()
if err != nil {
reason := fmt.Errorf("error listing node objects: %v", err)
return []ClusterCheckResult{MakeClusterDegradedError(CheckStatusOpenshiftAPIError, reason)}
}
// Map of host names and their ESXI versions
n.hostESXIVersions = map[string]bool{}
n.results = []ClusterCheckResult{}
workDone := false
n.createPool(workerCount)
for i := range nodes {
node := nodes[i]
workData := nodeChannelWorkData{
checkOpts: checkOpts,
node: node,
ctx: ctx,
}
ok := n.addWork(workData)
if !ok {
workDone = true
close(n.workChannel)
break
}
}
if !workDone {
close(n.workChannel)
}
return n.waitForWorkFinish()
}
func (n *NodeChecker) checkOrMarkHostForProcessing(hostName string) bool {
n.esxiVersionLock.Lock()
defer n.esxiVersionLock.Unlock()
_, found := n.hostESXIVersions[hostName]
if found {
return true
}
n.hostESXIVersions[hostName] = true
return false
}
func (n *NodeChecker) getHost(ctx context.Context, checkOpts CheckArgs, hostRef *types.ManagedObjectReference) (mo.HostSystem, error) {
var o mo.HostSystem
hostName := hostRef.Value
hostSystemObject := object.NewHostSystem(checkOpts.vmConnection.Client.Client, *hostRef)
err := hostSystemObject.Properties(ctx, hostSystemObject.Reference(), []string{"name", "config.product"}, &o)
if err != nil {
return o, fmt.Errorf("failed to load ESXi host %s: %v", hostName, err)
}
if o.Config == nil {
return o, fmt.Errorf("error getting ESXi host version %s: host.config is nil", hostName)
}
return o, nil
}
func getVM(ctx context.Context, checkOpts CheckArgs, node *v1.Node) (*mo.VirtualMachine, error) {
vmClient := checkOpts.vmConnection.Client.Client
vmConfig := checkOpts.vmConnection.Config
dataCenterNames, err := utils.GetDatacenters(vmConfig)
if err != nil {
return nil, err
}
vmUUID := strings.ToLower(strings.TrimSpace(strings.TrimPrefix(node.Spec.ProviderID, "vsphere://")))
for _, dcName := range dataCenterNames {
// Find datastore
finder := find.NewFinder(vmClient, false)
dc, err := finder.Datacenter(ctx, dcName)
if err != nil {
return nil, fmt.Errorf("failed to access Datacenter %s: %s", dcName, err)
}
// Find VM reference in the datastore, by UUID
s := object.NewSearchIndex(dc.Client())
svm, err := s.FindByUuid(ctx, dc, vmUUID, true, nil)
if err != nil {
return nil, fmt.Errorf("failed to find VM %s by UUID %s: %s", node.Name, vmUUID, err)
}
if svm == nil {
continue
}
// Find VM properties
vm := object.NewVirtualMachine(vmClient, svm.Reference())
var o mo.VirtualMachine
err = vm.Properties(ctx, vm.Reference(), nodeProperties, &o)
if err != nil {
return nil, fmt.Errorf("failed to load VM %s: %s", node.Name, err)
}
return &o, nil
}
return nil, fmt.Errorf("unable to find VM %s by UUID %s", node.Name, vmUUID)
}