Skip to content

refactor: Supports cleaning resources for static projects with test-acc-tf-p-keep prefix #3269

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

Merged
merged 5 commits into from
Apr 25, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 25 additions & 14 deletions internal/testutil/clean/org_clean_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ var (
"cfn-test-bot-",
"test-acc-tf-p-",
}
// keptPrefixes has the prefix of the projects that we want to delete their resources but keep the projects themselves.
// Useful when a feature flag or cloud provider is configured outside of the test
keptPrefixes = []string{
"test-acc-tf-p-keep",
}
Expand Down Expand Up @@ -82,30 +84,36 @@ func TestCleanProjectAndClusters(t *testing.T) {
projects := readAllProjects(t.Context(), t, client)
projectsBefore := len(projects)
t.Logf("found %d projects (DRY_RUN=%t)", projectsBefore, dryRun)
projectsToDelete := map[string]string{}
projectsToClean := map[string]string{}
projectInfos := []string{}
for _, p := range projects {
skipReason := projectSkipReason(&p, skipProjectsAfter, onlyZeroClusters)
projectName := p.GetName()
projectID := p.GetId()
if skipReason != "" {
t.Logf("skip project %s, reason: %s", projectName, skipReason)
t.Logf("skip project %s (%s), reason: %s", projectName, projectID, skipReason)
continue
}
projectInfos = append(projectInfos, fmt.Sprintf("Project created at %s name %s (%s)", p.GetCreated().Format(time.RFC3339), projectName, p.GetId()))
projectID := p.GetId()
projectsToDelete[projectName] = projectID
projectsToClean[projectName] = projectID
}
t.Logf("will try to delete %d projects:", len(projectsToDelete))
t.Logf("deleting project resources and optionally delete projects for %d projects", len(projectsToClean))
slices.Sort(projectInfos)
t.Log(strings.Join(projectInfos, "\n"))
var deleteErrors int
for name, projectID := range projectsToDelete {
var emptyProjectCount int
for name, projectID := range projectsToClean {
t.Run(name, func(t *testing.T) {
t.Parallel()
changes := removeProjectResources(t.Context(), t, dryRun, client, projectID)
if changes != "" {
t.Logf("project %s %s", name, changes)
}
if skipProjectDelete(name) {
t.Logf("keep project empty, but no delete %s (%s)", name, projectID)
emptyProjectCount++
return
}
var err error
for i := range runRetries {
attempt := i + 1
Expand All @@ -131,9 +139,8 @@ func TestCleanProjectAndClusters(t *testing.T) {
})
}
t.Cleanup(func() {
//nolint:usetesting // reason: using context.Background() here intentionally because t.Context() is canceled at cleanup
projectsAfter := readAllProjects(context.Background(), t, client)
t.Logf("SUMMARY\nProjects changed from %d to %d\ndelete_errors=%d\nDRY_RUN=%t", projectsBefore, len(projectsAfter), deleteErrors, dryRun)
projectsAfter := readAllProjects(context.Background(), t, client) //nolint:usetesting // reason: using context.Background() here intentionally because t.Context() is canceled at cleanup
t.Logf("SUMMARY\nProjects changed from %d to %d\ndelete_errors=%d\nempty_project_count=%d\nDRY_RUN=%t", projectsBefore, len(projectsAfter), deleteErrors, emptyProjectCount, dryRun)
})
}

Expand Down Expand Up @@ -204,11 +211,6 @@ func removeProjectResources(ctx context.Context, t *testing.T, dryRun bool, clie
}

func projectSkipReason(p *admin.Group, skipProjectsAfter time.Time, onlyEmpty bool) string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the difference between projectSkipReason and projectNoDeleteReason?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

projectSkipReason avoids cleaning the project alltogether. (All non bot projects are not cleaned by default)
skipProjectDelete cleans resources but doesn't delete the project.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the clarification

for _, blessedPrefix := range keptPrefixes {
if strings.HasPrefix(p.GetName(), blessedPrefix) {
return "blessed prefix: " + blessedPrefix
}
}
usesBotPrefix := false
for _, botPrefix := range botProjectPrefixes {
if strings.HasPrefix(p.GetName(), botPrefix) {
Expand All @@ -228,6 +230,15 @@ func projectSkipReason(p *admin.Group, skipProjectsAfter time.Time, onlyEmpty bo
return ""
}

func skipProjectDelete(name string) bool {
for _, keepPrefix := range keptPrefixes {
if strings.HasPrefix(name, keepPrefix) {
return true
}
}
return false
}

func removeClusters(ctx context.Context, t *testing.T, dryRun bool, client *admin.APIClient, projectID string) int {
t.Helper()
clusters, _, err := client.ClustersApi.ListClusters(ctx, projectID).ItemsPerPage(itemsPerPage).Execute()
Expand Down