Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Composed/setexclusiveconditions #780

Closed
10 changes: 0 additions & 10 deletions pkg/composed/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ package composed
import (
"errors"
"fmt"
"github.com/elliotchance/pie/v2"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"time"
)
Expand Down Expand Up @@ -96,10 +93,3 @@ func (e *stopWithRequeueDelay) Delay() time.Duration {
func StopWithRequeueDelay(d time.Duration) error {
return &stopWithRequeueDelay{delay: d}
}

func AnyConditionChanged(obj ObjWithConditions, conditionsToSet ...metav1.Condition) bool {
return pie.All(conditionsToSet, func(x metav1.Condition) bool {
c := meta.FindStatusCondition(*obj.Conditions(), x.Type)
return c == nil || c.Reason != x.Reason || c.Message != x.Message || c.Status != x.Status
})
}
25 changes: 25 additions & 0 deletions pkg/composed/setExclusiveConditions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package composed

import (
"github.com/elliotchance/pie/v2"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func SetExclusiveConditions(conditions *[]metav1.Condition, conditionsToSet ...metav1.Condition) bool {
changed := false

conditionsToRemove := pie.Filter(*conditions, func(condition metav1.Condition) bool {
return meta.FindStatusCondition(conditionsToSet, condition.Type) == nil
})

pie.Each(conditionsToRemove, func(condition metav1.Condition) {
changed = changed || meta.RemoveStatusCondition(conditions, condition.Type)
})

pie.Each(conditionsToSet, func(condition metav1.Condition) {
changed = changed || meta.SetStatusCondition(conditions, condition)
})

return changed
}
135 changes: 135 additions & 0 deletions pkg/composed/setExclusiveConditions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package composed

import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"testing"
)

type SetStatusConditionsSuite struct {
suite.Suite
}

const (
conditionTypeReady = "Ready"
conditionTypeError = "Error"
conditionTypeWarning = "Warning"
)

func (me *SetStatusConditionsSuite) TestConditionRemoved() {
conditions := &[]metav1.Condition{
{
Type: conditionTypeReady,
Status: metav1.ConditionTrue,
},
{
Type: conditionTypeError,
Status: metav1.ConditionTrue,
},
}

assert.True(me.T(), SetExclusiveConditions(conditions, metav1.Condition{
Type: conditionTypeReady,
Status: metav1.ConditionTrue,
}), "Conditions changes should be detected, but didn't")

assert.Equal(me.T(), 1, len(*conditions))
}

func (me *SetStatusConditionsSuite) TestConditionAdded() {
conditions := &[]metav1.Condition{
{
Type: conditionTypeReady,
Status: metav1.ConditionTrue,
},
{
Type: conditionTypeError,
Status: metav1.ConditionTrue,
},
}

assert.True(me.T(), SetExclusiveConditions(conditions,
[]metav1.Condition{
{
Type: conditionTypeReady,
Status: metav1.ConditionTrue,
},
{
Type: conditionTypeError,
Status: metav1.ConditionTrue,
},
{
Type: conditionTypeWarning,
Status: metav1.ConditionTrue,
},
}...,
), "Conditions changes should be detected, but didn't")

assert.Equal(me.T(), 3, len(*conditions))

}

func (me *SetStatusConditionsSuite) TestConditionChanged() {

conditions := &[]metav1.Condition{
{
Type: conditionTypeReady,
Status: metav1.ConditionTrue,
},
{
Type: conditionTypeError,
Status: metav1.ConditionTrue,
},
}

assert.True(me.T(), SetExclusiveConditions(conditions,
[]metav1.Condition{
{
Type: conditionTypeReady,
Status: metav1.ConditionFalse,
},
{
Type: conditionTypeError,
Status: metav1.ConditionTrue,
},
}...,
), "Conditions changes should be detected, but didn't")

assert.Equal(me.T(), 2, len(*conditions))

}

func (me *SetStatusConditionsSuite) TestConditionNotChanged() {

conditions := &[]metav1.Condition{
{
Type: conditionTypeReady,
Status: metav1.ConditionTrue,
},
{
Type: conditionTypeError,
Status: metav1.ConditionTrue,
},
}

assert.False(me.T(), SetExclusiveConditions(conditions,
[]metav1.Condition{
{
Type: conditionTypeReady,
Status: metav1.ConditionTrue,
},
{
Type: conditionTypeError,
Status: metav1.ConditionTrue,
},
}...,
), "Conditions changes should not be detected, but did")

assert.Equal(me.T(), 2, len(*conditions))

}

func TestSetStatusConditionsSuite(t *testing.T) {
suite.Run(t, new(SetStatusConditionsSuite))
}
14 changes: 4 additions & 10 deletions pkg/kcp/provider/aws/vpcpeering/loadRemoteVpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,16 @@ func loadRemoteVpc(ctx context.Context, st composed.State) (error, context.Conte
if err != nil {
logger.Error(err, "Error loading remote AWS VPC Networks")

condition := metav1.Condition{
if !composed.SetExclusiveConditions(obj.Conditions(), metav1.Condition{
Type: cloudcontrolv1beta1.ConditionTypeError,
Status: metav1.ConditionTrue,
Reason: cloudcontrolv1beta1.ReasonVpcNotFound,
Message: err.Error(),
}

if !composed.AnyConditionChanged(obj, condition) {
}) {
return composed.StopAndForget, nil
}

return composed.PatchStatus(obj).
SetExclusiveConditions(condition).
ErrorLogMessage("Error updating VpcPeering status when loading vpc").
SuccessError(composed.StopAndForget).
Run(ctx, st)
Expand All @@ -55,19 +52,16 @@ func loadRemoteVpc(ctx context.Context, st composed.State) (error, context.Conte
).
Info("VPC not found")

condition := metav1.Condition{
if !composed.SetExclusiveConditions(obj.Conditions(), metav1.Condition{
Type: cloudcontrolv1beta1.ConditionTypeError,
Status: metav1.ConditionTrue,
Reason: cloudcontrolv1beta1.ReasonVpcNotFound,
Message: fmt.Sprintf("AWS VPC ID %s not found", remoteVpcId),
}

if !composed.AnyConditionChanged(obj, condition) {
}) {
return composed.StopAndForget, nil
}

return composed.PatchStatus(obj).
SetExclusiveConditions(condition).
ErrorLogMessage("Error updating VpcPeering status when loading vpc").
SuccessError(composed.StopAndForget).
Run(ctx, st)
Expand Down
3 changes: 1 addition & 2 deletions pkg/skr/awsvpcpeering/updateStatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func updateStatus(ctx context.Context, st composed.State) (error, context.Contex

changed := false

if composed.AnyConditionChanged(state.ObjAsAwsVpcPeering(), *state.KcpVpcPeering.Conditions()...) {
if composed.SetExclusiveConditions(state.ObjAsAwsVpcPeering().Conditions(), *state.KcpVpcPeering.Conditions()...) {
changed = true
}

Expand All @@ -26,7 +26,6 @@ func updateStatus(ctx context.Context, st composed.State) (error, context.Contex
if changed {
state.ObjAsAwsVpcPeering().Status.State = state.KcpVpcPeering.Status.State
return composed.UpdateStatus(state.ObjAsAwsVpcPeering()).
SetExclusiveConditions(*state.KcpVpcPeering.Conditions()...).
ErrorLogMessage("Error updating SKR AwsVpcPeering status").
SuccessLogMsg("Updated and forgot SKR AwsVpcPeering status").
SuccessErrorNil().
Expand Down
3 changes: 1 addition & 2 deletions pkg/skr/azurevpcpeering/updateStatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func updateStatus(ctx context.Context, st composed.State) (error, context.Contex

changed := false

if composed.AnyConditionChanged(obj, *state.KcpVpcPeering.Conditions()...) {
if composed.SetExclusiveConditions(obj.Conditions(), *state.KcpVpcPeering.Conditions()...) {
changed = true
}

Expand All @@ -27,7 +27,6 @@ func updateStatus(ctx context.Context, st composed.State) (error, context.Contex
if changed {
obj.Status.State = state.KcpVpcPeering.Status.State
return composed.UpdateStatus(obj).
SetExclusiveConditions(*state.KcpVpcPeering.Conditions()...).
ErrorLogMessage("Error updating SKR AzureVpcPeering status").
SuccessLogMsg("Updated and forgot SKR AzureVpcPeering status").
SuccessErrorNil().
Expand Down
Loading