Switch KRShaped interface to retrieve entire condition set (#1307)

* Switch interface to retrieve entire condition set

* fix method under test

* type
This commit is contained in:
Weston Haught 2020-05-06 14:53:44 -07:00 committed by GitHub
parent 5b98a55816
commit 3769cd27e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 42 additions and 29 deletions

View File

@ -26,7 +26,7 @@ import (
"knative.dev/pkg/apis" "knative.dev/pkg/apis"
) )
// KRShaped is an interface for retrieving the duck elements of an arbitraty resource. // KRShaped is an interface for retrieving the duck elements of an arbitrary resource.
type KRShaped interface { type KRShaped interface {
metav1.ObjectMetaAccessor metav1.ObjectMetaAccessor
@ -34,7 +34,7 @@ type KRShaped interface {
GetStatus() *Status GetStatus() *Status
GetTopLevelConditionType() apis.ConditionType GetConditionSet() apis.ConditionSet
} }
// Asserts KResource conformance with KRShaped // Asserts KResource conformance with KRShaped
@ -91,12 +91,12 @@ func (t *KResource) GetStatus() *Status {
return &t.Status return &t.Status
} }
// GetTopLevelConditionType retrieves the happy condition of this resource. Implements the KRShaped interface. // GetConditionSet retrieves the condition set for this resource. Implements the KRShaped interface.
func (t *KResource) GetTopLevelConditionType() apis.ConditionType { func (t *KResource) GetConditionSet() apis.ConditionSet {
// Note: KResources are unmarshalled from existing resources. This will only work properly for resources that // Note: KResources are unmarshalled from existing resources. This will only work properly for resources that
// have already been initialized to their type. // have already been initialized to their type.
if cond := t.Status.GetCondition(apis.ConditionSucceeded); cond != nil { if cond := t.Status.GetCondition(apis.ConditionSucceeded); cond != nil {
return apis.ConditionSucceeded return apis.NewBatchConditionSet()
} }
return apis.ConditionReady return apis.NewLivingConditionSet()
} }

View File

@ -22,22 +22,35 @@ import (
"knative.dev/pkg/apis" "knative.dev/pkg/apis"
) )
func TestGetTopLevelCondition(t *testing.T) { func TestGetConditionSet(t *testing.T) {
resource := KResource{} testCases := []struct {
name string
condSet := apis.NewLivingConditionSet("Foo") condSet apis.ConditionSet
mgr := condSet.Manage(resource.GetStatus()) expectedTopLevelCondition apis.ConditionType
mgr.InitializeConditions() }{
{
if resource.GetTopLevelConditionType() != apis.ConditionReady { name: "living set",
t.Error("Expected Ready as happy condition for living condition set type") condSet: apis.NewLivingConditionSet(),
expectedTopLevelCondition: apis.ConditionReady,
},
{
name: "batch set",
condSet: apis.NewBatchConditionSet(),
expectedTopLevelCondition: apis.ConditionSucceeded,
},
} }
condSet = apis.NewBatchConditionSet("Foo") for _, test := range testCases {
mgr = condSet.Manage(resource.GetStatus()) t.Run(test.name, func(t *testing.T) {
mgr.InitializeConditions() resource := KResource{}
test.condSet.Manage(resource.GetStatus()).InitializeConditions()
if resource.GetTopLevelConditionType() != apis.ConditionSucceeded { mgr := resource.GetConditionSet().Manage(resource.GetStatus())
t.Error("Expected Succeeded as happy condition for living condition set type")
if mgr.GetTopLevelCondition().Type != test.expectedTopLevelCondition {
t.Errorf("wrong top-level condition got=%s want=%s",
mgr.GetTopLevelCondition().Type, test.expectedTopLevelCondition)
}
})
} }
} }

View File

@ -104,7 +104,7 @@ func (f *ClusterFiz) GetStatus() *duckv1.Status {
return &f.Status.Status return &f.Status.Status
} }
// GetTopLevelConditionType retrieves the happy condition of this resource. Implements the KRShaped interface. // GetConditionSet retrieves the condition set for this resource. Implements the KRShaped interface.
func (*ClusterFiz) GetTopLevelConditionType() apis.ConditionType { func (*ClusterFiz) GetConditionSet() apis.ConditionSet {
return apis.ConditionReady return apis.NewLivingConditionSet()
} }

View File

@ -103,7 +103,7 @@ func (f *Foo) GetStatus() *duckv1.Status {
return &f.Status.Status return &f.Status.Status
} }
// GetTopLevelConditionType retrieves the happy condition of this resource. Implements the KRShaped interface. // GetConditionSet retrieves the condition set for this resource. Implements the KRShaped interface.
func (*Foo) GetTopLevelConditionType() apis.ConditionType { func (*Foo) GetConditionSet() apis.ConditionSet {
return apis.ConditionSucceeded return apis.NewLivingConditionSet()
} }

View File

@ -103,7 +103,7 @@ func (b *Bar) GetStatus() *duckv1.Status {
return &b.Status.Status return &b.Status.Status
} }
// GetTopLevelConditionType retrieves the happy condition of this resource. Implements the KRShaped interface. // GetConditionSet retrieves the condition set for this resource. Implements the KRShaped interface.
func (*Bar) GetTopLevelConditionType() apis.ConditionType { func (*Bar) GetConditionSet() apis.ConditionSet {
return apis.ConditionReady return apis.NewLivingConditionSet()
} }