Centralize environment configuration (#6293)

* Define central config

* Use envConfig in GenericRun for segment parameters

* Pass the env config into the context passed to CLI methods

* Use PodmanCmd and DockerCmd from context

* Remove tests now that ODO_DISABLE_TELEMETRY is checked for a bool value

* deploy.Deploy: Use values from ctx instead of parameters + use FS from DI

* dev.Start: Use values from ctx instead of parameters

* image.Build*: Use values from ctx instead of parameters

* Use telemetry file from context

* Pass ctx to segment.getTelemetryForDevfileRegistry

* Use ctx in getTelemetryForDevfileRegistry

* Call IsTelemetryEnabled once and use scontext.GetTelemetryStatus after

* Fix unit tests

* Use envConfig in segment.IsTelemetryEnabled

* Define TelemetryCaller constant in test helper

* IsTrackingConsentEnabled: get value from envconfig instead of env

* Use ctx instead of GLOBALODOCONFIG

* Place ODO_EXPERIMENTAL_MODE in configuration

* Use maintained envconfig package

* Define default values when exist

* Document accepted boolean values
This commit is contained in:
Philippe Martin
2022-11-17 18:57:34 +01:00
committed by GitHub
parent ce42ce435e
commit 712254ae66
80 changed files with 2091 additions and 451 deletions

View File

@@ -1,6 +1,7 @@
package preference
import (
"context"
"fmt"
"os"
"os/user"
@@ -9,6 +10,7 @@ import (
"strings"
"time"
envcontext "github.com/redhat-developer/odo/pkg/config/context"
"github.com/redhat-developer/odo/pkg/log"
"github.com/redhat-developer/odo/pkg/odo/cli/ui"
"github.com/redhat-developer/odo/pkg/util"
@@ -69,9 +71,10 @@ type preferenceInfo struct {
var _ Client = (*preferenceInfo)(nil)
func getPreferenceFile() (string, error) {
if env, ok := os.LookupEnv(GlobalConfigEnvName); ok {
return env, nil
func getPreferenceFile(ctx context.Context) (string, error) {
envConfig := envcontext.GetEnvConfig(ctx)
if envConfig.Globalodoconfig != nil {
return *envConfig.Globalodoconfig, nil
}
if len(customHomeDir) != 0 {
@@ -85,8 +88,8 @@ func getPreferenceFile() (string, error) {
return filepath.Join(currentUser.HomeDir, ".odo", configFileName), nil
}
func NewClient() (Client, error) {
return newPreferenceInfo()
func NewClient(ctx context.Context) (Client, error) {
return newPreferenceInfo(ctx)
}
// newPreference creates an empty Preference struct with type meta information
@@ -101,8 +104,8 @@ func newPreference() Preference {
// newPreferenceInfo gets the PreferenceInfo from preference file
// or returns default PreferenceInfo if preference file does not exist
func newPreferenceInfo() (*preferenceInfo, error) {
preferenceFile, err := getPreferenceFile()
func newPreferenceInfo(ctx context.Context) (*preferenceInfo, error) {
preferenceFile, err := getPreferenceFile(ctx)
klog.V(4).Infof("The path for preference file is %+v", preferenceFile)
if err != nil {
return nil, err

View File

@@ -1,6 +1,7 @@
package preference
import (
"context"
"fmt"
"io/ioutil"
"os"
@@ -9,6 +10,9 @@ import (
"testing"
"time"
"github.com/redhat-developer/odo/pkg/config"
envcontext "github.com/redhat-developer/odo/pkg/config/context"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
@@ -19,7 +23,7 @@ func TestNew(t *testing.T) {
t.Fatal(err)
}
defer tempConfigFile.Close()
t.Setenv(GlobalConfigEnvName, tempConfigFile.Name())
tempConfigFileName := tempConfigFile.Name()
tests := []struct {
name string
@@ -52,7 +56,11 @@ func TestNew(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
cfi, err := newPreferenceInfo()
ctx := context.Background()
ctx = envcontext.WithEnvConfig(ctx, config.Configuration{
Globalodoconfig: &tempConfigFileName,
})
cfi, err := newPreferenceInfo(ctx)
switch test.success {
case true:
if err != nil {
@@ -72,12 +80,6 @@ func TestNew(t *testing.T) {
}
func TestGetPushTimeout(t *testing.T) {
tempConfigFile, err := ioutil.TempFile("", "odoconfig")
if err != nil {
t.Fatal(err)
}
defer tempConfigFile.Close()
t.Setenv(GlobalConfigEnvName, tempConfigFile.Name())
nonzeroValue := 5 * time.Second
@@ -103,7 +105,9 @@ func TestGetPushTimeout(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg, err := newPreferenceInfo()
ctx := context.Background()
ctx = envcontext.WithEnvConfig(ctx, config.Configuration{})
cfg, err := newPreferenceInfo(ctx)
if err != nil {
t.Error(err)
}
@@ -118,12 +122,6 @@ func TestGetPushTimeout(t *testing.T) {
}
func TestGetTimeout(t *testing.T) {
tempConfigFile, err := ioutil.TempFile("", "odoconfig")
if err != nil {
t.Fatal(err)
}
defer tempConfigFile.Close()
t.Setenv(GlobalConfigEnvName, tempConfigFile.Name())
zeroValue := 0 * time.Second
nonzeroValue := 5 * time.Second
tests := []struct {
@@ -159,7 +157,9 @@ func TestGetTimeout(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg, err := newPreferenceInfo()
ctx := context.Background()
ctx = envcontext.WithEnvConfig(ctx, config.Configuration{})
cfg, err := newPreferenceInfo(ctx)
if err != nil {
t.Error(err)
}
@@ -174,12 +174,6 @@ func TestGetTimeout(t *testing.T) {
}
func TestSetConfiguration(t *testing.T) {
tempConfigFile, err := ioutil.TempFile("", "odoconfig")
if err != nil {
t.Fatal(err)
}
defer tempConfigFile.Close()
t.Setenv(GlobalConfigEnvName, tempConfigFile.Name())
trueValue := true
falseValue := false
minValue := minimumDurationValue
@@ -357,7 +351,9 @@ func TestSetConfiguration(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg, err := newPreferenceInfo()
ctx := context.Background()
ctx = envcontext.WithEnvConfig(ctx, config.Configuration{})
cfg, err := newPreferenceInfo(ctx)
if err != nil {
t.Error(err)
}
@@ -402,12 +398,6 @@ func TestSetConfiguration(t *testing.T) {
}
func TestConsentTelemetry(t *testing.T) {
tempConfigFile, err := ioutil.TempFile("", "odoconfig")
if err != nil {
t.Fatal(err)
}
defer tempConfigFile.Close()
t.Setenv(GlobalConfigEnvName, tempConfigFile.Name())
trueValue := true
falseValue := false
@@ -457,13 +447,6 @@ func TestConsentTelemetry(t *testing.T) {
}
func TestGetupdateNotification(t *testing.T) {
tempConfigFile, err := ioutil.TempFile("", "odoconfig")
if err != nil {
t.Fatal(err)
}
defer tempConfigFile.Close()
t.Setenv(GlobalConfigEnvName, tempConfigFile.Name())
trueValue := true
falseValue := false
@@ -562,13 +545,15 @@ func TestIsSupportedParameter(t *testing.T) {
func TestPreferenceIsntCreatedWhenOdoIsUsed(t *testing.T) {
// cleaning up old odo files if any
filename, err := getPreferenceFile()
ctx := context.Background()
ctx = envcontext.WithEnvConfig(ctx, config.Configuration{})
filename, err := getPreferenceFile(ctx)
if err != nil {
t.Error(err)
}
os.RemoveAll(filename)
conf, err := newPreferenceInfo()
conf, err := newPreferenceInfo(ctx)
if err != nil {
t.Errorf("error while creating global preference %v", err)
}
@@ -578,7 +563,9 @@ func TestPreferenceIsntCreatedWhenOdoIsUsed(t *testing.T) {
}
func TestMetaTypePopulatedInPreference(t *testing.T) {
pi, err := newPreferenceInfo()
ctx := context.Background()
ctx = envcontext.WithEnvConfig(ctx, config.Configuration{})
pi, err := newPreferenceInfo(ctx)
if err != nil {
t.Error(err)
@@ -687,12 +674,6 @@ func TestHandleWithRegistryExist(t *testing.T) {
}
func TestGetConsentTelemetry(t *testing.T) {
tempConfigFile, err := ioutil.TempFile("", "odoconfig")
if err != nil {
t.Fatal(err)
}
defer tempConfigFile.Close()
t.Setenv(GlobalConfigEnvName, tempConfigFile.Name())
trueValue := true
falseValue := false