forked from openshift/machine-api-operator
/
status.go
137 lines (121 loc) · 4.1 KB
/
status.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
package operator
import (
osconfigv1 "github.com/openshift/api/config/v1"
osclientsetv1 "github.com/openshift/client-go/config/clientset/versioned/typed/config/v1"
cvoresourcemerge "github.com/openshift/cluster-version-operator/lib/resourcemerge"
"github.com/openshift/machine-api-operator/pkg/version"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"
)
// StatusReason is a MixedCaps string representing the reason for a
// status condition change.
type StatusReason string
// The default set of status change reasons.
const (
ReasonEmpty StatusReason = ""
ReasonSyncing StatusReason = "SyncingResources"
ReasonSyncFailed StatusReason = "SyncingFailed"
)
// statusProgressing sets the Progressing condition to True, with the given
// reason and message, and sets both the Available and Failing conditions to
// False.
func (optr *Operator) statusProgressing(reason StatusReason, message string) error {
conds := []osconfigv1.ClusterOperatorStatusCondition{
{
Type: osconfigv1.OperatorProgressing,
Status: osconfigv1.ConditionTrue,
Reason: string(reason),
Message: message,
},
{
Type: osconfigv1.OperatorAvailable,
Status: osconfigv1.ConditionFalse,
},
{
Type: osconfigv1.OperatorFailing,
Status: osconfigv1.ConditionFalse,
},
}
return optr.syncStatus(conds)
}
// statusAvailable sets the Available condition to True, with the given reason
// and message, and sets both the Progressing and Failing conditions to False.
func (optr *Operator) statusAvailable(reason StatusReason, message string) error {
conds := []osconfigv1.ClusterOperatorStatusCondition{
{
Type: osconfigv1.OperatorAvailable,
Status: osconfigv1.ConditionTrue,
Reason: string(reason),
Message: message,
},
{
Type: osconfigv1.OperatorProgressing,
Status: osconfigv1.ConditionFalse,
},
{
Type: osconfigv1.OperatorFailing,
Status: osconfigv1.ConditionFalse,
},
}
return optr.syncStatus(conds)
}
// statusFailing sets the Failing condition to True, with the given reason and
// message, and sets the Progressing condition to False, and the Available
// condition to True. This indicates that the operator is present and may be
// partially functioning, but is in a degraded or failing state.
func (optr *Operator) statusFailing(reason StatusReason, message string) error {
conds := []osconfigv1.ClusterOperatorStatusCondition{
{
Type: osconfigv1.OperatorFailing,
Status: osconfigv1.ConditionTrue,
Reason: string(reason),
Message: message,
},
{
Type: osconfigv1.OperatorProgressing,
Status: osconfigv1.ConditionFalse,
},
{
Type: osconfigv1.OperatorAvailable,
Status: osconfigv1.ConditionTrue,
},
}
return optr.syncStatus(conds)
}
//syncStatus applies the new condition to the mao ClusterOperator object.
func (optr *Operator) syncStatus(conds []osconfigv1.ClusterOperatorStatusCondition) error {
// to report the status of all the managed components.
clusterOperator := &osconfigv1.ClusterOperator{
ObjectMeta: metav1.ObjectMeta{
Namespace: optr.namespace,
Name: optr.name,
},
Status: osconfigv1.ClusterOperatorStatus{
Version: version.Raw,
},
}
for _, c := range conds {
cvoresourcemerge.SetOperatorStatusCondition(&clusterOperator.Status.Conditions, c)
}
_, _, err := ApplyClusterOperator(optr.osClient.ConfigV1(), clusterOperator)
return err
}
// ApplyClusterOperator applies the required ClusterOperator
func ApplyClusterOperator(client osclientsetv1.ClusterOperatorsGetter, required *osconfigv1.ClusterOperator) (*osconfigv1.ClusterOperator, bool, error) {
existing, err := client.ClusterOperators().Get(required.Name, metav1.GetOptions{})
if errors.IsNotFound(err) {
actual, err := client.ClusterOperators().Create(required)
return actual, true, err
}
if err != nil {
return nil, false, err
}
modified := pointer.BoolPtr(false)
cvoresourcemerge.EnsureClusterOperatorStatus(modified, existing, *required)
if !*modified {
return existing, false, nil
}
actual, err := client.ClusterOperators().UpdateStatus(existing)
return actual, true, err
}