This repository has been archived by the owner on Jun 19, 2022. It is now read-only.
/
brokercell_lifecycle.go
160 lines (134 loc) · 6.18 KB
/
brokercell_lifecycle.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
/*
Copyright 2020 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package v1alpha1
import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"knative.dev/eventing/pkg/apis/duck"
"knative.dev/pkg/apis"
)
var brokerCellCondSet = apis.NewLivingConditionSet(
BrokerCellConditionIngress,
BrokerCellConditionFanout,
BrokerCellConditionRetry,
BrokerCellConditionTargetsConfig,
)
const (
// BrokerCellConditionReady has status true when all subconditions below
// have been set to True.
BrokerCellConditionReady apis.ConditionType = apis.ConditionReady
// BrokerCellConditionIngress reports the availability of the
// BrokerCell's ingress service.
BrokerCellConditionIngress apis.ConditionType = "IngressReady"
// BrokerCellConditionFanout reports the readiness of the BrokerCell's
// fanout service.
BrokerCellConditionFanout apis.ConditionType = "FanoutReady"
// BrokerCellConditionRetry reports the readiness of the BrokerCell's retry
// service.
BrokerCellConditionRetry apis.ConditionType = "RetryReady"
// BrokerCellConditionTargetsConfig reports the readiness of the
// BrokerCell's targets configmap.
BrokerCellConditionTargetsConfig apis.ConditionType = "TargetsConfigReady"
)
// GetCondition returns the condition currently associated with the given type, or nil.
func (bs *BrokerCellStatus) GetCondition(t apis.ConditionType) *apis.Condition {
return brokerCellCondSet.Manage(bs).GetCondition(t)
}
// GetTopLevelCondition returns the top level Condition.
func (bs *BrokerCellStatus) GetTopLevelCondition() *apis.Condition {
return brokerCellCondSet.Manage(bs).GetTopLevelCondition()
}
// IsReady returns true if the resource is ready overall.
func (bs *BrokerCellStatus) IsReady() bool {
return brokerCellCondSet.Manage(bs).IsHappy()
}
// InitializeConditions sets relevant unset conditions to Unknown state.
func (bs *BrokerCellStatus) InitializeConditions() {
brokerCellCondSet.Manage(bs).InitializeConditions()
}
// PropagateIngressAvailability uses the availability of the provided Endpoints
// to determine if BrokerCellConditionIngress should be marked as true or
// false.
func (bs *BrokerCellStatus) PropagateIngressAvailability(ep *corev1.Endpoints) {
if duck.EndpointsAreAvailable(ep) {
brokerCellCondSet.Manage(bs).MarkTrue(BrokerCellConditionIngress)
} else {
brokerCellCondSet.Manage(bs).MarkFalse(BrokerCellConditionIngress, "EndpointsUnavailable", "Endpoints %q is unavailable.", ep.Name)
}
}
func (bs *BrokerCellStatus) MarkIngressFailed(reason, format string, args ...interface{}) {
brokerCellCondSet.Manage(bs).MarkFalse(BrokerCellConditionIngress, reason, format, args...)
}
// PropagateFanoutAvailability uses the availability of the provided Deployment
// to determine if BrokerCellConditionFanout should be marked as true or
// false.
func (bs *BrokerCellStatus) PropagateFanoutAvailability(d *appsv1.Deployment) {
deploymentAvailableFound := false
for _, cond := range d.Status.Conditions {
if cond.Type == appsv1.DeploymentAvailable {
deploymentAvailableFound = true
if cond.Status == corev1.ConditionTrue {
brokerCellCondSet.Manage(bs).MarkTrue(BrokerCellConditionFanout)
} else if cond.Status == corev1.ConditionFalse {
brokerCellCondSet.Manage(bs).MarkFalse(BrokerCellConditionFanout, cond.Reason, cond.Message)
} else if cond.Status == corev1.ConditionUnknown {
brokerCellCondSet.Manage(bs).MarkUnknown(BrokerCellConditionFanout, cond.Reason, cond.Message)
}
}
}
if !deploymentAvailableFound {
brokerCellCondSet.Manage(bs).MarkUnknown(BrokerCellConditionFanout, "DeploymentUnavailable", "Deployment %q is unavailable.", d.Name)
}
}
func (bs *BrokerCellStatus) MarkFanoutFailed(reason, format string, args ...interface{}) {
brokerCellCondSet.Manage(bs).MarkFalse(BrokerCellConditionFanout, reason, format, args...)
}
func (bs *BrokerCellStatus) MarkFanoutUnknown(reason, format string, args ...interface{}) {
brokerCellCondSet.Manage(bs).MarkUnknown(BrokerCellConditionFanout, reason, format, args...)
}
// PropagateRetryAvailability uses the availability of the provided Deployment
// to determine if BrokerCellConditionRetry should be marked as true or
// unknown.
func (bs *BrokerCellStatus) PropagateRetryAvailability(d *appsv1.Deployment) {
deploymentAvailableFound := false
for _, cond := range d.Status.Conditions {
if cond.Type == appsv1.DeploymentAvailable {
deploymentAvailableFound = true
if cond.Status == corev1.ConditionTrue {
brokerCellCondSet.Manage(bs).MarkTrue(BrokerCellConditionRetry)
} else if cond.Status == corev1.ConditionFalse {
brokerCellCondSet.Manage(bs).MarkFalse(BrokerCellConditionRetry, cond.Reason, cond.Message)
} else if cond.Status == corev1.ConditionUnknown {
brokerCellCondSet.Manage(bs).MarkUnknown(BrokerCellConditionRetry, cond.Reason, cond.Message)
}
}
}
if !deploymentAvailableFound {
brokerCellCondSet.Manage(bs).MarkUnknown(BrokerCellConditionRetry, "DeploymentUnavailable", "Deployment %q is unavailable.", d.Name)
}
}
func (bs *BrokerCellStatus) MarkRetryFailed(reason, format string, args ...interface{}) {
brokerCellCondSet.Manage(bs).MarkFalse(BrokerCellConditionRetry, reason, format, args...)
}
func (bs *BrokerCellStatus) MarkRetryUnknown(reason, format string, args ...interface{}) {
brokerCellCondSet.Manage(bs).MarkUnknown(BrokerCellConditionRetry, reason, format, args...)
}
func (bs *BrokerCellStatus) MarkTargetsConfigReady() {
brokerCellCondSet.Manage(bs).MarkTrue(BrokerCellConditionTargetsConfig)
}
func (bs *BrokerCellStatus) MarkTargetsConfigFailed(reason, format string, args ...interface{}) {
brokerCellCondSet.Manage(bs).MarkFalse(BrokerCellConditionTargetsConfig, reason, format, args...)
}
func (bs *BrokerCellStatus) SetIngressTemplate(address string) {
bs.IngressTemplate = address
}