diff --git a/pkg/apis/eventing/v1alpha1/channel_types.go b/pkg/apis/eventing/v1alpha1/channel_types.go index 316f2f7e407..8c69a2124fa 100644 --- a/pkg/apis/eventing/v1alpha1/channel_types.go +++ b/pkg/apis/eventing/v1alpha1/channel_types.go @@ -117,6 +117,21 @@ func (cs *ChannelStatus) GetCondition(t duckv1alpha1.ConditionType) *duckv1alpha return chanCondSet.Manage(cs).GetCondition(t) } +// IsReady returns true if the resource is ready overall. +func (cs *ChannelStatus) IsReady() bool { + return chanCondSet.Manage(cs).IsHappy() +} + +// InitializeConditions sets relevant unset conditions to Unknown state. +func (cs *ChannelStatus) InitializeConditions() { + chanCondSet.Manage(cs).InitializeConditions() +} + +// MarkProvisioned sets ChannelConditionProvisioned condition to True state. +func (cs *ChannelStatus) MarkProvisioned() { + chanCondSet.Manage(cs).MarkTrue(ChannelConditionProvisioned) +} + // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object // ChannelList is a collection of Channels. diff --git a/pkg/apis/eventing/v1alpha1/channel_types_test.go b/pkg/apis/eventing/v1alpha1/channel_types_test.go index e8a2b2d188a..2f208018207 100644 --- a/pkg/apis/eventing/v1alpha1/channel_types_test.go +++ b/pkg/apis/eventing/v1alpha1/channel_types_test.go @@ -20,6 +20,7 @@ import ( "testing" "github.com/google/go-cmp/cmp" + "github.com/google/go-cmp/cmp/cmpopts" duckv1alpha1 "github.com/knative/pkg/apis/duck/v1alpha1" corev1 "k8s.io/api/core/v1" ) @@ -80,3 +81,95 @@ func TestChannelGetCondition(t *testing.T) { }) } } + +func TestChannelInitializeConditions(t *testing.T) { + tests := []struct { + name string + cs *ChannelStatus + want *ChannelStatus + }{{ + name: "empty", + cs: &ChannelStatus{}, + want: &ChannelStatus{ + Conditions: []duckv1alpha1.Condition{{ + Type: ChannelConditionProvisioned, + Status: corev1.ConditionUnknown, + }, { + Type: ChannelConditionReady, + Status: corev1.ConditionUnknown, + }}, + }, + }, { + name: "one false", + cs: &ChannelStatus{ + Conditions: []duckv1alpha1.Condition{{ + Type: ChannelConditionProvisioned, + Status: corev1.ConditionFalse, + }}, + }, + want: &ChannelStatus{ + Conditions: []duckv1alpha1.Condition{{ + Type: ChannelConditionProvisioned, + Status: corev1.ConditionFalse, + }, { + Type: ChannelConditionReady, + Status: corev1.ConditionUnknown, + }}, + }, + }, { + name: "one true", + cs: &ChannelStatus{ + Conditions: []duckv1alpha1.Condition{{ + Type: ChannelConditionProvisioned, + Status: corev1.ConditionTrue, + }}, + }, + want: &ChannelStatus{ + Conditions: []duckv1alpha1.Condition{{ + Type: ChannelConditionProvisioned, + Status: corev1.ConditionTrue, + }, { + Type: ChannelConditionReady, + Status: corev1.ConditionUnknown, + }}}, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + test.cs.InitializeConditions() + ignore := cmpopts.IgnoreFields(duckv1alpha1.Condition{}, "LastTransitionTime") + if diff := cmp.Diff(test.want, test.cs, ignore); diff != "" { + t.Errorf("unexpected conditions (-want, +got) = %v", diff) + } + }) + } +} + +func TestChannelIsReady(t *testing.T) { + tests := []struct { + name string + markProvisioned bool + wantReady bool + }{{ + name: "all happy", + markProvisioned: true, + wantReady: true, + }, { + name: "one sad", + markProvisioned: false, + wantReady: false, + }} + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + cs := &ChannelStatus{} + if test.markProvisioned { + cs.MarkProvisioned() + } + got := cs.IsReady() + if test.wantReady != got { + t.Errorf("unexpected readiness: want %v, got %v", test.wantReady, got) + } + }) + } +}