Skip to content

Commit

Permalink
add consts and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
randmonkey committed Oct 29, 2024
1 parent 2d6d130 commit 7fdd5fd
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 14 deletions.
10 changes: 9 additions & 1 deletion controller/konnect/ops/conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,17 @@ const (
// ControlPlaneGroupMembersReferenceResolvedConditionType sets the condition for control plane groups
// to show whether all of its members are programmed and attached to the group.
ControlPlaneGroupMembersReferenceResolvedConditionType = "MembersReferenceResolved"
// ControlPlaneGroupMembersReferenceResolvedReasonResolved indicates that all members of the control plane groups
// ControlPlaneGroupMembersReferenceResolvedReasonNoMembers indicates that there are no members specified in the control plane group.
ControlPlaneGroupMembersReferenceResolvedReasonNoMembers consts.ConditionReason = "NoMembers"
// ControlPlaneGroupMembersReferenceResolvedReasonResolved indicates that all members of the control plane group
// are created and attached to the group in Konnect.
ControlPlaneGroupMembersReferenceResolvedReasonResolved consts.ConditionReason = "Resolved"
// ControlPlaneGroupMembersReferenceResolvedReasonPartialNotResolved indicates that some members of the control plane group
// are not resolved (not found or not created in Konnect).
ControlPlaneGroupMembersReferenceResolvedReasonPartialNotResolved consts.ConditionReason = "SomeMemberNotResolved"
// ControlPlaneGroupMembersReferenceResolvedReasonFailedToSet indicates that error happened on setting control plane as
// member of the control plane.
ControlPlaneGroupMembersReferenceResolvedReasonFailedToSet consts.ConditionReason = "SetGroupMemberFailed"
)

// SetControlPlaneGroupMembersReferenceResolvedCondition sets MembersReferenceResolved condition of control plane to True.
Expand Down
22 changes: 16 additions & 6 deletions controller/konnect/ops/ops_controlplane.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,21 @@ func setGroupMembers(
id string,
sdkGroups sdkops.ControlPlaneGroupSDK,
) error {
// REVIEW: should we add MembersReferenceResolved condition for CP groups with 0 members?
if len(cp.Spec.Members) == 0 ||
cp.Spec.ClusterType == nil ||
if cp.Spec.ClusterType == nil ||
*cp.Spec.ClusterType != sdkkonnectcomp.ClusterTypeClusterTypeControlPlaneGroup {
return nil
}

// Set MembersReferenceResolved condition to False if there are no members in the CP group.
if len(cp.Spec.Members) == 0 {
SetControlPlaneGroupMembersReferenceResolvedConditionFalse(
cp,
ControlPlaneGroupMembersReferenceResolvedReasonNoMembers,
"No members in the control plane group",
)
return nil
}

members, err := iter.MapErr(cp.Spec.Members,
func(member *corev1.LocalObjectReference) (sdkkonnectcomp.Members, error) {
var (
Expand Down Expand Up @@ -165,7 +173,7 @@ func setGroupMembers(
if err != nil {
SetControlPlaneGroupMembersReferenceResolvedConditionFalse(
cp,
"SomeMemberNotResolved",
ControlPlaneGroupMembersReferenceResolvedReasonPartialNotResolved,
err.Error(),
)
return fmt.Errorf("failed to set group members, some members couldn't be found: %w", err)
Expand All @@ -179,15 +187,17 @@ func setGroupMembers(
if err != nil {
SetControlPlaneGroupMembersReferenceResolvedConditionFalse(
cp,
"SetGroupMemberFailed",
ControlPlaneGroupMembersReferenceResolvedReasonFailedToSet,
err.Error(),
)
return fmt.Errorf("failed to set members on control plane group %s: %w",
client.ObjectKeyFromObject(cp), err,
)
}

SetControlPlaneGroupMembersReferenceResolvedCondition(cp)
SetControlPlaneGroupMembersReferenceResolvedCondition(
cp,
)
return nil
}

Expand Down
34 changes: 27 additions & 7 deletions controller/konnect/ops/ops_controlplane_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

sdkmocks "github.com/kong/gateway-operator/controller/konnect/ops/sdk/mocks"
"github.com/kong/gateway-operator/modules/manager/scheme"
"github.com/kong/gateway-operator/pkg/consts"

konnectv1alpha1 "github.com/kong/kubernetes-configuration/api/konnect/v1alpha1"
)
Expand Down Expand Up @@ -638,11 +639,13 @@ func TestCreateAndUpdateControlPlane_KubernetesMetadataConsistency(t *testing.T)

func TestSetGroupMembers(t *testing.T) {
testcases := []struct {
name string
group *konnectv1alpha1.KonnectGatewayControlPlane
cps []client.Object
sdk func(t *testing.T) *sdkmocks.MockControlPlaneGroupSDK
expectedErr bool
name string
group *konnectv1alpha1.KonnectGatewayControlPlane
cps []client.Object
sdk func(t *testing.T) *sdkmocks.MockControlPlaneGroupSDK
expectedErr bool
memberRefResolvedStatus metav1.ConditionStatus
memberRefResolvedReason consts.ConditionReason
}{
{
name: "no members",
Expand All @@ -662,6 +665,8 @@ func TestSetGroupMembers(t *testing.T) {
sdk := sdkmocks.NewMockControlPlaneGroupSDK(t)
return sdk
},
memberRefResolvedStatus: metav1.ConditionFalse,
memberRefResolvedReason: ControlPlaneGroupMembersReferenceResolvedReasonNoMembers,
},
{
name: "1 member with Konnect Status ID",
Expand Down Expand Up @@ -715,6 +720,8 @@ func TestSetGroupMembers(t *testing.T) {
)
return sdk
},
memberRefResolvedStatus: metav1.ConditionTrue,
memberRefResolvedReason: ControlPlaneGroupMembersReferenceResolvedReasonResolved,
},
{
name: "1 member without Konnect Status ID",
Expand Down Expand Up @@ -748,7 +755,9 @@ func TestSetGroupMembers(t *testing.T) {
sdk := sdkmocks.NewMockControlPlaneGroupSDK(t)
return sdk
},
expectedErr: true,
expectedErr: true,
memberRefResolvedStatus: metav1.ConditionFalse,
memberRefResolvedReason: ControlPlaneGroupMembersReferenceResolvedReasonPartialNotResolved,
},
{
name: "2 member with Konnect Status IDs",
Expand Down Expand Up @@ -819,6 +828,8 @@ func TestSetGroupMembers(t *testing.T) {
)
return sdk
},
memberRefResolvedStatus: metav1.ConditionTrue,
memberRefResolvedReason: ControlPlaneGroupMembersReferenceResolvedReasonResolved,
},
{
name: "2 member, 1 with Konnect Status IDs, 1 without it",
Expand Down Expand Up @@ -865,7 +876,9 @@ func TestSetGroupMembers(t *testing.T) {
sdk := sdkmocks.NewMockControlPlaneGroupSDK(t)
return sdk
},
expectedErr: true,
expectedErr: true,
memberRefResolvedStatus: metav1.ConditionFalse,
memberRefResolvedReason: ControlPlaneGroupMembersReferenceResolvedReasonPartialNotResolved,
},
}

Expand All @@ -885,6 +898,13 @@ func TestSetGroupMembers(t *testing.T) {
}
assert.NoError(t, err)
assert.True(t, sdk.AssertExpectations(t))

membersRefResolvedCondition, conditionFound := lo.Find(tc.group.Status.Conditions, func(c metav1.Condition) bool {
return c.Type == ControlPlaneGroupMembersReferenceResolvedConditionType
})
assert.True(t, conditionFound, "Should find MembersReferenceResolved condition")
assert.Equal(t, tc.memberRefResolvedStatus, membersRefResolvedCondition.Status, "Should have expected MembersReferenceResolved status")
assert.Equal(t, string(tc.memberRefResolvedReason), membersRefResolvedCondition.Reason, "Should have expected MembersReferenceResolved reason")
})
}
}

0 comments on commit 7fdd5fd

Please sign in to comment.