Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions pkg/apis/eventing/v1alpha1/broker_lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ func (bs *BrokerStatus) GetCondition(t apis.ConditionType) *apis.Condition {
return brokerCondSet.Manage(bs).GetCondition(t)
}

// GetTopLevelCondition returns the top level Condition.
func (bs *BrokerStatus) GetTopLevelCondition() *apis.Condition {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change needs to happen in Serving, Sample-Source and Sample-Controller now too right?

Copy link
Copy Markdown
Contributor Author

@capri-xiyue capri-xiyue Jan 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. I think this change needs to happen in Serving and Sample-Source. But it won't break anything if people don't use it now. People can also use GetCondition(t apis.ConditionType) to get the top level condition. GetTopLevelCondition just provides a more convenient way to achieve it. What do you mean by "Sample-Controller"(The controller for reconcile)?

return brokerCondSet.Manage(bs).GetTopLevelCondition()
}

// IsReady returns true if the resource is ready overall.
func (bs *BrokerStatus) IsReady() bool {
return brokerCondSet.Manage(bs).IsHappy()
Expand Down
41 changes: 39 additions & 2 deletions pkg/apis/eventing/v1alpha1/eventtype_lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package v1alpha1

import (
corev1 "k8s.io/api/core/v1"
"knative.dev/pkg/apis"
)

Expand All @@ -38,6 +39,11 @@ func (et *EventTypeStatus) IsReady() bool {
return eventTypeCondSet.Manage(et).IsHappy()
}

// GetTopLevelCondition returns the top level Condition.
func (et *EventTypeStatus) GetTopLevelCondition() *apis.Condition {
return eventTypeCondSet.Manage(et).GetTopLevelCondition()
}

// InitializeConditions sets relevant unset conditions to Unknown state.
func (et *EventTypeStatus) InitializeConditions() {
eventTypeCondSet.Manage(et).InitializeConditions()
Expand All @@ -51,10 +57,41 @@ func (et *EventTypeStatus) MarkBrokerDoesNotExist() {
eventTypeCondSet.Manage(et).MarkFalse(EventTypeConditionBrokerExists, "BrokerDoesNotExist", "Broker does not exist")
}

func (et *EventTypeStatus) MarkBrokerExistsUnknown(reason, messageFormat string, messageA ...interface{}) {
eventTypeCondSet.Manage(et).MarkUnknown(EventTypeConditionBrokerExists, reason, messageFormat, messageA...)
}

func (et *EventTypeStatus) MarkBrokerReady() {
eventTypeCondSet.Manage(et).MarkTrue(EventTypeConditionBrokerReady)
}

func (et *EventTypeStatus) MarkBrokerNotReady() {
eventTypeCondSet.Manage(et).MarkFalse(EventTypeConditionBrokerReady, "BrokerNotReady", "Broker is not ready")
func (et *EventTypeStatus) MarkBrokerFailed(reason, messageFormat string, messageA ...interface{}) {
eventTypeCondSet.Manage(et).MarkFalse(EventTypeConditionBrokerReady, reason, messageFormat, messageA...)
}

func (et *EventTypeStatus) MarkBrokerUnknown(reason, messageFormat string, messageA ...interface{}) {
eventTypeCondSet.Manage(et).MarkUnknown(EventTypeConditionBrokerReady, reason, messageFormat, messageA...)
}

func (et *EventTypeStatus) MarkBrokerNotConfigured() {
eventTypeCondSet.Manage(et).MarkUnknown(EventTypeConditionBrokerReady,
"BrokerNotConfigured", "Broker has not yet been reconciled.")
}

func (et *EventTypeStatus) PropagateBrokerStatus(bs *BrokerStatus) {
bc := brokerCondSet.Manage(bs).GetTopLevelCondition()
if bc == nil {
et.MarkBrokerNotConfigured()
return
}
switch {
case bc.Status == corev1.ConditionUnknown:
et.MarkBrokerUnknown(bc.Reason, bc.Message)
case bc.Status == corev1.ConditionTrue:
eventTypeCondSet.Manage(et).MarkTrue(EventTypeConditionBrokerReady)
case bc.Status == corev1.ConditionFalse:
et.MarkBrokerFailed(bc.Reason, bc.Message)
default:
et.MarkBrokerUnknown("BrokerUnknown", "The status of Broker is invalid: %v", bc.Status)
}
}
64 changes: 33 additions & 31 deletions pkg/apis/eventing/v1alpha1/eventtype_lifecycle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,33 +194,39 @@ func TestEventTypeInitializeConditions(t *testing.T) {
}
}

func TestEventTypeIsReady(t *testing.T) {
func TestEventTypeConditionStatus(t *testing.T) {
tests := []struct {
name string
markBrokerExists *bool
markBrokerReady *bool
wantReady bool
name string
markBrokerExists *bool
brokerStatus *BrokerStatus
wantConditionStatus corev1.ConditionStatus
}{{
name: "all happy",
markBrokerExists: &trueValue,
markBrokerReady: &trueValue,
wantReady: true,
name: "all happy",
markBrokerExists: &trueValue,
brokerStatus: TestHelper.ReadyBrokerStatus(),
wantConditionStatus: corev1.ConditionTrue,
}, {
name: "broker exist sad",
markBrokerExists: &falseValue,
markBrokerReady: &trueValue,
wantReady: false,
name: "broker exist sad",
markBrokerExists: &falseValue,
brokerStatus: nil,
wantConditionStatus: corev1.ConditionFalse,
}, {
name: "broker ready sad",
markBrokerExists: &trueValue,
markBrokerReady: &falseValue,
wantReady: false,
name: "broker ready sad",
markBrokerExists: &trueValue,
brokerStatus: TestHelper.FalseBrokerStatus(),
wantConditionStatus: corev1.ConditionFalse,
}, {
name: "all sad",
markBrokerExists: &falseValue,
markBrokerReady: &falseValue,
wantReady: false,
}}
name: "broker ready unknown",
markBrokerExists: &trueValue,
brokerStatus: TestHelper.UnknownBrokerStatus(),
wantConditionStatus: corev1.ConditionUnknown,
},
{
name: "all sad",
markBrokerExists: &falseValue,
brokerStatus: nil,
wantConditionStatus: corev1.ConditionFalse,
}}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ets := &EventTypeStatus{}
Expand All @@ -231,17 +237,13 @@ func TestEventTypeIsReady(t *testing.T) {
ets.MarkBrokerDoesNotExist()
}
}
if test.markBrokerReady != nil {
if *test.markBrokerReady {
ets.MarkBrokerReady()
} else {
ets.MarkBrokerNotReady()
}
if test.brokerStatus != nil {
ets.PropagateBrokerStatus(test.brokerStatus)
}

got := ets.IsReady()
if test.wantReady != got {
t.Errorf("unexpected readiness: want %v, got %v", test.wantReady, got)
got := ets.GetTopLevelCondition().Status
if test.wantConditionStatus != got {
t.Errorf("unexpected readiness: want %v, got %v", test.wantConditionStatus, got)
}
})
}
Expand Down
23 changes: 20 additions & 3 deletions pkg/apis/eventing/v1alpha1/test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package v1alpha1

import (
v1 "k8s.io/api/apps/v1"
"knative.dev/eventing/pkg/apis/legacysources/v1alpha1"
"knative.dev/pkg/apis"
pkgduckv1alpha1 "knative.dev/pkg/apis/duck/v1alpha1"

Expand Down Expand Up @@ -59,10 +60,10 @@ func (testHelper) ReadySubscriptionStatus() *messagingv1alpha1.SubscriptionStatu
return ss
}

func (testHelper) NotReadySubscriptionStatus() *messagingv1alpha1.SubscriptionStatus {
func (testHelper) FalseSubscriptionStatus() *messagingv1alpha1.SubscriptionStatus {
ss := &messagingv1alpha1.SubscriptionStatus{}
ss.MarkReferencesResolved()
ss.MarkChannelNotReady("testInducedError", "test induced %s", "error")
ss.MarkChannelFailed("testInducedError", "test induced %s", "error")
return ss
}

Expand All @@ -75,8 +76,18 @@ func (t testHelper) ReadyBrokerStatus() *BrokerStatus {
return bs
}

func (t testHelper) NotReadyBrokerStatus() *BrokerStatus {
func (t testHelper) UnknownBrokerStatus() *BrokerStatus {
bs := &BrokerStatus{}
bs.InitializeConditions()
return bs
}

func (t testHelper) FalseBrokerStatus() *BrokerStatus {
bs := &BrokerStatus{}
bs.MarkIngressFailed("DeploymentUnavailable", "The Deployment is unavailable.")
bs.MarkTriggerChannelFailed("ChannelNotReady", "trigger Channel is not ready: not addressalbe")
bs.MarkFilterFailed("DeploymentUnavailable", "The Deployment is unavailable.")
bs.SetAddress(nil)
return bs
}

Expand Down Expand Up @@ -112,3 +123,9 @@ func (t testHelper) AvailableDeployment() *v1.Deployment {
}
return d
}

func (t testHelper) UnknownCronJobSourceStatus() *v1alpha1.CronJobSourceStatus {
cjss := &v1alpha1.CronJobSourceStatus{}
cjss.InitializeConditions()
return cjss
}
91 changes: 70 additions & 21 deletions pkg/apis/eventing/v1alpha1/trigger_lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package v1alpha1

import (
corev1 "k8s.io/api/core/v1"
messagingv1alpha1 "knative.dev/eventing/pkg/apis/messaging/v1alpha1"
"knative.dev/pkg/apis"
duckv1 "knative.dev/pkg/apis/duck/v1"
Expand Down Expand Up @@ -45,6 +46,11 @@ func (ts *TriggerStatus) GetCondition(t apis.ConditionType) *apis.Condition {
return triggerCondSet.Manage(ts).GetCondition(t)
}

// GetTopLevelCondition returns the top level Condition.
func (ts *TriggerStatus) GetTopLevelCondition() *apis.Condition {
return triggerCondSet.Manage(ts).GetTopLevelCondition()
}

// IsReady returns true if the resource is ready overall.
func (ts *TriggerStatus) IsReady() bool {
return triggerCondSet.Manage(ts).IsHappy()
Expand All @@ -56,41 +62,73 @@ func (ts *TriggerStatus) InitializeConditions() {
}

func (ts *TriggerStatus) PropagateBrokerStatus(bs *BrokerStatus) {
if bs.IsReady() {
bc := brokerCondSet.Manage(bs).GetTopLevelCondition()
if bc == nil {
ts.MarkBrokerNotConfigured()
return
}

switch {
case bc.Status == corev1.ConditionUnknown:
ts.MarkBrokerUnknown(bc.Reason, bc.Message)
case bc.Status == corev1.ConditionTrue:
triggerCondSet.Manage(ts).MarkTrue(TriggerConditionBroker)
} else {
msg := "nil"
if bc := brokerCondSet.Manage(bs).GetCondition(BrokerConditionReady); bc != nil {
msg = bc.Message
}
ts.MarkBrokerFailed("BrokerNotReady", "Broker is not ready: %s", msg)
case bc.Status == corev1.ConditionFalse:
ts.MarkBrokerFailed(bc.Reason, bc.Message)
default:
ts.MarkBrokerUnknown("BrokerUnknown", "The status of Broker is invalid: %v", bc.Status)
}
}

func (ts *TriggerStatus) MarkBrokerFailed(reason, messageFormat string, messageA ...interface{}) {
triggerCondSet.Manage(ts).MarkFalse(TriggerConditionBroker, reason, messageFormat, messageA...)
}

func (ts *TriggerStatus) MarkBrokerUnknown(reason, messageFormat string, messageA ...interface{}) {
triggerCondSet.Manage(ts).MarkUnknown(TriggerConditionBroker, reason, messageFormat, messageA...)
}

func (ts *TriggerStatus) MarkBrokerNotConfigured() {
triggerCondSet.Manage(ts).MarkUnknown(TriggerConditionBroker,
"BrokerNotConfigured", "Broker has not yet been reconciled.")
}

func (ts *TriggerStatus) PropagateSubscriptionStatus(ss *messagingv1alpha1.SubscriptionStatus) {
if ss.IsReady() {
sc := messagingv1alpha1.SubCondSet.Manage(ss).GetTopLevelCondition()
if sc == nil {
ts.MarkSubscriptionNotConfigured()
return
}

switch {
case sc.Status == corev1.ConditionUnknown:
ts.MarkSubscribedUnknown(sc.Reason, sc.Message)
case sc.Status == corev1.ConditionTrue:
triggerCondSet.Manage(ts).MarkTrue(TriggerConditionSubscribed)
} else {
msg := "nil"
if sc := ss.Status.GetCondition(messagingv1alpha1.SubscriptionConditionReady); sc != nil {
msg = sc.Message
}
ts.MarkNotSubscribed("SubscriptionNotReady", "Subscription is not ready: %s", msg)
case sc.Status == corev1.ConditionFalse:
ts.MarkNotSubscribed(sc.Reason, sc.Message)
default:
ts.MarkSubscribedUnknown("SubscriptionUnknown", "The status of Subscription is invalid: %v", sc.Status)
}
}

func (ts *TriggerStatus) MarkNotSubscribed(reason, messageFormat string, messageA ...interface{}) {
triggerCondSet.Manage(ts).MarkFalse(TriggerConditionSubscribed, reason, messageFormat, messageA...)
}

func (ts *TriggerStatus) MarkSubscribedUnknown(reason, messageFormat string, messageA ...interface{}) {
triggerCondSet.Manage(ts).MarkUnknown(TriggerConditionSubscribed, reason, messageFormat, messageA...)
}

func (ts *TriggerStatus) MarkSubscriptionNotOwned(sub *messagingv1alpha1.Subscription) {
triggerCondSet.Manage(ts).MarkFalse(TriggerConditionSubscribed, "SubscriptionNotOwned", "Subscription %q is not owned by this Trigger.", sub.Name)
}

func (ts *TriggerStatus) MarkSubscriptionNotConfigured() {
triggerCondSet.Manage(ts).MarkUnknown(TriggerConditionSubscribed,
"SubscriptionNotConfigured", "Subscription has not yet been reconciled.")
}

func (ts *TriggerStatus) MarkSubscriberResolvedSucceeded() {
triggerCondSet.Manage(ts).MarkTrue(TriggerConditionSubscriberResolved)
}
Expand All @@ -115,15 +153,26 @@ func (ts *TriggerStatus) MarkDependencyUnknown(reason, messageFormat string, mes
triggerCondSet.Manage(ts).MarkUnknown(TriggerConditionDependency, reason, messageFormat, messageA...)
}

func (ts *TriggerStatus) MarkDependencyNotConfigured() {
triggerCondSet.Manage(ts).MarkUnknown(EventTypeConditionBrokerReady,
"DependencyNotConfigured", "Dependency has not yet been reconciled.")
}

func (ts *TriggerStatus) PropagateDependencyStatus(ks *duckv1.KResource) {
kc := ks.Status.GetCondition(apis.ConditionReady)
if kc != nil && kc.IsTrue() {
if kc == nil {
ts.MarkDependencyNotConfigured()
return
}

switch {
case kc.Status == corev1.ConditionUnknown:
ts.MarkDependencyUnknown(kc.Reason, kc.Message)
case kc.Status == corev1.ConditionTrue:
ts.MarkDependencySucceeded()
} else {
msg := "nil"
if kc != nil {
msg = kc.Message
}
ts.MarkDependencyFailed("DependencyNotReady", "Dependency is not ready: %s", msg)
case kc.Status == corev1.ConditionFalse:
ts.MarkDependencyFailed(kc.Reason, kc.Message)
default:
ts.MarkDependencyUnknown("DependencyUnknown", "The status of Dependency is invalid: %v", kc.Status)
}
}
Loading