This repository has been archived by the owner on Jun 19, 2022. It is now read-only.
/
topic_lifecycle.go
126 lines (110 loc) · 5.19 KB
/
topic_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
/*
* Copyright 2019 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 v1alpha1
import (
corev1 "k8s.io/api/core/v1"
"knative.dev/pkg/apis"
"knative.dev/pkg/apis/duck/v1alpha1"
v1 "knative.dev/serving/pkg/apis/serving/v1"
)
// GetCondition returns the condition currently associated with the given type,
// or nil.
func (ts *TopicStatus) GetCondition(t apis.ConditionType) *apis.Condition {
return topicCondSet.Manage(ts).GetCondition(t)
}
// GetTopLevelCondition returns the top level condition
func (ts *TopicStatus) GetTopLevelCondition() *apis.Condition {
return topicCondSet.Manage(ts).GetTopLevelCondition()
}
// IsReady returns true if the resource is ready overall.
func (ts *TopicStatus) IsReady() bool {
return topicCondSet.Manage(ts).IsHappy()
}
// InitializeConditions sets relevant unset conditions to Unknown state.
func (ts *TopicStatus) InitializeConditions() {
topicCondSet.Manage(ts).InitializeConditions()
}
// TODO: Use the new beta duck types.
func (ts *TopicStatus) SetAddress(url *apis.URL) {
if ts.Address == nil {
ts.Address = &v1alpha1.Addressable{}
}
if url != nil {
ts.Address.Hostname = url.Host
ts.Address.URL = url
topicCondSet.Manage(ts).MarkTrue(TopicConditionAddressable)
} else {
ts.Address.Hostname = ""
ts.Address.URL = nil
topicCondSet.Manage(ts).MarkFalse(TopicConditionAddressable, "emptyUrl", "url is the empty string")
// The TopicConditionAddressable is not included in the ready set as we don't want to create Publishers for Sources.
// We therefore need to set the ConditionReady to false here.
topicCondSet.Manage(ts).MarkFalse(apis.ConditionReady, "emptyUrl", "url is the empty string")
}
}
func (ts *TopicStatus) PropagatePublisherStatus(ss *v1.ServiceStatus) {
sc := ss.GetCondition(apis.ConditionReady)
if sc == nil {
ts.MarkPublisherNotConfigured()
return
}
switch {
case sc.Status == corev1.ConditionUnknown:
ts.MarkPublisherUnknown(sc.Reason, sc.Message)
case sc.Status == corev1.ConditionTrue:
ts.SetAddress(ss.Address.URL)
ts.MarkPublisherDeployed()
case sc.Status == corev1.ConditionFalse:
ts.MarkPublisherNotDeployed(sc.Reason, sc.Message)
default:
ts.MarkPublisherUnknown("TopicUnknown", "The status of Topic is invalid: %v", sc.Status)
}
}
// MarkPublisherDeployed sets the condition that the publisher has been deployed.
func (ts *TopicStatus) MarkPublisherDeployed() {
topicCondSet.Manage(ts).MarkTrue(TopicConditionPublisherReady)
}
// MarkPublisherUnknown sets the condition that the status of publisher is Unknown.
func (ts *TopicStatus) MarkPublisherUnknown(reason, messageFormat string, messageA ...interface{}) {
topicCondSet.Manage(ts).MarkUnknown(TopicConditionPublisherReady, reason, messageFormat, messageA...)
// The TopicConditionPublisherReady is not included in the ready set as we don't want to create Publishers for Sources.
// We therefore need to set the ConditionReady to unknown here.
topicCondSet.Manage(ts).MarkUnknown(apis.ConditionReady, reason, messageFormat, messageA...)
}
// MarkPublisherNotDeployed sets the condition that the publisher has not been deployed.
func (ts *TopicStatus) MarkPublisherNotDeployed(reason, messageFormat string, messageA ...interface{}) {
topicCondSet.Manage(ts).MarkFalse(TopicConditionPublisherReady, reason, messageFormat, messageA...)
// The TopicConditionPublisherReady is not included in the ready set as we don't want to create Publishers for Sources.
// We therefore need to set the ConditionReady to false here.
topicCondSet.Manage(ts).MarkFalse(apis.ConditionReady, reason, messageFormat, messageA...)
}
// MarkPublisherNotConfigured changes the PublisherReady condition to be unknown to reflect
// that the Publisher does not yet have a Status.
func (ts *TopicStatus) MarkPublisherNotConfigured() {
topicCondSet.Manage(ts).MarkUnknown(TopicConditionPublisherReady, "PublisherNotConfigured", "Publisher has not yet been reconciled")
// The TopicConditionPublisherReady is not included in the ready set as we don't want to create Publishers for Sources.
// We therefore need to set the ConditionReady to unknown here.
topicCondSet.Manage(ts).MarkUnknown(apis.ConditionReady, "PublisherNotConfigured", "Publisher has not yet been reconciled")
}
// MarkTopicReady sets the condition that the topic has been created.
func (ts *TopicStatus) MarkTopicReady() {
topicCondSet.Manage(ts).MarkTrue(TopicConditionTopicExists)
}
// MarkNoTopic sets the condition that signals there is not a topic for this
// Topic. This could be because of an error or the Topic is being deleted.
func (ts *TopicStatus) MarkNoTopic(reason, messageFormat string, messageA ...interface{}) {
topicCondSet.Manage(ts).MarkFalse(TopicConditionTopicExists, reason, messageFormat, messageA...)
}