Skip to content

Commit 90eff1f

Browse files
committed
PR feedback
1 parent dd39d69 commit 90eff1f

File tree

5 files changed

+47
-28
lines changed

5 files changed

+47
-28
lines changed

cmd/postgres-operator/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,10 @@ func main() {
134134

135135
features := feature.NewGate()
136136
assertNoError(features.Set(os.Getenv("PGO_FEATURE_GATES")))
137+
138+
ctx = feature.NewContext(ctx, features)
137139
// This logs just the feature gates as set by the user
138-
log.Info("feature gates enabled during deployment", "PGO_FEATURE_GATES", features.String())
140+
log.Info("feature gates enabled during deployment", "PGO_FEATURE_GATES", feature.ShowAssigned(ctx))
139141

140142
cfg, err := runtime.GetConfig()
141143
assertNoError(err)

internal/feature/features.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ package feature
4343
import (
4444
"context"
4545
"fmt"
46-
"sort"
46+
"slices"
4747
"strings"
4848

4949
"k8s.io/component-base/featuregate"
@@ -54,8 +54,6 @@ type Feature = featuregate.Feature
5454
// Gate indicates what features exist and which are enabled.
5555
type Gate interface {
5656
Enabled(Feature) bool
57-
String() string
58-
GetAll() map[Feature]featuregate.FeatureSpec
5957
}
6058

6159
// MutableGate contains features that can be enabled or disabled.
@@ -126,13 +124,13 @@ func NewContext(ctx context.Context, gate Gate) context.Context {
126124
return context.WithValue(ctx, contextKey{}, gate)
127125
}
128126

129-
// ShowGates returns all the gates that are on by default
130-
// (if not turned off by the user) or were explicitly set
131-
// on by the user.
132-
func ShowGates(ctx context.Context) string {
127+
// ShowEnabled returns all the features enabled in the Gate contained in ctx.
128+
func ShowEnabled(ctx context.Context) string {
133129
featuresEnabled := []string{}
134-
gate, ok := ctx.Value(contextKey{}).(Gate)
135-
if ok {
130+
if gate, ok := ctx.Value(contextKey{}).(interface {
131+
Gate
132+
GetAll() map[Feature]featuregate.FeatureSpec
133+
}); ok {
136134
specs := gate.GetAll()
137135
for feature := range specs {
138136
// `gate.Enabled` first checks if the feature is enabled;
@@ -143,6 +141,19 @@ func ShowGates(ctx context.Context) string {
143141
}
144142
}
145143
}
146-
sort.Strings(featuresEnabled)
144+
slices.Sort(featuresEnabled)
147145
return strings.Join(featuresEnabled, ",")
148146
}
147+
148+
// ShowAssigned returns the features enabled or disabled by Set and SetFromMap
149+
// in the Gate contained in ctx.
150+
func ShowAssigned(ctx context.Context) string {
151+
featuresAssigned := ""
152+
if gate, ok := ctx.Value(contextKey{}).(interface {
153+
Gate
154+
String() string
155+
}); ok {
156+
featuresAssigned = gate.String()
157+
}
158+
return featuresAssigned
159+
}

internal/feature/features_test.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package feature
66

77
import (
88
"context"
9+
"strings"
910
"testing"
1011

1112
"gotest.tools/v3/assert"
@@ -14,6 +15,7 @@ import (
1415
func TestDefaults(t *testing.T) {
1516
t.Parallel()
1617
gate := NewGate()
18+
ctx := NewContext(context.Background(), gate)
1719

1820
assert.Assert(t, false == gate.Enabled(AppendCustomQueries))
1921
assert.Assert(t, true == gate.Enabled(AutoCreateUserSchema))
@@ -24,16 +26,17 @@ func TestDefaults(t *testing.T) {
2426
assert.Assert(t, false == gate.Enabled(TablespaceVolumes))
2527
assert.Assert(t, false == gate.Enabled(VolumeSnapshots))
2628

27-
assert.Equal(t, gate.String(), "")
29+
assert.Equal(t, ShowAssigned(ctx), "")
2830
}
2931

3032
func TestStringFormat(t *testing.T) {
3133
t.Parallel()
3234
gate := NewGate()
35+
ctx := NewContext(context.Background(), gate)
3336

3437
assert.NilError(t, gate.Set(""))
3538
assert.NilError(t, gate.Set("TablespaceVolumes=true"))
36-
assert.Equal(t, gate.String(), "TablespaceVolumes=true")
39+
assert.Equal(t, ShowAssigned(ctx), "TablespaceVolumes=true")
3740
assert.Assert(t, true == gate.Enabled(TablespaceVolumes))
3841

3942
err := gate.Set("NotAGate=true")
@@ -54,20 +57,20 @@ func TestContext(t *testing.T) {
5457
gate := NewGate()
5558
ctx := NewContext(context.Background(), gate)
5659

57-
// ShowGates returns all fields that are turned on, whether explicitly
58-
// by the user or implicitly due to feature default.
59-
// Currently, the only feature defaulting to true is `AutoCreateUserSchema`.
60-
assert.Equal(t, ShowGates(ctx), "AutoCreateUserSchema=true")
60+
assert.Equal(t, ShowAssigned(ctx), "")
61+
assert.Assert(t, ShowEnabled(ctx) != "") // This assumes some feature is enabled by default.
6162

6263
assert.NilError(t, gate.Set("TablespaceVolumes=true"))
63-
assert.Assert(t, true == Enabled(ctx, TablespaceVolumes))
64-
assert.Equal(t, ShowGates(ctx), "AutoCreateUserSchema=true,TablespaceVolumes=true")
64+
assert.Assert(t, Enabled(ctx, TablespaceVolumes))
65+
assert.Equal(t, ShowAssigned(ctx), "TablespaceVolumes=true")
66+
assert.Assert(t,
67+
strings.Contains(ShowEnabled(ctx), "TablespaceVolumes=true"),
68+
"got: %v", ShowEnabled(ctx))
6569

66-
assert.NilError(t, gate.SetFromMap(map[string]bool{
67-
TablespaceVolumes: false,
68-
AutoCreateUserSchema: false,
69-
}))
70-
assert.Assert(t, false == Enabled(ctx, TablespaceVolumes))
71-
assert.Assert(t, false == Enabled(ctx, AutoCreateUserSchema))
72-
assert.Equal(t, ShowGates(ctx), "")
70+
assert.NilError(t, gate.SetFromMap(map[string]bool{TablespaceVolumes: false}))
71+
assert.Assert(t, !Enabled(ctx, TablespaceVolumes))
72+
assert.Equal(t, ShowAssigned(ctx), "TablespaceVolumes=false")
73+
assert.Assert(t,
74+
!strings.Contains(ShowEnabled(ctx), "TablespaceVolumes"),
75+
"got: %v", ShowEnabled(ctx))
7376
}

internal/upgradecheck/header.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func generateHeader(ctx context.Context, cfg *rest.Config, crClient crclient.Cli
5757
BridgeClustersTotal: getBridgeClusters(ctx, crClient),
5858
BuildSource: os.Getenv("BUILD_SOURCE"),
5959
DeploymentID: ensureDeploymentID(ctx, crClient),
60-
FeatureGatesEnabled: feature.ShowGates(ctx),
60+
FeatureGatesEnabled: feature.ShowEnabled(ctx),
6161
IsOpenShift: isOpenShift,
6262
KubernetesEnv: getServerVersion(ctx, cfg),
6363
PGOClustersTotal: getManagedClusters(ctx, crClient),

internal/upgradecheck/header_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,10 @@ func TestGenerateHeader(t *testing.T) {
136136
assert.Equal(t, len(pgoList.Items), res.PGOClustersTotal)
137137
assert.Equal(t, "1.2.3", res.PGOVersion)
138138
assert.Equal(t, server.String(), res.KubernetesEnv)
139-
assert.Equal(t, "TablespaceVolumes=true", res.FeatureGatesEnabled)
139+
assert.Check(t, strings.Contains(
140+
res.FeatureGatesEnabled,
141+
"TablespaceVolumes=true",
142+
))
140143
assert.Equal(t, "test", res.PGOInstaller)
141144
assert.Equal(t, "test-origin", res.PGOInstallerOrigin)
142145
assert.Equal(t, "developer", res.BuildSource)

0 commit comments

Comments
 (0)