Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion cmd/postgres-operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ func main() {

features := feature.NewGate()
assertNoError(features.Set(os.Getenv("PGO_FEATURE_GATES")))
log.Info("feature gates enabled", "PGO_FEATURE_GATES", features.String())
// This logs just the feature gates as set by the user
log.Info("feature gates enabled during deployment", "PGO_FEATURE_GATES", features.String())

cfg, err := runtime.GetConfig()
assertNoError(err)
Expand Down
22 changes: 19 additions & 3 deletions internal/feature/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ package feature

import (
"context"
"fmt"
"sort"
"strings"

"k8s.io/component-base/featuregate"
)
Expand All @@ -52,6 +55,7 @@ type Feature = featuregate.Feature
type Gate interface {
Enabled(Feature) bool
String() string
GetAll() map[Feature]featuregate.FeatureSpec
}

// MutableGate contains features that can be enabled or disabled.
Expand Down Expand Up @@ -122,11 +126,23 @@ func NewContext(ctx context.Context, gate Gate) context.Context {
return context.WithValue(ctx, contextKey{}, gate)
}

// ShowGates returns all the gates that are on by default
// (if not turned off by the user) or were explicitly set
// on by the user.
func ShowGates(ctx context.Context) string {
featuresEnabled := ""
featuresEnabled := []string{}
gate, ok := ctx.Value(contextKey{}).(Gate)
if ok {
featuresEnabled = gate.String()
specs := gate.GetAll()
for feature := range specs {
// `gate.Enabled` first checks if the feature is enabled;
// then (if not explicitly set by the user),
// it checks if the feature is on/true by default
if gate.Enabled(feature) {
featuresEnabled = append(featuresEnabled, fmt.Sprintf("%s=true", feature))
}
}
}
return featuresEnabled
sort.Strings(featuresEnabled)
return strings.Join(featuresEnabled, ",")
}
16 changes: 12 additions & 4 deletions internal/feature/features_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,21 @@ func TestContext(t *testing.T) {
t.Parallel()
gate := NewGate()
ctx := NewContext(context.Background(), gate)
assert.Equal(t, ShowGates(ctx), "")

// ShowGates returns all fields that are turned on, whether explicitly
// by the user or implicitly due to feature default.
// Currently, the only feature defaulting to true is `AutoCreateUserSchema`.
assert.Equal(t, ShowGates(ctx), "AutoCreateUserSchema=true")

assert.NilError(t, gate.Set("TablespaceVolumes=true"))
assert.Assert(t, true == Enabled(ctx, TablespaceVolumes))
assert.Equal(t, ShowGates(ctx), "TablespaceVolumes=true")
assert.Equal(t, ShowGates(ctx), "AutoCreateUserSchema=true,TablespaceVolumes=true")

assert.NilError(t, gate.SetFromMap(map[string]bool{TablespaceVolumes: false}))
assert.NilError(t, gate.SetFromMap(map[string]bool{
TablespaceVolumes: false,
AutoCreateUserSchema: false,
}))
assert.Assert(t, false == Enabled(ctx, TablespaceVolumes))
assert.Equal(t, ShowGates(ctx), "TablespaceVolumes=false")
assert.Assert(t, false == Enabled(ctx, AutoCreateUserSchema))
assert.Equal(t, ShowGates(ctx), "")
}
2 changes: 1 addition & 1 deletion internal/upgradecheck/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestCheckForUpgrades(t *testing.T) {
assert.Equal(t, data.RegistrationToken, "speakFriend")
assert.Equal(t, data.BridgeClustersTotal, 2)
assert.Equal(t, data.PGOClustersTotal, 2)
assert.Equal(t, data.FeatureGatesEnabled, "TablespaceVolumes=true")
assert.Equal(t, data.FeatureGatesEnabled, "AutoCreateUserSchema=true,TablespaceVolumes=true")
Copy link
Member

Choose a reason for hiding this comment

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

👍🏻 This isn't Contains, but I don't mind seeing the format. 🌱 We can change the test if it is ever a problem.

}

t.Run("success", func(t *testing.T) {
Expand Down
Loading