Skip to content
Merged
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
11 changes: 11 additions & 0 deletions pkg/feature/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,17 @@ func NewVersionGate(currentVersion string, versionConstraints []VersionConstrain
}
}

// NewBooleanGate creates a VersionGate that is enabled only when enabled is true.
//
// It is shorthand for NewVersionGate("", nil).When(enabled): a gate with no
// version constraints whose result is driven solely by the boolean. Use it for a
// mutation or resource toggled by a spec flag rather than an application version.
// Because it returns a *VersionGate, further conditions can still be composed with
// When.
func NewBooleanGate(enabled bool) *VersionGate {
return NewVersionGate("", nil).When(enabled)
}

// When adds a boolean condition that must be true for the gate to be enabled.
//
// Calls are additive: all values passed through When must be true for Enabled()
Expand Down
30 changes: 30 additions & 0 deletions pkg/feature/feature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,36 @@ func TestVersionGate_WhenChaining(t *testing.T) {
assert.False(t, enabled)
}

func TestNewBooleanGate(t *testing.T) {
tests := []struct {
name string
enabled bool
want bool
}{
{name: "enabled", enabled: true, want: true},
{name: "disabled", enabled: false, want: false},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gate := NewBooleanGate(tt.enabled)
enabled, err := gate.Enabled()
assert.NoError(t, err)
assert.Equal(t, tt.want, enabled)
})
}
}

func TestNewBooleanGate_Chainable(t *testing.T) {
// NewBooleanGate returns a *VersionGate, so additional When conditions still
// compose: every condition must be true for the gate to be enabled.
gate := NewBooleanGate(true).When(false)

enabled, err := gate.Enabled()
assert.NoError(t, err)
assert.False(t, enabled)
}

func TestMutation_ApplyIntent(t *testing.T) {
const newValue = "new-value"
type testObj struct {
Expand Down
Loading