Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Package for feature toggles #342

Merged
merged 1 commit into from
Sep 30, 2021
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
37 changes: 37 additions & 0 deletions pkg/features/features.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package features

import (
"os"
)

const ComponentsUpgradesEnvVar = "COMPONENTS_UPGRADE"

var cache = newMutexMap()

type Feature struct {
Name string
IsActive func() bool
}

func IsActive(feature Feature) bool {
return feature.IsActive()
}

func isActiveForEnvVar(envVar string) func() bool {
return func() bool {
active, ok := cache.load(envVar)
if !ok {
active = os.Getenv(envVar) == "true"
cache.store(envVar, active)
}

return active
}
}

func ComponentsUpgrade() Feature {
return Feature{
Name: "Core components upgrade",
IsActive: isActiveForEnvVar(ComponentsUpgradesEnvVar),
}
}
54 changes: 54 additions & 0 deletions pkg/features/features_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package features

import (
"os"
"testing"

. "github.com/onsi/gomega"
)

const fakeFeatureEnvVar = "fakeFeatureEnvVar"

func fakeFeature() Feature {
return Feature{
Name: "Core components upgrade",
IsActive: isActiveForEnvVar(fakeFeatureEnvVar),
}
}

func setupContext(t *testing.T) {
envVarOrgValue, set := os.LookupEnv(fakeFeatureEnvVar)
t.Cleanup(func() {
// cleanup cache
cache = newMutexMap()
if set {
os.Setenv(fakeFeatureEnvVar, envVarOrgValue)
} else {
os.Unsetenv(fakeFeatureEnvVar)
}
})
}

func TestIsActiveEnvVarUnset(t *testing.T) {
g := NewWithT(t)
setupContext(t)

os.Unsetenv(fakeFeatureEnvVar)
g.Expect(IsActive(fakeFeature())).To(BeFalse())
}

func TestIsActiveEnvVarSetFalse(t *testing.T) {
g := NewWithT(t)
setupContext(t)

os.Setenv(fakeFeatureEnvVar, "false")
g.Expect(IsActive(fakeFeature())).To(BeFalse())
}

func TestIsActiveEnvVarSetTrue(t *testing.T) {
g := NewWithT(t)
setupContext(t)

g.Expect(os.Setenv(fakeFeatureEnvVar, "true")).To(Succeed())
g.Expect(IsActive(fakeFeature())).To(BeTrue())
}
27 changes: 27 additions & 0 deletions pkg/features/mutexmap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package features

import "sync"

func newMutexMap() *mutexMap {
return &mutexMap{
internal: make(map[string]bool),
}
}

type mutexMap struct {
internal map[string]bool
sync.RWMutex
}

func (m *mutexMap) load(key string) (value bool, ok bool) {
m.RLock()
result, ok := m.internal[key]
m.RUnlock()
return result, ok
}

func (m *mutexMap) store(key string, value bool) {
m.Lock()
m.internal[key] = value
m.Unlock()
}
23 changes: 23 additions & 0 deletions pkg/features/mutexmap_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package features

import (
"testing"

. "github.com/onsi/gomega"
)

func TestMutextMapLoadAndStore(t *testing.T) {
g := NewWithT(t)
m := newMutexMap()

key := "key"
value := true
v, ok := m.load(key)
g.Expect(ok).To(BeFalse())
g.Expect(v).To(BeFalse())

m.store(key, value)
v, ok = m.load(key)
g.Expect(ok).To(BeTrue())
g.Expect(v).To(Equal(value))
}