/
wait.go
80 lines (65 loc) 路 2.85 KB
/
wait.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
package cluster
import (
"context"
"fmt"
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/cluster-api/util/conditions"
anywherev1 "github.com/aws/eks-anywhere/pkg/api/v1alpha1"
"github.com/aws/eks-anywhere/pkg/clients/kubernetes"
"github.com/aws/eks-anywhere/pkg/constants"
"github.com/aws/eks-anywhere/pkg/retrier"
)
// WaitForCondition blocks until either the cluster has this condition as True
// or the retrier timeouts. If observedGeneration is not equal to generation,
// the condition is considered false regardless of the status value.
// total field is to check the total number of times the given condition is met for consistency.
func WaitForCondition(ctx context.Context, log logr.Logger, client kubernetes.Reader, cluster *anywherev1.Cluster, total int, retrier *retrier.Retrier, conditionType anywherev1.ConditionType) error {
return WaitFor(ctx, log, client, cluster, total, retrier, func(c *anywherev1.Cluster) error {
condition := conditions.Get(c, conditionType)
if condition == nil {
return fmt.Errorf("cluster doesn't yet have condition %s", conditionType)
}
if condition.Status != corev1.ConditionTrue {
return fmt.Errorf("cluster condition %s is %s: %s", conditionType, condition.Status, condition.Message)
}
return nil
})
}
// Matcher matches the given condition.
type Matcher func(*anywherev1.Cluster) error
// WaitFor gets the cluster object from the client
// checks for generation and observedGeneration condition
// matches condition and returns error if the condition is not met.
func WaitFor(ctx context.Context, log logr.Logger, client kubernetes.Reader, cluster *anywherev1.Cluster, total int, retrier *retrier.Retrier, matcher Matcher) error {
count := 0
return retrier.Retry(func() error {
c := &anywherev1.Cluster{}
namespace := cluster.Namespace
if namespace == "" {
namespace = constants.DefaultNamespace
}
if err := client.Get(ctx, cluster.Name, namespace, c); err != nil {
return err
}
observedGeneration := c.Status.ObservedGeneration
generation := c.Generation
log.V(9).Info("Cluster generation and observedGeneration", "Generation", generation, "ObservedGeneration", observedGeneration)
if observedGeneration != generation {
return fmt.Errorf("cluster generation (%d) and observedGeneration (%d) differ", generation, observedGeneration)
}
if err := matcher(c); err != nil {
count = 0
return err
}
count++
// total field is to check the total number of times the given condition is met.
// for ex, the total is set to 5 and we want to check certain condition is met
// we check the condition is met for 5 times to make sure the given behavior is consistent.
if count < total {
return fmt.Errorf("cluster has reached to expected condition in %d/%d times", count, total)
}
// when the count matches total number it returns without error
return nil
})
}