forked from knative/eventing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sequence_lifecycle.go
169 lines (147 loc) · 6.23 KB
/
sequence_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
161
162
163
164
165
166
167
168
169
/*
* Copyright 2020 The Knative Authors
*
* 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 v1beta1
import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
duckv1beta1 "knative.dev/eventing/pkg/apis/duck/v1beta1"
messagingv1beta1 "knative.dev/eventing/pkg/apis/messaging/v1beta1"
"knative.dev/pkg/apis"
pkgduckv1 "knative.dev/pkg/apis/duck/v1"
)
var pCondSet = apis.NewLivingConditionSet(SequenceConditionReady, SequenceConditionChannelsReady, SequenceConditionSubscriptionsReady, SequenceConditionAddressable)
const (
// SequenceConditionReady has status True when all subconditions below have been set to True.
SequenceConditionReady = apis.ConditionReady
// SequenceChannelsReady has status True when all the channels created as part of
// this sequence are ready.
SequenceConditionChannelsReady apis.ConditionType = "ChannelsReady"
// SequenceSubscriptionsReady has status True when all the subscriptions created as part of
// this sequence are ready.
SequenceConditionSubscriptionsReady apis.ConditionType = "SubscriptionsReady"
// SequenceConditionAddressable has status true when this Sequence meets
// the Addressable contract and has a non-empty hostname.
SequenceConditionAddressable apis.ConditionType = "Addressable"
)
// GetGroupVersionKind returns GroupVersionKind for InMemoryChannels
func (p *Sequence) GetGroupVersionKind() schema.GroupVersionKind {
return SchemeGroupVersion.WithKind("Sequence")
}
// GetUntypedSpec returns the spec of the Sequence.
func (s *Sequence) GetUntypedSpec() interface{} {
return s.Spec
}
// GetCondition returns the condition currently associated with the given type, or nil.
func (ss *SequenceStatus) GetCondition(t apis.ConditionType) *apis.Condition {
return pCondSet.Manage(ss).GetCondition(t)
}
// IsReady returns true if the resource is ready overall.
func (ss *SequenceStatus) IsReady() bool {
return pCondSet.Manage(ss).IsHappy()
}
// InitializeConditions sets relevant unset conditions to Unknown state.
func (ss *SequenceStatus) InitializeConditions() {
pCondSet.Manage(ss).InitializeConditions()
}
// PropagateSubscriptionStatuses sets the SubscriptionStatuses and SequenceConditionSubscriptionsReady based on
// the status of the incoming subscriptions.
func (ss *SequenceStatus) PropagateSubscriptionStatuses(subscriptions []*messagingv1beta1.Subscription) {
ss.SubscriptionStatuses = make([]SequenceSubscriptionStatus, len(subscriptions))
allReady := true
// If there are no subscriptions, treat that as a False case. Could go either way, but this seems right.
if len(subscriptions) == 0 {
allReady = false
}
for i, s := range subscriptions {
ss.SubscriptionStatuses[i] = SequenceSubscriptionStatus{
Subscription: corev1.ObjectReference{
APIVersion: s.APIVersion,
Kind: s.Kind,
Name: s.Name,
Namespace: s.Namespace,
},
}
readyCondition := s.Status.GetCondition(messagingv1beta1.SubscriptionConditionReady)
if readyCondition != nil {
ss.SubscriptionStatuses[i].ReadyCondition = *readyCondition
if readyCondition.Status != corev1.ConditionTrue {
allReady = false
}
} else {
allReady = false
}
}
if allReady {
pCondSet.Manage(ss).MarkTrue(SequenceConditionSubscriptionsReady)
} else {
ss.MarkSubscriptionsNotReady("SubscriptionsNotReady", "Subscriptions are not ready yet, or there are none")
}
}
// PropagateChannelStatuses sets the ChannelStatuses and SequenceConditionChannelsReady based on the
// status of the incoming channels.
func (ss *SequenceStatus) PropagateChannelStatuses(channels []*duckv1beta1.Channelable) {
ss.ChannelStatuses = make([]SequenceChannelStatus, len(channels))
allReady := true
// If there are no channels, treat that as a False case. Could go either way, but this seems right.
if len(channels) == 0 {
allReady = false
}
for i, c := range channels {
ss.ChannelStatuses[i] = SequenceChannelStatus{
Channel: corev1.ObjectReference{
APIVersion: c.APIVersion,
Kind: c.Kind,
Name: c.Name,
Namespace: c.Namespace,
},
}
// TODO: Once the addressable has a real status to dig through, use that here instead of
// addressable, because it might be addressable but not ready.
address := c.Status.AddressStatus.Address
if address != nil {
ss.ChannelStatuses[i].ReadyCondition = apis.Condition{Type: apis.ConditionReady, Status: corev1.ConditionTrue}
} else {
ss.ChannelStatuses[i].ReadyCondition = apis.Condition{Type: apis.ConditionReady, Status: corev1.ConditionFalse, Reason: "NotAddressable", Message: "Channel is not addressable"}
allReady = false
}
// Mark the Sequence address as the Address of the first channel.
if i == 0 {
ss.setAddress(address)
}
}
if allReady {
pCondSet.Manage(ss).MarkTrue(SequenceConditionChannelsReady)
} else {
ss.MarkChannelsNotReady("ChannelsNotReady", "Channels are not ready yet, or there are none")
}
}
func (ss *SequenceStatus) MarkChannelsNotReady(reason, messageFormat string, messageA ...interface{}) {
pCondSet.Manage(ss).MarkFalse(SequenceConditionChannelsReady, reason, messageFormat, messageA...)
}
func (ss *SequenceStatus) MarkSubscriptionsNotReady(reason, messageFormat string, messageA ...interface{}) {
pCondSet.Manage(ss).MarkFalse(SequenceConditionSubscriptionsReady, reason, messageFormat, messageA...)
}
func (ss *SequenceStatus) MarkAddressableNotReady(reason, messageFormat string, messageA ...interface{}) {
pCondSet.Manage(ss).MarkFalse(SequenceConditionAddressable, reason, messageFormat, messageA...)
}
func (ss *SequenceStatus) setAddress(address *pkgduckv1.Addressable) {
ss.Address = address
if address == nil {
pCondSet.Manage(ss).MarkFalse(SequenceConditionAddressable, "emptyAddress", "addressable is nil")
} else {
pCondSet.Manage(ss).MarkTrue(SequenceConditionAddressable)
}
}