Skip to content

Commit

Permalink
tests: fix Konnect ControlPlane role cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
pmalek committed Nov 13, 2024
1 parent 12a4875 commit 815cc92
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 19 deletions.
7 changes: 3 additions & 4 deletions hack/cleanup/konnect_control_planes.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ import (
)

const (
konnectControlPlanesLimit = int64(100)
createdInTestsControlPlaneLabel = "created_in_tests"
timeUntilControlPlaneOrphaned = time.Hour
konnectControlPlanesLimit = int64(100)
timeUntilControlPlaneOrphaned = time.Hour
)

// cleanupKonnectControlPlanes deletes orphaned control planes created by the tests and their roles.
Expand Down Expand Up @@ -91,7 +90,7 @@ func findOrphanedControlPlanes(

var orphanedControlPlanes []string
for _, ControlPlane := range response.ListControlPlanesResponse.Data {
if ControlPlane.Labels[createdInTestsControlPlaneLabel] != "true" {
if ControlPlane.Labels[test.KonnectControlPlaneLabelCreatedInTests] != "true" {
log.Info("Control plane was not created by the tests, skipping", "name", ControlPlane.Name)
continue
}
Expand Down
37 changes: 22 additions & 15 deletions test/internal/helpers/konnect/control_plane.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

sdkkonnectcomp "github.com/Kong/sdk-konnect-go/models/components"
sdkkonnectops "github.com/Kong/sdk-konnect-go/models/operations"
sdkkonnecterrs "github.com/Kong/sdk-konnect-go/models/sdkerrors"

"github.com/kong/kubernetes-ingress-controller/v3/internal/adminapi"
"github.com/kong/kubernetes-ingress-controller/v3/internal/konnect/sdk"
Expand All @@ -31,14 +32,14 @@ func CreateTestControlPlane(ctx context.Context, t *testing.T) string {

sdk := sdk.New(accessToken(), serverURLOpt())

var rgID string
var cpID string
createRgErr := retry.Do(func() error {
createResp, err := sdk.ControlPlanes.CreateControlPlane(ctx,
sdkkonnectcomp.CreateControlPlaneRequest{
Name: uuid.NewString(),
Description: lo.ToPtr(generateTestKonnectControlPlaneDescription(t)),
Labels: map[string]string{
"created_in_tests": "true",
test.KonnectControlPlaneLabelCreatedInTests: "true",
},
ClusterType: sdkkonnectcomp.CreateControlPlaneRequestClusterTypeClusterTypeK8SIngressController.ToPointer(),
},
Expand All @@ -62,21 +63,21 @@ func CreateTestControlPlane(ctx context.Context, t *testing.T) string {
return errors.New("No control plane ID in response")
}

rgID = createResp.ControlPlane.ID
cpID = createResp.ControlPlane.ID
return nil
}, retry.Attempts(5), retry.Delay(time.Second))
require.NoError(t, createRgErr)

t.Cleanup(func() {
t.Logf("deleting test Konnect Control Plane: %q", rgID)
t.Logf("deleting test Konnect Control Plane: %q", cpID)
err := retry.Do(
func() error {
_, err := sdk.ControlPlanes.DeleteControlPlane(ctx, rgID)
_, err := sdk.ControlPlanes.DeleteControlPlane(ctx, cpID)
return err
},
retry.Attempts(5), retry.Delay(time.Second),
)
assert.NoErrorf(t, err, "failed to cleanup a control plane: %q", rgID)
assert.NoErrorf(t, err, "failed to cleanup a control plane: %q", cpID)

me, err := sdk.Me.GetUsersMe(ctx,
// NOTE: Otherwise we use prod server by default.
Expand All @@ -99,25 +100,31 @@ func CreateTestControlPlane(ctx context.Context, t *testing.T) string {
// Related issue: https://github.com/Kong/sdk-konnect-go/issues/20
sdkkonnectops.WithServerURL(test.KonnectServerURL()),
)
require.NoErrorf(t, err, "failed to list control plane roles for cleanup: %q", rgID)
require.NoErrorf(t, err, "failed to list control plane roles for cleanup: %q", cpID)

for _, role := range resp.AssignedRoleCollection.Data {
if role.EntityID == nil || role.ID == nil {
continue
}
if *role.EntityID == rgID { // Delete only roles created for the control plane.
t.Logf("deleting test Konnect Control Plane role: %q", *role.ID)
_, err := sdk.Roles.UsersRemoveRole(ctx, *me.User.ID, *role.ID,
// Related issue: https://github.com/Kong/sdk-konnect-go/issues/20
sdkkonnectops.WithServerURL(test.KonnectServerURL()),
)
if *role.EntityID != cpID {
continue
}

// Delete only roles created for the control plane.
t.Logf("deleting test Konnect Control Plane role: %q", *role.ID)
_, err := sdk.Roles.UsersRemoveRole(ctx, *me.User.ID, *role.ID,
// Related issue: https://github.com/Kong/sdk-konnect-go/issues/20
sdkkonnectops.WithServerURL(test.KonnectServerURL()),
)
notFoundErr := &sdkkonnecterrs.NotFoundError{}
if !errors.As(err, &notFoundErr) {
assert.NoErrorf(t, err, "failed to cleanup a control plane role: %q", *role.ID)
}
}
})

t.Logf("created test Konnect Control Plane: %q", rgID)
return rgID
t.Logf("created test Konnect Control Plane: %q", cpID)
return cpID
}

func generateTestKonnectControlPlaneDescription(t *testing.T) string {
Expand Down
6 changes: 6 additions & 0 deletions test/konnect.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ package test

import "os"

const (
// KonnectControlPlaneLabelCreatedInTests is the label that is set on all
// Konnect control planes created by tests.
KonnectControlPlaneLabelCreatedInTests = "created_in_tests"
)

// KonnectServerURL returns the Konnect server URL to be used for Konnect API
// requests in tests and CI.
// It is driven by the TEST_KONG_KONNECT_SERVER_URL environment variable.
Expand Down

0 comments on commit 815cc92

Please sign in to comment.