Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 22 additions & 19 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,25 +302,28 @@ func main() {
}
pgFleetMetricsCollector := pgprometheus.NewFleetCollector()

if err := (&controller.PostgresDatabaseReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Recorder: mgr.GetEventRecorderFor("postgresdatabase-controller"),
Metrics: pgMetricsRecorder,
FleetCollector: pgFleetMetricsCollector,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "PostgresDatabase")
os.Exit(1)
}
if err := (&controller.PostgresClusterReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Recorder: mgr.GetEventRecorderFor("postgrescluster-controller"),
Metrics: pgMetricsRecorder,
FleetCollector: pgFleetMetricsCollector,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "PostgresCluster")
os.Exit(1)
if config.DefaultMutableFeatureGate.Enabled(config.PostgresController) {
if err := (&controller.PostgresDatabaseReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Recorder: mgr.GetEventRecorderFor("postgresdatabase-controller"),
Metrics: pgMetricsRecorder,
FleetCollector: pgFleetMetricsCollector,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "PostgresDatabase")
os.Exit(1)
}

if err := (&controller.PostgresClusterReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Recorder: mgr.GetEventRecorderFor("postgrescluster-controller"),
Metrics: pgMetricsRecorder,
FleetCollector: pgFleetMetricsCollector,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "PostgresCluster")
os.Exit(1)
}
}

if _, ok := os.LookupEnv("ENABLE_VALIDATION_WEBHOOK"); ok {
Expand Down
4 changes: 2 additions & 2 deletions config/samples/enterprise_v4_postgrescluster_dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ spec:
clusterDeletionPolicy: Retain
instances: 3
# Storage and PostgreSQL version are overridden from the ClusterClass defaults. Validation rules on the PostgresCluster resource will prevent removing these fields or setting them to lower values than the original overrides.
storage: 1Gi
postgresVersion: "15.10"
storage: 20Gi
postgresVersion: "18.10"
resources:
requests:
cpu: "250m"
Expand Down
1 change: 1 addition & 0 deletions config/webhook/manifests.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion docs/FeatureGates.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ When running the operator binary directly, pass feature gates at startup:

| Gate | Default | Stage | Since | Description |
|-----------------------|---------|-------|---------|----------------------------------------------------------|
| `ValidationWebhook` | `false` | Alpha | v3.2.0 | Centralized validation webhook server for CR admission |
| `ValidationWebhook` | `false` | Alpha | v3.2.0 | Centralized validation webhook server for CR admission |
| `PostgresController` | `false` | Alpha | ? | PostgresCluster, PostgresClusterClass, and PostgresDatabase controllers and CRDs |

## Adding a New Feature Gate

Expand Down Expand Up @@ -130,6 +131,20 @@ metadata:
splunk.com/feature-stage: alpha
```

### d. Enable the gate in tests

Validators and controllers gated behind a feature flag will reject all operations in tests unless the gate is explicitly enabled. Enable it via `SetFromMap` before your tests run:

```go
func init() {
if err := config.DefaultMutableFeatureGate.SetFromMap(map[string]bool{
string(config.MyNewFeature): true,
}); err != nil {
panic(err)
}
}
```

## Promoting a Gate

- **Alpha → Beta**: Change `Default: false` to `Default: true` in `featuregates.go`; update the CRD label to `beta`
Expand Down
6 changes: 4 additions & 2 deletions pkg/config/featuregates.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,16 @@ const (
// When enabled, the operator runs a validating webhook that enforces
// CR schema rules at admission time.
// Replaces the legacy ENABLE_VALIDATION_WEBHOOK env var.
ValidationWebhook featuregate.Feature = "ValidationWebhook"
ValidationWebhook featuregate.Feature = "ValidationWebhook"
PostgresController featuregate.Feature = "PostgresController"
)

// defaultFeatureGates is the authoritative registry of all feature gates and
// their default state / maturity. Each entry here automatically becomes
// available via --feature-gates on the operator binary.
var defaultFeatureGates = map[featuregate.Feature]featuregate.FeatureSpec{
ValidationWebhook: {Default: false, PreRelease: featuregate.Alpha},
ValidationWebhook: {Default: false, PreRelease: featuregate.Alpha},
PostgresController: {Default: false, PreRelease: featuregate.Alpha},
}

var DefaultMutableFeatureGate featuregate.MutableFeatureGate = featuregate.NewFeatureGate()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,22 @@ import (
"k8s.io/apimachinery/pkg/util/validation/field"

enterpriseApi "github.com/splunk/splunk-operator/api/v4"
"github.com/splunk/splunk-operator/pkg/config"
hba "github.com/splunk/splunk-operator/pkg/postgresql/cluster/core"
)

// ValidatePostgresClusterCreate validates a PostgresCluster on CREATE.
func ValidatePostgresClusterCreate(obj *enterpriseApi.PostgresCluster) field.ErrorList {
var allErrs field.ErrorList

if !config.DefaultMutableFeatureGate.Enabled(config.PostgresController) {
allErrs = append(allErrs, field.Forbidden(
field.NewPath("spec"),
"the PostgresController feature is not enabled; set --feature-gates=PostgresController=true to activate"))

return allErrs
}

if len(obj.Spec.PgHBA) > 0 {
pgHBAPath := field.NewPath("spec").Child("pgHBA")
for _, re := range hba.ValidateRules(obj.Spec.PgHBA) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"testing"

enterpriseApi "github.com/splunk/splunk-operator/api/v4"
"github.com/splunk/splunk-operator/pkg/config"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -174,6 +175,22 @@ func TestValidatePostgresClusterUpdate(t *testing.T) {
}
}

func TestValidatePostgresClusterCreateFeatureGateDisabled(t *testing.T) {
config.DefaultMutableFeatureGate.SetFromMap(map[string]bool{string(config.PostgresController): false})
t.Cleanup(func() {
config.DefaultMutableFeatureGate.SetFromMap(map[string]bool{string(config.PostgresController): true})
})

obj := &enterpriseApi.PostgresCluster{
Spec: enterpriseApi.PostgresClusterSpec{Class: "dev"},
}

errs := ValidatePostgresClusterCreate(obj)
assert.Len(t, errs, 1)
assert.Equal(t, "spec", errs[0].Field)
assert.Equal(t, "the PostgresController feature is not enabled; set --feature-gates=PostgresController=true to activate", errs[0].Detail)
}

func TestGetPostgresClusterWarningsOnCreate(t *testing.T) {
obj := &enterpriseApi.PostgresCluster{
Spec: enterpriseApi.PostgresClusterSpec{Class: "dev"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,22 @@ import (
"k8s.io/apimachinery/pkg/util/validation/field"

enterpriseApi "github.com/splunk/splunk-operator/api/v4"
"github.com/splunk/splunk-operator/pkg/config"
hba "github.com/splunk/splunk-operator/pkg/postgresql/cluster/core"
)

// ValidatePostgresClusterClassCreate validates a PostgresClusterClass on CREATE.
func ValidatePostgresClusterClassCreate(obj *enterpriseApi.PostgresClusterClass) field.ErrorList {
var allErrs field.ErrorList

if !config.DefaultMutableFeatureGate.Enabled(config.PostgresController) {
allErrs = append(allErrs, field.Forbidden(
field.NewPath("spec"),
"the PostgresController feature is not enabled; set --feature-gates=PostgresController=true to activate"))

return allErrs
}

if obj.Spec.Config != nil && len(obj.Spec.Config.PgHBA) > 0 {
pgHBAPath := field.NewPath("spec").Child("config").Child("pgHBA")
for _, re := range hba.ValidateRules(obj.Spec.Config.PgHBA) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"testing"

enterpriseApi "github.com/splunk/splunk-operator/api/v4"
"github.com/splunk/splunk-operator/pkg/config"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -173,6 +174,22 @@ func TestValidatePostgresClusterClassUpdate(t *testing.T) {
}
}

func TestValidatePostgresClusterClassCreateFeatureGateDisabled(t *testing.T) {
config.DefaultMutableFeatureGate.SetFromMap(map[string]bool{string(config.PostgresController): false})
t.Cleanup(func() {
config.DefaultMutableFeatureGate.SetFromMap(map[string]bool{string(config.PostgresController): true})
})

obj := &enterpriseApi.PostgresClusterClass{
Spec: enterpriseApi.PostgresClusterClassSpec{Provisioner: "postgresql.cnpg.io"},
}

errs := ValidatePostgresClusterClassCreate(obj)
assert.Len(t, errs, 1)
assert.Equal(t, "spec", errs[0].Field)
assert.Equal(t, "the PostgresController feature is not enabled; set --feature-gates=PostgresController=true to activate", errs[0].Detail)
}

func TestGetPostgresClusterClassWarningsOnCreate(t *testing.T) {
obj := &enterpriseApi.PostgresClusterClass{
Spec: enterpriseApi.PostgresClusterClassSpec{Provisioner: "postgresql.cnpg.io"},
Expand Down
26 changes: 26 additions & 0 deletions pkg/postgresql/cluster/adapter/webhook/testhelpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
Copyright 2026.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package webhook_test

import (
"github.com/splunk/splunk-operator/pkg/config"
"github.com/splunk/splunk-operator/pkg/postgresql/shared/testhelpers"
)

func init() {
testhelpers.EnableFeatureGate(config.PostgresController)
}
26 changes: 26 additions & 0 deletions pkg/postgresql/cluster/adapter/webhook/testsetup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
Copyright 2026.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package webhook

import (
"github.com/splunk/splunk-operator/pkg/config"
"github.com/splunk/splunk-operator/pkg/postgresql/shared/testhelpers"
)

func init() {
testhelpers.EnableFeatureGate(config.PostgresController)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
Copyright 2026.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package webhook

import (
"k8s.io/apimachinery/pkg/util/validation/field"

enterpriseApi "github.com/splunk/splunk-operator/api/v4"
"github.com/splunk/splunk-operator/pkg/config"
)

// ValidatePostgresDatabaseCreate validates a PostgresDatabase on CREATE.
func ValidatePostgresDatabaseCreate(obj *enterpriseApi.PostgresDatabase) field.ErrorList {
var allErrs field.ErrorList

if !config.DefaultMutableFeatureGate.Enabled(config.PostgresController) {
allErrs = append(allErrs, field.Forbidden(
field.NewPath("spec"),
"the PostgresController feature is not enabled; set --feature-gates=PostgresController=true to activate"))

return allErrs
}

return allErrs
}

// ValidatePostgresDatabaseUpdate validates a PostgresDatabase on UPDATE.
func ValidatePostgresDatabaseUpdate(obj, oldObj *enterpriseApi.PostgresDatabase) field.ErrorList {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: if we only alias the method here, maybe It would be fine to limit the functionality to only one method with a more direct name? fe. IsPostgresEnabled. Or is there potential that the update/create validation will diverge in the future?

return ValidatePostgresDatabaseCreate(obj)
}

// GetPostgresDatabaseWarningsOnCreate returns warnings for PostgresDatabase CREATE.
func GetPostgresDatabaseWarningsOnCreate(obj *enterpriseApi.PostgresDatabase) []string {
return nil
}

// GetPostgresDatabaseWarningsOnUpdate returns warnings for PostgresDatabase UPDATE.
func GetPostgresDatabaseWarningsOnUpdate(obj, oldObj *enterpriseApi.PostgresDatabase) []string {
return nil
}
Loading
Loading