forked from openshift/cluster-api-provider-gcp
/
conditions.go
79 lines (74 loc) · 2.72 KB
/
conditions.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
package machine
import (
"github.com/openshift/cluster-api-provider-gcp/pkg/apis/gcpprovider/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog"
)
const (
machineCreationSucceedReason = "MachineCreationSucceeded"
machineCreationSucceedMessage = "machine successfully created"
machineCreationFailedReason = "MachineCreationFailed"
)
func shouldUpdateCondition(
oldCondition v1beta1.GCPMachineProviderCondition,
newCondition v1beta1.GCPMachineProviderCondition,
) bool {
if oldCondition.Status != newCondition.Status ||
oldCondition.Reason != newCondition.Reason ||
oldCondition.Message != newCondition.Message {
return true
}
return false
}
// setProviderCondition sets the condition for the machine and
// returns the new slice of conditions.
// If the machine does not already have a condition with the specified type,
// a condition will be added to the slice.
// If the machine does already have a condition with the specified type,
// the condition will be updated if either of the following are true.
// 1) Requested Status is different than existing status.
// 2) requested Reason is different that existing one.
// 3) requested Message is different that existing one.
func reconcileProviderConditions(conditions []v1beta1.GCPMachineProviderCondition, newCondition v1beta1.GCPMachineProviderCondition) []v1beta1.GCPMachineProviderCondition {
now := metav1.Now()
currentCondition := findCondition(conditions, newCondition.Type)
if currentCondition == nil {
klog.V(4).Infof("Adding new provider condition %v", newCondition)
conditions = append(
conditions,
v1beta1.GCPMachineProviderCondition{
Type: newCondition.Type,
Status: newCondition.Status,
Reason: newCondition.Reason,
Message: newCondition.Message,
LastTransitionTime: now,
LastProbeTime: now,
},
)
} else {
if shouldUpdateCondition(
*currentCondition,
newCondition,
) {
klog.V(4).Infof("Updating provider condition %v", newCondition)
if currentCondition.Status != newCondition.Status {
currentCondition.LastTransitionTime = now
}
currentCondition.Status = newCondition.Status
currentCondition.Reason = newCondition.Reason
currentCondition.Message = newCondition.Message
currentCondition.LastProbeTime = now
}
}
return conditions
}
// findCondition finds in the machine the condition that has the
// specified condition type. If none exists, then returns nil.
func findCondition(conditions []v1beta1.GCPMachineProviderCondition, conditionType v1beta1.GCPMachineProviderConditionType) *v1beta1.GCPMachineProviderCondition {
for i, condition := range conditions {
if condition.Type == conditionType {
return &conditions[i]
}
}
return nil
}