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"
)
// 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 {
metav1.ObjectMetaAccessor
@ -34,7 +34,7 @@ type KRShaped interface {
GetStatus() *Status
GetTopLevelConditionType() apis.ConditionType
GetConditionSet() apis.ConditionSet
}
// Asserts KResource conformance with KRShaped
@ -91,12 +91,12 @@ func (t *KResource) GetStatus() *Status {
return &t.Status
}
// GetTopLevelConditionType retrieves the happy condition of this resource. Implements the KRShaped interface.
func (t *KResource) GetTopLevelConditionType() apis.ConditionType {
// GetConditionSet retrieves the condition set for this resource. Implements the KRShaped interface.
func (t *KResource) GetConditionSet() apis.ConditionSet {
// Note: KResources are unmarshalled from existing resources. This will only work properly for resources that
// have already been initialized to their type.
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"
)
func TestGetTopLevelCondition(t *testing.T) {
resource := KResource{}
condSet := apis.NewLivingConditionSet("Foo")
mgr := condSet.Manage(resource.GetStatus())
mgr.InitializeConditions()
if resource.GetTopLevelConditionType() != apis.ConditionReady {
t.Error("Expected Ready as happy condition for living condition set type")
func TestGetConditionSet(t *testing.T) {
testCases := []struct {
name string
condSet apis.ConditionSet
expectedTopLevelCondition apis.ConditionType
}{
{
name: "living set",
condSet: apis.NewLivingConditionSet(),
expectedTopLevelCondition: apis.ConditionReady,
},
{
name: "batch set",
condSet: apis.NewBatchConditionSet(),
expectedTopLevelCondition: apis.ConditionSucceeded,
},
}
condSet = apis.NewBatchConditionSet("Foo")
mgr = condSet.Manage(resource.GetStatus())
mgr.InitializeConditions()
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
resource := KResource{}
test.condSet.Manage(resource.GetStatus()).InitializeConditions()
if resource.GetTopLevelConditionType() != apis.ConditionSucceeded {
t.Error("Expected Succeeded as happy condition for living condition set type")
mgr := resource.GetConditionSet().Manage(resource.GetStatus())
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
}
// GetTopLevelConditionType retrieves the happy condition of this resource. Implements the KRShaped interface.
func (*ClusterFiz) GetTopLevelConditionType() apis.ConditionType {
return apis.ConditionReady
// GetConditionSet retrieves the condition set for this resource. Implements the KRShaped interface.
func (*ClusterFiz) GetConditionSet() apis.ConditionSet {
return apis.NewLivingConditionSet()
}

View File

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

View File

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