Skip to content

Commit 1648f58

Browse files
committed
check for prefix and suffix for disk encryption sets deletion
1 parent 4d0ecb0 commit 1648f58

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

pkg/util/purge/serviceprincipals.go

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,26 @@ var (
3636
// - Service principals without V{BUILDID} pattern
3737
// - Service principals whose resource groups have the 'persist' tag
3838
// - Service principals younger than the TTL
39+
//
40+
// This function only processes the first page of results (~100 items per prefix)
41+
// from Microsoft Graph API. Since the cleanup runs on a schedule, orphaned resources
42+
// will eventually be cleaned across multiple runs.
3943
func (rc *ResourceCleaner) CleanOrphanedE2EServicePrincipals(ctx context.Context, ttl time.Duration) error {
4044
rc.log.Info("Starting orphaned service principal cleanup")
4145

42-
prefixes := []struct {
43-
prefix string
44-
description string
45-
}{
46-
{"aro-v4-e2e-", "Cluster service principals"},
47-
{"v4-e2e-", "Disk encryption set managed identities"},
48-
{"mock-msi-", "Mock MSI service principals (MIWI e2e tests)"},
46+
rc.log.Info("Cleaning cluster service principals (prefix: aro-v4-e2e-)")
47+
if err := rc.cleanServicePrincipals(ctx, "aro-v4-e2e-", "", ttl); err != nil {
48+
rc.log.Errorf("Error cleaning cluster service principals: %v", err)
4949
}
5050

51-
for _, p := range prefixes {
52-
rc.log.Infof("Cleaning %s (prefix: %s)", p.description, p.prefix)
53-
if err := rc.cleanServicePrincipalsByPrefix(ctx, p.prefix, ttl); err != nil {
54-
rc.log.Errorf("Error cleaning prefix '%s': %v", p.prefix, err)
55-
}
51+
rc.log.Info("Cleaning disk encryption set managed identities (prefix: v4-e2e-, suffix: -disk-encryption-set)")
52+
if err := rc.cleanServicePrincipals(ctx, "v4-e2e-", "-disk-encryption-set", ttl); err != nil {
53+
rc.log.Errorf("Error cleaning disk encryption set identities: %v", err)
54+
}
55+
56+
rc.log.Info("Cleaning mock MSI service principals (prefix: mock-msi-)")
57+
if err := rc.cleanServicePrincipals(ctx, "mock-msi-", "", ttl); err != nil {
58+
rc.log.Errorf("Error cleaning mock MSI service principals: %v", err)
5659
}
5760

5861
return nil
@@ -75,7 +78,7 @@ func (rc *ResourceCleaner) listApplicationsByPrefix(ctx context.Context, prefix
7578
return result.GetValue(), nil
7679
}
7780

78-
func (rc *ResourceCleaner) cleanServicePrincipalsByPrefix(ctx context.Context, prefix string, ttl time.Duration) error {
81+
func (rc *ResourceCleaner) cleanServicePrincipals(ctx context.Context, prefix string, suffix string, ttl time.Duration) error {
7982
apps, err := rc.listApplicationsByPrefix(ctx, prefix)
8083
if err != nil {
8184
return err
@@ -104,6 +107,11 @@ func (rc *ResourceCleaner) cleanServicePrincipalsByPrefix(ctx context.Context, p
104107
objectID = *app.GetId()
105108
}
106109

110+
if suffix != "" && !strings.HasSuffix(displayName, suffix) {
111+
rc.log.Debugf("SKIP '%s': Does not have suffix '%s'", displayName, suffix)
112+
continue
113+
}
114+
107115
isMockMSI := strings.HasPrefix(displayName, "mock-msi-")
108116
createdDateTime := app.GetCreatedDateTime()
109117

0 commit comments

Comments
 (0)