Skip to content

Commit

Permalink
use new custom webhook defaulter
Browse files Browse the repository at this point in the history
  • Loading branch information
gkech committed Jan 23, 2025
1 parent 345a2b5 commit edce57e
Show file tree
Hide file tree
Showing 19 changed files with 98 additions and 43 deletions.
2 changes: 1 addition & 1 deletion internal/controller/postgrescluster/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (r *Reconciler) Reconcile(
// Set any defaults that may not have been stored in the API. No DeepCopy
// is necessary because controller-runtime makes a copy before returning
// from its cache.
cluster.Default()
_ = cluster.Default(ctx, nil)

if cluster.Spec.OpenShift == nil {
cluster.Spec.OpenShift = &r.IsOpenShift
Expand Down
6 changes: 4 additions & 2 deletions internal/controller/postgrescluster/instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,8 @@ func TestAddPGBackRestToInstancePodSpec(t *testing.T) {
ctx := context.Background()
cluster := v1beta1.PostgresCluster{}
cluster.Name = "hippo"
cluster.Default()
err := cluster.Default(ctx, nil)
assert.NilError(t, err)
cluster.SetLabels(map[string]string{
naming.LabelVersion: "2.5.0",
})
Expand Down Expand Up @@ -1725,7 +1726,8 @@ func TestGenerateInstanceStatefulSetIntent(t *testing.T) {
cluster = testCluster()
}

cluster.Default()
err := cluster.Default(context.Background(), nil)
assert.NilError(t, err)
cluster.UID = types.UID("hippouid")
cluster.Namespace = test.name + "-ns"
cluster.Spec.Shutdown = &test.ip.shutdown
Expand Down
3 changes: 2 additions & 1 deletion internal/controller/postgrescluster/pgbouncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,8 @@ namespace: ns3
cluster.Spec.Proxy = &v1beta1.PostgresProxySpec{
PGBouncer: &v1beta1.PGBouncerPodSpec{},
}
cluster.Default()
err := cluster.Default(context.Background(), nil)
assert.NilError(t, err)

configmap := &corev1.ConfigMap{}
configmap.Name = "some-cm2"
Expand Down
6 changes: 4 additions & 2 deletions internal/controller/postgrescluster/pgmonitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,8 @@ func TestReconcileMonitoringSecret(t *testing.T) {
reconciler := &Reconciler{Client: cc, Owner: client.FieldOwner(t.Name())}

cluster := testCluster()
cluster.Default()
err := cluster.Default(context.Background(), nil)
assert.NilError(t, err)
cluster.UID = types.UID("hippouid")
cluster.Namespace = setupNamespace(t, cc).Name

Expand Down Expand Up @@ -787,7 +788,8 @@ func TestReconcileExporterQueriesConfig(t *testing.T) {
reconciler := &Reconciler{Client: cc, Owner: client.FieldOwner(t.Name())}

cluster := testCluster()
cluster.Default()
err := cluster.Default(context.Background(), nil)
assert.NilError(t, err)
cluster.UID = types.UID("hippouid")
cluster.Namespace = setupNamespace(t, cc).Name

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 @@ -105,7 +105,7 @@ func (r *PGAdminReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
log.V(1).Info("Reconciling pgAdmin")

// Set defaults if unset
pgAdmin.Default()
_ = pgAdmin.Default(ctx, nil)

var (
configmap *corev1.ConfigMap
Expand Down
13 changes: 9 additions & 4 deletions internal/patroni/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package patroni

import (
"context"
"os"
"os/exec"
"path/filepath"
Expand All @@ -29,7 +30,8 @@ func TestClusterYAML(t *testing.T) {

t.Run("PG version defaulted", func(t *testing.T) {
cluster := new(v1beta1.PostgresCluster)
cluster.Default()
err := cluster.Default(context.Background(), nil)
assert.NilError(t, err)
cluster.Namespace = "some-namespace"
cluster.Name = "cluster-name"

Expand Down Expand Up @@ -86,7 +88,8 @@ watchdog:

t.Run(">PG10", func(t *testing.T) {
cluster := new(v1beta1.PostgresCluster)
cluster.Default()
err := cluster.Default(context.Background(), nil)
assert.NilError(t, err)
cluster.Namespace = "some-namespace"
cluster.Name = "cluster-name"
cluster.Spec.PostgresVersion = 14
Expand Down Expand Up @@ -759,7 +762,8 @@ func TestDynamicConfiguration(t *testing.T) {
if cluster.Spec.PostgresVersion == 0 {
cluster.Spec.PostgresVersion = 14
}
cluster.Default()
err := cluster.Default(context.Background(), nil)
assert.NilError(t, err)
actual := DynamicConfiguration(cluster, tt.input, tt.hbas, tt.params)
assert.DeepEqual(t, tt.expected, actual)
})
Expand Down Expand Up @@ -792,7 +796,8 @@ func TestInstanceEnvironment(t *testing.T) {
t.Parallel()

cluster := new(v1beta1.PostgresCluster)
cluster.Default()
err := cluster.Default(context.Background(), nil)
assert.NilError(t, err)
cluster.Spec.PostgresVersion = 12
leaderService := new(corev1.Service)
podService := new(corev1.Service)
Expand Down
4 changes: 3 additions & 1 deletion internal/patroni/rbac_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package patroni

import (
"context"
"testing"

"gotest.tools/v3/assert"
Expand All @@ -28,7 +29,8 @@ func isUniqueAndSorted(slice []string) bool {

func TestPermissions(t *testing.T) {
cluster := new(v1beta1.PostgresCluster)
cluster.Default()
err := cluster.Default(context.Background(), nil)
assert.NilError(t, err)

t.Run("Upstream", func(t *testing.T) {
permissions := Permissions(cluster)
Expand Down
6 changes: 4 additions & 2 deletions internal/patroni/reconcile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ func TestClusterConfigMap(t *testing.T) {
pgHBAs := postgres.HBAs{}
pgParameters := postgres.Parameters{}

cluster.Default()
err := cluster.Default(context.Background(), nil)
assert.NilError(t, err)
config := new(corev1.ConfigMap)
assert.NilError(t, ClusterConfigMap(ctx, cluster, pgHBAs, pgParameters, config))

Expand Down Expand Up @@ -114,7 +115,8 @@ func TestInstancePod(t *testing.T) {
t.Parallel()

cluster := new(v1beta1.PostgresCluster)
cluster.Default()
err := cluster.Default(context.Background(), nil)
assert.NilError(t, err)
cluster.Name = "some-such"
cluster.Spec.PostgresVersion = 11
cluster.Spec.Image = "image"
Expand Down
10 changes: 7 additions & 3 deletions internal/pgadmin/reconcile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package pgadmin

import (
"context"
"testing"

"gotest.tools/v3/assert"
Expand Down Expand Up @@ -33,7 +34,8 @@ func TestConfigMap(t *testing.T) {
t.Run("Defaults", func(t *testing.T) {
cluster.Spec.UserInterface = new(v1beta1.UserInterfaceSpec)
cluster.Spec.UserInterface.PGAdmin = new(v1beta1.PGAdminPodSpec)
cluster.Default()
err := cluster.Default(context.Background(), nil)
assert.NilError(t, err)

assert.NilError(t, ConfigMap(cluster, config))

Expand All @@ -52,7 +54,8 @@ pgadmin-settings.json: |
"some": "thing",
"UPPER_CASE": false,
}
cluster.Default()
err := cluster.Default(context.Background(), nil)
assert.NilError(t, err)

assert.NilError(t, ConfigMap(cluster, config))

Expand Down Expand Up @@ -92,7 +95,8 @@ func TestPod(t *testing.T) {
t.Run("Defaults", func(t *testing.T) {
cluster.Spec.UserInterface = new(v1beta1.UserInterfaceSpec)
cluster.Spec.UserInterface.PGAdmin = new(v1beta1.PGAdminPodSpec)
cluster.Default()
err := cluster.Default(context.Background(), nil)
assert.NilError(t, err)

call()

Expand Down
4 changes: 3 additions & 1 deletion internal/pgbackrest/rbac_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package pgbackrest

import (
"context"
"testing"

"gotest.tools/v3/assert"
Expand All @@ -28,7 +29,8 @@ func isUniqueAndSorted(slice []string) bool {

func TestPermissions(t *testing.T) {
cluster := new(v1beta1.PostgresCluster)
cluster.Default()
err := cluster.Default(context.Background(), nil)
assert.NilError(t, err)

permissions := Permissions(cluster)
for _, rule := range permissions {
Expand Down
15 changes: 10 additions & 5 deletions internal/pgbackrest/reconcile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ func TestAddConfigToInstancePod(t *testing.T) {
},
}
cluster.Name = "hippo"
cluster.Default()
err := cluster.Default(context.Background(), nil)
assert.NilError(t, err)

pod := corev1.PodSpec{
Containers: []corev1.Container{
Expand Down Expand Up @@ -332,7 +333,8 @@ func TestAddConfigToInstancePod(t *testing.T) {
func TestAddConfigToRepoPod(t *testing.T) {
cluster := v1beta1.PostgresCluster{}
cluster.Name = "hippo"
cluster.Default()
err := cluster.Default(context.Background(), nil)
assert.NilError(t, err)

pod := corev1.PodSpec{
Containers: []corev1.Container{
Expand Down Expand Up @@ -405,7 +407,8 @@ func TestAddConfigToRepoPod(t *testing.T) {
func TestAddConfigToRestorePod(t *testing.T) {
cluster := v1beta1.PostgresCluster{}
cluster.Name = "source"
cluster.Default()
err := cluster.Default(context.Background(), nil)
assert.NilError(t, err)

pod := corev1.PodSpec{
Containers: []corev1.Container{
Expand Down Expand Up @@ -575,7 +578,8 @@ func TestAddServerToInstancePod(t *testing.T) {
ctx := context.Background()
cluster := v1beta1.PostgresCluster{}
cluster.Name = "hippo"
cluster.Default()
err := cluster.Default(context.Background(), nil)
assert.NilError(t, err)
cluster.SetLabels(map[string]string{
naming.LabelVersion: "2.5.0",
})
Expand Down Expand Up @@ -838,7 +842,8 @@ func TestAddServerToRepoPod(t *testing.T) {
ctx := context.Background()
cluster := v1beta1.PostgresCluster{}
cluster.Name = "hippo"
cluster.Default()
err := cluster.Default(context.Background(), nil)
assert.NilError(t, err)
cluster.SetLabels(map[string]string{
naming.LabelVersion: "2.5.0",
})
Expand Down
4 changes: 3 additions & 1 deletion internal/pgbouncer/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package pgbouncer

import (
"context"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -43,7 +44,8 @@ func TestClusterINI(t *testing.T) {
t.Parallel()

cluster := new(v1beta1.PostgresCluster)
cluster.Default()
err := cluster.Default(context.Background(), nil)
assert.NilError(t, err)

cluster.Name = "foo-baz"
*cluster.Spec.Port = 9999
Expand Down
12 changes: 8 additions & 4 deletions internal/pgbouncer/reconcile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ func TestConfigMap(t *testing.T) {

cluster.Spec.Proxy = new(v1beta1.PostgresProxySpec)
cluster.Spec.Proxy.PGBouncer = new(v1beta1.PGBouncerPodSpec)
cluster.Default()
err := cluster.Default(context.Background(), nil)
assert.NilError(t, err)

ConfigMap(cluster, config)

Expand Down Expand Up @@ -71,7 +72,8 @@ func TestSecret(t *testing.T) {

cluster.Spec.Proxy = new(v1beta1.PostgresProxySpec)
cluster.Spec.Proxy.PGBouncer = new(v1beta1.PGBouncerPodSpec)
cluster.Default()
err = cluster.Default(context.Background(), nil)
assert.NilError(t, err)

constant := existing.DeepCopy()
assert.NilError(t, Secret(ctx, cluster, root, existing, service, intent))
Expand Down Expand Up @@ -120,7 +122,8 @@ func TestPod(t *testing.T) {
t.Run("Defaults", func(t *testing.T) {
cluster.Spec.Proxy = new(v1beta1.PostgresProxySpec)
cluster.Spec.Proxy.PGBouncer = new(v1beta1.PGBouncerPodSpec)
cluster.Default()
err := cluster.Default(context.Background(), nil)
assert.NilError(t, err)

call()

Expand Down Expand Up @@ -487,7 +490,8 @@ func TestPostgreSQL(t *testing.T) {
t.Run("Enabled", func(t *testing.T) {
cluster.Spec.Proxy = new(v1beta1.PostgresProxySpec)
cluster.Spec.Proxy.PGBouncer = new(v1beta1.PGBouncerPodSpec)
cluster.Default()
err := cluster.Default(context.Background(), nil)
assert.NilError(t, err)

PostgreSQL(cluster, hbas)

Expand Down
6 changes: 4 additions & 2 deletions internal/postgres/reconcile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ func TestInstancePod(t *testing.T) {

ctx := context.Background()
cluster := new(v1beta1.PostgresCluster)
cluster.Default()
err := cluster.Default(context.Background(), nil)
assert.NilError(t, err)
cluster.Spec.ImagePullPolicy = corev1.PullAlways
cluster.Spec.PostgresVersion = 11
cluster.SetLabels(map[string]string{
Expand Down Expand Up @@ -711,7 +712,8 @@ volumes:

func TestPodSecurityContext(t *testing.T) {
cluster := new(v1beta1.PostgresCluster)
cluster.Default()
err := cluster.Default(context.Background(), nil)
assert.NilError(t, err)

assert.Assert(t, cmp.MarshalMatches(PodSecurityContext(cluster), `
fsGroup: 26
Expand Down
3 changes: 2 additions & 1 deletion pkg/apis/pgv2.percona.com/v2/perconapgcluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,8 @@ func (cr *PerconaPGCluster) ToCrunchy(ctx context.Context, postgresCluster *crun
return nil, err
}

postgresCluster.Default()
// omitting error because it is always nil
_ = postgresCluster.Default(ctx, postgresCluster)

annotations := make(map[string]string)
for k, v := range cr.Annotations {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
package v1beta1

import (
"context"

"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)

// CrunchyBridgeClusterSpec defines the desired state of CrunchyBridgeCluster
Expand Down Expand Up @@ -205,16 +208,17 @@ type CrunchyBridgeCluster struct {
Status CrunchyBridgeClusterStatus `json:"status,omitempty"`
}

// Default implements "sigs.k8s.io/controller-runtime/pkg/webhook.Defaulter" so
// Default implements webhook.CustomDefaulter so
// a webhook can be registered for the type.
// - https://book.kubebuilder.io/reference/webhook-overview.html
func (c *CrunchyBridgeCluster) Default() {
func (c *CrunchyBridgeCluster) Default(_ context.Context, _ runtime.Object) error {
if len(c.APIVersion) == 0 {
c.APIVersion = GroupVersion.String()
}
if len(c.Kind) == 0 {
c.Kind = "CrunchyBridgeCluster"
}
return nil
}

// +kubebuilder:object:root=true
Expand Down
Loading

0 comments on commit edce57e

Please sign in to comment.