From ed01d88ad9ff3165a539000553fc476efd7a50ec Mon Sep 17 00:00:00 2001 From: Yi Tao Date: Wed, 30 Oct 2024 15:25:58 +0800 Subject: [PATCH] add setGroupMembers in getControlPlaneByUID --- controller/konnect/ops/ops.go | 2 +- controller/konnect/ops/ops_controlplane.go | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/controller/konnect/ops/ops.go b/controller/konnect/ops/ops.go index 59bcf49a..2d616e48 100644 --- a/controller/konnect/ops/ops.go +++ b/controller/konnect/ops/ops.go @@ -109,7 +109,7 @@ func Create[ var id string switch ent := any(e).(type) { case *konnectv1alpha1.KonnectGatewayControlPlane: - id, err = getControlPlaneForUID(ctx, sdk.GetControlPlaneSDK(), ent) + id, err = getControlPlaneForUID(ctx, sdk.GetControlPlaneSDK(), sdk.GetControlPlaneGroupSDK(), cl, ent) case *configurationv1alpha1.KongService: id, err = getKongServiceForUID(ctx, sdk.GetServicesSDK(), ent) case *configurationv1alpha1.KongRoute: diff --git a/controller/konnect/ops/ops_controlplane.go b/controller/konnect/ops/ops_controlplane.go index 719be8ad..7386ba47 100644 --- a/controller/konnect/ops/ops_controlplane.go +++ b/controller/konnect/ops/ops_controlplane.go @@ -212,6 +212,8 @@ func (m membersByID) Swap(i, j int) { m[i], m[j] = m[j], m[i] } func getControlPlaneForUID( ctx context.Context, sdk sdkops.ControlPlaneSDK, + sdkGroups sdkops.ControlPlaneGroupSDK, + cl client.Client, cp *konnectv1alpha1.KonnectGatewayControlPlane, ) (string, error) { reqList := sdkkonnectops.ListControlPlanesRequest{ @@ -230,5 +232,20 @@ func getControlPlaneForUID( return "", fmt.Errorf("failed listing %s: %w", cp.GetTypeName(), ErrNilResponse) } - return getMatchingEntryFromListResponseData(sliceToEntityWithIDSlice(resp.ListControlPlanesResponse.Data), cp) + id, err := getMatchingEntryFromListResponseData(sliceToEntityWithIDSlice(resp.ListControlPlanesResponse.Data), cp) + if err != nil { + return "", err + } + + if err := setGroupMembers(ctx, cl, cp, id, sdkGroups); err != nil { + // If we failed to set group membership, we should return a specific error with a reason + // so the downstream can handle it properly. + return id, KonnectEntityCreatedButRelationsFailedError{ + KonnectID: id, + Err: err, + Reason: konnectv1alpha1.KonnectGatewayControlPlaneProgrammedReasonFailedToSetControlPlaneGroupMembers, + } + } + + return id, nil }