From 7123613cc23b0bf09fc5f0f60ebb33bc71b1d323 Mon Sep 17 00:00:00 2001 From: Chris Bandy Date: Wed, 27 Nov 2024 10:26:18 -0600 Subject: [PATCH] Pass slices of *PostgresCluster rather than *PostgresClusterList --- .../standalone_pgadmin/configmap.go | 17 +++++++------- .../standalone_pgadmin/configmap_test.go | 13 +++++------ .../standalone_pgadmin/controller.go | 2 +- .../standalone_pgadmin/postgrescluster.go | 22 +++++++++---------- 4 files changed, 25 insertions(+), 29 deletions(-) diff --git a/internal/controller/standalone_pgadmin/configmap.go b/internal/controller/standalone_pgadmin/configmap.go index d1ec39bf1..4d3a2f1a8 100644 --- a/internal/controller/standalone_pgadmin/configmap.go +++ b/internal/controller/standalone_pgadmin/configmap.go @@ -9,8 +9,10 @@ import ( "context" "encoding/json" "fmt" + "slices" "sort" "strconv" + "strings" corev1 "k8s.io/api/core/v1" @@ -27,7 +29,7 @@ import ( // reconcilePGAdminConfigMap writes the ConfigMap for pgAdmin. func (r *PGAdminReconciler) reconcilePGAdminConfigMap( ctx context.Context, pgadmin *v1beta1.PGAdmin, - clusters map[string]*v1beta1.PostgresClusterList, + clusters map[string][]*v1beta1.PostgresCluster, ) (*corev1.ConfigMap, error) { configmap, err := configmap(pgadmin, clusters) if err == nil { @@ -42,7 +44,7 @@ func (r *PGAdminReconciler) reconcilePGAdminConfigMap( // configmap returns a v1.ConfigMap for pgAdmin. func configmap(pgadmin *v1beta1.PGAdmin, - clusters map[string]*v1beta1.PostgresClusterList, + clusters map[string][]*v1beta1.PostgresCluster, ) (*corev1.ConfigMap, error) { configmap := &corev1.ConfigMap{ObjectMeta: naming.StandalonePGAdmin(pgadmin)} configmap.SetGroupVersionKind(corev1.SchemeGroupVersion.WithKind("ConfigMap")) @@ -126,7 +128,7 @@ func generateConfig(pgadmin *v1beta1.PGAdmin) (string, error) { // } // } func generateClusterConfig( - clusters map[string]*v1beta1.PostgresClusterList, + clusters map[string][]*v1beta1.PostgresCluster, ) (string, error) { // To avoid spurious reconciles, the following value must not change when // the spec does not change. [json.Encoder] and [json.Marshal] do this by @@ -149,11 +151,10 @@ func generateClusterConfig( clusterServers := map[int]any{} for _, serverGroupName := range keys { - sort.Slice(clusters[serverGroupName].Items, - func(i, j int) bool { - return clusters[serverGroupName].Items[i].Name < clusters[serverGroupName].Items[j].Name - }) - for _, cluster := range clusters[serverGroupName].Items { + slices.SortFunc(clusters[serverGroupName], func(a, b *v1beta1.PostgresCluster) int { + return strings.Compare(a.Name, b.Name) + }) + for _, cluster := range clusters[serverGroupName] { object := map[string]any{ "Name": cluster.Name, "Group": serverGroupName, diff --git a/internal/controller/standalone_pgadmin/configmap_test.go b/internal/controller/standalone_pgadmin/configmap_test.go index 5a844e520..359f79f9a 100644 --- a/internal/controller/standalone_pgadmin/configmap_test.go +++ b/internal/controller/standalone_pgadmin/configmap_test.go @@ -78,13 +78,10 @@ func TestGenerateClusterConfig(t *testing.T) { cluster := testCluster() cluster.Namespace = "postgres-operator" - clusterList := &v1beta1.PostgresClusterList{ - Items: []v1beta1.PostgresCluster{*cluster, *cluster}, - } - clusters := map[string]*v1beta1.PostgresClusterList{ - "shared": clusterList, - "test": clusterList, - "hello": clusterList, + clusters := map[string][]*v1beta1.PostgresCluster{ + "shared": []*v1beta1.PostgresCluster{cluster, cluster}, + "test": []*v1beta1.PostgresCluster{cluster, cluster}, + "hello": []*v1beta1.PostgresCluster{cluster, cluster}, } expectedString := `{ @@ -163,7 +160,7 @@ func TestGeneratePGAdminConfigMap(t *testing.T) { pgadmin := new(v1beta1.PGAdmin) pgadmin.Namespace = "some-ns" pgadmin.Name = "pg1" - clusters := map[string]*v1beta1.PostgresClusterList{} + clusters := map[string][]*v1beta1.PostgresCluster{} t.Run("Data,ObjectMeta,TypeMeta", func(t *testing.T) { pgadmin := pgadmin.DeepCopy() diff --git a/internal/controller/standalone_pgadmin/controller.go b/internal/controller/standalone_pgadmin/controller.go index 8edb22cd5..55d5461f8 100644 --- a/internal/controller/standalone_pgadmin/controller.go +++ b/internal/controller/standalone_pgadmin/controller.go @@ -110,7 +110,7 @@ func (r *PGAdminReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct var ( configmap *corev1.ConfigMap dataVolume *corev1.PersistentVolumeClaim - clusters map[string]*v1beta1.PostgresClusterList + clusters map[string][]*v1beta1.PostgresCluster _ *corev1.Service ) diff --git a/internal/controller/standalone_pgadmin/postgrescluster.go b/internal/controller/standalone_pgadmin/postgrescluster.go index 5327b8ae7..bc7d28dea 100644 --- a/internal/controller/standalone_pgadmin/postgrescluster.go +++ b/internal/controller/standalone_pgadmin/postgrescluster.go @@ -7,11 +7,11 @@ package standalone_pgadmin import ( "context" + "github.com/crunchydata/postgres-operator/internal/initialize" "github.com/crunchydata/postgres-operator/internal/naming" "github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1" "k8s.io/apimachinery/pkg/labels" - "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/client" ) @@ -56,33 +56,31 @@ func (r *PGAdminReconciler) findPGAdminsForPostgresCluster( func (r *PGAdminReconciler) getClustersForPGAdmin( ctx context.Context, pgAdmin *v1beta1.PGAdmin, -) (map[string]*v1beta1.PostgresClusterList, error) { - matching := make(map[string]*v1beta1.PostgresClusterList) +) (map[string][]*v1beta1.PostgresCluster, error) { + matching := make(map[string][]*v1beta1.PostgresCluster) var err error var selector labels.Selector for _, serverGroup := range pgAdmin.Spec.ServerGroups { - cluster := &v1beta1.PostgresCluster{} + var cluster v1beta1.PostgresCluster if serverGroup.PostgresClusterName != "" { - err = r.Get(ctx, types.NamespacedName{ + err = r.Get(ctx, client.ObjectKey{ Name: serverGroup.PostgresClusterName, Namespace: pgAdmin.GetNamespace(), - }, cluster) + }, &cluster) if err == nil { - matching[serverGroup.Name] = &v1beta1.PostgresClusterList{ - Items: []v1beta1.PostgresCluster{*cluster}, - } + matching[serverGroup.Name] = []*v1beta1.PostgresCluster{&cluster} } continue } if selector, err = naming.AsSelector(serverGroup.PostgresClusterSelector); err == nil { - var filteredList v1beta1.PostgresClusterList - err = r.List(ctx, &filteredList, + var list v1beta1.PostgresClusterList + err = r.List(ctx, &list, client.InNamespace(pgAdmin.Namespace), client.MatchingLabelsSelector{Selector: selector}, ) if err == nil { - matching[serverGroup.Name] = &filteredList + matching[serverGroup.Name] = initialize.Pointers(list.Items...) } } }