@@ -23,6 +23,7 @@ import (
23
23
"net"
24
24
"net/url"
25
25
"regexp"
26
+ "sort"
26
27
"strings"
27
28
28
29
"github.com/pkg/errors"
@@ -411,6 +412,36 @@ func (r *Reconciler) reconcilePostgresUserSecrets(
411
412
))
412
413
}
413
414
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
+
414
445
// Index secrets by PostgreSQL user name and delete any that are not in the
415
446
// cluster spec. Keep track of the deprecated default secret to migrate its
416
447
// contents when the current secret doesn't exist.
0 commit comments