/
check.go
152 lines (119 loc) · 3.63 KB
/
check.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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package capi
import (
"context"
"fmt"
"github.com/siderolabs/go-retry/retry"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
)
// CheckClusterReady verifies that cluster ready from the CAPI point of view.
//
//nolint:cyclop,gocyclo,gocognit
func (clusterAPI *Manager) CheckClusterReady(ctx context.Context, cluster *Cluster) error {
var (
initialized bool
ready bool
found bool
conditions []interface{}
err error
)
if err = cluster.sync(ctx); err != nil {
return err
}
if conditions, found, err = unstructured.NestedSlice(cluster.cluster.Object, "status", "conditions"); err != nil {
return err
}
if !found {
return retry.ExpectedError(fmt.Errorf("cluster status is unknown"))
}
for _, cond := range conditions {
var (
t string
status string
)
c, ok := cond.(map[string]interface{})
if !ok {
return fmt.Errorf("failed to convert condition to map[string]interface{}")
}
if t, found, err = unstructured.NestedString(c, "type"); err != nil {
return err
} else if !found {
return fieldNotFound("type")
}
if status, found, err = unstructured.NestedString(c, "status"); err != nil {
return err
} else if !found {
return fieldNotFound("status")
}
if clusterv1.ConditionType(t) == clusterv1.ReadyCondition && corev1.ConditionStatus(status) == corev1.ConditionTrue {
ready = true
break
}
}
if !ready {
return retry.ExpectedError(fmt.Errorf("cluster is not ready"))
}
controlPlane, err := cluster.ControlPlanes(ctx)
if err != nil {
return err
}
if ready, found, err = unstructured.NestedBool(controlPlane.Object, "status", "ready"); err != nil {
return err
}
if !ready || !found {
return retry.ExpectedError(fmt.Errorf("control plane is not ready"))
}
if initialized, found, err = unstructured.NestedBool(controlPlane.Object, "status", "initialized"); err != nil {
return err
}
if !initialized || !found {
return retry.ExpectedError(fmt.Errorf("control plane is not ready"))
}
if err = checkReplicasReady(*controlPlane); err != nil {
return err
}
machineDeployments, err := cluster.Workers(ctx)
if err != nil {
return err
}
for _, machineDeployment := range machineDeployments.Items {
var phase string
if phase, found, err = unstructured.NestedString(machineDeployment.Object, "status", "phase"); err != nil {
return err
} else if !found {
return fieldNotFound("status", "phase")
}
if clusterv1.MachineDeploymentPhase(phase) != clusterv1.MachineDeploymentPhaseRunning {
return retry.ExpectedError(fmt.Errorf("machineDeployment phase is %s", phase))
}
if err = checkReplicasReady(machineDeployment); err != nil {
return err
}
}
return nil
}
func checkReplicasReady(in unstructured.Unstructured) error {
object := in.Object
readyReplicas, found, err := unstructured.NestedInt64(object, "status", "readyReplicas")
if err != nil {
return err
}
if !found {
return retry.ExpectedError(fieldNotFound("status", "readyReplicas"))
}
expectedReplicas, found, err := unstructured.NestedInt64(object, "status", "replicas")
if err != nil {
return err
}
if !found {
return retry.ExpectedError(fieldNotFound("status", "replicas"))
}
if readyReplicas != expectedReplicas {
return retry.ExpectedError(fmt.Errorf("%s %s replicas %d != ready replicas %d", in.GetKind(), in.GetName(), expectedReplicas, readyReplicas))
}
return nil
}