From ce38a0326cd371ce6fed72940f7cb47ce6ab09e8 Mon Sep 17 00:00:00 2001 From: Weston Haught Date: Tue, 5 May 2020 12:19:29 -0700 Subject: [PATCH 1/2] Include GetHappyConditionType for KRShaped --- apis/duck/v1/kresource_type.go | 12 +++++++ apis/duck/v1/kresource_type_test.go | 43 +++++++++++++++++++++++++ apis/test/example/v1alpha1/fiz_types.go | 5 +++ apis/test/example/v1alpha1/foo_types.go | 5 +++ apis/test/pub/v1alpha1/bar_types.go | 5 +++ 5 files changed, 70 insertions(+) create mode 100644 apis/duck/v1/kresource_type_test.go diff --git a/apis/duck/v1/kresource_type.go b/apis/duck/v1/kresource_type.go index 7ea2d70c92..0ee7ba67c9 100644 --- a/apis/duck/v1/kresource_type.go +++ b/apis/duck/v1/kresource_type.go @@ -33,6 +33,8 @@ type KRShaped interface { GetTypeMeta() *metav1.TypeMeta GetStatus() *Status + + GetHappyConditionType() apis.ConditionType } // Asserts KResource conformance with KRShaped @@ -88,3 +90,13 @@ func (t *KResource) GetTypeMeta() *metav1.TypeMeta { func (t *KResource) GetStatus() *Status { return &t.Status } + +// GetHappyConditionType retrieves the happy condition of this resource. Implements the KRShaped interface. +func (t *KResource) GetHappyConditionType() apis.ConditionType { + // 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.ConditionReady +} diff --git a/apis/duck/v1/kresource_type_test.go b/apis/duck/v1/kresource_type_test.go new file mode 100644 index 0000000000..1700694574 --- /dev/null +++ b/apis/duck/v1/kresource_type_test.go @@ -0,0 +1,43 @@ +/* +Copyright 2020 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 v1 + +import ( + "testing" + + "knative.dev/pkg/apis" +) + +func TestGetHappyCondition(t *testing.T) { + resource := KResource{} + + condSet := apis.NewLivingConditionSet("Foo") + mgr := condSet.Manage(resource.GetStatus()) + mgr.InitializeConditions() + + if resource.GetHappyConditionType() != apis.ConditionReady { + t.Error("Expected Ready as happy condition for living condition set type") + } + + condSet = apis.NewBatchConditionSet("Foo") + mgr = condSet.Manage(resource.GetStatus()) + mgr.InitializeConditions() + + if resource.GetHappyConditionType() != apis.ConditionSucceeded { + t.Error("Expected Succeeded as happy condition for living condition set type") + } +} diff --git a/apis/test/example/v1alpha1/fiz_types.go b/apis/test/example/v1alpha1/fiz_types.go index 513366886e..cd93bc76b6 100644 --- a/apis/test/example/v1alpha1/fiz_types.go +++ b/apis/test/example/v1alpha1/fiz_types.go @@ -103,3 +103,8 @@ func (f *ClusterFiz) GetTypeMeta() *metav1.TypeMeta { func (f *ClusterFiz) GetStatus() *duckv1.Status { return &f.Status.Status } + +// GetHappyConditionType retrieves the happy condition of this resource. Implements the KRShaped interface. +func (f *ClusterFiz) GetHappyConditionType() apis.ConditionType { + return apis.ConditionReady +} diff --git a/apis/test/example/v1alpha1/foo_types.go b/apis/test/example/v1alpha1/foo_types.go index 616a7c6a94..df7dddb295 100644 --- a/apis/test/example/v1alpha1/foo_types.go +++ b/apis/test/example/v1alpha1/foo_types.go @@ -102,3 +102,8 @@ func (f *Foo) GetTypeMeta() *metav1.TypeMeta { func (f *Foo) GetStatus() *duckv1.Status { return &f.Status.Status } + +// GetHappyConditionType retrieves the happy condition of this resource. Implements the KRShaped interface. +func (t *Foo) GetHappyConditionType() apis.ConditionType { + return apis.ConditionSucceeded +} diff --git a/apis/test/pub/v1alpha1/bar_types.go b/apis/test/pub/v1alpha1/bar_types.go index 90b371fb75..6f8e37f536 100644 --- a/apis/test/pub/v1alpha1/bar_types.go +++ b/apis/test/pub/v1alpha1/bar_types.go @@ -102,3 +102,8 @@ func (b *Bar) GetTypeMeta() *metav1.TypeMeta { func (b *Bar) GetStatus() *duckv1.Status { return &b.Status.Status } + +// GetHappyConditionType retrieves the happy condition of this resource. Implements the KRShaped interface. +func (t *Bar) GetHappyConditionType() apis.ConditionType { + return apis.ConditionReady +} From 474919f04073edea6c2ded550e937ade02cee123 Mon Sep 17 00:00:00 2001 From: Weston Haught Date: Tue, 5 May 2020 14:01:00 -0700 Subject: [PATCH 2/2] Rename to GetTopLevelCondition --- apis/duck/v1/kresource_type.go | 6 +++--- apis/duck/v1/kresource_type_test.go | 6 +++--- apis/test/example/v1alpha1/fiz_types.go | 4 ++-- apis/test/example/v1alpha1/foo_types.go | 4 ++-- apis/test/pub/v1alpha1/bar_types.go | 4 ++-- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/apis/duck/v1/kresource_type.go b/apis/duck/v1/kresource_type.go index 0ee7ba67c9..b64977fcb5 100644 --- a/apis/duck/v1/kresource_type.go +++ b/apis/duck/v1/kresource_type.go @@ -34,7 +34,7 @@ type KRShaped interface { GetStatus() *Status - GetHappyConditionType() apis.ConditionType + GetTopLevelConditionType() apis.ConditionType } // Asserts KResource conformance with KRShaped @@ -91,8 +91,8 @@ func (t *KResource) GetStatus() *Status { return &t.Status } -// GetHappyConditionType retrieves the happy condition of this resource. Implements the KRShaped interface. -func (t *KResource) GetHappyConditionType() apis.ConditionType { +// GetTopLevelConditionType retrieves the happy condition of this resource. Implements the KRShaped interface. +func (t *KResource) GetTopLevelConditionType() apis.ConditionType { // 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 { diff --git a/apis/duck/v1/kresource_type_test.go b/apis/duck/v1/kresource_type_test.go index 1700694574..357353b14b 100644 --- a/apis/duck/v1/kresource_type_test.go +++ b/apis/duck/v1/kresource_type_test.go @@ -22,14 +22,14 @@ import ( "knative.dev/pkg/apis" ) -func TestGetHappyCondition(t *testing.T) { +func TestGetTopLevelCondition(t *testing.T) { resource := KResource{} condSet := apis.NewLivingConditionSet("Foo") mgr := condSet.Manage(resource.GetStatus()) mgr.InitializeConditions() - if resource.GetHappyConditionType() != apis.ConditionReady { + if resource.GetTopLevelConditionType() != apis.ConditionReady { t.Error("Expected Ready as happy condition for living condition set type") } @@ -37,7 +37,7 @@ func TestGetHappyCondition(t *testing.T) { mgr = condSet.Manage(resource.GetStatus()) mgr.InitializeConditions() - if resource.GetHappyConditionType() != apis.ConditionSucceeded { + if resource.GetTopLevelConditionType() != apis.ConditionSucceeded { t.Error("Expected Succeeded as happy condition for living condition set type") } } diff --git a/apis/test/example/v1alpha1/fiz_types.go b/apis/test/example/v1alpha1/fiz_types.go index cd93bc76b6..334e827a5c 100644 --- a/apis/test/example/v1alpha1/fiz_types.go +++ b/apis/test/example/v1alpha1/fiz_types.go @@ -104,7 +104,7 @@ func (f *ClusterFiz) GetStatus() *duckv1.Status { return &f.Status.Status } -// GetHappyConditionType retrieves the happy condition of this resource. Implements the KRShaped interface. -func (f *ClusterFiz) GetHappyConditionType() apis.ConditionType { +// GetTopLevelConditionType retrieves the happy condition of this resource. Implements the KRShaped interface. +func (*ClusterFiz) GetTopLevelConditionType() apis.ConditionType { return apis.ConditionReady } diff --git a/apis/test/example/v1alpha1/foo_types.go b/apis/test/example/v1alpha1/foo_types.go index df7dddb295..948254353e 100644 --- a/apis/test/example/v1alpha1/foo_types.go +++ b/apis/test/example/v1alpha1/foo_types.go @@ -103,7 +103,7 @@ func (f *Foo) GetStatus() *duckv1.Status { return &f.Status.Status } -// GetHappyConditionType retrieves the happy condition of this resource. Implements the KRShaped interface. -func (t *Foo) GetHappyConditionType() apis.ConditionType { +// GetTopLevelConditionType retrieves the happy condition of this resource. Implements the KRShaped interface. +func (*Foo) GetTopLevelConditionType() apis.ConditionType { return apis.ConditionSucceeded } diff --git a/apis/test/pub/v1alpha1/bar_types.go b/apis/test/pub/v1alpha1/bar_types.go index 6f8e37f536..f27d5f6608 100644 --- a/apis/test/pub/v1alpha1/bar_types.go +++ b/apis/test/pub/v1alpha1/bar_types.go @@ -103,7 +103,7 @@ func (b *Bar) GetStatus() *duckv1.Status { return &b.Status.Status } -// GetHappyConditionType retrieves the happy condition of this resource. Implements the KRShaped interface. -func (t *Bar) GetHappyConditionType() apis.ConditionType { +// GetTopLevelConditionType retrieves the happy condition of this resource. Implements the KRShaped interface. +func (*Bar) GetTopLevelConditionType() apis.ConditionType { return apis.ConditionReady }