Skip to content

Commit ae5beed

Browse files
committed
sort secrets with the same labels by "pguser" being in the name
and then sort by creation timestamp, if "pguser' is not in the name sort by creation time stamp if the creation timestamps are all equal sort by name
1 parent c3fa600 commit ae5beed

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

internal/controller/postgrescluster/postgres.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"net"
2424
"net/url"
2525
"regexp"
26+
"sort"
2627
"strings"
2728

2829
"github.com/pkg/errors"
@@ -411,6 +412,36 @@ func (r *Reconciler) reconcilePostgresUserSecrets(
411412
))
412413
}
413414

415+
// Sorts the slice of secrets.Items based on secrets with identical labels
416+
// If one secret has "pguser" in its name and the other does not, the
417+
// one without "pguser" is moved to the front.
418+
// If both secrets have "pguser" in their names or neither has "pguser", they
419+
// are sorted by creation timestamp.
420+
// If two secrets have the same creation timestamp, they are further sorted by name.
421+
// The secret to be used by PGO is put at the end of the sorted slice.
422+
sort.Slice(secrets.Items, func(i, j int) bool {
423+
// Check if either secrets have "pguser" in their names
424+
isIPgUser := strings.Contains(secrets.Items[i].Name, "pguser")
425+
isJPgUser := strings.Contains(secrets.Items[j].Name, "pguser")
426+
427+
// If one secret has "pguser" and the other does not,
428+
// move the one without "pguser" to the front
429+
if isIPgUser && !isJPgUser {
430+
return false
431+
} else if !isIPgUser && isJPgUser {
432+
return true
433+
}
434+
435+
if secrets.Items[i].CreationTimestamp.Time.Equal(secrets.Items[j].CreationTimestamp.Time) {
436+
// If the creation timestamps are equal, sort by name
437+
return secrets.Items[i].Name < secrets.Items[j].Name
438+
}
439+
440+
// If both secrets have "pguser" or neither have "pguser",
441+
// sort by creation timestamp
442+
return secrets.Items[i].CreationTimestamp.Time.After(secrets.Items[j].CreationTimestamp.Time)
443+
})
444+
414445
// Index secrets by PostgreSQL user name and delete any that are not in the
415446
// cluster spec. Keep track of the deprecated default secret to migrate its
416447
// contents when the current secret doesn't exist.

0 commit comments

Comments
 (0)