-
Notifications
You must be signed in to change notification settings - Fork 26
/
meets.go
304 lines (242 loc) · 9.98 KB
/
meets.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package meets
import (
"fmt"
"net"
"time"
"github.com/hashicorp/go-version"
"github.com/openshift-kni/eco-goinfra/pkg/hive"
"github.com/openshift-kni/eco-goinfra/pkg/pod"
. "github.com/openshift-kni/eco-gotests/tests/assisted/ztp/internal/ztpinittools"
"github.com/openshift-kni/eco-gotests/tests/internal/cluster"
configv1 "github.com/openshift/api/config/v1"
corev1 "k8s.io/api/core/v1"
)
// AllRequirements accepts multiple requirement functions to ensure the environment meets all requirements.
func AllRequirements(f ...func() (bool, string)) (bool, string) {
for _, req := range f {
met, msg := req()
if !met {
return met, msg
}
}
return true, ""
}
// HubInfrastructureOperandRunningRequirement ensures that both
// the assisted-service and assisted-image-service pods are running on the hub cluster.
func HubInfrastructureOperandRunningRequirement() (bool, string) {
servicePodBuilder := ZTPConfig.HubAssistedServicePod()
running, msg := checkPodRunning(servicePodBuilder)
if !running {
return running, msg
}
imageBuilder := ZTPConfig.HubAssistedImageServicePod()
return checkPodRunning(imageBuilder)
}
// SpokeAPIClientReadyRequirement checks that the spoke APIClient has been properly initialized.
func SpokeAPIClientReadyRequirement() (bool, string) {
if SpokeAPIClient == nil {
return false, "spoke APIClient has not been initialized"
}
return true, ""
}
// SpokeClusterImageSetVersionRequirement checks that the provided clusterimageset meets the version provided.
func SpokeClusterImageSetVersionRequirement(requiredVersion string) (bool, string) {
if ZTPConfig.SpokeClusterImageSet == "" {
return false, "Spoke clusterimageset version was not provided through environment"
}
_, err := hive.PullClusterImageSet(HubAPIClient, ZTPConfig.SpokeClusterImageSet)
if err != nil {
return false, fmt.Sprintf("ClusterImageSet could not be found: %v", err)
}
imgSetVersion, _ := version.NewVersion(ZTPConfig.SpokeClusterImageSet)
currentVersion, _ := version.NewVersion(requiredVersion)
if imgSetVersion.LessThan(currentVersion) {
return false, fmt.Sprintf("Discovered clusterimageset version does not meet requirement: %v",
imgSetVersion.String())
}
return true, ""
}
// HubOCPVersionRequirement checks that hub ocp version meets the version provided.
func HubOCPVersionRequirement(requiredVersion string) (bool, string) {
return ocpVersionRequirement(HubAPIClient, requiredVersion)
}
// SpokeOCPVersionRequirement checks that spoke ocp version meets the version provided.
func SpokeOCPVersionRequirement(requiredVersion string) (bool, string) {
return ocpVersionRequirement(SpokeAPIClient, requiredVersion)
}
// HubProxyConfiguredRequirement checks that the cluster proxy is configured on the hub.
func HubProxyConfiguredRequirement() (bool, string) {
return proxyConfiguredRequirement(HubAPIClient)
}
// SpokeProxyConfiguredRequirement checks that the cluster proxy is configured on the spoke.
func SpokeProxyConfiguredRequirement() (bool, string) {
return proxyConfiguredRequirement(SpokeAPIClient)
}
// HubDisconnectedRequirement checks that the hub is disconnected.
func HubDisconnectedRequirement() (bool, string) {
return disconnectedRequirement(HubAPIClient)
}
// SpokeDisconnectedRequirement checks that the spoke is disconnected.
func SpokeDisconnectedRequirement() (bool, string) {
return disconnectedRequirement(SpokeAPIClient)
}
// HubConnectedRequirement checks that the hub is connected.
func HubConnectedRequirement() (bool, string) {
return connectedRequirement(HubAPIClient)
}
// SpokeConnectedRequirement checks that the spoke is connected.
func SpokeConnectedRequirement() (bool, string) {
return connectedRequirement(SpokeAPIClient)
}
// HubSingleStackIPv4Requirement checks that the hub has IPv4 single-stack networking.
func HubSingleStackIPv4Requirement() (bool, string) {
return singleStackIPv4Requirement(HubAPIClient)
}
// SpokeSingleStackIPv4Requirement checks that the spoke has IPv4 single-stack networking.
func SpokeSingleStackIPv4Requirement() (bool, string) {
return singleStackIPv4Requirement(SpokeAPIClient)
}
// HubSingleStackIPv6Requirement checks that the hub has IPv6 single-stack networking.
func HubSingleStackIPv6Requirement() (bool, string) {
return singleStackIPv6Requirement(HubAPIClient)
}
// SpokeSingleStackIPv6Requirement checks that the spoke has IPv6 single-stack networking.
func SpokeSingleStackIPv6Requirement() (bool, string) {
return singleStackIPv6Requirement(SpokeAPIClient)
}
// HubDualStackRequirement checks that the hub has dual-stack networking.
func HubDualStackRequirement() (bool, string) {
return dualStackRequirement(HubAPIClient)
}
// SpokeDualStackRequirement checks that the spoke has dual-stack networking.
func SpokeDualStackRequirement() (bool, string) {
return dualStackRequirement(SpokeAPIClient)
}
// checkPodRunning waits for the specified pod to be running.
func checkPodRunning(podBuilder *pod.Builder) (bool, string) {
err := podBuilder.WaitUntilInStatus(corev1.PodRunning, time.Second*10)
if err != nil {
return false, fmt.Sprintf("%s pod found but was not running", podBuilder.Definition.Name)
}
return true, ""
}
// ocpVersionRequirement checks that the OCP version of the provided client meets requiredVersion.
func ocpVersionRequirement(clusterobj cluster.APIClientGetter, requiredVersion string) (bool, string) {
clusterVersion, err := cluster.GetOCPClusterVersion(clusterobj)
if err != nil {
return false, fmt.Sprintf("Failed to get clusterversion from %s cluster: %v", getClusterType(clusterobj), err)
}
ocpVersion, _ := version.NewVersion(clusterVersion.Definition.Status.Desired.Version)
currentVersion, _ := version.NewVersion(requiredVersion)
if ocpVersion.LessThan(currentVersion) {
return false, fmt.Sprintf("Discovered openshift version does not meet requirement: %v",
ocpVersion.String())
}
return true, ""
}
// proxyConfiguredRequirement checks that the OCP proxy of the provided client is configured.
func proxyConfiguredRequirement(clusterobj cluster.APIClientGetter) (bool, string) {
ocpProxy, err := cluster.GetOCPProxy(clusterobj)
if err != nil {
return false, fmt.Sprintf("Failed to get cluster proxy from %s cluster: %v", getClusterType(clusterobj), err)
}
if ocpProxy.Object.Status.HTTPProxy == "" &&
ocpProxy.Object.Status.HTTPSProxy == "" &&
ocpProxy.Object.Status.NoProxy == "" {
return false, fmt.Sprintf("Discovered proxy not configured: %v", ocpProxy.Object.Status)
}
return true, ""
}
// disconnectedRequirement checks that the OCP cluster of the provided client is disconnected.
func disconnectedRequirement(clusterobj cluster.APIClientGetter) (bool, string) {
clusterVersion, err := cluster.GetOCPClusterVersion(clusterobj)
if err != nil {
return false, fmt.Sprintf("Failed to get clusterversion from %s cluster: %v", getClusterType(clusterobj), err)
}
for _, condition := range clusterVersion.Object.Status.Conditions {
if condition.Type == configv1.RetrievedUpdates {
if condition.Reason == "RemoteFailed" {
return true, ""
}
return false, "Provided cluster is connected"
}
}
return false, fmt.Sprintf("Failed to determine if cluster is disconnected, "+
"could not find '%s' condition", configv1.RetrievedUpdates)
}
// connectedRequirement checks that the OCP cluster of the provided client is connected.
func connectedRequirement(clusterobj cluster.APIClientGetter) (bool, string) {
clusterVersion, err := cluster.GetOCPClusterVersion(clusterobj)
if err != nil {
return false, fmt.Sprintf("Failed to get clusterversion from %s cluster: %v", getClusterType(clusterobj), err)
}
for _, condition := range clusterVersion.Object.Status.Conditions {
if condition.Type == configv1.RetrievedUpdates {
if condition.Reason == "RemoteFailed" {
return false, "Provided cluster is disconnected"
}
return true, ""
}
}
return false, fmt.Sprintf("Failed to determine if cluster is connected, "+
"could not find '%s' condition", configv1.RetrievedUpdates)
}
// singleStackIPv4Requirement checks that the OCP network of the provided client is single-stack ipv4.
func singleStackIPv4Requirement(clusterobj cluster.APIClientGetter) (bool, string) {
ocpNetwork, err := cluster.GetOCPNetworkConfig(clusterobj)
if err != nil {
return false, fmt.Sprintf("Failed to get cluster network from %s cluster: %v", getClusterType(clusterobj), err)
}
for _, clusterNet := range ocpNetwork.Object.Status.ClusterNetwork {
ip, _, _ := net.ParseCIDR(clusterNet.CIDR)
v4Check := ip.To4()
if v4Check == nil {
return false, "ClusterNetwork was not IPv4"
}
}
return true, ""
}
// singleStackIPv6Requirement checks that the OCP network of the provided client is single-stack ipv6.
func singleStackIPv6Requirement(clusterobj cluster.APIClientGetter) (bool, string) {
ocpNetwork, err := cluster.GetOCPNetworkConfig(clusterobj)
if err != nil {
return false, fmt.Sprintf("Failed to get cluster network from %s cluster: %v", getClusterType(clusterobj), err)
}
for _, clusterNet := range ocpNetwork.Object.Status.ClusterNetwork {
ip, _, _ := net.ParseCIDR(clusterNet.CIDR)
v4Check := ip.To4()
if v4Check != nil {
return false, "ClusterNetwork was not IPv6"
}
}
return true, ""
}
// dualStackRequirement checks that the OCP network of the provided client is dual-stack.
func dualStackRequirement(clusterobj cluster.APIClientGetter) (bool, string) {
ipv4 := false
ipv6 := false
hubNetwork, err := cluster.GetOCPNetworkConfig(clusterobj)
if err != nil {
return false, fmt.Sprintf("Failed to get cluster network from %s cluster: %v", getClusterType(clusterobj), err)
}
for _, clusterNet := range hubNetwork.Object.Status.ClusterNetwork {
ip, _, _ := net.ParseCIDR(clusterNet.CIDR)
v4Check := ip.To4()
if v4Check != nil {
ipv4 = true
} else {
ipv6 = true
}
}
if !ipv4 || !ipv6 {
return false, "Only found cluster networks in one address family"
}
return true, ""
}
// getClusterType returns cluster type based on provided apiClient.
func getClusterType(clusterobj cluster.APIClientGetter) string {
if clusterobj == HubAPIClient {
return "hub"
}
return "spoke"
}