Skip to content

Commit

Permalink
Place ODO_EXPERIMENTAL_MODE in configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
feloy committed Nov 14, 2022
1 parent a3a0d0e commit c43101c
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 37 deletions.
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Configuration struct {
OdoTrackingConsent *string `split_words:"true"`
PodmanCmd *string `split_words:"true"`
TelemetryCaller *string `split_words:"true"`
OdoExperimentalMode *bool `split_words:"true"`
}

func GetConfiguration() (*Configuration, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/odo/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func odoRootCmd(ctx context.Context, name, fullName string) *cobra.Command {
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.odo.yaml)")

commonflags.AddOutputFlag()
commonflags.AddRunOnFlag()
commonflags.AddRunOnFlag(ctx)
commonflags.AddVariablesFlags()

// Here we add the necessary "logging" flags.. However, we choose to hide some of these from the user
Expand Down
9 changes: 6 additions & 3 deletions pkg/odo/cli/feature/experimental.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package feature

import (
"os"
"context"

envcontext "github.com/redhat-developer/odo/pkg/config/context"
"k8s.io/utils/pointer"
)

const (
OdoExperimentalModeEnvVar = "ODO_EXPERIMENTAL_MODE"
OdoExperimentalModeTrue = "true"
)

func isExperimentalModeEnabled() bool {
return os.Getenv(OdoExperimentalModeEnvVar) == OdoExperimentalModeTrue
func isExperimentalModeEnabled(ctx context.Context) bool {
return pointer.BoolDeref(envcontext.GetEnvConfig(ctx).OdoExperimentalMode, false)
}
10 changes: 7 additions & 3 deletions pkg/odo/cli/feature/features.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package feature

import "github.com/redhat-developer/odo/pkg/log"
import (
"context"

"github.com/redhat-developer/odo/pkg/log"
)

// OdoFeature represents a uniquely identifiable feature of odo.
// It can either be a CLI command or flag.
Expand Down Expand Up @@ -29,14 +33,14 @@ var (
// IsEnabled returns whether the specified feature should be enabled or not.
// If the feature is not marked as experimental, it should always be enabled.
// Otherwise, it is enabled only if the experimental mode is enabled (see the isExperimentalModeEnabled package-level function).
func IsEnabled(feat OdoFeature) bool {
func IsEnabled(ctx context.Context, feat OdoFeature) bool {
// Features not marked as experimental are always enabled, regardless of the experimental mode
if !feat.isExperimental {
return true
}

// Features marked as experimental are enabled only if the experimental mode is set
experimentalModeEnabled := isExperimentalModeEnabled()
experimentalModeEnabled := isExperimentalModeEnabled(ctx)
if experimentalModeEnabled {
log.Experimentalf("Experimental mode enabled for %s. Use at your own risk. More details on https://odo.dev/docs/user-guides/advanced/experimental-mode",
feat.description)
Expand Down
43 changes: 19 additions & 24 deletions pkg/odo/cli/feature/features_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package feature

import "testing"
import (
"context"
"testing"

"github.com/redhat-developer/odo/pkg/config"
envcontext "github.com/redhat-developer/odo/pkg/config/context"

"k8s.io/utils/pointer"
)

func TestIsEnabled(t *testing.T) {
type args struct {
feature OdoFeature
}
type env struct {
hasExperimentalModeEnvVar bool
experimentalMode string
experimentalMode *bool
}
type testCase struct {
name string
Expand Down Expand Up @@ -37,17 +44,15 @@ func TestIsEnabled(t *testing.T) {
name: "non-experimental feature should always be enabled regardless of experimental mode",
args: args{feature: nonExperimentalFeature},
env: env{
hasExperimentalModeEnvVar: true,
experimentalMode: OdoExperimentalModeTrue,
experimentalMode: pointer.Bool(true),
},
want: true,
},
{
name: "non-experimental feature should always be enabled even if experimental mode is not enabled",
args: args{feature: nonExperimentalFeature},
env: env{
hasExperimentalModeEnvVar: true,
experimentalMode: "false",
experimentalMode: pointer.Bool(false),
},
want: true,
},
Expand All @@ -60,36 +65,26 @@ func TestIsEnabled(t *testing.T) {
name: "experimental feature should be disabled if experimental mode has an unknown value",
args: args{feature: experimentalFeature},
env: env{
hasExperimentalModeEnvVar: true,
experimentalMode: "false",
},
want: false,
},
{
name: "experimental feature should be disabled if experimental mode has an unknown value",
args: args{feature: experimentalFeature},
env: env{
hasExperimentalModeEnvVar: true,
experimentalMode: "foobar",
experimentalMode: pointer.Bool(false),
},
want: false,
},
{
name: "experimental feature should be enabled only if experimental mode is enabled",
args: args{feature: experimentalFeature},
env: env{
hasExperimentalModeEnvVar: true,
experimentalMode: OdoExperimentalModeTrue,
experimentalMode: pointer.Bool(true),
},
want: true,
},
} {
t.Run(tt.name, func(t *testing.T) {
if tt.env.hasExperimentalModeEnvVar {
t.Setenv(OdoExperimentalModeEnvVar, tt.env.experimentalMode)
}
ctx := context.Background()
cfg := config.Configuration{}
cfg.OdoExperimentalMode = tt.env.experimentalMode
ctx = envcontext.WithEnvConfig(ctx, cfg)

got := IsEnabled(tt.args.feature)
got := IsEnabled(ctx, tt.args.feature)

if got != tt.want {
t.Errorf("IsEnabled: expected %v, but got %v. Env: %v", tt.want, got, tt.env)
Expand Down
15 changes: 11 additions & 4 deletions pkg/odo/commonflags/main_test.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
package commonflags

import (
"context"
"flag"
"os"
"testing"

"github.com/redhat-developer/odo/pkg/config"
envcontext "github.com/redhat-developer/odo/pkg/config/context"

"github.com/spf13/pflag"
"k8s.io/klog"

"github.com/redhat-developer/odo/pkg/odo/cli/feature"
"k8s.io/utils/pointer"
)

func TestMain(m *testing.M) {
// --run-on is considered experimental for now. As such, to exist, it requires the ODO_EXPERIMENTAL_MODE env var to be set.
os.Setenv(feature.OdoExperimentalModeEnvVar, feature.OdoExperimentalModeTrue)
ctx := context.Background()
cfg := config.Configuration{
OdoExperimentalMode: pointer.Bool(true),
}
ctx = envcontext.WithEnvConfig(ctx, cfg)
klog.InitFlags(nil)
AddOutputFlag()
AddRunOnFlag()
AddRunOnFlag(ctx)
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)

os.Exit(m.Run())
Expand Down
5 changes: 3 additions & 2 deletions pkg/odo/commonflags/run_on.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package commonflags

import (
"context"
"errors"
"flag"
"fmt"
Expand Down Expand Up @@ -32,8 +33,8 @@ func UseRunOnFlag(cmd *cobra.Command) {
// We use "flag" in order to make this accessible throughtout ALL of odo, rather than the
// above traditional "persistentflags" usage that does not make it a pointer within the 'pflag'
// package
func AddRunOnFlag() {
if feature.IsEnabled(feature.GenericRunOnFlag) {
func AddRunOnFlag(ctx context.Context) {
if feature.IsEnabled(ctx, feature.GenericRunOnFlag) {
flag.CommandLine.String(RunOnFlagName, "", `Specify target platform, supported platforms: "cluster" (default), "podman" (experimental)`)
_ = pflag.CommandLine.MarkHidden(RunOnFlagName)
}
Expand Down

0 comments on commit c43101c

Please sign in to comment.