Skip to content

Commit

Permalink
Pass slices of *PostgresCluster rather than *PostgresClusterList
Browse files Browse the repository at this point in the history
  • Loading branch information
cbandy committed Nov 27, 2024
1 parent 9cacf66 commit 7123613
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 29 deletions.
17 changes: 9 additions & 8 deletions internal/controller/standalone_pgadmin/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import (
"context"
"encoding/json"
"fmt"
"slices"
"sort"
"strconv"
"strings"

corev1 "k8s.io/api/core/v1"

Expand All @@ -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 {
Expand All @@ -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"))
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down
13 changes: 5 additions & 8 deletions internal/controller/standalone_pgadmin/configmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 := `{
Expand Down Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion internal/controller/standalone_pgadmin/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down
22 changes: 10 additions & 12 deletions internal/controller/standalone_pgadmin/postgrescluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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...)
}
}
}
Expand Down

0 comments on commit 7123613

Please sign in to comment.