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

Add feature flag support #544

Merged
merged 4 commits into from
Jan 13, 2023
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
4 changes: 4 additions & 0 deletions charts/ratify/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ spec:
valueFrom:
fieldRef:
fieldPath: metadata.namespace
{{- range $k, $v := .Values.featureFlags }}
- name: {{ $k }}
value: {{ $v | ternary 1 0 | quote }}
{{- end}}
resources:
{{- toYaml .Values.resources | nindent 12 }}
volumes:
Expand Down
4 changes: 4 additions & 0 deletions charts/ratify/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,7 @@ crds:
runAsGroup: 65532
runAsNonRoot: true
runAsUser: 65532

# See https://github.com/deislabs/ratify/blob/main/docs/reference/usage.md for a list of available feature flags
featureFlags:
# RATIFY_FEATURE_NAME: true
3 changes: 3 additions & 0 deletions cmd/ratify/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cmd

import (
"github.com/deislabs/ratify/pkg/common"
"github.com/deislabs/ratify/pkg/featureflag"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
Expand All @@ -30,6 +31,8 @@ var Root = New(use, shortDesc)

func New(use, short string) *cobra.Command {
common.SetLoggingLevelFromEnv(logrus.StandardLogger())
featureflag.InitFeatureFlagsFromEnv()

root := &cobra.Command{
Use: use,
Short: short,
Expand Down
5 changes: 5 additions & 0 deletions docs/reference/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ This page documents useful flags and options supported by Ratify
- `DEBUG`
- `TRACE`
- `RATIFY_CONFIG`: change the default Ratify configuration directory. Defaults to `~/.ratify`

## Feature flags

Ratify may roll out new features behind feature flags, which are activated by setting the corresponding environment variable `RATIFY_<FEATURE_NAME>=1`.
A value of `1` indicates the feature is active; any other value disables the flag.
60 changes: 60 additions & 0 deletions pkg/featureflag/featureflag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
Copyright The Ratify Authors.
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 featureflag

import (
"os"

"github.com/sirupsen/logrus"
)

// Feature flags are used to enable/disable experimental features.
// They are activated via environment variables, starting with "RATIFY_", ex: RATIFY_DYNAMIC_PLUGINS=1
// Remember to capture changes in the usage guide and release notes.
var (
DynamicPlugins = new("DYNAMIC_PLUGINS", false)
)

var flags = make(map[string]*FeatureFlag)

func InitFeatureFlagsFromEnv() {
for _, f := range flags {
value, ok := os.LookupEnv("RATIFY_" + f.Name)
if ok {
f.Enabled = value == "1"

if f.Enabled {
logrus.Infof("Feature flag %s is enabled", f.Name)
} else {
logrus.Infof("Feature flag %s is disabled", f.Name)
}
}
}
}

type FeatureFlag struct {
Name string
Enabled bool
}

func new(name string, defaultValue bool) *FeatureFlag {
flag := &FeatureFlag{
Name: name,
Enabled: defaultValue,
}
flags[name] = flag
return flag
}
62 changes: 62 additions & 0 deletions pkg/featureflag/featureflag_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
Copyright The Ratify Authors.
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 featureflag

import (
"os"
"testing"
)

func TestFeatureFlag_UsesDefaultValues(t *testing.T) {
flag1 := new("TEST_FLAG1", false)
if flag1.Enabled {
t.Fatal("expected flag1 to be disabled")
}

flag2 := new("TEST_FLAG2", true)
if !flag2.Enabled {
t.Fatal("expected flag2 to be enabled")
}
}

func TestInitFeatureFlagsFromEnv_SetsValues(t *testing.T) {
flag1 := new("TEST_FLAG1", false)
flag2 := new("TEST_FLAG2", false)
flag3 := new("TEST_FLAG3", true)
flag4 := new("TEST_FLAG4", true)

// override flag defaults
os.Setenv("RATIFY_TEST_FLAG2", "1")
os.Setenv("RATIFY_TEST_FLAG4", "0")

InitFeatureFlagsFromEnv()

if flag1.Enabled {
t.Fatal("expected flag1 to be disabled")
}

if !flag2.Enabled {
t.Fatal("expected flag2 to be enabled")
}

if !flag3.Enabled {
t.Fatal("expected flag3 to be enabled")
}

if flag4.Enabled {
t.Fatal("expected flag4 to be disabled")
}
}