mirror of
https://github.com/redhat-developer/odo.git
synced 2025-10-19 03:06:19 +03:00
Separate layers : Preference (#5296)
* Refactor preference package * Unit tests * Apply suggestions from code review Co-authored-by: Parthvi Vala <pvala@redhat.com> * Review * Review from Dharmit * Replace panic with LogErrorAndExit * Remove preference from kclient/oc_server * Remove preference.New from devfile * Remove preference.New from kclient/WaitAndGetPodWithEvents * Get prefClient from CreateOptions * Parthvi review Co-authored-by: Parthvi Vala <pvala@redhat.com>
This commit is contained in:
@@ -55,7 +55,7 @@ func main() {
|
||||
return
|
||||
}
|
||||
|
||||
cfg, err := preference.New()
|
||||
cfg, err := preference.NewClient()
|
||||
if err != nil {
|
||||
util.LogErrorAndExit(err, "")
|
||||
}
|
||||
|
||||
1
go.mod
1
go.mod
@@ -59,6 +59,7 @@ require (
|
||||
k8s.io/klog v1.0.0
|
||||
k8s.io/klog/v2 v2.10.0
|
||||
k8s.io/kubectl v0.22.1
|
||||
k8s.io/utils v0.0.0-20210819203725-bdf08cb9a70a
|
||||
sigs.k8s.io/controller-runtime v0.10.2
|
||||
sigs.k8s.io/yaml v1.3.0
|
||||
|
||||
|
||||
@@ -28,14 +28,14 @@ import (
|
||||
func GetDevfileRegistries(registryName string) ([]Registry, error) {
|
||||
var devfileRegistries []Registry
|
||||
|
||||
cfg, err := preference.New()
|
||||
cfg, err := preference.NewClient()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
hasName := len(registryName) != 0
|
||||
if cfg.OdoSettings.RegistryList != nil {
|
||||
registryList := *cfg.OdoSettings.RegistryList
|
||||
if cfg.RegistryList() != nil {
|
||||
registryList := *cfg.RegistryList()
|
||||
// Loop backwards here to ensure the registry display order is correct (display latest newly added registry firstly)
|
||||
for i := len(registryList) - 1; i >= 0; i-- {
|
||||
registry := registryList[i]
|
||||
@@ -119,10 +119,14 @@ func getRegistryDevfiles(registry Registry) ([]DevfileComponentType, error) {
|
||||
request := util.HTTPRequestParams{
|
||||
URL: indexLink,
|
||||
}
|
||||
secure, err := registryUtil.IsSecure(registry.Name)
|
||||
|
||||
// TODO(feloy) Get from DI
|
||||
cfg, err := preference.NewClient()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
secure := registryUtil.IsSecure(cfg, registry.Name)
|
||||
if secure {
|
||||
token, e := keyring.Get(fmt.Sprintf("%s%s", util.CredentialPrefix, registry.Name), registryUtil.RegistryUser)
|
||||
if e != nil {
|
||||
@@ -131,11 +135,6 @@ func getRegistryDevfiles(registry Registry) ([]DevfileComponentType, error) {
|
||||
request.Token = token
|
||||
}
|
||||
|
||||
cfg, err := preference.New()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
jsonBytes, err := util.HTTPGetRequest(request, cfg.GetRegistryCacheTime())
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "unable to download the devfile index.json from %s", indexLink)
|
||||
|
||||
@@ -60,8 +60,9 @@ func GetComponentDir(path string) (string, error) {
|
||||
// GetDefaultComponentName generates a unique component name
|
||||
// Parameters: desired default component name(w/o prefix) and slice of existing component names
|
||||
// Returns: Unique component name and error if any
|
||||
func GetDefaultComponentName(componentPath string, componentType string, existingComponentList ComponentList) (string, error) {
|
||||
func GetDefaultComponentName(cfg preference.Client, componentPath string, componentType string, existingComponentList ComponentList) (string, error) {
|
||||
var prefix string
|
||||
var err error
|
||||
|
||||
// Get component names from component list
|
||||
var existingComponentNames []string
|
||||
@@ -69,14 +70,8 @@ func GetDefaultComponentName(componentPath string, componentType string, existin
|
||||
existingComponentNames = append(existingComponentNames, component.Name)
|
||||
}
|
||||
|
||||
// Fetch config
|
||||
cfg, err := preference.New()
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "unable to generate random component name")
|
||||
}
|
||||
|
||||
// If there's no prefix in config file, or its value is empty string use safe default - the current directory along with component type
|
||||
if cfg.OdoSettings.NamePrefix == nil || *cfg.OdoSettings.NamePrefix == "" {
|
||||
if cfg.NamePrefix() == nil || *cfg.NamePrefix() == "" {
|
||||
prefix, err = GetComponentDir(componentPath)
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "unable to generate random component name")
|
||||
@@ -84,7 +79,7 @@ func GetDefaultComponentName(componentPath string, componentType string, existin
|
||||
prefix = util.TruncateString(prefix, componentRandomNamePartsMaxLen)
|
||||
} else {
|
||||
// Set the required prefix into componentName
|
||||
prefix = *cfg.OdoSettings.NamePrefix
|
||||
prefix = *cfg.NamePrefix()
|
||||
}
|
||||
|
||||
// Generate unique name for the component using prefix and unique random suffix
|
||||
|
||||
@@ -2,7 +2,6 @@ package component
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"testing"
|
||||
@@ -17,6 +16,7 @@ import (
|
||||
componentlabels "github.com/redhat-developer/odo/pkg/component/labels"
|
||||
"github.com/redhat-developer/odo/pkg/kclient"
|
||||
"github.com/redhat-developer/odo/pkg/localConfigProvider"
|
||||
"github.com/redhat-developer/odo/pkg/preference"
|
||||
"github.com/redhat-developer/odo/pkg/testingutil"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
@@ -253,27 +253,11 @@ func TestGetDefaultComponentName(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
t.Log("Running test: ", tt.testName)
|
||||
t.Run(tt.testName, func(t *testing.T) {
|
||||
odoConfigFile, kubeConfigFile, err := testingutil.SetUp(
|
||||
testingutil.ConfigDetails{
|
||||
FileName: "odo-test-config",
|
||||
Config: testingutil.FakeOdoConfig("odo-test-config", false, ""),
|
||||
ConfigPathEnv: "GLOBALODOCONFIG",
|
||||
}, testingutil.ConfigDetails{
|
||||
FileName: "kube-test-config",
|
||||
Config: testingutil.FakeKubeClientConfig(),
|
||||
ConfigPathEnv: "KUBECONFIG",
|
||||
},
|
||||
)
|
||||
defer testingutil.CleanupEnv([]*os.File{odoConfigFile, kubeConfigFile}, t)
|
||||
if err != nil {
|
||||
t.Errorf("failed to setup test env. Error %v", err)
|
||||
}
|
||||
|
||||
name, err := GetDefaultComponentName(tt.componentPath, tt.componentType, tt.existingComponents)
|
||||
if err != nil {
|
||||
t.Errorf("failed to setup mock environment. Error: %v", err)
|
||||
}
|
||||
ctrl := gomock.NewController(t)
|
||||
cfg := preference.NewMockClient(ctrl)
|
||||
cfg.EXPECT().NamePrefix().Return(nil)
|
||||
|
||||
name, err := GetDefaultComponentName(cfg, tt.componentPath, tt.componentType, tt.existingComponents)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("expected err: %v, but err is %v", tt.wantErr, err)
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"github.com/redhat-developer/odo/pkg/devfile/adapters/common"
|
||||
"github.com/redhat-developer/odo/pkg/devfile/adapters/kubernetes"
|
||||
"github.com/redhat-developer/odo/pkg/kclient"
|
||||
"github.com/redhat-developer/odo/pkg/preference"
|
||||
)
|
||||
|
||||
// NewComponentAdapter returns a Devfile adapter for the targeted platform
|
||||
@@ -32,17 +33,21 @@ func createKubernetesAdapter(adapterContext common.AdapterContext, namespace str
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
prefClient, err := preference.NewClient()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// If a namespace was passed in
|
||||
if namespace != "" {
|
||||
client.Namespace = namespace
|
||||
}
|
||||
return newKubernetesAdapter(adapterContext, client)
|
||||
return newKubernetesAdapter(adapterContext, client, prefClient)
|
||||
}
|
||||
|
||||
func newKubernetesAdapter(adapterContext common.AdapterContext, client kclient.ClientInterface) (common.ComponentAdapter, error) {
|
||||
func newKubernetesAdapter(adapterContext common.AdapterContext, client kclient.ClientInterface, prefClient preference.Client) (common.ComponentAdapter, error) {
|
||||
// Feed the common metadata to the platform-specific adapter
|
||||
kubernetesAdapter := kubernetes.New(adapterContext, client)
|
||||
kubernetesAdapter := kubernetes.New(adapterContext, client, prefClient)
|
||||
|
||||
return kubernetesAdapter, nil
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ func TestNewPlatformAdapter(t *testing.T) {
|
||||
Devfile: devObj,
|
||||
}
|
||||
fkclient, _ := kclient.FakeNew()
|
||||
adapter, err := newKubernetesAdapter(adapterContext, fkclient)
|
||||
adapter, err := newKubernetesAdapter(adapterContext, fkclient, nil)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: '%v'", err)
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
devfilev1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
|
||||
"github.com/redhat-developer/odo/pkg/kclient"
|
||||
"github.com/redhat-developer/odo/pkg/machineoutput"
|
||||
"github.com/redhat-developer/odo/pkg/preference"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/redhat-developer/odo/pkg/devfile/adapters/common"
|
||||
@@ -22,9 +23,9 @@ type KubernetesContext struct {
|
||||
}
|
||||
|
||||
// New instantiates a kubernetes adapter
|
||||
func New(adapterContext common.AdapterContext, client kclient.ClientInterface) Adapter {
|
||||
func New(adapterContext common.AdapterContext, client kclient.ClientInterface, prefClient preference.Client) Adapter {
|
||||
|
||||
compAdapter := component.New(adapterContext, client)
|
||||
compAdapter := component.New(adapterContext, client, prefClient)
|
||||
|
||||
return Adapter{
|
||||
componentAdapter: &compAdapter,
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
componentlabels "github.com/redhat-developer/odo/pkg/component/labels"
|
||||
"github.com/redhat-developer/odo/pkg/devfile"
|
||||
"github.com/redhat-developer/odo/pkg/envinfo"
|
||||
"github.com/redhat-developer/odo/pkg/preference"
|
||||
"github.com/redhat-developer/odo/pkg/service"
|
||||
"github.com/redhat-developer/odo/pkg/util"
|
||||
|
||||
@@ -38,9 +39,9 @@ import (
|
||||
const supervisorDStatusWaitTimeInterval = 1
|
||||
|
||||
// New instantiates a component adapter
|
||||
func New(adapterContext common.AdapterContext, client kclient.ClientInterface) Adapter {
|
||||
func New(adapterContext common.AdapterContext, client kclient.ClientInterface, prefClient preference.Client) Adapter {
|
||||
|
||||
adapter := Adapter{Client: client}
|
||||
adapter := Adapter{Client: client, prefClient: prefClient}
|
||||
adapter.GenericAdapter = common.NewGenericAdapter(&adapter, adapterContext)
|
||||
adapter.GenericAdapter.InitWith(&adapter)
|
||||
return adapter
|
||||
@@ -53,7 +54,7 @@ func (a *Adapter) getPod(refresh bool) (*corev1.Pod, error) {
|
||||
podSelector := fmt.Sprintf("component=%s", a.ComponentName)
|
||||
|
||||
// Wait for Pod to be in running state otherwise we can't sync data to it.
|
||||
pod, err := a.Client.WaitAndGetPodWithEvents(podSelector, corev1.PodRunning, "Waiting for component to start")
|
||||
pod, err := a.Client.WaitAndGetPodWithEvents(podSelector, corev1.PodRunning, "Waiting for component to start", time.Duration(a.prefClient.GetPushTimeout())*time.Second)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "error while waiting for pod %s", podSelector)
|
||||
}
|
||||
@@ -91,7 +92,9 @@ func (a *Adapter) SupervisorComponentInfo(command devfilev1.Command) (common.Com
|
||||
|
||||
// Adapter is a component adapter implementation for Kubernetes
|
||||
type Adapter struct {
|
||||
Client kclient.ClientInterface
|
||||
Client kclient.ClientInterface
|
||||
prefClient preference.Client
|
||||
|
||||
*common.GenericAdapter
|
||||
|
||||
devfileBuildCmd string
|
||||
@@ -196,7 +199,8 @@ func (a Adapter) Push(parameters common.PushParameters) (err error) {
|
||||
|
||||
log.Infof("\nCreating Kubernetes resources for component %s", a.ComponentName)
|
||||
|
||||
err = a.createOrUpdateComponent(componentExists, parameters.EnvSpecificInfo)
|
||||
isMainStorageEphemeral := a.prefClient.GetEphemeralSourceVolume()
|
||||
err = a.createOrUpdateComponent(componentExists, parameters.EnvSpecificInfo, isMainStorageEphemeral)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "unable to create or update component")
|
||||
}
|
||||
@@ -421,7 +425,7 @@ func (a Adapter) DoesComponentExist(cmpName string, appName string) (bool, error
|
||||
return utils.ComponentExists(a.Client, cmpName, appName)
|
||||
}
|
||||
|
||||
func (a *Adapter) createOrUpdateComponent(componentExists bool, ei envinfo.EnvSpecificInfo) (err error) {
|
||||
func (a *Adapter) createOrUpdateComponent(componentExists bool, ei envinfo.EnvSpecificInfo, isMainStorageEphemeral bool) (err error) {
|
||||
ei.SetDevfileObj(a.Devfile)
|
||||
|
||||
storageClient := storagepkg.NewClient(storagepkg.ClientOptions{
|
||||
@@ -430,7 +434,7 @@ func (a *Adapter) createOrUpdateComponent(componentExists bool, ei envinfo.EnvSp
|
||||
})
|
||||
|
||||
// handle the ephemeral storage
|
||||
err = storage.HandleEphemeralStorage(a.Client, storageClient, a.ComponentName)
|
||||
err = storage.HandleEphemeralStorage(a.Client, storageClient, a.ComponentName, isMainStorageEphemeral)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -6,10 +6,12 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/devfile/library/pkg/devfile/parser/data"
|
||||
"github.com/golang/mock/gomock"
|
||||
|
||||
"github.com/devfile/library/pkg/devfile/generator"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/redhat-developer/odo/pkg/envinfo"
|
||||
"github.com/redhat-developer/odo/pkg/preference"
|
||||
"github.com/redhat-developer/odo/pkg/util"
|
||||
|
||||
devfilev1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
|
||||
@@ -136,8 +138,8 @@ func TestCreateOrUpdateComponent(t *testing.T) {
|
||||
Name: testComponentName,
|
||||
AppName: testAppName,
|
||||
})
|
||||
componentAdapter := New(adapterCtx, fkclient)
|
||||
err := componentAdapter.createOrUpdateComponent(tt.running, tt.envInfo)
|
||||
componentAdapter := New(adapterCtx, fkclient, nil)
|
||||
err := componentAdapter.createOrUpdateComponent(tt.running, tt.envInfo, false)
|
||||
|
||||
// Checks for unexpected error cases
|
||||
if !tt.wantErr == (err != nil) {
|
||||
@@ -350,8 +352,8 @@ func TestDoesComponentExist(t *testing.T) {
|
||||
})
|
||||
|
||||
// DoesComponentExist requires an already started component, so start it.
|
||||
componentAdapter := New(adapterCtx, fkclient)
|
||||
err := componentAdapter.createOrUpdateComponent(false, tt.envInfo)
|
||||
componentAdapter := New(adapterCtx, fkclient, nil)
|
||||
err := componentAdapter.createOrUpdateComponent(false, tt.envInfo, false)
|
||||
|
||||
// Checks for unexpected error cases
|
||||
if err != nil {
|
||||
@@ -443,7 +445,10 @@ func TestWaitAndGetComponentPod(t *testing.T) {
|
||||
return true, fkWatch, nil
|
||||
})
|
||||
|
||||
componentAdapter := New(adapterCtx, fkclient)
|
||||
ctrl := gomock.NewController(t)
|
||||
prefClient := preference.NewMockClient(ctrl)
|
||||
prefClient.EXPECT().GetPushTimeout().Return(10)
|
||||
componentAdapter := New(adapterCtx, fkclient, prefClient)
|
||||
_, err := componentAdapter.getPod(false)
|
||||
|
||||
// Checks for unexpected error cases
|
||||
@@ -564,7 +569,7 @@ func TestAdapterDelete(t *testing.T) {
|
||||
|
||||
fkclient, fkclientset := kclient.FakeNew()
|
||||
|
||||
a := New(adapterCtx, fkclient)
|
||||
a := New(adapterCtx, fkclient, nil)
|
||||
|
||||
fkclientset.Kubernetes.PrependReactor("delete-collection", "deployments", func(action ktesting.Action) (bool, runtime.Object, error) {
|
||||
if util.ConvertLabelsToSelector(tt.args.labels) != action.(ktesting.DeleteCollectionAction).GetListRestrictions().Labels.String() {
|
||||
|
||||
@@ -294,7 +294,7 @@ func TestStatusReconciler(t *testing.T) {
|
||||
|
||||
fkclient, _ := kclient.FakeNew()
|
||||
|
||||
adapter := New(adapterCtx, fkclient)
|
||||
adapter := New(adapterCtx, fkclient, nil)
|
||||
|
||||
lfo := logFuncOutput{}
|
||||
adapter.GenericAdapter.SetLogger(machineoutput.NewConsoleMachineEventLoggingClientWithFunction(lfo.logFunc))
|
||||
|
||||
@@ -249,9 +249,9 @@ func TestGetDeploymentStatus(t *testing.T) {
|
||||
AppName: testAppName,
|
||||
})
|
||||
|
||||
componentAdapter := New(adapterCtx, fkclient)
|
||||
componentAdapter := New(adapterCtx, fkclient, nil)
|
||||
fkclient.Namespace = componentAdapter.Client.GetCurrentNamespace()
|
||||
err := componentAdapter.createOrUpdateComponent(tt.running, tt.envInfo)
|
||||
err := componentAdapter.createOrUpdateComponent(tt.running, tt.envInfo, false)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@ import (
|
||||
componentlabels "github.com/redhat-developer/odo/pkg/component/labels"
|
||||
"github.com/redhat-developer/odo/pkg/envinfo"
|
||||
"github.com/redhat-developer/odo/pkg/kclient"
|
||||
"github.com/redhat-developer/odo/pkg/preference"
|
||||
"github.com/redhat-developer/odo/pkg/storage"
|
||||
storagepkg "github.com/redhat-developer/odo/pkg/storage"
|
||||
storagelabels "github.com/redhat-developer/odo/pkg/storage/labels"
|
||||
@@ -187,12 +186,7 @@ func generateVolumeNameFromPVC(pvc string) (volumeName string, err error) {
|
||||
}
|
||||
|
||||
// HandleEphemeralStorage creates or deletes the ephemeral volume based on the preference setting
|
||||
func HandleEphemeralStorage(client kclient.ClientInterface, storageClient storage.Client, componentName string) error {
|
||||
pref, err := preference.New()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
func HandleEphemeralStorage(client kclient.ClientInterface, storageClient storage.Client, componentName string, isEphemeral bool) error {
|
||||
selector := fmt.Sprintf("%v=%s,%s=%s", componentlabels.ComponentLabel, componentName, storagelabels.SourcePVCLabel, storage.OdoSourceVolume)
|
||||
|
||||
pvcs, err := client.ListPVCs(selector)
|
||||
@@ -200,7 +194,7 @@ func HandleEphemeralStorage(client kclient.ClientInterface, storageClient storag
|
||||
return err
|
||||
}
|
||||
|
||||
if !pref.GetEphemeralSourceVolume() {
|
||||
if !isEphemeral {
|
||||
if len(pvcs) == 0 {
|
||||
err := storageClient.Create(storage.Storage{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
|
||||
@@ -2,6 +2,7 @@ package kclient
|
||||
|
||||
import (
|
||||
"io"
|
||||
"time"
|
||||
|
||||
"k8s.io/apimachinery/pkg/api/meta"
|
||||
"k8s.io/apimachinery/pkg/util/intstr"
|
||||
@@ -82,7 +83,7 @@ type ClientInterface interface {
|
||||
WaitForServiceAccountInNamespace(namespace, serviceAccountName string) error
|
||||
|
||||
// oc_server.go
|
||||
GetServerVersion() (*ServerInfo, error)
|
||||
GetServerVersion(timeout time.Duration) (*ServerInfo, error)
|
||||
|
||||
// operators.go
|
||||
IsServiceBindingSupported() (bool, error)
|
||||
@@ -100,7 +101,7 @@ type ClientInterface interface {
|
||||
GetOperatorGVRList() ([]meta.RESTMapping, error)
|
||||
|
||||
// pods.go
|
||||
WaitAndGetPodWithEvents(selector string, desiredPhase corev1.PodPhase, waitMessage string) (*corev1.Pod, error)
|
||||
WaitAndGetPodWithEvents(selector string, desiredPhase corev1.PodPhase, waitMessage string, pushTimeout time.Duration) (*corev1.Pod, error)
|
||||
ExecCMDInContainer(containerName, podName string, cmd []string, stdout io.Writer, stderr io.Writer, stdin io.Reader, tty bool) error
|
||||
ExtractProjectToComponent(containerName, podName string, targetPath string, stdin io.Reader) error
|
||||
GetOnePod(componentName, appName string) (*corev1.Pod, error)
|
||||
|
||||
@@ -7,6 +7,7 @@ package kclient
|
||||
import (
|
||||
io "io"
|
||||
reflect "reflect"
|
||||
time "time"
|
||||
|
||||
spec "github.com/go-openapi/spec"
|
||||
gomock "github.com/golang/mock/gomock"
|
||||
@@ -1004,18 +1005,18 @@ func (mr *MockClientInterfaceMockRecorder) GetSecret(name, namespace interface{}
|
||||
}
|
||||
|
||||
// GetServerVersion mocks base method.
|
||||
func (m *MockClientInterface) GetServerVersion() (*ServerInfo, error) {
|
||||
func (m *MockClientInterface) GetServerVersion(timeout time.Duration) (*ServerInfo, error) {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "GetServerVersion")
|
||||
ret := m.ctrl.Call(m, "GetServerVersion", timeout)
|
||||
ret0, _ := ret[0].(*ServerInfo)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// GetServerVersion indicates an expected call of GetServerVersion.
|
||||
func (mr *MockClientInterfaceMockRecorder) GetServerVersion() *gomock.Call {
|
||||
func (mr *MockClientInterfaceMockRecorder) GetServerVersion(timeout interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetServerVersion", reflect.TypeOf((*MockClientInterface)(nil).GetServerVersion))
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetServerVersion", reflect.TypeOf((*MockClientInterface)(nil).GetServerVersion), timeout)
|
||||
}
|
||||
|
||||
// GetService mocks base method.
|
||||
@@ -1475,18 +1476,18 @@ func (mr *MockClientInterfaceMockRecorder) UpdateStorageOwnerReference(pvc inter
|
||||
}
|
||||
|
||||
// WaitAndGetPodWithEvents mocks base method.
|
||||
func (m *MockClientInterface) WaitAndGetPodWithEvents(selector string, desiredPhase v12.PodPhase, waitMessage string) (*v12.Pod, error) {
|
||||
func (m *MockClientInterface) WaitAndGetPodWithEvents(selector string, desiredPhase v12.PodPhase, waitMessage string, pushTimeout time.Duration) (*v12.Pod, error) {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "WaitAndGetPodWithEvents", selector, desiredPhase, waitMessage)
|
||||
ret := m.ctrl.Call(m, "WaitAndGetPodWithEvents", selector, desiredPhase, waitMessage, pushTimeout)
|
||||
ret0, _ := ret[0].(*v12.Pod)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// WaitAndGetPodWithEvents indicates an expected call of WaitAndGetPodWithEvents.
|
||||
func (mr *MockClientInterfaceMockRecorder) WaitAndGetPodWithEvents(selector, desiredPhase, waitMessage interface{}) *gomock.Call {
|
||||
func (mr *MockClientInterfaceMockRecorder) WaitAndGetPodWithEvents(selector, desiredPhase, waitMessage, pushTimeout interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "WaitAndGetPodWithEvents", reflect.TypeOf((*MockClientInterface)(nil).WaitAndGetPodWithEvents), selector, desiredPhase, waitMessage)
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "WaitAndGetPodWithEvents", reflect.TypeOf((*MockClientInterface)(nil).WaitAndGetPodWithEvents), selector, desiredPhase, waitMessage, pushTimeout)
|
||||
}
|
||||
|
||||
// WaitAndGetSecret mocks base method.
|
||||
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/redhat-developer/odo/pkg/preference"
|
||||
"github.com/redhat-developer/odo/pkg/util"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/version"
|
||||
@@ -16,24 +15,13 @@ import (
|
||||
|
||||
// isServerUp returns true if server is up and running
|
||||
// server parameter has to be a valid url
|
||||
func isServerUp(server string) bool {
|
||||
// initialising the default timeout, this will be used
|
||||
// when the value is not readable from config
|
||||
ocRequestTimeout := preference.DefaultTimeout * time.Second
|
||||
// checking the value of timeout in config
|
||||
// before proceeding with default timeout
|
||||
cfg, configReadErr := preference.New()
|
||||
if configReadErr != nil {
|
||||
klog.V(3).Info(errors.Wrap(configReadErr, "unable to read config file"))
|
||||
} else {
|
||||
ocRequestTimeout = time.Duration(cfg.GetTimeout()) * time.Second
|
||||
}
|
||||
func isServerUp(server string, timeout time.Duration) bool {
|
||||
address, err := util.GetHostWithPort(server)
|
||||
if err != nil {
|
||||
klog.V(3).Infof("Unable to parse url %s (%s)", server, err)
|
||||
}
|
||||
klog.V(3).Infof("Trying to connect to server %s", address)
|
||||
_, connectionError := net.DialTimeout("tcp", address, time.Duration(ocRequestTimeout))
|
||||
_, connectionError := net.DialTimeout("tcp", address, timeout)
|
||||
if connectionError != nil {
|
||||
klog.V(3).Info(errors.Wrap(connectionError, "unable to connect to server"))
|
||||
return false
|
||||
@@ -53,7 +41,7 @@ type ServerInfo struct {
|
||||
|
||||
// GetServerVersion will fetch the Server Host, OpenShift and Kubernetes Version
|
||||
// It will be shown on the execution of odo version command
|
||||
func (c *Client) GetServerVersion() (*ServerInfo, error) {
|
||||
func (c *Client) GetServerVersion(timeout time.Duration) (*ServerInfo, error) {
|
||||
var info ServerInfo
|
||||
|
||||
// This will fetch the information about Server Address
|
||||
@@ -64,7 +52,7 @@ func (c *Client) GetServerVersion() (*ServerInfo, error) {
|
||||
info.Address = config.Host
|
||||
|
||||
// checking if the server is reachable
|
||||
if !isServerUp(config.Host) {
|
||||
if !isServerUp(config.Host, timeout) {
|
||||
return nil, errors.New("Unable to connect to OpenShift cluster, is it down?")
|
||||
}
|
||||
|
||||
|
||||
@@ -9,8 +9,6 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/redhat-developer/odo/pkg/preference"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/redhat-developer/odo/pkg/log"
|
||||
"k8s.io/klog"
|
||||
@@ -26,16 +24,7 @@ import (
|
||||
|
||||
// WaitAndGetPod block and waits until pod matching selector is in in Running state
|
||||
// desiredPhase cannot be PodFailed or PodUnknown
|
||||
func (c *Client) WaitAndGetPodWithEvents(selector string, desiredPhase corev1.PodPhase, waitMessage string) (*corev1.Pod, error) {
|
||||
|
||||
// Try to grab the preference in order to set a timeout.. but if not, we'll use the default.
|
||||
pushTimeout := preference.DefaultPushTimeout * time.Second
|
||||
cfg, configReadErr := preference.New()
|
||||
if configReadErr != nil {
|
||||
klog.V(3).Info(errors.Wrap(configReadErr, "unable to read config file"))
|
||||
} else {
|
||||
pushTimeout = time.Duration(cfg.GetPushTimeout()) * time.Second
|
||||
}
|
||||
func (c *Client) WaitAndGetPodWithEvents(selector string, desiredPhase corev1.PodPhase, waitMessage string, pushTimeout time.Duration) (*corev1.Pod, error) {
|
||||
|
||||
klog.V(3).Infof("Waiting for %s pod", selector)
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/redhat-developer/odo/pkg/preference"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
@@ -80,7 +81,7 @@ func TestWaitAndGetPodWithEvents(t *testing.T) {
|
||||
|
||||
podSelector := fmt.Sprintf("deploymentconfig=%s", tt.podName)
|
||||
|
||||
pod, err := fakeClient.WaitAndGetPodWithEvents(podSelector, corev1.PodRunning, "Waiting for component to start")
|
||||
pod, err := fakeClient.WaitAndGetPodWithEvents(podSelector, corev1.PodRunning, "Waiting for component to start", preference.DefaultPushTimeout*time.Second)
|
||||
|
||||
if !tt.wantErr == (err != nil) {
|
||||
t.Errorf("client.WaitAndGetPod(string) unexpected error %v, wantErr %v", err, tt.wantErr)
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"github.com/redhat-developer/odo/pkg/odo/cmdline"
|
||||
"github.com/redhat-developer/odo/pkg/odo/genericclioptions"
|
||||
odoutil "github.com/redhat-developer/odo/pkg/odo/util"
|
||||
"github.com/redhat-developer/odo/pkg/preference"
|
||||
"github.com/redhat-developer/odo/pkg/project"
|
||||
"github.com/redhat-developer/odo/pkg/util"
|
||||
"github.com/spf13/cobra"
|
||||
@@ -23,7 +24,8 @@ type CommonPushOptions struct {
|
||||
*genericclioptions.Context
|
||||
|
||||
// Clients
|
||||
prjClient project.Client
|
||||
prjClient project.Client
|
||||
prefClient preference.Client
|
||||
|
||||
//Flags
|
||||
// TODO(feloy) Fixme
|
||||
@@ -36,9 +38,10 @@ type CommonPushOptions struct {
|
||||
}
|
||||
|
||||
// NewCommonPushOptions instantiates a commonPushOptions object
|
||||
func NewCommonPushOptions(prjClient project.Client) *CommonPushOptions {
|
||||
func NewCommonPushOptions(prjClient project.Client, prefClient preference.Client) *CommonPushOptions {
|
||||
return &CommonPushOptions{
|
||||
prjClient: prjClient,
|
||||
prjClient: prjClient,
|
||||
prefClient: prefClient,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/redhat-developer/odo/pkg/kclient"
|
||||
registryUtil "github.com/redhat-developer/odo/pkg/odo/cli/registry/util"
|
||||
"github.com/redhat-developer/odo/pkg/odo/cmdline"
|
||||
"github.com/redhat-developer/odo/pkg/preference"
|
||||
"github.com/redhat-developer/odo/pkg/project"
|
||||
"github.com/zalando/go-keyring"
|
||||
|
||||
@@ -115,9 +116,9 @@ odo catalog list components
|
||||
%[1]s nodejs --app myapp --project myproject`)
|
||||
|
||||
// NewCreateOptions returns new instance of CreateOptions
|
||||
func NewCreateOptions(prjClient project.Client) *CreateOptions {
|
||||
func NewCreateOptions(prjClient project.Client, prefClient preference.Client) *CreateOptions {
|
||||
return &CreateOptions{
|
||||
PushOptions: NewPushOptions(prjClient),
|
||||
PushOptions: NewPushOptions(prjClient, prefClient),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -208,11 +209,7 @@ func (co *CreateOptions) Complete(cmdline cmdline.Cmdline, args []string) (err e
|
||||
}()
|
||||
// Set the starter project token if required
|
||||
if co.devfileMetadata.starter != "" {
|
||||
var secure bool
|
||||
secure, err = registryUtil.IsSecure(co.devfileMetadata.devfileRegistry.Name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
secure := registryUtil.IsSecure(co.prefClient, co.devfileMetadata.devfileRegistry.Name)
|
||||
if co.devfileMetadata.starterToken == "" && secure {
|
||||
var token string
|
||||
token, err = keyring.Get(fmt.Sprintf("%s%s", util.CredentialPrefix, co.devfileMetadata.devfileRegistry.Name), registryUtil.RegistryUser)
|
||||
@@ -349,7 +346,11 @@ func (co *CreateOptions) Run() (err error) {
|
||||
func NewCmdCreate(name, fullName string) *cobra.Command {
|
||||
// The error is not handled at this point, it will be handled during Context creation
|
||||
kubclient, _ := kclient.New()
|
||||
co := NewCreateOptions(project.NewClient(kubclient))
|
||||
prefClient, err := preference.NewClient()
|
||||
if err != nil {
|
||||
odoutil.LogErrorAndExit(err, "unable to set preference, something is wrong with odo, kindly raise an issue at https://github.com/redhat-developer/odo/issues/new?template=Bug.md")
|
||||
}
|
||||
co := NewCreateOptions(project.NewClient(kubclient), prefClient)
|
||||
var componentCreateCmd = &cobra.Command{
|
||||
Use: fmt.Sprintf("%s <component_type> [component_name] [flags]", name),
|
||||
Short: "Create a new component",
|
||||
|
||||
@@ -2,11 +2,12 @@ package component
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/redhat-developer/odo/pkg/kclient"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/redhat-developer/odo/pkg/kclient"
|
||||
|
||||
"github.com/redhat-developer/odo/pkg/segment"
|
||||
|
||||
"github.com/redhat-developer/odo/pkg/odo/cli/component/ui"
|
||||
@@ -87,7 +88,7 @@ func (icm InteractiveCreateMethod) FetchDevfileAndCreateComponent(co *CreateOpti
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return fetchDevfileFromRegistry(co.devfileMetadata.devfileRegistry, co.devfileMetadata.devfileLink, co.DevfilePath, co.devfileMetadata.componentType, co.contextFlag)
|
||||
return fetchDevfileFromRegistry(co.devfileMetadata.devfileRegistry, co.devfileMetadata.devfileLink, co.DevfilePath, co.devfileMetadata.componentType, co.contextFlag, co.prefClient)
|
||||
}
|
||||
|
||||
func (icm InteractiveCreateMethod) Rollback(devfile, componentContext string) {
|
||||
@@ -121,6 +122,7 @@ func (dcm DirectCreateMethod) FetchDevfileAndCreateComponent(co *CreateOptions,
|
||||
componentName, err = createDefaultComponentName(
|
||||
co.devfileMetadata.componentType,
|
||||
co.contextFlag,
|
||||
co.prefClient,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -131,7 +133,7 @@ func (dcm DirectCreateMethod) FetchDevfileAndCreateComponent(co *CreateOptions,
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return fetchDevfileFromRegistry(co.devfileMetadata.devfileRegistry, co.devfileMetadata.devfileLink, co.DevfilePath, co.devfileMetadata.componentType, co.contextFlag)
|
||||
return fetchDevfileFromRegistry(co.devfileMetadata.devfileRegistry, co.devfileMetadata.devfileLink, co.DevfilePath, co.devfileMetadata.componentType, co.contextFlag, co.prefClient)
|
||||
}
|
||||
|
||||
func (dcm DirectCreateMethod) Rollback(devfile, componentContext string) {
|
||||
@@ -308,7 +310,7 @@ func findDevfileFromRegistry(catalogDevfileList catalog.DevfileComponentTypeList
|
||||
}
|
||||
|
||||
// fetchDevfileFromRegistry fetches the required devfile from the list catalogDevfileList
|
||||
func fetchDevfileFromRegistry(registry catalog.Registry, devfileLink, devfilePath, componentType, componentContext string) (err error) {
|
||||
func fetchDevfileFromRegistry(registry catalog.Registry, devfileLink, devfilePath, componentType, componentContext string, prefClient preference.Client) (err error) {
|
||||
// Download devfile from registry
|
||||
registrySpinner := log.Spinnerf("Creating a devfile component from registry %q", registry.Name)
|
||||
defer registrySpinner.End(false)
|
||||
@@ -321,11 +323,7 @@ func fetchDevfileFromRegistry(registry catalog.Registry, devfileLink, devfilePat
|
||||
URL: registry.URL + devfileLink,
|
||||
}
|
||||
|
||||
secure, err := registryUtil.IsSecure(registry.Name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
secure := registryUtil.IsSecure(prefClient, registry.Name)
|
||||
if secure {
|
||||
var token string
|
||||
token, err = keyring.Get(fmt.Sprintf("%s%s", util.CredentialPrefix, registry.Name), registryUtil.RegistryUser)
|
||||
@@ -335,11 +333,7 @@ func fetchDevfileFromRegistry(registry catalog.Registry, devfileLink, devfilePat
|
||||
params.Token = token
|
||||
}
|
||||
|
||||
cfg, err := preference.New()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
devfileData, err := util.DownloadFileInMemoryWithCache(params, cfg.GetRegistryCacheTime())
|
||||
devfileData, err := util.DownloadFileInMemoryWithCache(params, prefClient.GetRegistryCacheTime())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -376,7 +370,7 @@ func getMetadataForExistingDevfile(co *CreateOptions, args []string) (componentN
|
||||
componentName = devObj.GetMetadataName()
|
||||
} else {
|
||||
// default name
|
||||
componentName, err = createDefaultComponentName(co.devfileMetadata.componentType, co.contextFlag)
|
||||
componentName, err = createDefaultComponentName(co.devfileMetadata.componentType, co.contextFlag, co.prefClient)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
@@ -387,7 +381,7 @@ func getMetadataForExistingDevfile(co *CreateOptions, args []string) (componentN
|
||||
}
|
||||
|
||||
// createDefaultComponentName creates a default unique component name with the help of component context
|
||||
func createDefaultComponentName(componentType string, sourcePath string) (string, error) {
|
||||
func createDefaultComponentName(componentType string, sourcePath string, prefClient preference.Client) (string, error) {
|
||||
var finalSourcePath string
|
||||
var err error
|
||||
if sourcePath != "" {
|
||||
@@ -400,6 +394,7 @@ func createDefaultComponentName(componentType string, sourcePath string) (string
|
||||
}
|
||||
|
||||
return component.GetDefaultComponentName(
|
||||
prefClient,
|
||||
finalSourcePath,
|
||||
componentType,
|
||||
component.ComponentList{},
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"path/filepath"
|
||||
|
||||
"github.com/redhat-developer/odo/pkg/kclient"
|
||||
"github.com/redhat-developer/odo/pkg/preference"
|
||||
"github.com/redhat-developer/odo/pkg/project"
|
||||
scontext "github.com/redhat-developer/odo/pkg/segment/context"
|
||||
|
||||
@@ -74,9 +75,9 @@ type PushOptions struct {
|
||||
|
||||
// NewPushOptions returns new instance of PushOptions
|
||||
// with "default" values for certain values, for example, show is "false"
|
||||
func NewPushOptions(prjClient project.Client) *PushOptions {
|
||||
func NewPushOptions(prjClient project.Client, prefClient preference.Client) *PushOptions {
|
||||
return &PushOptions{
|
||||
CommonPushOptions: NewCommonPushOptions(prjClient),
|
||||
CommonPushOptions: NewCommonPushOptions(prjClient, prefClient),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -231,7 +232,11 @@ func (po *PushOptions) Run() (err error) {
|
||||
func NewCmdPush(name, fullName string) *cobra.Command {
|
||||
// The error is not handled at this point, it will be handled during Context creation
|
||||
kubclient, _ := kclient.New()
|
||||
po := NewPushOptions(project.NewClient(kubclient))
|
||||
prefClient, err := preference.NewClient()
|
||||
if err != nil {
|
||||
odoutil.LogErrorAndExit(err, "unable to set preference, something is wrong with odo, kindly raise an issue at https://github.com/redhat-developer/odo/issues/new?template=Bug.md")
|
||||
}
|
||||
po := NewPushOptions(project.NewClient(kubclient), prefClient)
|
||||
|
||||
var pushCmd = &cobra.Command{
|
||||
Use: fmt.Sprintf("%s [component name]", name),
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"github.com/redhat-developer/odo/pkg/kclient"
|
||||
"github.com/redhat-developer/odo/pkg/odo/cmdline"
|
||||
"github.com/redhat-developer/odo/pkg/preference"
|
||||
"github.com/redhat-developer/odo/pkg/project"
|
||||
"github.com/redhat-developer/odo/pkg/util"
|
||||
|
||||
@@ -56,9 +57,9 @@ type SetOptions struct {
|
||||
}
|
||||
|
||||
// NewSetOptions creates a new SetOptions instance
|
||||
func NewSetOptions(prjClient project.Client) *SetOptions {
|
||||
func NewSetOptions(prjClient project.Client, prefClient preference.Client) *SetOptions {
|
||||
return &SetOptions{
|
||||
PushOptions: clicomponent.NewPushOptions(prjClient),
|
||||
PushOptions: clicomponent.NewPushOptions(prjClient, prefClient),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -180,7 +181,11 @@ func isValidArgumentList(args []string) error {
|
||||
func NewCmdSet(name, fullName string) *cobra.Command {
|
||||
// The error is not handled at this point, it will be handled during Context creation
|
||||
kubclient, _ := kclient.New()
|
||||
o := NewSetOptions(project.NewClient(kubclient))
|
||||
prefClient, err := preference.NewClient()
|
||||
if err != nil {
|
||||
odoutil.LogErrorAndExit(err, "unable to set preference, something is wrong with odo, kindly raise an issue at https://github.com/redhat-developer/odo/issues/new?template=Bug.md")
|
||||
}
|
||||
o := NewSetOptions(project.NewClient(kubclient), prefClient)
|
||||
configurationSetCmd := &cobra.Command{
|
||||
Use: name,
|
||||
Short: "Set a value in odo config file",
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/redhat-developer/odo/pkg/kclient"
|
||||
"github.com/redhat-developer/odo/pkg/preference"
|
||||
"github.com/redhat-developer/odo/pkg/project"
|
||||
"github.com/redhat-developer/odo/pkg/util"
|
||||
|
||||
@@ -51,9 +52,9 @@ type UnsetOptions struct {
|
||||
}
|
||||
|
||||
// NewUnsetOptions creates a new UnsetOptions instance
|
||||
func NewUnsetOptions(prjClient project.Client) *UnsetOptions {
|
||||
func NewUnsetOptions(prjClient project.Client, prefClient preference.Client) *UnsetOptions {
|
||||
return &UnsetOptions{
|
||||
PushOptions: clicomponent.NewPushOptions(prjClient),
|
||||
PushOptions: clicomponent.NewPushOptions(prjClient, prefClient),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -131,7 +132,11 @@ func (o *UnsetOptions) Run() error {
|
||||
func NewCmdUnset(name, fullName string) *cobra.Command {
|
||||
// The error is not handled at this point, it will be handled during Context creation
|
||||
kubclient, _ := kclient.New()
|
||||
o := NewUnsetOptions(project.NewClient(kubclient))
|
||||
prefClient, err := preference.NewClient()
|
||||
if err != nil {
|
||||
odoutil.LogErrorAndExit(err, "unable to set preference, something is wrong with odo, kindly raise an issue at https://github.com/redhat-developer/odo/issues/new?template=Bug.md")
|
||||
}
|
||||
o := NewUnsetOptions(project.NewClient(kubclient), prefClient)
|
||||
configurationUnsetCmd := &cobra.Command{
|
||||
Use: name,
|
||||
Short: "Unset a value in odo config file",
|
||||
|
||||
@@ -6,11 +6,11 @@ import (
|
||||
|
||||
"github.com/redhat-developer/odo/pkg/log"
|
||||
"github.com/redhat-developer/odo/pkg/odo/cmdline"
|
||||
"github.com/redhat-developer/odo/pkg/odo/util"
|
||||
|
||||
"github.com/redhat-developer/odo/pkg/odo/cli/ui"
|
||||
"github.com/redhat-developer/odo/pkg/preference"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/redhat-developer/odo/pkg/odo/genericclioptions"
|
||||
"github.com/spf13/cobra"
|
||||
ktemplates "k8s.io/kubectl/pkg/util/templates"
|
||||
@@ -28,6 +28,9 @@ var (
|
||||
|
||||
// SetOptions encapsulates the options for the command
|
||||
type SetOptions struct {
|
||||
// Clients
|
||||
prefClient preference.Client
|
||||
|
||||
// Flags
|
||||
forceFlag bool
|
||||
|
||||
@@ -37,13 +40,15 @@ type SetOptions struct {
|
||||
}
|
||||
|
||||
// NewSetOptions creates a new SetOptions instance
|
||||
func NewSetOptions() *SetOptions {
|
||||
return &SetOptions{}
|
||||
func NewSetOptions(prefClient preference.Client) *SetOptions {
|
||||
return &SetOptions{
|
||||
prefClient: prefClient,
|
||||
}
|
||||
}
|
||||
|
||||
// Complete completes SetOptions after they've been created
|
||||
func (o *SetOptions) Complete(cmdline cmdline.Cmdline, args []string) (err error) {
|
||||
o.paramName = args[0]
|
||||
o.paramName = strings.ToLower(args[0])
|
||||
o.paramValue = args[1]
|
||||
return
|
||||
}
|
||||
@@ -56,14 +61,8 @@ func (o *SetOptions) Validate() (err error) {
|
||||
// Run contains the logic for the command
|
||||
func (o *SetOptions) Run() (err error) {
|
||||
|
||||
cfg, err := preference.New()
|
||||
|
||||
if err != nil {
|
||||
return errors.Errorf("unable to set preference, something is wrong with odo, kindly raise an issue at https://github.com/redhat-developer/odo/issues/new?template=Bug.md")
|
||||
}
|
||||
|
||||
if !o.forceFlag {
|
||||
if isSet := cfg.IsSet(o.paramName); isSet {
|
||||
if isSet := o.prefClient.IsSet(o.paramName); isSet {
|
||||
// TODO: could add a logic to check if the new value set by the user is not same as the current value
|
||||
if !ui.Proceed(fmt.Sprintf("%v is already set. Do you want to override it in the config", o.paramName)) {
|
||||
log.Info("Aborted by the user")
|
||||
@@ -72,7 +71,7 @@ func (o *SetOptions) Run() (err error) {
|
||||
}
|
||||
}
|
||||
|
||||
err = cfg.SetConfiguration(strings.ToLower(o.paramName), o.paramValue)
|
||||
err = o.prefClient.SetConfiguration(o.paramName, o.paramValue)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -83,14 +82,17 @@ func (o *SetOptions) Run() (err error) {
|
||||
|
||||
// NewCmdSet implements the config set odo command
|
||||
func NewCmdSet(name, fullName string) *cobra.Command {
|
||||
o := NewSetOptions()
|
||||
prefClient, err := preference.NewClient()
|
||||
if err != nil {
|
||||
util.LogErrorAndExit(err, "unable to set preference, something is wrong with odo, kindly raise an issue at https://github.com/redhat-developer/odo/issues/new?template=Bug.md")
|
||||
}
|
||||
o := NewSetOptions(prefClient)
|
||||
preferenceSetCmd := &cobra.Command{
|
||||
Use: name,
|
||||
Short: "Set a value in odo config file",
|
||||
Long: fmt.Sprintf(setLongDesc, preference.FormatSupportedParameters()),
|
||||
Example: func(exampleString, fullName string) string {
|
||||
cfg, _ := preference.New()
|
||||
properties := preference.NewPreferenceList(*cfg)
|
||||
properties := prefClient.NewPreferenceList()
|
||||
for _, property := range properties.Items {
|
||||
value := property.Default
|
||||
if value == "" {
|
||||
|
||||
44
pkg/odo/cli/preference/set_test.go
Normal file
44
pkg/odo/cli/preference/set_test.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package preference
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/redhat-developer/odo/pkg/odo/cmdline"
|
||||
"github.com/redhat-developer/odo/pkg/preference"
|
||||
)
|
||||
|
||||
func TestSet(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
prefClient := preference.NewMockClient(ctrl)
|
||||
opts := NewSetOptions(prefClient)
|
||||
opts.forceFlag = true
|
||||
|
||||
cmdline := cmdline.NewMockCmdline(ctrl)
|
||||
|
||||
args := []string{"Arg1", "Arg2"}
|
||||
err := opts.Complete(cmdline, args)
|
||||
if err != nil {
|
||||
t.Errorf("Expected nil error, got %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
if opts.paramName != "arg1" {
|
||||
t.Errorf("Expected paramName %q, got %q", "arg1", opts.paramName)
|
||||
}
|
||||
if opts.paramValue != "Arg2" {
|
||||
t.Errorf("Expected paramValue %q, got %q", "Arg2", opts.paramName)
|
||||
}
|
||||
|
||||
err = opts.Validate()
|
||||
if err != nil {
|
||||
t.Errorf("Expected nil error, got %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
prefClient.EXPECT().SetConfiguration("arg1", "Arg2")
|
||||
err = opts.Run()
|
||||
if err != nil {
|
||||
t.Errorf("Expected nil error, got %s", err)
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"github.com/redhat-developer/odo/pkg/odo/cli/ui"
|
||||
"github.com/redhat-developer/odo/pkg/odo/cmdline"
|
||||
"github.com/redhat-developer/odo/pkg/odo/genericclioptions"
|
||||
"github.com/redhat-developer/odo/pkg/odo/util"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/redhat-developer/odo/pkg/preference"
|
||||
@@ -28,6 +29,9 @@ var (
|
||||
|
||||
// UnsetOptions encapsulates the options for the command
|
||||
type UnsetOptions struct {
|
||||
// Clients
|
||||
prefClient preference.Client
|
||||
|
||||
//Parameters
|
||||
paramName string
|
||||
|
||||
@@ -36,13 +40,15 @@ type UnsetOptions struct {
|
||||
}
|
||||
|
||||
// NewUnsetOptions creates a new UnsetOptions instance
|
||||
func NewUnsetOptions() *UnsetOptions {
|
||||
return &UnsetOptions{}
|
||||
func NewUnsetOptions(prefClient preference.Client) *UnsetOptions {
|
||||
return &UnsetOptions{
|
||||
prefClient: prefClient,
|
||||
}
|
||||
}
|
||||
|
||||
// Complete completes UnsetOptions after they've been created
|
||||
func (o *UnsetOptions) Complete(cmdline cmdline.Cmdline, args []string) (err error) {
|
||||
o.paramName = args[0]
|
||||
o.paramName = strings.ToLower(args[0])
|
||||
return
|
||||
}
|
||||
|
||||
@@ -54,15 +60,9 @@ func (o *UnsetOptions) Validate() (err error) {
|
||||
// Run contains the logic for the command
|
||||
func (o *UnsetOptions) Run() (err error) {
|
||||
|
||||
cfg, err := preference.New()
|
||||
|
||||
if err != nil {
|
||||
return errors.Errorf("something is wrong with odo, kindly raise an issue at https://github.com/redhat-developer/odo/issues/new?template=Bug.md")
|
||||
}
|
||||
|
||||
if !o.forceFlag {
|
||||
|
||||
if isSet := cfg.IsSet(o.paramName); isSet {
|
||||
if isSet := o.prefClient.IsSet(o.paramName); isSet {
|
||||
if !ui.Proceed(fmt.Sprintf("Do you want to unset %s in the preference", o.paramName)) {
|
||||
log.Infof("Aborted by the user")
|
||||
return nil
|
||||
@@ -72,7 +72,7 @@ func (o *UnsetOptions) Run() (err error) {
|
||||
}
|
||||
}
|
||||
|
||||
err = cfg.DeleteConfiguration(strings.ToLower(o.paramName))
|
||||
err = o.prefClient.DeleteConfiguration(o.paramName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -84,7 +84,11 @@ func (o *UnsetOptions) Run() (err error) {
|
||||
|
||||
// NewCmdUnset implements the preference unset odo command
|
||||
func NewCmdUnset(name, fullName string) *cobra.Command {
|
||||
o := NewUnsetOptions()
|
||||
prefClient, err := preference.NewClient()
|
||||
if err != nil {
|
||||
util.LogErrorAndExit(err, "unable to set preference, something is wrong with odo, kindly raise an issue at https://github.com/redhat-developer/odo/issues/new?template=Bug.md")
|
||||
}
|
||||
o := NewUnsetOptions(prefClient)
|
||||
preferenceUnsetCmd := &cobra.Command{
|
||||
Use: name,
|
||||
Short: "Unset a value in odo preference file",
|
||||
|
||||
80
pkg/odo/cli/preference/unset_test.go
Normal file
80
pkg/odo/cli/preference/unset_test.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package preference
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/redhat-developer/odo/pkg/odo/cmdline"
|
||||
"github.com/redhat-developer/odo/pkg/preference"
|
||||
)
|
||||
|
||||
func TestUnsetForce(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
forceFlag bool
|
||||
exists bool
|
||||
expectedRunErr string
|
||||
}{
|
||||
{
|
||||
name: "force && parameter exists",
|
||||
forceFlag: true,
|
||||
exists: true,
|
||||
},
|
||||
{
|
||||
name: "no force and parameter not exists",
|
||||
forceFlag: false,
|
||||
exists: false,
|
||||
expectedRunErr: "preference already unset",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
prefClient := preference.NewMockClient(ctrl)
|
||||
opts := NewUnsetOptions(prefClient)
|
||||
opts.forceFlag = tt.forceFlag
|
||||
|
||||
cmdline := cmdline.NewMockCmdline(ctrl)
|
||||
|
||||
args := []string{"Arg1"}
|
||||
err := opts.Complete(cmdline, args)
|
||||
if err != nil {
|
||||
t.Errorf("Expected nil error, got %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
if opts.paramName != "arg1" {
|
||||
t.Errorf("Expected paramName %q, got %q", "arg1", opts.paramName)
|
||||
}
|
||||
|
||||
err = opts.Validate()
|
||||
if err != nil {
|
||||
t.Errorf("Expected nil error, got %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
if tt.exists || tt.forceFlag {
|
||||
prefClient.EXPECT().DeleteConfiguration("arg1")
|
||||
}
|
||||
if !tt.forceFlag {
|
||||
prefClient.EXPECT().IsSet("arg1").Return(tt.exists)
|
||||
}
|
||||
err = opts.Run()
|
||||
|
||||
if err == nil && tt.expectedRunErr != "" {
|
||||
t.Errorf("Expected %v, got no error", tt.expectedRunErr)
|
||||
return
|
||||
}
|
||||
if err != nil && tt.expectedRunErr == "" {
|
||||
t.Errorf("Expected no error, got %v", err.Error())
|
||||
return
|
||||
}
|
||||
if err != nil && tt.expectedRunErr != "" && !strings.Contains(err.Error(), tt.expectedRunErr) {
|
||||
t.Errorf("Expected error %v, got %v", tt.expectedRunErr, err.Error())
|
||||
return
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"github.com/redhat-developer/odo/pkg/machineoutput"
|
||||
"github.com/redhat-developer/odo/pkg/odo/cmdline"
|
||||
"github.com/redhat-developer/odo/pkg/odo/genericclioptions"
|
||||
"github.com/redhat-developer/odo/pkg/odo/util"
|
||||
"github.com/redhat-developer/odo/pkg/preference"
|
||||
"github.com/spf13/cobra"
|
||||
ktemplates "k8s.io/kubectl/pkg/util/templates"
|
||||
@@ -22,11 +23,16 @@ var viewExample = ktemplates.Examples(`# For viewing the current preference valu
|
||||
`)
|
||||
|
||||
// ViewOptions encapsulates the options for the command
|
||||
type ViewOptions struct{}
|
||||
type ViewOptions struct {
|
||||
// Clients
|
||||
prefClient preference.Client
|
||||
}
|
||||
|
||||
// NewViewOptions creates a new ViewOptions instance
|
||||
func NewViewOptions() *ViewOptions {
|
||||
return &ViewOptions{}
|
||||
func NewViewOptions(prefClient preference.Client) *ViewOptions {
|
||||
return &ViewOptions{
|
||||
prefClient: prefClient,
|
||||
}
|
||||
}
|
||||
|
||||
// Complete completes ViewOptions after they've been created
|
||||
@@ -42,27 +48,21 @@ func (o *ViewOptions) Validate() (err error) {
|
||||
// Run contains the logic for the command
|
||||
func (o *ViewOptions) Run() (err error) {
|
||||
|
||||
cfg, err := preference.New()
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if log.IsJSON() {
|
||||
prefDef := preference.NewPreferenceList(*cfg)
|
||||
prefDef := o.prefClient.NewPreferenceList()
|
||||
machineoutput.OutputSuccess(prefDef)
|
||||
|
||||
return
|
||||
}
|
||||
w := tabwriter.NewWriter(os.Stdout, 5, 2, 2, ' ', tabwriter.TabIndent)
|
||||
fmt.Fprintln(w, "PARAMETER", "\t", "CURRENT_VALUE")
|
||||
fmt.Fprintln(w, "UpdateNotification", "\t", showBlankIfNil(cfg.OdoSettings.UpdateNotification))
|
||||
fmt.Fprintln(w, "NamePrefix", "\t", showBlankIfNil(cfg.OdoSettings.NamePrefix))
|
||||
fmt.Fprintln(w, "Timeout", "\t", showBlankIfNil(cfg.OdoSettings.Timeout))
|
||||
fmt.Fprintln(w, "BuildTimeout", "\t", showBlankIfNil(cfg.OdoSettings.BuildTimeout))
|
||||
fmt.Fprintln(w, "PushTimeout", "\t", showBlankIfNil(cfg.OdoSettings.PushTimeout))
|
||||
fmt.Fprintln(w, "Ephemeral", "\t", showBlankIfNil(cfg.OdoSettings.Ephemeral))
|
||||
fmt.Fprintln(w, "ConsentTelemetry", "\t", showBlankIfNil(cfg.OdoSettings.ConsentTelemetry))
|
||||
fmt.Fprintln(w, "UpdateNotification", "\t", showBlankIfNil(o.prefClient.UpdateNotification()))
|
||||
fmt.Fprintln(w, "NamePrefix", "\t", showBlankIfNil(o.prefClient.NamePrefix()))
|
||||
fmt.Fprintln(w, "Timeout", "\t", showBlankIfNil(o.prefClient.Timeout()))
|
||||
fmt.Fprintln(w, "BuildTimeout", "\t", showBlankIfNil(o.prefClient.BuildTimeout()))
|
||||
fmt.Fprintln(w, "PushTimeout", "\t", showBlankIfNil(o.prefClient.PushTimeout()))
|
||||
fmt.Fprintln(w, "Ephemeral", "\t", showBlankIfNil(o.prefClient.EphemeralSourceVolume()))
|
||||
fmt.Fprintln(w, "ConsentTelemetry", "\t", showBlankIfNil(o.prefClient.ConsentTelemetry()))
|
||||
|
||||
w.Flush()
|
||||
return
|
||||
@@ -86,7 +86,11 @@ func showBlankIfNil(intf interface{}) interface{} {
|
||||
|
||||
// NewCmdView implements the config view odo command
|
||||
func NewCmdView(name, fullName string) *cobra.Command {
|
||||
o := NewViewOptions()
|
||||
prefClient, err := preference.NewClient()
|
||||
if err != nil {
|
||||
util.LogErrorAndExit(err, "unable to set preference, something is wrong with odo, kindly raise an issue at https://github.com/redhat-developer/odo/issues/new?template=Bug.md")
|
||||
}
|
||||
o := NewViewOptions(prefClient)
|
||||
preferenceViewCmd := &cobra.Command{
|
||||
Use: name,
|
||||
Short: "View current preference values",
|
||||
|
||||
46
pkg/odo/cli/preference/view_test.go
Normal file
46
pkg/odo/cli/preference/view_test.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package preference
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/golang/mock/gomock"
|
||||
|
||||
"github.com/redhat-developer/odo/pkg/odo/cmdline"
|
||||
"github.com/redhat-developer/odo/pkg/preference"
|
||||
|
||||
"k8s.io/utils/pointer"
|
||||
)
|
||||
|
||||
func TestView(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
prefClient := preference.NewMockClient(ctrl)
|
||||
opts := NewViewOptions(prefClient)
|
||||
|
||||
cmdline := cmdline.NewMockCmdline(ctrl)
|
||||
|
||||
args := []string{}
|
||||
err := opts.Complete(cmdline, args)
|
||||
if err != nil {
|
||||
t.Errorf("Expected nil error, got %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
err = opts.Validate()
|
||||
if err != nil {
|
||||
t.Errorf("Expected nil error, got %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
prefClient.EXPECT().UpdateNotification().Return(pointer.Bool(false))
|
||||
prefClient.EXPECT().NamePrefix().Return(pointer.String("aprefix"))
|
||||
prefClient.EXPECT().Timeout().Return(pointer.Int(10))
|
||||
prefClient.EXPECT().BuildTimeout().Return(pointer.Int(10))
|
||||
prefClient.EXPECT().PushTimeout().Return(pointer.Int(10))
|
||||
prefClient.EXPECT().EphemeralSourceVolume().Return(pointer.Bool(false))
|
||||
prefClient.EXPECT().ConsentTelemetry().Return(pointer.Bool(false))
|
||||
|
||||
err = opts.Run()
|
||||
if err != nil {
|
||||
t.Errorf(`Expected nil error, got %s`, err)
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,7 @@ import (
|
||||
// odo packages
|
||||
"github.com/redhat-developer/odo/pkg/log"
|
||||
"github.com/redhat-developer/odo/pkg/odo/genericclioptions"
|
||||
odoutil "github.com/redhat-developer/odo/pkg/odo/util"
|
||||
"github.com/redhat-developer/odo/pkg/preference"
|
||||
"github.com/redhat-developer/odo/pkg/util"
|
||||
)
|
||||
@@ -35,6 +36,9 @@ var (
|
||||
|
||||
// AddOptions encapsulates the options for the "odo registry add" command
|
||||
type AddOptions struct {
|
||||
// Clients
|
||||
prefClient preference.Client
|
||||
|
||||
// Parameters
|
||||
registryName string
|
||||
registryURL string
|
||||
@@ -47,8 +51,10 @@ type AddOptions struct {
|
||||
}
|
||||
|
||||
// NewAddOptions creates a new AddOptions instance
|
||||
func NewAddOptions() *AddOptions {
|
||||
return &AddOptions{}
|
||||
func NewAddOptions(prefClient preference.Client) *AddOptions {
|
||||
return &AddOptions{
|
||||
prefClient: prefClient,
|
||||
}
|
||||
}
|
||||
|
||||
// Complete completes AddOptions after they've been created
|
||||
@@ -79,11 +85,7 @@ func (o *AddOptions) Run() (err error) {
|
||||
isSecure = true
|
||||
}
|
||||
|
||||
cfg, err := preference.New()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "unable to add registry")
|
||||
}
|
||||
err = cfg.RegistryHandler(o.operation, o.registryName, o.registryURL, false, isSecure)
|
||||
err = o.prefClient.RegistryHandler(o.operation, o.registryName, o.registryURL, false, isSecure)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -101,7 +103,11 @@ func (o *AddOptions) Run() (err error) {
|
||||
|
||||
// NewCmdAdd implements the "odo registry add" command
|
||||
func NewCmdAdd(name, fullName string) *cobra.Command {
|
||||
o := NewAddOptions()
|
||||
prefClient, err := preference.NewClient()
|
||||
if err != nil {
|
||||
odoutil.LogErrorAndExit(err, "unable to set preference, something is wrong with odo, kindly raise an issue at https://github.com/redhat-developer/odo/issues/new?template=Bug.md")
|
||||
}
|
||||
o := NewAddOptions(prefClient)
|
||||
registryAddCmd := &cobra.Command{
|
||||
Use: fmt.Sprintf("%s <registry name> <registry URL>", name),
|
||||
Short: addLongDesc,
|
||||
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
registryUtil "github.com/redhat-developer/odo/pkg/odo/cli/registry/util"
|
||||
"github.com/redhat-developer/odo/pkg/odo/cmdline"
|
||||
"github.com/redhat-developer/odo/pkg/odo/genericclioptions"
|
||||
odoutil "github.com/redhat-developer/odo/pkg/odo/util"
|
||||
"github.com/redhat-developer/odo/pkg/preference"
|
||||
"github.com/redhat-developer/odo/pkg/util"
|
||||
)
|
||||
@@ -31,6 +32,9 @@ var (
|
||||
|
||||
// DeleteOptions encapsulates the options for the "odo registry delete" command
|
||||
type DeleteOptions struct {
|
||||
// Clients
|
||||
prefClient preference.Client
|
||||
|
||||
// Parameters
|
||||
registryName string
|
||||
|
||||
@@ -43,8 +47,10 @@ type DeleteOptions struct {
|
||||
}
|
||||
|
||||
// NewDeleteOptions creates a new DeleteOptions instance
|
||||
func NewDeleteOptions() *DeleteOptions {
|
||||
return &DeleteOptions{}
|
||||
func NewDeleteOptions(prefClient preference.Client) *DeleteOptions {
|
||||
return &DeleteOptions{
|
||||
prefClient: prefClient,
|
||||
}
|
||||
}
|
||||
|
||||
// Complete completes DeleteOptions after they've been created
|
||||
@@ -63,15 +69,8 @@ func (o *DeleteOptions) Validate() (err error) {
|
||||
|
||||
// Run contains the logic for "odo registry delete" command
|
||||
func (o *DeleteOptions) Run() (err error) {
|
||||
isSecure, err := registryUtil.IsSecure(o.registryName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cfg, err := preference.New()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "unable to delete registry")
|
||||
}
|
||||
err = cfg.RegistryHandler(o.operation, o.registryName, o.registryURL, o.forceFlag, false)
|
||||
isSecure := registryUtil.IsSecure(o.prefClient, o.registryName)
|
||||
err = o.prefClient.RegistryHandler(o.operation, o.registryName, o.registryURL, o.forceFlag, false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -88,7 +87,11 @@ func (o *DeleteOptions) Run() (err error) {
|
||||
|
||||
// NewCmdDelete implements the "odo registry delete" command
|
||||
func NewCmdDelete(name, fullName string) *cobra.Command {
|
||||
o := NewDeleteOptions()
|
||||
prefClient, err := preference.NewClient()
|
||||
if err != nil {
|
||||
odoutil.LogErrorAndExit(err, "unable to set preference, something is wrong with odo, kindly raise an issue at https://github.com/redhat-developer/odo/issues/new?template=Bug.md")
|
||||
}
|
||||
o := NewDeleteOptions(prefClient)
|
||||
registryDeleteCmd := &cobra.Command{
|
||||
Use: fmt.Sprintf("%s <registry name>", name),
|
||||
Short: deleteLongDesc,
|
||||
|
||||
@@ -17,6 +17,7 @@ import (
|
||||
util "github.com/redhat-developer/odo/pkg/odo/cli/registry/util"
|
||||
"github.com/redhat-developer/odo/pkg/odo/cmdline"
|
||||
"github.com/redhat-developer/odo/pkg/odo/genericclioptions"
|
||||
odoutil "github.com/redhat-developer/odo/pkg/odo/util"
|
||||
"github.com/redhat-developer/odo/pkg/preference"
|
||||
)
|
||||
|
||||
@@ -33,12 +34,17 @@ var (
|
||||
|
||||
// ListOptions encapsulates the options for "odo registry list" command
|
||||
type ListOptions struct {
|
||||
// Clients
|
||||
prefClient preference.Client
|
||||
|
||||
printGitRegistryDeprecationWarning bool
|
||||
}
|
||||
|
||||
// NewListOptions creates a new ListOptions instance
|
||||
func NewListOptions() *ListOptions {
|
||||
return &ListOptions{}
|
||||
func NewListOptions(prefClient preference.Client) *ListOptions {
|
||||
return &ListOptions{
|
||||
prefClient: prefClient,
|
||||
}
|
||||
}
|
||||
|
||||
// Complete completes ListOptions after they've been created
|
||||
@@ -53,12 +59,7 @@ func (o *ListOptions) Validate() (err error) {
|
||||
|
||||
// Run contains the logic for "odo registry list" command
|
||||
func (o *ListOptions) Run() (err error) {
|
||||
cfg, err := preference.New()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
registryList := cfg.OdoSettings.RegistryList
|
||||
registryList := o.prefClient.RegistryList()
|
||||
if registryList == nil || len(*registryList) == 0 {
|
||||
return fmt.Errorf("No devfile registries added to the configuration. Refer `odo registry add -h` to add one")
|
||||
}
|
||||
@@ -100,7 +101,11 @@ func (o *ListOptions) printRegistryList(w io.Writer, registryList *[]preference.
|
||||
|
||||
// NewCmdList implements the "odo registry list" command
|
||||
func NewCmdList(name, fullName string) *cobra.Command {
|
||||
o := NewListOptions()
|
||||
prefClient, err := preference.NewClient()
|
||||
if err != nil {
|
||||
odoutil.LogErrorAndExit(err, "unable to set preference, something is wrong with odo, kindly raise an issue at https://github.com/redhat-developer/odo/issues/new?template=Bug.md")
|
||||
}
|
||||
o := NewListOptions(prefClient)
|
||||
registryListCmd := &cobra.Command{
|
||||
Use: name,
|
||||
Short: listDesc,
|
||||
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
registryUtil "github.com/redhat-developer/odo/pkg/odo/cli/registry/util"
|
||||
"github.com/redhat-developer/odo/pkg/odo/cmdline"
|
||||
"github.com/redhat-developer/odo/pkg/odo/genericclioptions"
|
||||
odoutil "github.com/redhat-developer/odo/pkg/odo/util"
|
||||
"github.com/redhat-developer/odo/pkg/preference"
|
||||
"github.com/redhat-developer/odo/pkg/util"
|
||||
)
|
||||
@@ -31,6 +32,9 @@ var (
|
||||
|
||||
// UpdateOptions encapsulates the options for the "odo registry update" command
|
||||
type UpdateOptions struct {
|
||||
// Clients
|
||||
prefClient preference.Client
|
||||
|
||||
// Parameters
|
||||
registryName string
|
||||
registryURL string
|
||||
@@ -44,8 +48,10 @@ type UpdateOptions struct {
|
||||
}
|
||||
|
||||
// NewUpdateOptions creates a new UpdateOptions instance
|
||||
func NewUpdateOptions() *UpdateOptions {
|
||||
return &UpdateOptions{}
|
||||
func NewUpdateOptions(prefClient preference.Client) *UpdateOptions {
|
||||
return &UpdateOptions{
|
||||
prefClient: prefClient,
|
||||
}
|
||||
}
|
||||
|
||||
// Complete completes UpdateOptions after they've been created
|
||||
@@ -74,11 +80,7 @@ func (o *UpdateOptions) Run() (err error) {
|
||||
secureBeforeUpdate := false
|
||||
secureAfterUpdate := false
|
||||
|
||||
secure, err := registryUtil.IsSecure(o.registryName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
secure := registryUtil.IsSecure(o.prefClient, o.registryName)
|
||||
if secure {
|
||||
secureBeforeUpdate = true
|
||||
}
|
||||
@@ -87,11 +89,7 @@ func (o *UpdateOptions) Run() (err error) {
|
||||
secureAfterUpdate = true
|
||||
}
|
||||
|
||||
cfg, err := preference.New()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "unable to update registry")
|
||||
}
|
||||
err = cfg.RegistryHandler(o.operation, o.registryName, o.registryURL, o.forceFlag, secureAfterUpdate)
|
||||
err = o.prefClient.RegistryHandler(o.operation, o.registryName, o.registryURL, o.forceFlag, secureAfterUpdate)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -113,7 +111,11 @@ func (o *UpdateOptions) Run() (err error) {
|
||||
|
||||
// NewCmdUpdate implements the "odo registry update" command
|
||||
func NewCmdUpdate(name, fullName string) *cobra.Command {
|
||||
o := NewUpdateOptions()
|
||||
prefClient, err := preference.NewClient()
|
||||
if err != nil {
|
||||
odoutil.LogErrorAndExit(err, "unable to set preference, something is wrong with odo, kindly raise an issue at https://github.com/redhat-developer/odo/issues/new?template=Bug.md")
|
||||
}
|
||||
o := NewUpdateOptions(prefClient)
|
||||
registryUpdateCmd := &cobra.Command{
|
||||
Use: fmt.Sprintf("%s <registry name> <registry URL>", name),
|
||||
Short: updateLongDesc,
|
||||
|
||||
@@ -14,15 +14,10 @@ const (
|
||||
)
|
||||
|
||||
// IsSecure checks if the registry is secure
|
||||
func IsSecure(registryName string) (bool, error) {
|
||||
cfg, err := preference.New()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
func IsSecure(prefClient preference.Client, registryName string) bool {
|
||||
isSecure := false
|
||||
if cfg.OdoSettings.RegistryList != nil {
|
||||
for _, registry := range *cfg.OdoSettings.RegistryList {
|
||||
if prefClient.RegistryList() != nil {
|
||||
for _, registry := range *prefClient.RegistryList() {
|
||||
if registry.Name == registryName && registry.Secure {
|
||||
isSecure = true
|
||||
break
|
||||
@@ -30,7 +25,7 @@ func IsSecure(registryName string) (bool, error) {
|
||||
}
|
||||
}
|
||||
|
||||
return isSecure, nil
|
||||
return isSecure
|
||||
}
|
||||
|
||||
func IsGitBasedRegistry(url string) bool {
|
||||
|
||||
@@ -53,7 +53,7 @@ func TestIsSecure(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
cfg, err := preference.New()
|
||||
cfg, err := preference.NewClient()
|
||||
if err != nil {
|
||||
t.Errorf("Unable to get preference file with error: %v", err)
|
||||
}
|
||||
@@ -62,10 +62,7 @@ func TestIsSecure(t *testing.T) {
|
||||
t.Errorf("Unable to add registry to preference file with error: %v", err)
|
||||
}
|
||||
|
||||
got, err := IsSecure(tt.registryName)
|
||||
if err != nil {
|
||||
t.Errorf("Unable to check if the registry is secure or not")
|
||||
}
|
||||
got := IsSecure(cfg, tt.registryName)
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("Got: %t, want %t", got, tt.want)
|
||||
}
|
||||
|
||||
@@ -5,9 +5,8 @@ import (
|
||||
|
||||
"github.com/redhat-developer/odo/pkg/odo/cmdline"
|
||||
"github.com/redhat-developer/odo/pkg/odo/genericclioptions"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/redhat-developer/odo/pkg/preference"
|
||||
|
||||
"github.com/redhat-developer/odo/pkg/segment"
|
||||
"github.com/spf13/cobra"
|
||||
"k8s.io/klog"
|
||||
@@ -16,11 +15,14 @@ import (
|
||||
const RecommendedCommandName = "telemetry"
|
||||
|
||||
type TelemetryOptions struct {
|
||||
prefClient preference.Client
|
||||
telemetryData segment.TelemetryData
|
||||
}
|
||||
|
||||
func NewTelemetryOptions() *TelemetryOptions {
|
||||
return &TelemetryOptions{}
|
||||
func NewTelemetryOptions(prefClient preference.Client) *TelemetryOptions {
|
||||
return &TelemetryOptions{
|
||||
prefClient: prefClient,
|
||||
}
|
||||
}
|
||||
|
||||
func (o *TelemetryOptions) Complete(cmdline cmdline.Cmdline, args []string) (err error) {
|
||||
@@ -33,16 +35,11 @@ func (o *TelemetryOptions) Validate() (err error) {
|
||||
}
|
||||
|
||||
func (o *TelemetryOptions) Run() (err error) {
|
||||
cfg, err := preference.New()
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "unable to upload telemetry data")
|
||||
}
|
||||
|
||||
if !segment.IsTelemetryEnabled(cfg) {
|
||||
if !segment.IsTelemetryEnabled(o.prefClient) {
|
||||
return nil
|
||||
}
|
||||
|
||||
segmentClient, err := segment.NewClient(cfg)
|
||||
segmentClient, err := segment.NewClient(o.prefClient)
|
||||
if err != nil {
|
||||
klog.V(4).Infof("Cannot create a segment client. Will not send any data: %q", err)
|
||||
}
|
||||
@@ -57,7 +54,8 @@ func (o *TelemetryOptions) Run() (err error) {
|
||||
}
|
||||
|
||||
func NewCmdTelemetry(name string) *cobra.Command {
|
||||
o := NewTelemetryOptions()
|
||||
prefClient, _ := preference.NewClient()
|
||||
o := NewTelemetryOptions(prefClient)
|
||||
telemetryCmd := &cobra.Command{
|
||||
Use: name,
|
||||
Short: "Collect and upload usage data.",
|
||||
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
"github.com/redhat-developer/odo/pkg/odo/genericclioptions"
|
||||
odoutil "github.com/redhat-developer/odo/pkg/odo/util"
|
||||
"github.com/redhat-developer/odo/pkg/odo/util/completion"
|
||||
"github.com/redhat-developer/odo/pkg/preference"
|
||||
"github.com/redhat-developer/odo/pkg/project"
|
||||
"github.com/redhat-developer/odo/pkg/url"
|
||||
|
||||
@@ -77,8 +78,8 @@ type CreateOptions struct {
|
||||
}
|
||||
|
||||
// NewURLCreateOptions creates a new CreateOptions instance
|
||||
func NewURLCreateOptions(prjClient project.Client) *CreateOptions {
|
||||
return &CreateOptions{PushOptions: clicomponent.NewPushOptions(prjClient)}
|
||||
func NewURLCreateOptions(prjClient project.Client, prefClient preference.Client) *CreateOptions {
|
||||
return &CreateOptions{PushOptions: clicomponent.NewPushOptions(prjClient, prefClient)}
|
||||
}
|
||||
|
||||
// Complete completes CreateOptions after they've been Created
|
||||
@@ -197,7 +198,11 @@ func (o *CreateOptions) Run() (err error) {
|
||||
func NewCmdURLCreate(name, fullName string) *cobra.Command {
|
||||
// The error is not handled at this point, it will be handled during Context creation
|
||||
kubclient, _ := kclient.New()
|
||||
o := NewURLCreateOptions(project.NewClient(kubclient))
|
||||
prefClient, err := preference.NewClient()
|
||||
if err != nil {
|
||||
odoutil.LogErrorAndExit(err, "unable to set preference, something is wrong with odo, kindly raise an issue at https://github.com/redhat-developer/odo/issues/new?template=Bug.md")
|
||||
}
|
||||
o := NewURLCreateOptions(project.NewClient(kubclient), prefClient)
|
||||
urlCreateCmd := &cobra.Command{
|
||||
Use: name + " [url name]",
|
||||
Short: urlCreateShortDesc,
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"github.com/redhat-developer/odo/pkg/odo/genericclioptions"
|
||||
odoutil "github.com/redhat-developer/odo/pkg/odo/util"
|
||||
"github.com/redhat-developer/odo/pkg/odo/util/completion"
|
||||
"github.com/redhat-developer/odo/pkg/preference"
|
||||
"github.com/redhat-developer/odo/pkg/project"
|
||||
"github.com/spf13/cobra"
|
||||
ktemplates "k8s.io/kubectl/pkg/util/templates"
|
||||
@@ -40,8 +41,8 @@ type DeleteOptions struct {
|
||||
}
|
||||
|
||||
// NewURLDeleteOptions creates a new DeleteOptions instance
|
||||
func NewURLDeleteOptions(prjClient project.Client) *DeleteOptions {
|
||||
return &DeleteOptions{PushOptions: clicomponent.NewPushOptions(prjClient)}
|
||||
func NewURLDeleteOptions(prjClient project.Client, prefClient preference.Client) *DeleteOptions {
|
||||
return &DeleteOptions{PushOptions: clicomponent.NewPushOptions(prjClient, prefClient)}
|
||||
}
|
||||
|
||||
// Complete completes DeleteOptions after they've been Deleted
|
||||
@@ -108,7 +109,11 @@ func (o *DeleteOptions) Run() (err error) {
|
||||
func NewCmdURLDelete(name, fullName string) *cobra.Command {
|
||||
// The error is not handled at this point, it will be handled during Context creation
|
||||
kubclient, _ := kclient.New()
|
||||
o := NewURLDeleteOptions(project.NewClient(kubclient))
|
||||
prefClient, err := preference.NewClient()
|
||||
if err != nil {
|
||||
odoutil.LogErrorAndExit(err, "unable to set preference, something is wrong with odo, kindly raise an issue at https://github.com/redhat-developer/odo/issues/new?template=Bug.md")
|
||||
}
|
||||
o := NewURLDeleteOptions(project.NewClient(kubclient), prefClient)
|
||||
urlDeleteCmd := &cobra.Command{
|
||||
Use: name + " [url name]",
|
||||
Short: urlDeleteShortDesc,
|
||||
|
||||
@@ -4,10 +4,13 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/redhat-developer/odo/pkg/kclient"
|
||||
"github.com/redhat-developer/odo/pkg/odo/cmdline"
|
||||
"github.com/redhat-developer/odo/pkg/odo/genericclioptions"
|
||||
"github.com/redhat-developer/odo/pkg/preference"
|
||||
odoversion "github.com/redhat-developer/odo/pkg/version"
|
||||
|
||||
"github.com/redhat-developer/odo/pkg/notify"
|
||||
@@ -37,11 +40,15 @@ type VersionOptions struct {
|
||||
|
||||
// serverInfo contains the remote server information if the user asked for it, nil otherwise
|
||||
serverInfo *kclient.ServerInfo
|
||||
|
||||
prefClient preference.Client
|
||||
}
|
||||
|
||||
// NewVersionOptions creates a new VersionOptions instance
|
||||
func NewVersionOptions() *VersionOptions {
|
||||
return &VersionOptions{}
|
||||
func NewVersionOptions(prefClient preference.Client) *VersionOptions {
|
||||
return &VersionOptions{
|
||||
prefClient: prefClient,
|
||||
}
|
||||
}
|
||||
|
||||
// Complete completes VersionOptions after they have been created
|
||||
@@ -49,8 +56,18 @@ func (o *VersionOptions) Complete(cmdline cmdline.Cmdline, args []string) (err e
|
||||
if !o.clientFlag {
|
||||
// Let's fetch the info about the server, ignoring errors
|
||||
client, err := kclient.New()
|
||||
|
||||
if err == nil {
|
||||
o.serverInfo, _ = client.GetServerVersion()
|
||||
// checking the value of timeout in preference
|
||||
var timeout time.Duration
|
||||
if o.prefClient != nil {
|
||||
timeout = time.Duration(o.prefClient.GetTimeout()) * time.Second
|
||||
} else {
|
||||
// the default timeout will be used
|
||||
// when the value is not readable from preference
|
||||
timeout = preference.DefaultTimeout * time.Second
|
||||
}
|
||||
o.serverInfo, _ = client.GetServerVersion(timeout)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
@@ -93,7 +110,11 @@ func (o *VersionOptions) Run() (err error) {
|
||||
|
||||
// NewCmdVersion implements the version odo command
|
||||
func NewCmdVersion(name, fullName string) *cobra.Command {
|
||||
o := NewVersionOptions()
|
||||
prefClient, err := preference.NewClient()
|
||||
if err != nil {
|
||||
klog.V(3).Info(errors.Wrap(err, "unable to read preference file"))
|
||||
}
|
||||
o := NewVersionOptions(prefClient)
|
||||
// versionCmd represents the version command
|
||||
var versionCmd = &cobra.Command{
|
||||
Use: name,
|
||||
|
||||
@@ -41,7 +41,7 @@ type Runnable interface {
|
||||
func GenericRun(o Runnable, cmd *cobra.Command, args []string) {
|
||||
var err error
|
||||
startTime := time.Now()
|
||||
cfg, _ := preference.New()
|
||||
cfg, _ := preference.NewClient()
|
||||
disableTelemetry, _ := strconv.ParseBool(os.Getenv(segment.DisableTelemetryEnv))
|
||||
|
||||
// Prompt the user to consent for telemetry if a value is not set already
|
||||
|
||||
457
pkg/preference/implem.go
Normal file
457
pkg/preference/implem.go
Normal file
@@ -0,0 +1,457 @@
|
||||
package preference
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/klog"
|
||||
|
||||
"github.com/redhat-developer/odo/pkg/log"
|
||||
"github.com/redhat-developer/odo/pkg/odo/cli/ui"
|
||||
"github.com/redhat-developer/odo/pkg/util"
|
||||
)
|
||||
|
||||
// odoSettings holds all odo specific configurations
|
||||
// these configurations are applicable across the odo components
|
||||
type odoSettings struct {
|
||||
// Controls if an update notification is shown or not
|
||||
UpdateNotification *bool `yaml:"UpdateNotification,omitempty"`
|
||||
|
||||
// Holds the prefix part of generated random application name
|
||||
NamePrefix *string `yaml:"NamePrefix,omitempty"`
|
||||
|
||||
// Timeout for OpenShift server connection check
|
||||
Timeout *int `yaml:"Timeout,omitempty"`
|
||||
|
||||
// BuildTimeout for OpenShift build timeout check
|
||||
BuildTimeout *int `yaml:"BuildTimeout,omitempty"`
|
||||
|
||||
// PushTimeout for OpenShift pod timeout check
|
||||
PushTimeout *int `yaml:"PushTimeout,omitempty"`
|
||||
|
||||
// RegistryList for telling odo to connect to all the registries in the registry list
|
||||
RegistryList *[]Registry `yaml:"RegistryList,omitempty"`
|
||||
|
||||
// RegistryCacheTime how long odo should cache information from registry
|
||||
RegistryCacheTime *int `yaml:"RegistryCacheTime,omitempty"`
|
||||
|
||||
// Ephemeral if true creates odo emptyDir to store odo source code
|
||||
Ephemeral *bool `yaml:"Ephemeral,omitempty"`
|
||||
|
||||
// ConsentTelemetry if true collects telemetry for odo
|
||||
ConsentTelemetry *bool `yaml:"ConsentTelemetry,omitempty"`
|
||||
}
|
||||
|
||||
// Registry includes the registry metadata
|
||||
type Registry struct {
|
||||
Name string `yaml:"Name,omitempty"`
|
||||
URL string `yaml:"URL,omitempty"`
|
||||
Secure bool
|
||||
}
|
||||
|
||||
// Preference stores all the preferences related to odo
|
||||
type Preference struct {
|
||||
metav1.TypeMeta `yaml:",inline"`
|
||||
|
||||
// Odo settings holds the odo specific global settings
|
||||
OdoSettings odoSettings `yaml:"OdoSettings,omitempty"`
|
||||
}
|
||||
|
||||
// preferenceInfo wraps the preference and provides helpers to
|
||||
// serialize it.
|
||||
type preferenceInfo struct {
|
||||
Filename string `yaml:"FileName,omitempty"`
|
||||
Preference `yaml:",omitempty"`
|
||||
}
|
||||
|
||||
func getPreferenceFile() (string, error) {
|
||||
if env, ok := os.LookupEnv(GlobalConfigEnvName); ok {
|
||||
return env, nil
|
||||
}
|
||||
|
||||
if len(customHomeDir) != 0 {
|
||||
return filepath.Join(customHomeDir, ".odo", configFileName), nil
|
||||
}
|
||||
|
||||
currentUser, err := user.Current()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return filepath.Join(currentUser.HomeDir, ".odo", configFileName), nil
|
||||
}
|
||||
|
||||
func NewClient() (Client, error) {
|
||||
return newPreferenceInfo()
|
||||
}
|
||||
|
||||
// newPreference creates an empty Preference struct with type meta information
|
||||
func newPreference() Preference {
|
||||
return Preference{
|
||||
TypeMeta: metav1.TypeMeta{
|
||||
Kind: preferenceKind,
|
||||
APIVersion: preferenceAPIVersion,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// newPreferenceInfo gets the PreferenceInfo from preference file
|
||||
// or returns default PreferenceInfo if preference file does not exist
|
||||
func newPreferenceInfo() (*preferenceInfo, error) {
|
||||
preferenceFile, err := getPreferenceFile()
|
||||
klog.V(4).Infof("The path for preference file is %+v", preferenceFile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c := preferenceInfo{
|
||||
Preference: newPreference(),
|
||||
Filename: preferenceFile,
|
||||
}
|
||||
|
||||
// Default devfile registry
|
||||
defaultRegistryList := []Registry{
|
||||
{
|
||||
Name: DefaultDevfileRegistryName,
|
||||
URL: DefaultDevfileRegistryURL,
|
||||
Secure: false,
|
||||
},
|
||||
}
|
||||
|
||||
// If the preference file doesn't exist then we return with default preference
|
||||
if _, err = os.Stat(preferenceFile); os.IsNotExist(err) {
|
||||
c.OdoSettings.RegistryList = &defaultRegistryList
|
||||
return &c, nil
|
||||
}
|
||||
|
||||
err = util.GetFromFile(&c.Preference, c.Filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Handle user has preference file but doesn't use dynamic registry before
|
||||
if c.OdoSettings.RegistryList == nil {
|
||||
c.OdoSettings.RegistryList = &defaultRegistryList
|
||||
}
|
||||
|
||||
// Handle OCI-based default registry migration
|
||||
if c.OdoSettings.RegistryList != nil {
|
||||
for index, registry := range *c.OdoSettings.RegistryList {
|
||||
if registry.Name == DefaultDevfileRegistryName && registry.URL == OldDefaultDevfileRegistryURL {
|
||||
registryList := *c.OdoSettings.RegistryList
|
||||
registryList[index].URL = DefaultDevfileRegistryURL
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return &c, nil
|
||||
}
|
||||
|
||||
// RegistryHandler handles registry add, update and delete operations
|
||||
func (c *preferenceInfo) RegistryHandler(operation string, registryName string, registryURL string, forceFlag bool, isSecure bool) error {
|
||||
var registryList []Registry
|
||||
var err error
|
||||
var registryExist bool
|
||||
|
||||
// Registry list is empty
|
||||
if c.OdoSettings.RegistryList == nil {
|
||||
registryList, err = handleWithoutRegistryExist(registryList, operation, registryName, registryURL, isSecure)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
// The target registry exists in the registry list
|
||||
registryList = *c.OdoSettings.RegistryList
|
||||
for index, registry := range registryList {
|
||||
if registry.Name == registryName {
|
||||
registryExist = true
|
||||
registryList, err = handleWithRegistryExist(index, registryList, operation, registryName, registryURL, forceFlag, isSecure)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The target registry doesn't exist in the registry list
|
||||
if !registryExist {
|
||||
registryList, err = handleWithoutRegistryExist(registryList, operation, registryName, registryURL, isSecure)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
c.OdoSettings.RegistryList = ®istryList
|
||||
err = util.WriteToFile(&c.Preference, c.Filename)
|
||||
if err != nil {
|
||||
return errors.Errorf("unable to write the configuration of %q operation to preference file", operation)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func handleWithoutRegistryExist(registryList []Registry, operation string, registryName string, registryURL string, isSecure bool) ([]Registry, error) {
|
||||
switch operation {
|
||||
|
||||
case "add":
|
||||
registry := Registry{
|
||||
Name: registryName,
|
||||
URL: registryURL,
|
||||
Secure: isSecure,
|
||||
}
|
||||
registryList = append(registryList, registry)
|
||||
|
||||
case "update", "delete":
|
||||
return nil, errors.Errorf("failed to %v registry: registry %q doesn't exist", operation, registryName)
|
||||
}
|
||||
|
||||
return registryList, nil
|
||||
}
|
||||
|
||||
func handleWithRegistryExist(index int, registryList []Registry, operation string, registryName string, registryURL string, forceFlag bool, isSecure bool) ([]Registry, error) {
|
||||
switch operation {
|
||||
|
||||
case "add":
|
||||
return nil, errors.Errorf("failed to add registry: registry %q already exists", registryName)
|
||||
|
||||
case "update":
|
||||
if !forceFlag {
|
||||
if !ui.Proceed(fmt.Sprintf("Are you sure you want to update registry %q", registryName)) {
|
||||
log.Info("Aborted by the user")
|
||||
return registryList, nil
|
||||
}
|
||||
}
|
||||
|
||||
registryList[index].URL = registryURL
|
||||
registryList[index].Secure = isSecure
|
||||
log.Info("Successfully updated registry")
|
||||
|
||||
case "delete":
|
||||
if !forceFlag {
|
||||
if !ui.Proceed(fmt.Sprintf("Are you sure you want to delete registry %q", registryName)) {
|
||||
log.Info("Aborted by the user")
|
||||
return registryList, nil
|
||||
}
|
||||
}
|
||||
|
||||
copy(registryList[index:], registryList[index+1:])
|
||||
registryList[len(registryList)-1] = Registry{}
|
||||
registryList = registryList[:len(registryList)-1]
|
||||
log.Info("Successfully deleted registry")
|
||||
}
|
||||
|
||||
return registryList, nil
|
||||
}
|
||||
|
||||
// SetConfiguration modifies odo preferences in the preference file
|
||||
// TODO: Use reflect to set parameters
|
||||
func (c *preferenceInfo) SetConfiguration(parameter string, value string) error {
|
||||
if p, ok := asSupportedParameter(parameter); ok {
|
||||
// processing values according to the parameter names
|
||||
switch p {
|
||||
|
||||
case "timeout":
|
||||
typedval, err := strconv.Atoi(value)
|
||||
if err != nil {
|
||||
return errors.Errorf("unable to set %q to %q", parameter, value)
|
||||
}
|
||||
if typedval < 0 {
|
||||
return errors.Errorf("cannot set timeout to less than 0")
|
||||
}
|
||||
c.OdoSettings.Timeout = &typedval
|
||||
|
||||
case "buildtimeout":
|
||||
typedval, err := strconv.Atoi(value)
|
||||
if err != nil {
|
||||
return errors.Errorf("unable to set %q to %q, value must be an integer", parameter, value)
|
||||
}
|
||||
if typedval < 0 {
|
||||
return errors.Errorf("cannot set timeout to less than 0")
|
||||
}
|
||||
c.OdoSettings.BuildTimeout = &typedval
|
||||
|
||||
case "pushtimeout":
|
||||
typedval, err := strconv.Atoi(value)
|
||||
if err != nil {
|
||||
return errors.Errorf("unable to set %q to %q, value must be an integer", parameter, value)
|
||||
}
|
||||
if typedval < 0 {
|
||||
return errors.Errorf("cannot set timeout to less than 0")
|
||||
}
|
||||
c.OdoSettings.PushTimeout = &typedval
|
||||
|
||||
case "registrycachetime":
|
||||
typedval, err := strconv.Atoi(value)
|
||||
if err != nil {
|
||||
return errors.Errorf("unable to set %q to %q, value must be an integer", parameter, value)
|
||||
}
|
||||
if typedval < 0 {
|
||||
return errors.Errorf("cannot set timeout to less than 0")
|
||||
}
|
||||
c.OdoSettings.RegistryCacheTime = &typedval
|
||||
|
||||
case "updatenotification":
|
||||
val, err := strconv.ParseBool(strings.ToLower(value))
|
||||
if err != nil {
|
||||
return errors.Errorf("unable to set %q to %q, value must be a boolean", parameter, value)
|
||||
}
|
||||
c.OdoSettings.UpdateNotification = &val
|
||||
|
||||
// TODO: should we add a validator here? What is the use of nameprefix?
|
||||
case "nameprefix":
|
||||
c.OdoSettings.NamePrefix = &value
|
||||
|
||||
case "ephemeral":
|
||||
val, err := strconv.ParseBool(strings.ToLower(value))
|
||||
if err != nil {
|
||||
return errors.Errorf("unable to set %q to %q, value must be a boolean", parameter, value)
|
||||
}
|
||||
c.OdoSettings.Ephemeral = &val
|
||||
|
||||
case "consenttelemetry":
|
||||
val, err := strconv.ParseBool(strings.ToLower(value))
|
||||
if err != nil {
|
||||
return errors.Errorf("unable to set %q to %q, value must be a boolean", parameter, value)
|
||||
}
|
||||
c.OdoSettings.ConsentTelemetry = &val
|
||||
}
|
||||
} else {
|
||||
return errors.Errorf("unknown parameter : %q is not a parameter in odo preference, run `odo preference -h` to see list of available parameters", parameter)
|
||||
}
|
||||
|
||||
err := util.WriteToFile(&c.Preference, c.Filename)
|
||||
if err != nil {
|
||||
return errors.Errorf("unable to set %q, something is wrong with odo, kindly raise an issue at https://github.com/redhat-developer/odo/issues/new?template=Bug.md", parameter)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteConfiguration deletes odo preference from the odo preference file
|
||||
func (c *preferenceInfo) DeleteConfiguration(parameter string) error {
|
||||
if p, ok := asSupportedParameter(parameter); ok {
|
||||
// processing values according to the parameter names
|
||||
|
||||
if err := util.DeleteConfiguration(&c.OdoSettings, p); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
return errors.Errorf("unknown parameter :%q is not a parameter in the odo preference", parameter)
|
||||
}
|
||||
|
||||
err := util.WriteToFile(&c.Preference, c.Filename)
|
||||
if err != nil {
|
||||
return errors.Errorf("unable to set %q, something is wrong with odo, kindly raise an issue at https://github.com/redhat-developer/odo/issues/new?template=Bug.md", parameter)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// IsSet checks if the value is set in the preference
|
||||
func (c *preferenceInfo) IsSet(parameter string) bool {
|
||||
return util.IsSet(c.OdoSettings, parameter)
|
||||
}
|
||||
|
||||
// GetTimeout returns the value of Timeout from config
|
||||
// and if absent then returns default
|
||||
func (c *preferenceInfo) GetTimeout() int {
|
||||
// default timeout value is 1
|
||||
return util.GetIntOrDefault(c.OdoSettings.Timeout, DefaultTimeout)
|
||||
}
|
||||
|
||||
// GetBuildTimeout gets the value set by BuildTimeout
|
||||
func (c *preferenceInfo) GetBuildTimeout() int {
|
||||
// default timeout value is 300
|
||||
return util.GetIntOrDefault(c.OdoSettings.BuildTimeout, DefaultBuildTimeout)
|
||||
}
|
||||
|
||||
// GetPushTimeout gets the value set by PushTimeout
|
||||
func (c *preferenceInfo) GetPushTimeout() int {
|
||||
// default timeout value is 1
|
||||
return util.GetIntOrDefault(c.OdoSettings.PushTimeout, DefaultPushTimeout)
|
||||
}
|
||||
|
||||
// GetRegistryCacheTime gets the value set by RegistryCacheTime
|
||||
func (c *preferenceInfo) GetRegistryCacheTime() int {
|
||||
return util.GetIntOrDefault(c.OdoSettings.RegistryCacheTime, DefaultRegistryCacheTime)
|
||||
}
|
||||
|
||||
// GetUpdateNotification returns the value of UpdateNotification from preferences
|
||||
// and if absent then returns default
|
||||
func (c *preferenceInfo) GetUpdateNotification() bool {
|
||||
return util.GetBoolOrDefault(c.OdoSettings.UpdateNotification, true)
|
||||
}
|
||||
|
||||
// GetEphemeralSourceVolume returns the value of ephemeral from preferences
|
||||
// and if absent then returns default
|
||||
func (c *preferenceInfo) GetEphemeralSourceVolume() bool {
|
||||
return util.GetBoolOrDefault(c.OdoSettings.Ephemeral, DefaultEphemeralSettings)
|
||||
}
|
||||
|
||||
// GetNamePrefix returns the value of Prefix from preferences
|
||||
// and if absent then returns default
|
||||
func (c *preferenceInfo) GetNamePrefix() string {
|
||||
return util.GetStringOrEmpty(c.OdoSettings.NamePrefix)
|
||||
}
|
||||
|
||||
// GetConsentTelemetry returns the value of ConsentTelemetry from preferences
|
||||
// and if absent then returns default
|
||||
// default value: false, consent telemetry is disabled by default
|
||||
func (c *preferenceInfo) GetConsentTelemetry() bool {
|
||||
return util.GetBoolOrDefault(c.OdoSettings.ConsentTelemetry, DefaultConsentTelemetrySetting)
|
||||
}
|
||||
|
||||
func (c *preferenceInfo) UpdateNotification() *bool {
|
||||
return c.OdoSettings.UpdateNotification
|
||||
}
|
||||
|
||||
func (c *preferenceInfo) NamePrefix() *string {
|
||||
return c.OdoSettings.NamePrefix
|
||||
}
|
||||
|
||||
func (c *preferenceInfo) Timeout() *int {
|
||||
return c.OdoSettings.Timeout
|
||||
}
|
||||
|
||||
func (c *preferenceInfo) BuildTimeout() *int {
|
||||
return c.OdoSettings.BuildTimeout
|
||||
}
|
||||
|
||||
func (c *preferenceInfo) PushTimeout() *int {
|
||||
return c.OdoSettings.PushTimeout
|
||||
}
|
||||
|
||||
func (c *preferenceInfo) EphemeralSourceVolume() *bool {
|
||||
return c.OdoSettings.Ephemeral
|
||||
}
|
||||
|
||||
func (c *preferenceInfo) ConsentTelemetry() *bool {
|
||||
return c.OdoSettings.ConsentTelemetry
|
||||
}
|
||||
|
||||
func (c *preferenceInfo) RegistryList() *[]Registry {
|
||||
return c.OdoSettings.RegistryList
|
||||
}
|
||||
|
||||
// FormatSupportedParameters outputs supported parameters and their description
|
||||
func FormatSupportedParameters() (result string) {
|
||||
for _, v := range GetSupportedParameters() {
|
||||
result = result + " " + v + " - " + supportedParameterDescriptions[v] + "\n"
|
||||
}
|
||||
return "\nAvailable Global Parameters:\n" + result
|
||||
}
|
||||
|
||||
// asSupportedParameter checks that the given parameter is supported and returns a lower case version of it if it is
|
||||
func asSupportedParameter(param string) (string, bool) {
|
||||
lower := strings.ToLower(param)
|
||||
return lower, lowerCaseParameters[lower]
|
||||
}
|
||||
|
||||
// GetSupportedParameters returns the name of the supported parameters
|
||||
func GetSupportedParameters() []string {
|
||||
return util.GetSortedKeys(supportedParameterDescriptions)
|
||||
}
|
||||
@@ -22,19 +22,19 @@ func TestNew(t *testing.T) {
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
output *PreferenceInfo
|
||||
output *preferenceInfo
|
||||
success bool
|
||||
}{
|
||||
{
|
||||
name: "Test filename is being set",
|
||||
output: &PreferenceInfo{
|
||||
output: &preferenceInfo{
|
||||
Filename: tempConfigFile.Name(),
|
||||
Preference: Preference{
|
||||
TypeMeta: metav1.TypeMeta{
|
||||
Kind: preferenceKind,
|
||||
APIVersion: preferenceAPIVersion,
|
||||
},
|
||||
OdoSettings: OdoSettings{
|
||||
OdoSettings: odoSettings{
|
||||
RegistryList: &[]Registry{
|
||||
{
|
||||
Name: DefaultDevfileRegistryName,
|
||||
@@ -51,7 +51,7 @@ func TestNew(t *testing.T) {
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
cfi, err := NewPreferenceInfo()
|
||||
cfi, err := newPreferenceInfo()
|
||||
switch test.success {
|
||||
case true:
|
||||
if err != nil {
|
||||
@@ -93,7 +93,7 @@ func TestGetBuildTimeout(t *testing.T) {
|
||||
{
|
||||
name: "Case 2: Validating value 0 from configuration",
|
||||
existingConfig: Preference{
|
||||
OdoSettings: OdoSettings{
|
||||
OdoSettings: odoSettings{
|
||||
BuildTimeout: &zeroValue,
|
||||
},
|
||||
},
|
||||
@@ -103,7 +103,7 @@ func TestGetBuildTimeout(t *testing.T) {
|
||||
{
|
||||
name: "Case 3: Validating value 5 from configuration",
|
||||
existingConfig: Preference{
|
||||
OdoSettings: OdoSettings{
|
||||
OdoSettings: odoSettings{
|
||||
BuildTimeout: &nonzeroValue,
|
||||
},
|
||||
},
|
||||
@@ -112,7 +112,7 @@ func TestGetBuildTimeout(t *testing.T) {
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
cfg, err := NewPreferenceInfo()
|
||||
cfg, err := newPreferenceInfo()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
@@ -149,7 +149,7 @@ func TestGetPushTimeout(t *testing.T) {
|
||||
{
|
||||
name: "Case 2: Validating value 0 from configuration",
|
||||
existingConfig: Preference{
|
||||
OdoSettings: OdoSettings{
|
||||
OdoSettings: odoSettings{
|
||||
PushTimeout: &zeroValue,
|
||||
},
|
||||
},
|
||||
@@ -159,7 +159,7 @@ func TestGetPushTimeout(t *testing.T) {
|
||||
{
|
||||
name: "Case 3: Validating value 5 from configuration",
|
||||
existingConfig: Preference{
|
||||
OdoSettings: OdoSettings{
|
||||
OdoSettings: odoSettings{
|
||||
PushTimeout: &nonzeroValue,
|
||||
},
|
||||
},
|
||||
@@ -168,7 +168,7 @@ func TestGetPushTimeout(t *testing.T) {
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
cfg, err := NewPreferenceInfo()
|
||||
cfg, err := newPreferenceInfo()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
@@ -205,7 +205,7 @@ func TestGetTimeout(t *testing.T) {
|
||||
{
|
||||
name: "Case 2: validating value 0 from config",
|
||||
existingConfig: Preference{
|
||||
OdoSettings: OdoSettings{
|
||||
OdoSettings: odoSettings{
|
||||
Timeout: &zeroValue,
|
||||
},
|
||||
},
|
||||
@@ -215,7 +215,7 @@ func TestGetTimeout(t *testing.T) {
|
||||
{
|
||||
name: "Case 3: validating value 5 from config",
|
||||
existingConfig: Preference{
|
||||
OdoSettings: OdoSettings{
|
||||
OdoSettings: odoSettings{
|
||||
Timeout: &nonzeroValue,
|
||||
},
|
||||
},
|
||||
@@ -224,7 +224,7 @@ func TestGetTimeout(t *testing.T) {
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
cfg, err := NewPreferenceInfo()
|
||||
cfg, err := newPreferenceInfo()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
@@ -272,7 +272,7 @@ func TestSetConfiguration(t *testing.T) {
|
||||
parameter: UpdateNotificationSetting,
|
||||
value: "false",
|
||||
existingConfig: Preference{
|
||||
OdoSettings: OdoSettings{
|
||||
OdoSettings: odoSettings{
|
||||
UpdateNotification: &trueValue,
|
||||
},
|
||||
},
|
||||
@@ -284,7 +284,7 @@ func TestSetConfiguration(t *testing.T) {
|
||||
parameter: UpdateNotificationSetting,
|
||||
value: "true",
|
||||
existingConfig: Preference{
|
||||
OdoSettings: OdoSettings{
|
||||
OdoSettings: odoSettings{
|
||||
UpdateNotification: &falseValue,
|
||||
},
|
||||
},
|
||||
@@ -305,7 +305,7 @@ func TestSetConfiguration(t *testing.T) {
|
||||
parameter: TimeoutSetting,
|
||||
value: "5",
|
||||
existingConfig: Preference{
|
||||
OdoSettings: OdoSettings{
|
||||
OdoSettings: odoSettings{
|
||||
Timeout: &zeroValue,
|
||||
},
|
||||
},
|
||||
@@ -429,7 +429,7 @@ func TestSetConfiguration(t *testing.T) {
|
||||
parameter: ConsentTelemetrySetting,
|
||||
value: "false",
|
||||
existingConfig: Preference{
|
||||
OdoSettings: OdoSettings{
|
||||
OdoSettings: odoSettings{
|
||||
ConsentTelemetry: &trueValue,
|
||||
},
|
||||
},
|
||||
@@ -439,7 +439,7 @@ func TestSetConfiguration(t *testing.T) {
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
cfg, err := NewPreferenceInfo()
|
||||
cfg, err := newPreferenceInfo()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
@@ -506,7 +506,7 @@ func TestConsentTelemetry(t *testing.T) {
|
||||
{
|
||||
name: fmt.Sprintf("Case 2: %s true", ConsentTelemetrySetting),
|
||||
existingConfig: Preference{
|
||||
OdoSettings: OdoSettings{
|
||||
OdoSettings: odoSettings{
|
||||
ConsentTelemetry: &trueValue,
|
||||
},
|
||||
},
|
||||
@@ -515,7 +515,7 @@ func TestConsentTelemetry(t *testing.T) {
|
||||
{
|
||||
name: fmt.Sprintf("Case 3: %s false", ConsentTelemetrySetting),
|
||||
existingConfig: Preference{
|
||||
OdoSettings: OdoSettings{
|
||||
OdoSettings: odoSettings{
|
||||
ConsentTelemetry: &falseValue,
|
||||
},
|
||||
},
|
||||
@@ -525,7 +525,7 @@ func TestConsentTelemetry(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
cfg := PreferenceInfo{
|
||||
cfg := preferenceInfo{
|
||||
Preference: tt.existingConfig,
|
||||
}
|
||||
output := cfg.GetConsentTelemetry()
|
||||
@@ -562,7 +562,7 @@ func TestGetupdateNotification(t *testing.T) {
|
||||
{
|
||||
name: fmt.Sprintf("Case 2: %s true", UpdateNotificationSetting),
|
||||
existingConfig: Preference{
|
||||
OdoSettings: OdoSettings{
|
||||
OdoSettings: odoSettings{
|
||||
UpdateNotification: &trueValue,
|
||||
},
|
||||
},
|
||||
@@ -571,7 +571,7 @@ func TestGetupdateNotification(t *testing.T) {
|
||||
{
|
||||
name: fmt.Sprintf("Case 3: %s false", UpdateNotificationSetting),
|
||||
existingConfig: Preference{
|
||||
OdoSettings: OdoSettings{
|
||||
OdoSettings: odoSettings{
|
||||
UpdateNotification: &falseValue,
|
||||
},
|
||||
},
|
||||
@@ -581,7 +581,7 @@ func TestGetupdateNotification(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
cfg := PreferenceInfo{
|
||||
cfg := preferenceInfo{
|
||||
Preference: tt.existingConfig,
|
||||
}
|
||||
output := cfg.GetUpdateNotification()
|
||||
@@ -650,7 +650,7 @@ func TestPreferenceIsntCreatedWhenOdoIsUsed(t *testing.T) {
|
||||
}
|
||||
os.RemoveAll(filename)
|
||||
|
||||
conf, err := NewPreferenceInfo()
|
||||
conf, err := newPreferenceInfo()
|
||||
if err != nil {
|
||||
t.Errorf("error while creating global preference %v", err)
|
||||
}
|
||||
@@ -660,7 +660,7 @@ func TestPreferenceIsntCreatedWhenOdoIsUsed(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestMetaTypePopulatedInPreference(t *testing.T) {
|
||||
pi, err := NewPreferenceInfo()
|
||||
pi, err := newPreferenceInfo()
|
||||
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
@@ -821,7 +821,7 @@ func TestGetConsentTelemetry(t *testing.T) {
|
||||
{
|
||||
name: fmt.Sprintf("Case 2: %s true", ConsentTelemetrySetting),
|
||||
existingConfig: Preference{
|
||||
OdoSettings: OdoSettings{
|
||||
OdoSettings: odoSettings{
|
||||
ConsentTelemetry: &trueValue,
|
||||
},
|
||||
},
|
||||
@@ -830,7 +830,7 @@ func TestGetConsentTelemetry(t *testing.T) {
|
||||
{
|
||||
name: fmt.Sprintf("Case 3: %s false", ConsentTelemetrySetting),
|
||||
existingConfig: Preference{
|
||||
OdoSettings: OdoSettings{
|
||||
OdoSettings: odoSettings{
|
||||
ConsentTelemetry: &falseValue,
|
||||
},
|
||||
},
|
||||
@@ -840,7 +840,7 @@ func TestGetConsentTelemetry(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
cfg := PreferenceInfo{
|
||||
cfg := preferenceInfo{
|
||||
Preference: tt.existingConfig,
|
||||
}
|
||||
output := cfg.GetConsentTelemetry()
|
||||
@@ -24,57 +24,57 @@ type PreferenceItem struct {
|
||||
Description string // The description of the preference
|
||||
}
|
||||
|
||||
func NewPreferenceList(prefInfo PreferenceInfo) PreferenceList {
|
||||
func (o *preferenceInfo) NewPreferenceList() PreferenceList {
|
||||
return PreferenceList{
|
||||
TypeMeta: metav1.TypeMeta{
|
||||
APIVersion: prefAPIVersion,
|
||||
Kind: prefKind,
|
||||
},
|
||||
Items: toPreferenceItems(prefInfo),
|
||||
Items: toPreferenceItems(*o),
|
||||
}
|
||||
}
|
||||
|
||||
func toPreferenceItems(prefInfo PreferenceInfo) []PreferenceItem {
|
||||
odoSettings := prefInfo.OdoSettings
|
||||
func toPreferenceItems(prefInfo preferenceInfo) []PreferenceItem {
|
||||
settings := prefInfo.OdoSettings
|
||||
return []PreferenceItem{
|
||||
{
|
||||
Name: UpdateNotificationSetting,
|
||||
Value: odoSettings.UpdateNotification,
|
||||
Value: settings.UpdateNotification,
|
||||
Default: true,
|
||||
Type: getType(prefInfo.GetUpdateNotification()), // use the Getter here to determine type
|
||||
Description: UpdateNotificationSettingDescription,
|
||||
},
|
||||
{
|
||||
Name: NamePrefixSetting,
|
||||
Value: odoSettings.NamePrefix,
|
||||
Value: settings.NamePrefix,
|
||||
Default: "",
|
||||
Type: getType(prefInfo.GetNamePrefix()),
|
||||
Description: NamePrefixSettingDescription,
|
||||
},
|
||||
{
|
||||
Name: TimeoutSetting,
|
||||
Value: odoSettings.Timeout,
|
||||
Value: settings.Timeout,
|
||||
Default: DefaultTimeout,
|
||||
Type: getType(prefInfo.GetTimeout()),
|
||||
Description: TimeoutSettingDescription,
|
||||
},
|
||||
{
|
||||
Name: BuildTimeoutSetting,
|
||||
Value: odoSettings.BuildTimeout,
|
||||
Value: settings.BuildTimeout,
|
||||
Default: DefaultBuildTimeout,
|
||||
Type: getType(prefInfo.GetBuildTimeout()),
|
||||
Description: BuildTimeoutSettingDescription,
|
||||
},
|
||||
{
|
||||
Name: PushTimeoutSetting,
|
||||
Value: odoSettings.PushTimeout,
|
||||
Value: settings.PushTimeout,
|
||||
Default: DefaultPushTimeout,
|
||||
Type: getType(prefInfo.GetPushTimeout()),
|
||||
Description: PushTimeoutSettingDescription,
|
||||
},
|
||||
{
|
||||
Name: ConsentTelemetrySetting,
|
||||
Value: odoSettings.ConsentTelemetry,
|
||||
Value: settings.ConsentTelemetry,
|
||||
Default: DefaultConsentTelemetrySetting,
|
||||
Type: getType(prefInfo.GetConsentTelemetry()),
|
||||
Description: ConsentTelemetryDescription,
|
||||
|
||||
328
pkg/preference/mock.go
Normal file
328
pkg/preference/mock.go
Normal file
@@ -0,0 +1,328 @@
|
||||
// Code generated by MockGen. DO NOT EDIT.
|
||||
// Source: pkg/preference/preference.go
|
||||
|
||||
// Package preference is a generated GoMock package.
|
||||
package preference
|
||||
|
||||
import (
|
||||
reflect "reflect"
|
||||
|
||||
gomock "github.com/golang/mock/gomock"
|
||||
)
|
||||
|
||||
// MockClient is a mock of Client interface.
|
||||
type MockClient struct {
|
||||
ctrl *gomock.Controller
|
||||
recorder *MockClientMockRecorder
|
||||
}
|
||||
|
||||
// MockClientMockRecorder is the mock recorder for MockClient.
|
||||
type MockClientMockRecorder struct {
|
||||
mock *MockClient
|
||||
}
|
||||
|
||||
// NewMockClient creates a new mock instance.
|
||||
func NewMockClient(ctrl *gomock.Controller) *MockClient {
|
||||
mock := &MockClient{ctrl: ctrl}
|
||||
mock.recorder = &MockClientMockRecorder{mock}
|
||||
return mock
|
||||
}
|
||||
|
||||
// EXPECT returns an object that allows the caller to indicate expected use.
|
||||
func (m *MockClient) EXPECT() *MockClientMockRecorder {
|
||||
return m.recorder
|
||||
}
|
||||
|
||||
// BuildTimeout mocks base method.
|
||||
func (m *MockClient) BuildTimeout() *int {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "BuildTimeout")
|
||||
ret0, _ := ret[0].(*int)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// BuildTimeout indicates an expected call of BuildTimeout.
|
||||
func (mr *MockClientMockRecorder) BuildTimeout() *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "BuildTimeout", reflect.TypeOf((*MockClient)(nil).BuildTimeout))
|
||||
}
|
||||
|
||||
// ConsentTelemetry mocks base method.
|
||||
func (m *MockClient) ConsentTelemetry() *bool {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "ConsentTelemetry")
|
||||
ret0, _ := ret[0].(*bool)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// ConsentTelemetry indicates an expected call of ConsentTelemetry.
|
||||
func (mr *MockClientMockRecorder) ConsentTelemetry() *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ConsentTelemetry", reflect.TypeOf((*MockClient)(nil).ConsentTelemetry))
|
||||
}
|
||||
|
||||
// DeleteConfiguration mocks base method.
|
||||
func (m *MockClient) DeleteConfiguration(parameter string) error {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "DeleteConfiguration", parameter)
|
||||
ret0, _ := ret[0].(error)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// DeleteConfiguration indicates an expected call of DeleteConfiguration.
|
||||
func (mr *MockClientMockRecorder) DeleteConfiguration(parameter interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteConfiguration", reflect.TypeOf((*MockClient)(nil).DeleteConfiguration), parameter)
|
||||
}
|
||||
|
||||
// EphemeralSourceVolume mocks base method.
|
||||
func (m *MockClient) EphemeralSourceVolume() *bool {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "EphemeralSourceVolume")
|
||||
ret0, _ := ret[0].(*bool)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// EphemeralSourceVolume indicates an expected call of EphemeralSourceVolume.
|
||||
func (mr *MockClientMockRecorder) EphemeralSourceVolume() *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "EphemeralSourceVolume", reflect.TypeOf((*MockClient)(nil).EphemeralSourceVolume))
|
||||
}
|
||||
|
||||
// GetBuildTimeout mocks base method.
|
||||
func (m *MockClient) GetBuildTimeout() int {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "GetBuildTimeout")
|
||||
ret0, _ := ret[0].(int)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// GetBuildTimeout indicates an expected call of GetBuildTimeout.
|
||||
func (mr *MockClientMockRecorder) GetBuildTimeout() *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetBuildTimeout", reflect.TypeOf((*MockClient)(nil).GetBuildTimeout))
|
||||
}
|
||||
|
||||
// GetConsentTelemetry mocks base method.
|
||||
func (m *MockClient) GetConsentTelemetry() bool {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "GetConsentTelemetry")
|
||||
ret0, _ := ret[0].(bool)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// GetConsentTelemetry indicates an expected call of GetConsentTelemetry.
|
||||
func (mr *MockClientMockRecorder) GetConsentTelemetry() *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetConsentTelemetry", reflect.TypeOf((*MockClient)(nil).GetConsentTelemetry))
|
||||
}
|
||||
|
||||
// GetEphemeralSourceVolume mocks base method.
|
||||
func (m *MockClient) GetEphemeralSourceVolume() bool {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "GetEphemeralSourceVolume")
|
||||
ret0, _ := ret[0].(bool)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// GetEphemeralSourceVolume indicates an expected call of GetEphemeralSourceVolume.
|
||||
func (mr *MockClientMockRecorder) GetEphemeralSourceVolume() *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetEphemeralSourceVolume", reflect.TypeOf((*MockClient)(nil).GetEphemeralSourceVolume))
|
||||
}
|
||||
|
||||
// GetNamePrefix mocks base method.
|
||||
func (m *MockClient) GetNamePrefix() string {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "GetNamePrefix")
|
||||
ret0, _ := ret[0].(string)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// GetNamePrefix indicates an expected call of GetNamePrefix.
|
||||
func (mr *MockClientMockRecorder) GetNamePrefix() *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetNamePrefix", reflect.TypeOf((*MockClient)(nil).GetNamePrefix))
|
||||
}
|
||||
|
||||
// GetPushTimeout mocks base method.
|
||||
func (m *MockClient) GetPushTimeout() int {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "GetPushTimeout")
|
||||
ret0, _ := ret[0].(int)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// GetPushTimeout indicates an expected call of GetPushTimeout.
|
||||
func (mr *MockClientMockRecorder) GetPushTimeout() *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetPushTimeout", reflect.TypeOf((*MockClient)(nil).GetPushTimeout))
|
||||
}
|
||||
|
||||
// GetRegistryCacheTime mocks base method.
|
||||
func (m *MockClient) GetRegistryCacheTime() int {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "GetRegistryCacheTime")
|
||||
ret0, _ := ret[0].(int)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// GetRegistryCacheTime indicates an expected call of GetRegistryCacheTime.
|
||||
func (mr *MockClientMockRecorder) GetRegistryCacheTime() *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetRegistryCacheTime", reflect.TypeOf((*MockClient)(nil).GetRegistryCacheTime))
|
||||
}
|
||||
|
||||
// GetTimeout mocks base method.
|
||||
func (m *MockClient) GetTimeout() int {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "GetTimeout")
|
||||
ret0, _ := ret[0].(int)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// GetTimeout indicates an expected call of GetTimeout.
|
||||
func (mr *MockClientMockRecorder) GetTimeout() *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetTimeout", reflect.TypeOf((*MockClient)(nil).GetTimeout))
|
||||
}
|
||||
|
||||
// GetUpdateNotification mocks base method.
|
||||
func (m *MockClient) GetUpdateNotification() bool {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "GetUpdateNotification")
|
||||
ret0, _ := ret[0].(bool)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// GetUpdateNotification indicates an expected call of GetUpdateNotification.
|
||||
func (mr *MockClientMockRecorder) GetUpdateNotification() *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetUpdateNotification", reflect.TypeOf((*MockClient)(nil).GetUpdateNotification))
|
||||
}
|
||||
|
||||
// IsSet mocks base method.
|
||||
func (m *MockClient) IsSet(parameter string) bool {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "IsSet", parameter)
|
||||
ret0, _ := ret[0].(bool)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// IsSet indicates an expected call of IsSet.
|
||||
func (mr *MockClientMockRecorder) IsSet(parameter interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "IsSet", reflect.TypeOf((*MockClient)(nil).IsSet), parameter)
|
||||
}
|
||||
|
||||
// NamePrefix mocks base method.
|
||||
func (m *MockClient) NamePrefix() *string {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "NamePrefix")
|
||||
ret0, _ := ret[0].(*string)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// NamePrefix indicates an expected call of NamePrefix.
|
||||
func (mr *MockClientMockRecorder) NamePrefix() *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NamePrefix", reflect.TypeOf((*MockClient)(nil).NamePrefix))
|
||||
}
|
||||
|
||||
// NewPreferenceList mocks base method.
|
||||
func (m *MockClient) NewPreferenceList() PreferenceList {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "NewPreferenceList")
|
||||
ret0, _ := ret[0].(PreferenceList)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// NewPreferenceList indicates an expected call of NewPreferenceList.
|
||||
func (mr *MockClientMockRecorder) NewPreferenceList() *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NewPreferenceList", reflect.TypeOf((*MockClient)(nil).NewPreferenceList))
|
||||
}
|
||||
|
||||
// PushTimeout mocks base method.
|
||||
func (m *MockClient) PushTimeout() *int {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "PushTimeout")
|
||||
ret0, _ := ret[0].(*int)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// PushTimeout indicates an expected call of PushTimeout.
|
||||
func (mr *MockClientMockRecorder) PushTimeout() *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "PushTimeout", reflect.TypeOf((*MockClient)(nil).PushTimeout))
|
||||
}
|
||||
|
||||
// RegistryHandler mocks base method.
|
||||
func (m *MockClient) RegistryHandler(operation, registryName, registryURL string, forceFlag, isSecure bool) error {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "RegistryHandler", operation, registryName, registryURL, forceFlag, isSecure)
|
||||
ret0, _ := ret[0].(error)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// RegistryHandler indicates an expected call of RegistryHandler.
|
||||
func (mr *MockClientMockRecorder) RegistryHandler(operation, registryName, registryURL, forceFlag, isSecure interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RegistryHandler", reflect.TypeOf((*MockClient)(nil).RegistryHandler), operation, registryName, registryURL, forceFlag, isSecure)
|
||||
}
|
||||
|
||||
// RegistryList mocks base method.
|
||||
func (m *MockClient) RegistryList() *[]Registry {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "RegistryList")
|
||||
ret0, _ := ret[0].(*[]Registry)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// RegistryList indicates an expected call of RegistryList.
|
||||
func (mr *MockClientMockRecorder) RegistryList() *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RegistryList", reflect.TypeOf((*MockClient)(nil).RegistryList))
|
||||
}
|
||||
|
||||
// SetConfiguration mocks base method.
|
||||
func (m *MockClient) SetConfiguration(parameter, value string) error {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "SetConfiguration", parameter, value)
|
||||
ret0, _ := ret[0].(error)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// SetConfiguration indicates an expected call of SetConfiguration.
|
||||
func (mr *MockClientMockRecorder) SetConfiguration(parameter, value interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetConfiguration", reflect.TypeOf((*MockClient)(nil).SetConfiguration), parameter, value)
|
||||
}
|
||||
|
||||
// Timeout mocks base method.
|
||||
func (m *MockClient) Timeout() *int {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "Timeout")
|
||||
ret0, _ := ret[0].(*int)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// Timeout indicates an expected call of Timeout.
|
||||
func (mr *MockClientMockRecorder) Timeout() *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Timeout", reflect.TypeOf((*MockClient)(nil).Timeout))
|
||||
}
|
||||
|
||||
// UpdateNotification mocks base method.
|
||||
func (m *MockClient) UpdateNotification() *bool {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "UpdateNotification")
|
||||
ret0, _ := ret[0].(*bool)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// UpdateNotification indicates an expected call of UpdateNotification.
|
||||
func (mr *MockClientMockRecorder) UpdateNotification() *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateNotification", reflect.TypeOf((*MockClient)(nil).UpdateNotification))
|
||||
}
|
||||
@@ -1,533 +1,28 @@
|
||||
package preference
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
type Client interface {
|
||||
IsSet(parameter string) bool
|
||||
SetConfiguration(parameter string, value string) error
|
||||
DeleteConfiguration(parameter string) error
|
||||
|
||||
"github.com/pkg/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/klog"
|
||||
GetUpdateNotification() bool
|
||||
GetNamePrefix() string
|
||||
GetTimeout() int
|
||||
GetBuildTimeout() int
|
||||
GetPushTimeout() int
|
||||
GetEphemeralSourceVolume() bool
|
||||
GetConsentTelemetry() bool
|
||||
GetRegistryCacheTime() int
|
||||
RegistryHandler(operation string, registryName string, registryURL string, forceFlag bool, isSecure bool) error
|
||||
|
||||
"github.com/redhat-developer/odo/pkg/log"
|
||||
"github.com/redhat-developer/odo/pkg/odo/cli/ui"
|
||||
"github.com/redhat-developer/odo/pkg/util"
|
||||
)
|
||||
UpdateNotification() *bool
|
||||
NamePrefix() *string
|
||||
Timeout() *int
|
||||
BuildTimeout() *int
|
||||
PushTimeout() *int
|
||||
EphemeralSourceVolume() *bool
|
||||
ConsentTelemetry() *bool
|
||||
RegistryList() *[]Registry
|
||||
|
||||
const (
|
||||
GlobalConfigEnvName = "GLOBALODOCONFIG"
|
||||
configFileName = "preference.yaml"
|
||||
preferenceKind = "Preference"
|
||||
preferenceAPIVersion = "odo.dev/v1alpha1"
|
||||
|
||||
//DefaultTimeout for openshift server connection check (in seconds)
|
||||
DefaultTimeout = 1
|
||||
|
||||
// DefaultPushTimeout is the default timeout for pods (in seconds)
|
||||
DefaultPushTimeout = 240
|
||||
|
||||
// DefaultBuildTimeout is the default build timeout for pods (in seconds)
|
||||
DefaultBuildTimeout = 300
|
||||
|
||||
// UpdateNotificationSetting is the name of the setting controlling update notification
|
||||
UpdateNotificationSetting = "UpdateNotification"
|
||||
|
||||
// UpdateNotificationSettingDescription is human-readable description for the update notification setting
|
||||
UpdateNotificationSettingDescription = "Flag to control if an update notification is shown or not (Default: true)"
|
||||
|
||||
// NamePrefixSetting is the name of the setting controlling name prefix
|
||||
NamePrefixSetting = "NamePrefix"
|
||||
|
||||
// NamePrefixSettingDescription is human-readable description for the name prefix setting
|
||||
NamePrefixSettingDescription = "Use this value to set a default name prefix (Default: current directory name)"
|
||||
|
||||
// TimeoutSetting is the name of the setting controlling timeout for connection check
|
||||
TimeoutSetting = "Timeout"
|
||||
|
||||
// BuildTimeoutSetting is the name of the setting controlling BuildTimeout
|
||||
BuildTimeoutSetting = "BuildTimeout"
|
||||
|
||||
// PushTimeoutSetting is the name of the setting controlling PushTimeout
|
||||
PushTimeoutSetting = "PushTimeout"
|
||||
|
||||
// RegistryCacheTimeSetting is human-readable description for the registrycachetime setting
|
||||
RegistryCacheTimeSetting = "RegistryCacheTime"
|
||||
|
||||
// DefaultDevfileRegistryName is the name of default devfile registry
|
||||
DefaultDevfileRegistryName = "DefaultDevfileRegistry"
|
||||
|
||||
// DefaultDevfileRegistryURL is the URL of default devfile registry
|
||||
DefaultDevfileRegistryURL = "https://registry.devfile.io"
|
||||
|
||||
// OldDefaultDevfileRegistryURL is the URL of old default devfile registry for registry migration purpose
|
||||
OldDefaultDevfileRegistryURL = "https://github.com/odo-devfiles/registry"
|
||||
|
||||
// DefaultRegistryCacheTime is time (in minutes) for how long odo will cache information from Devfile registry
|
||||
DefaultRegistryCacheTime = 15
|
||||
|
||||
// EphemeralSetting specifies if ephemeral volumes needs to be used as source volume.
|
||||
EphemeralSetting = "Ephemeral"
|
||||
|
||||
// DefaultEphemeralSettings is a default value for Ephemeral preference
|
||||
DefaultEphemeralSettings = true
|
||||
|
||||
// ConsentTelemetrySettings specifies if the user consents to telemetry
|
||||
ConsentTelemetrySetting = "ConsentTelemetry"
|
||||
|
||||
// DefaultConsentTelemetry is a default value for ConsentTelemetry preference
|
||||
DefaultConsentTelemetrySetting = false
|
||||
)
|
||||
|
||||
// TimeoutSettingDescription is human-readable description for the timeout setting
|
||||
var TimeoutSettingDescription = fmt.Sprintf("Timeout (in seconds) for OpenShift server connection check (Default: %d)", DefaultTimeout)
|
||||
|
||||
// PushTimeoutSettingDescription adds a description for PushTimeout
|
||||
var PushTimeoutSettingDescription = fmt.Sprintf("PushTimeout (in seconds) for waiting for a Pod to come up (Default: %d)", DefaultPushTimeout)
|
||||
|
||||
// BuildTimeoutSettingDescription adds a description for BuildTimeout
|
||||
var BuildTimeoutSettingDescription = fmt.Sprintf("BuildTimeout (in seconds) for waiting for a build of the git component to complete (Default: %d)", DefaultBuildTimeout)
|
||||
|
||||
// RegistryCacheTimeDescription adds a description for RegistryCacheTime
|
||||
var RegistryCacheTimeDescription = fmt.Sprintf("For how long (in minutes) odo will cache information from Devfile registry (Default: %d)", DefaultRegistryCacheTime)
|
||||
|
||||
// EphemeralDescription adds a description for EphemeralSourceVolume
|
||||
var EphemeralDescription = fmt.Sprintf("If true odo will create a emptyDir volume to store source code (Default: %t)", DefaultEphemeralSettings)
|
||||
|
||||
//TelemetryConsentDescription adds a description for TelemetryConsentSetting
|
||||
var ConsentTelemetryDescription = fmt.Sprintf("If true odo will collect telemetry for the user's odo usage (Default: %t)\n\t\t For more information: https://developers.redhat.com/article/tool-data-collection", DefaultConsentTelemetrySetting)
|
||||
|
||||
// This value can be provided to set a seperate directory for users 'homedir' resolution
|
||||
// note for mocking purpose ONLY
|
||||
var customHomeDir = os.Getenv("CUSTOM_HOMEDIR")
|
||||
|
||||
var (
|
||||
// records information on supported parameters
|
||||
supportedParameterDescriptions = map[string]string{
|
||||
UpdateNotificationSetting: UpdateNotificationSettingDescription,
|
||||
NamePrefixSetting: NamePrefixSettingDescription,
|
||||
TimeoutSetting: TimeoutSettingDescription,
|
||||
BuildTimeoutSetting: BuildTimeoutSettingDescription,
|
||||
PushTimeoutSetting: PushTimeoutSettingDescription,
|
||||
RegistryCacheTimeSetting: RegistryCacheTimeDescription,
|
||||
EphemeralSetting: EphemeralDescription,
|
||||
ConsentTelemetrySetting: ConsentTelemetryDescription,
|
||||
}
|
||||
|
||||
// set-like map to quickly check if a parameter is supported
|
||||
lowerCaseParameters = util.GetLowerCaseParameters(GetSupportedParameters())
|
||||
)
|
||||
|
||||
// PreferenceInfo wraps the preference and provides helpers to
|
||||
// serialize it.
|
||||
type PreferenceInfo struct {
|
||||
Filename string `yaml:"FileName,omitempty"`
|
||||
Preference `yaml:",omitempty"`
|
||||
}
|
||||
|
||||
// OdoSettings holds all odo specific configurations
|
||||
type OdoSettings struct {
|
||||
// Controls if an update notification is shown or not
|
||||
UpdateNotification *bool `yaml:"UpdateNotification,omitempty"`
|
||||
|
||||
// Holds the prefix part of generated random application name
|
||||
NamePrefix *string `yaml:"NamePrefix,omitempty"`
|
||||
|
||||
// Timeout for OpenShift server connection check
|
||||
Timeout *int `yaml:"Timeout,omitempty"`
|
||||
|
||||
// BuildTimeout for OpenShift build timeout check
|
||||
BuildTimeout *int `yaml:"BuildTimeout,omitempty"`
|
||||
|
||||
// PushTimeout for OpenShift pod timeout check
|
||||
PushTimeout *int `yaml:"PushTimeout,omitempty"`
|
||||
|
||||
// RegistryList for telling odo to connect to all the registries in the registry list
|
||||
RegistryList *[]Registry `yaml:"RegistryList,omitempty"`
|
||||
|
||||
// RegistryCacheTime how long odo should cache information from registry
|
||||
RegistryCacheTime *int `yaml:"RegistryCacheTime,omitempty"`
|
||||
|
||||
// Ephemeral if true creates odo emptyDir to store odo source code
|
||||
Ephemeral *bool `yaml:"Ephemeral,omitempty"`
|
||||
|
||||
// ConsentTelemetry if true collects telemetry for odo
|
||||
ConsentTelemetry *bool `yaml:"ConsentTelemetry,omitempty"`
|
||||
}
|
||||
|
||||
// Registry includes the registry metadata
|
||||
type Registry struct {
|
||||
Name string `yaml:"Name,omitempty"`
|
||||
URL string `yaml:"URL,omitempty"`
|
||||
Secure bool
|
||||
}
|
||||
|
||||
// Preference stores all the preferences related to odo
|
||||
type Preference struct {
|
||||
metav1.TypeMeta `yaml:",inline"`
|
||||
|
||||
// Odo settings holds the odo specific global settings
|
||||
OdoSettings OdoSettings `yaml:"OdoSettings,omitempty"`
|
||||
}
|
||||
|
||||
func getPreferenceFile() (string, error) {
|
||||
if env, ok := os.LookupEnv(GlobalConfigEnvName); ok {
|
||||
return env, nil
|
||||
}
|
||||
|
||||
if len(customHomeDir) != 0 {
|
||||
return filepath.Join(customHomeDir, ".odo", configFileName), nil
|
||||
}
|
||||
|
||||
currentUser, err := user.Current()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return filepath.Join(currentUser.HomeDir, ".odo", configFileName), nil
|
||||
}
|
||||
|
||||
// New returns the PreferenceInfo to retain the expected behavior
|
||||
func New() (*PreferenceInfo, error) {
|
||||
return NewPreferenceInfo()
|
||||
}
|
||||
|
||||
// NewPreference creates an empty Preference struct with type meta information
|
||||
func NewPreference() Preference {
|
||||
return Preference{
|
||||
TypeMeta: metav1.TypeMeta{
|
||||
Kind: preferenceKind,
|
||||
APIVersion: preferenceAPIVersion,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// NewPreferenceInfo gets the PreferenceInfo from preference file and creates the preference file in case it's
|
||||
// not present
|
||||
func NewPreferenceInfo() (*PreferenceInfo, error) {
|
||||
preferenceFile, err := getPreferenceFile()
|
||||
klog.V(4).Infof("The path for preference file is %+v", preferenceFile)
|
||||
if err != nil {
|
||||
return nil, errors.Errorf("unable to get odo preference file, run the command with more verbosity to check if the preference path is correct")
|
||||
}
|
||||
|
||||
c := PreferenceInfo{
|
||||
Preference: NewPreference(),
|
||||
Filename: preferenceFile,
|
||||
}
|
||||
|
||||
// Default devfile registry
|
||||
defaultRegistryList := []Registry{
|
||||
{
|
||||
Name: DefaultDevfileRegistryName,
|
||||
URL: DefaultDevfileRegistryURL,
|
||||
Secure: false,
|
||||
},
|
||||
}
|
||||
|
||||
// If the preference file doesn't exist then we return with default preference
|
||||
if _, err = os.Stat(preferenceFile); os.IsNotExist(err) {
|
||||
c.OdoSettings.RegistryList = &defaultRegistryList
|
||||
return &c, nil
|
||||
}
|
||||
|
||||
err = util.GetFromFile(&c.Preference, c.Filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Handle user has preference file but doesn't use dynamic registry before
|
||||
if c.OdoSettings.RegistryList == nil {
|
||||
c.OdoSettings.RegistryList = &defaultRegistryList
|
||||
}
|
||||
|
||||
// Handle OCI-based default registry migration
|
||||
if c.OdoSettings.RegistryList != nil {
|
||||
for index, registry := range *c.OdoSettings.RegistryList {
|
||||
if registry.Name == DefaultDevfileRegistryName && registry.URL == OldDefaultDevfileRegistryURL {
|
||||
registryList := *c.OdoSettings.RegistryList
|
||||
registryList[index].URL = DefaultDevfileRegistryURL
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return &c, nil
|
||||
}
|
||||
|
||||
// RegistryHandler handles registry add, update and delete operations
|
||||
func (c *PreferenceInfo) RegistryHandler(operation string, registryName string, registryURL string, forceFlag bool, isSecure bool) error {
|
||||
var registryList []Registry
|
||||
var err error
|
||||
registryExist := false
|
||||
|
||||
// Registry list is empty
|
||||
if c.OdoSettings.RegistryList == nil {
|
||||
registryList, err = handleWithoutRegistryExist(registryList, operation, registryName, registryURL, isSecure)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
// The target registry exists in the registry list
|
||||
registryList = *c.OdoSettings.RegistryList
|
||||
for index, registry := range registryList {
|
||||
if registry.Name == registryName {
|
||||
registryExist = true
|
||||
registryList, err = handleWithRegistryExist(index, registryList, operation, registryName, registryURL, forceFlag, isSecure)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The target registry doesn't exist in the registry list
|
||||
if !registryExist {
|
||||
registryList, err = handleWithoutRegistryExist(registryList, operation, registryName, registryURL, isSecure)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
c.OdoSettings.RegistryList = ®istryList
|
||||
err = util.WriteToFile(&c.Preference, c.Filename)
|
||||
if err != nil {
|
||||
return errors.Errorf("unable to write the configuration of %q operation to preference file", operation)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func handleWithoutRegistryExist(registryList []Registry, operation string, registryName string, registryURL string, isSecure bool) ([]Registry, error) {
|
||||
switch operation {
|
||||
|
||||
case "add":
|
||||
registry := Registry{
|
||||
Name: registryName,
|
||||
URL: registryURL,
|
||||
Secure: isSecure,
|
||||
}
|
||||
registryList = append(registryList, registry)
|
||||
|
||||
case "update":
|
||||
return nil, errors.Errorf("failed to update registry: registry %q doesn't exist", registryName)
|
||||
|
||||
case "delete":
|
||||
return nil, errors.Errorf("failed to delete registry: registry %q doesn't exist", registryName)
|
||||
}
|
||||
|
||||
return registryList, nil
|
||||
}
|
||||
|
||||
func handleWithRegistryExist(index int, registryList []Registry, operation string, registryName string, registryURL string, forceFlag bool, isSecure bool) ([]Registry, error) {
|
||||
switch operation {
|
||||
|
||||
case "add":
|
||||
return nil, errors.Errorf("failed to add registry: registry %q already exists", registryName)
|
||||
|
||||
case "update":
|
||||
if !forceFlag {
|
||||
if !ui.Proceed(fmt.Sprintf("Are you sure you want to update registry %q", registryName)) {
|
||||
log.Info("Aborted by the user")
|
||||
return registryList, nil
|
||||
}
|
||||
}
|
||||
|
||||
registryList[index].URL = registryURL
|
||||
registryList[index].Secure = isSecure
|
||||
log.Info("Successfully updated registry")
|
||||
|
||||
case "delete":
|
||||
if !forceFlag {
|
||||
if !ui.Proceed(fmt.Sprintf("Are you sure you want to delete registry %q", registryName)) {
|
||||
log.Info("Aborted by the user")
|
||||
return registryList, nil
|
||||
}
|
||||
}
|
||||
|
||||
copy(registryList[index:], registryList[index+1:])
|
||||
registryList[len(registryList)-1] = Registry{}
|
||||
registryList = registryList[:len(registryList)-1]
|
||||
log.Info("Successfully deleted registry")
|
||||
}
|
||||
|
||||
return registryList, nil
|
||||
}
|
||||
|
||||
// SetConfiguration modifies Odo configurations in the config file
|
||||
// as of now being used for nameprefix, timeout, updatenotification
|
||||
// TODO: Use reflect to set parameters
|
||||
func (c *PreferenceInfo) SetConfiguration(parameter string, value string) error {
|
||||
if p, ok := asSupportedParameter(parameter); ok {
|
||||
// processing values according to the parameter names
|
||||
switch p {
|
||||
|
||||
case "timeout":
|
||||
typedval, err := strconv.Atoi(value)
|
||||
if err != nil {
|
||||
return errors.Errorf("unable to set %q to %q", parameter, value)
|
||||
}
|
||||
if typedval < 0 {
|
||||
return errors.Errorf("cannot set timeout to less than 0")
|
||||
}
|
||||
c.OdoSettings.Timeout = &typedval
|
||||
|
||||
case "buildtimeout":
|
||||
typedval, err := strconv.Atoi(value)
|
||||
if err != nil {
|
||||
return errors.Errorf("unable to set %q to %q, value must be an integer", parameter, value)
|
||||
}
|
||||
if typedval < 0 {
|
||||
return errors.Errorf("cannot set timeout to less than 0")
|
||||
}
|
||||
c.OdoSettings.BuildTimeout = &typedval
|
||||
|
||||
case "pushtimeout":
|
||||
typedval, err := strconv.Atoi(value)
|
||||
if err != nil {
|
||||
return errors.Errorf("unable to set %q to %q, value must be an integer", parameter, value)
|
||||
}
|
||||
if typedval < 0 {
|
||||
return errors.Errorf("cannot set timeout to less than 0")
|
||||
}
|
||||
c.OdoSettings.PushTimeout = &typedval
|
||||
|
||||
case "registrycachetime":
|
||||
typedval, err := strconv.Atoi(value)
|
||||
if err != nil {
|
||||
return errors.Errorf("unable to set %q to %q, value must be an integer", parameter, value)
|
||||
}
|
||||
if typedval < 0 {
|
||||
return errors.Errorf("cannot set timeout to less than 0")
|
||||
}
|
||||
c.OdoSettings.RegistryCacheTime = &typedval
|
||||
|
||||
case "updatenotification":
|
||||
val, err := strconv.ParseBool(strings.ToLower(value))
|
||||
if err != nil {
|
||||
return errors.Errorf("unable to set %q to %q, value must be a boolean", parameter, value)
|
||||
}
|
||||
c.OdoSettings.UpdateNotification = &val
|
||||
|
||||
// TODO: should we add a validator here? What is the use of nameprefix?
|
||||
case "nameprefix":
|
||||
c.OdoSettings.NamePrefix = &value
|
||||
|
||||
case "ephemeral":
|
||||
val, err := strconv.ParseBool(strings.ToLower(value))
|
||||
if err != nil {
|
||||
return errors.Errorf("unable to set %q to %q, value must be a boolean", parameter, value)
|
||||
}
|
||||
c.OdoSettings.Ephemeral = &val
|
||||
|
||||
case "consenttelemetry":
|
||||
val, err := strconv.ParseBool(strings.ToLower(value))
|
||||
if err != nil {
|
||||
return errors.Errorf("unable to set %q to %q, value must be a boolean", parameter, value)
|
||||
}
|
||||
c.OdoSettings.ConsentTelemetry = &val
|
||||
}
|
||||
} else {
|
||||
return errors.Errorf("unknown parameter : %q is not a parameter in odo preference, run help to see list of available parameters", parameter)
|
||||
}
|
||||
|
||||
err := util.WriteToFile(&c.Preference, c.Filename)
|
||||
if err != nil {
|
||||
return errors.Errorf("unable to set %q, something is wrong with odo, kindly raise an issue at https://github.com/redhat-developer/odo/issues/new?template=Bug.md", parameter)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteConfiguration delete Odo configurations in the global config file
|
||||
// as of now being used for nameprefix, timeout, updatenotification
|
||||
func (c *PreferenceInfo) DeleteConfiguration(parameter string) error {
|
||||
if p, ok := asSupportedParameter(parameter); ok {
|
||||
// processing values according to the parameter names
|
||||
|
||||
if err := util.DeleteConfiguration(&c.OdoSettings, p); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
return errors.Errorf("unknown parameter :%q is not a parameter in the odo preference", parameter)
|
||||
}
|
||||
|
||||
err := util.WriteToFile(&c.Preference, c.Filename)
|
||||
if err != nil {
|
||||
return errors.Errorf("unable to set %q, something is wrong with odo, kindly raise an issue at https://github.com/redhat-developer/odo/issues/new?template=Bug.md", parameter)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// IsSet checks if the value is set in the preference
|
||||
func (c *PreferenceInfo) IsSet(parameter string) bool {
|
||||
return util.IsSet(c.OdoSettings, parameter)
|
||||
}
|
||||
|
||||
// GetTimeout returns the value of Timeout from config
|
||||
// and if absent then returns default
|
||||
func (c *PreferenceInfo) GetTimeout() int {
|
||||
// default timeout value is 1
|
||||
return util.GetIntOrDefault(c.OdoSettings.Timeout, DefaultTimeout)
|
||||
}
|
||||
|
||||
// GetBuildTimeout gets the value set by BuildTimeout
|
||||
func (c *PreferenceInfo) GetBuildTimeout() int {
|
||||
// default timeout value is 300
|
||||
return util.GetIntOrDefault(c.OdoSettings.BuildTimeout, DefaultBuildTimeout)
|
||||
}
|
||||
|
||||
// GetPushTimeout gets the value set by PushTimeout
|
||||
func (c *PreferenceInfo) GetPushTimeout() int {
|
||||
// default timeout value is 1
|
||||
return util.GetIntOrDefault(c.OdoSettings.PushTimeout, DefaultPushTimeout)
|
||||
}
|
||||
|
||||
// GetRegistryCacheTime gets the value set by RegistryCacheTime
|
||||
func (c *PreferenceInfo) GetRegistryCacheTime() int {
|
||||
return util.GetIntOrDefault(c.OdoSettings.RegistryCacheTime, DefaultRegistryCacheTime)
|
||||
}
|
||||
|
||||
// GetUpdateNotification returns the value of UpdateNotification from preferences
|
||||
// and if absent then returns default
|
||||
func (c *PreferenceInfo) GetUpdateNotification() bool {
|
||||
return util.GetBoolOrDefault(c.OdoSettings.UpdateNotification, true)
|
||||
}
|
||||
|
||||
// GetEphemeralSourceVolume returns the value of ephemeral from preferences
|
||||
// and if absent then returns default
|
||||
func (c *PreferenceInfo) GetEphemeralSourceVolume() bool {
|
||||
return util.GetBoolOrDefault(c.OdoSettings.Ephemeral, DefaultEphemeralSettings)
|
||||
}
|
||||
|
||||
// GetNamePrefix returns the value of Prefix from preferences
|
||||
// and if absent then returns default
|
||||
func (c *PreferenceInfo) GetNamePrefix() string {
|
||||
return util.GetStringOrEmpty(c.OdoSettings.NamePrefix)
|
||||
}
|
||||
|
||||
// GetConsentTelemetry returns the value of ConsentTelemetry from preferences
|
||||
// and if absent then returns default
|
||||
// default value: false, consent telemetry is disabled by default
|
||||
func (c *PreferenceInfo) GetConsentTelemetry() bool {
|
||||
return util.GetBoolOrDefault(c.OdoSettings.ConsentTelemetry, DefaultConsentTelemetrySetting)
|
||||
}
|
||||
|
||||
// FormatSupportedParameters outputs supported parameters and their description
|
||||
func FormatSupportedParameters() (result string) {
|
||||
for _, v := range GetSupportedParameters() {
|
||||
result = result + " " + v + " - " + supportedParameterDescriptions[v] + "\n"
|
||||
}
|
||||
return "\nAvailable Global Parameters:\n" + result
|
||||
}
|
||||
|
||||
// asSupportedParameter checks that the given parameter is supported and returns a lower case version of it if it is
|
||||
func asSupportedParameter(param string) (string, bool) {
|
||||
lower := strings.ToLower(param)
|
||||
return lower, lowerCaseParameters[lower]
|
||||
}
|
||||
|
||||
// GetSupportedParameters returns the name of the supported parameters
|
||||
func GetSupportedParameters() []string {
|
||||
return util.GetSortedKeys(supportedParameterDescriptions)
|
||||
NewPreferenceList() PreferenceList
|
||||
}
|
||||
|
||||
111
pkg/preference/variables.go
Normal file
111
pkg/preference/variables.go
Normal file
@@ -0,0 +1,111 @@
|
||||
package preference
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/redhat-developer/odo/pkg/util"
|
||||
)
|
||||
|
||||
const (
|
||||
GlobalConfigEnvName = "GLOBALODOCONFIG"
|
||||
configFileName = "preference.yaml"
|
||||
preferenceKind = "Preference"
|
||||
preferenceAPIVersion = "odo.dev/v1alpha1"
|
||||
|
||||
//DefaultTimeout for openshift server connection check (in seconds)
|
||||
DefaultTimeout = 1
|
||||
|
||||
// DefaultPushTimeout is the default timeout for pods (in seconds)
|
||||
DefaultPushTimeout = 240
|
||||
|
||||
// DefaultBuildTimeout is the default build timeout for pods (in seconds)
|
||||
DefaultBuildTimeout = 300
|
||||
|
||||
// UpdateNotificationSetting is the name of the setting controlling update notification
|
||||
UpdateNotificationSetting = "UpdateNotification"
|
||||
|
||||
// UpdateNotificationSettingDescription is human-readable description for the update notification setting
|
||||
UpdateNotificationSettingDescription = "Flag to control if an update notification is shown or not (Default: true)"
|
||||
|
||||
// NamePrefixSetting is the name of the setting controlling name prefix
|
||||
NamePrefixSetting = "NamePrefix"
|
||||
|
||||
// NamePrefixSettingDescription is human-readable description for the name prefix setting
|
||||
NamePrefixSettingDescription = "Use this value to set a default name prefix (Default: current directory name)"
|
||||
|
||||
// TimeoutSetting is the name of the setting controlling timeout for connection check
|
||||
TimeoutSetting = "Timeout"
|
||||
|
||||
// BuildTimeoutSetting is the name of the setting controlling BuildTimeout
|
||||
BuildTimeoutSetting = "BuildTimeout"
|
||||
|
||||
// PushTimeoutSetting is the name of the setting controlling PushTimeout
|
||||
PushTimeoutSetting = "PushTimeout"
|
||||
|
||||
// RegistryCacheTimeSetting is human-readable description for the registrycachetime setting
|
||||
RegistryCacheTimeSetting = "RegistryCacheTime"
|
||||
|
||||
// DefaultDevfileRegistryName is the name of default devfile registry
|
||||
DefaultDevfileRegistryName = "DefaultDevfileRegistry"
|
||||
|
||||
// DefaultDevfileRegistryURL is the URL of default devfile registry
|
||||
DefaultDevfileRegistryURL = "https://registry.devfile.io"
|
||||
|
||||
// OldDefaultDevfileRegistryURL is the URL of old default devfile registry for registry migration purpose
|
||||
OldDefaultDevfileRegistryURL = "https://github.com/odo-devfiles/registry"
|
||||
|
||||
// DefaultRegistryCacheTime is time (in minutes) for how long odo will cache information from Devfile registry
|
||||
DefaultRegistryCacheTime = 15
|
||||
|
||||
// EphemeralSetting specifies if ephemeral volumes needs to be used as source volume.
|
||||
EphemeralSetting = "Ephemeral"
|
||||
|
||||
// DefaultEphemeralSettings is a default value for Ephemeral preference
|
||||
DefaultEphemeralSettings = true
|
||||
|
||||
// ConsentTelemetrySettings specifies if the user consents to telemetry
|
||||
ConsentTelemetrySetting = "ConsentTelemetry"
|
||||
|
||||
// DefaultConsentTelemetry is a default value for ConsentTelemetry preference
|
||||
DefaultConsentTelemetrySetting = false
|
||||
)
|
||||
|
||||
// TimeoutSettingDescription is human-readable description for the timeout setting
|
||||
var TimeoutSettingDescription = fmt.Sprintf("Timeout (in seconds) for OpenShift server connection check (Default: %d)", DefaultTimeout)
|
||||
|
||||
// PushTimeoutSettingDescription adds a description for PushTimeout
|
||||
var PushTimeoutSettingDescription = fmt.Sprintf("PushTimeout (in seconds) for waiting for a Pod to come up (Default: %d)", DefaultPushTimeout)
|
||||
|
||||
// BuildTimeoutSettingDescription adds a description for BuildTimeout
|
||||
var BuildTimeoutSettingDescription = fmt.Sprintf("BuildTimeout (in seconds) for waiting for a build of the git component to complete (Default: %d)", DefaultBuildTimeout)
|
||||
|
||||
// RegistryCacheTimeDescription adds a description for RegistryCacheTime
|
||||
var RegistryCacheTimeDescription = fmt.Sprintf("For how long (in minutes) odo will cache information from Devfile registry (Default: %d)", DefaultRegistryCacheTime)
|
||||
|
||||
// EphemeralDescription adds a description for EphemeralSourceVolume
|
||||
var EphemeralDescription = fmt.Sprintf("If true, odo will create an emptyDir volume to store source code (Default: %t)", DefaultEphemeralSettings)
|
||||
|
||||
//TelemetryConsentDescription adds a description for TelemetryConsentSetting
|
||||
var ConsentTelemetryDescription = fmt.Sprintf("If true, odo will collect telemetry for the user's odo usage (Default: %t)\n\t\t For more information: https://developers.redhat.com/article/tool-data-collection", DefaultConsentTelemetrySetting)
|
||||
|
||||
// This value can be provided to set a seperate directory for users 'homedir' resolution
|
||||
// note for mocking purpose ONLY
|
||||
var customHomeDir = os.Getenv("CUSTOM_HOMEDIR")
|
||||
|
||||
var (
|
||||
// records information on supported parameters
|
||||
supportedParameterDescriptions = map[string]string{
|
||||
UpdateNotificationSetting: UpdateNotificationSettingDescription,
|
||||
NamePrefixSetting: NamePrefixSettingDescription,
|
||||
TimeoutSetting: TimeoutSettingDescription,
|
||||
BuildTimeoutSetting: BuildTimeoutSettingDescription,
|
||||
PushTimeoutSetting: PushTimeoutSettingDescription,
|
||||
RegistryCacheTimeSetting: RegistryCacheTimeDescription,
|
||||
EphemeralSetting: EphemeralDescription,
|
||||
ConsentTelemetrySetting: ConsentTelemetryDescription,
|
||||
}
|
||||
|
||||
// set-like map to quickly check if a parameter is supported
|
||||
lowerCaseParameters = util.GetLowerCaseParameters(GetSupportedParameters())
|
||||
)
|
||||
@@ -17,7 +17,8 @@ func getTelemetryForDevfileRegistry() (registryLibrary.TelemetryData, error) {
|
||||
Client: registryConsts.TelemetryClient,
|
||||
}
|
||||
|
||||
cfg, err := preference.New()
|
||||
// TODO(feloy) Get from DI
|
||||
cfg, err := preference.NewClient()
|
||||
if err != nil {
|
||||
return td, err
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ func TestGetRegistryOptions(t *testing.T) {
|
||||
tests := []struct {
|
||||
testName string
|
||||
consent string
|
||||
cfg *preference.PreferenceInfo
|
||||
cfg preference.Client
|
||||
}{
|
||||
{
|
||||
testName: "Registry options with telemetry consent",
|
||||
@@ -42,7 +42,7 @@ func TestGetRegistryOptions(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.testName, func(t *testing.T) {
|
||||
cfg, err := preference.NewPreferenceInfo()
|
||||
cfg, err := preference.NewClient()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
@@ -54,13 +54,13 @@ type Client struct {
|
||||
// SegmentClient helps interact with the segment API
|
||||
SegmentClient analytics.Client
|
||||
// Preference points to the global odo config
|
||||
Preference *preference.PreferenceInfo
|
||||
Preference preference.Client
|
||||
// TelemetryFilePath points to the file containing anonymousID used for tracking odo commands executed by the user
|
||||
TelemetryFilePath string
|
||||
}
|
||||
|
||||
// NewClient returns a Client created with the default args
|
||||
func NewClient(preference *preference.PreferenceInfo) (*Client, error) {
|
||||
func NewClient(preference preference.Client) (*Client, error) {
|
||||
return newCustomClient(preference,
|
||||
GetTelemetryFilePath(),
|
||||
analytics.DefaultEndpoint,
|
||||
@@ -68,7 +68,7 @@ func NewClient(preference *preference.PreferenceInfo) (*Client, error) {
|
||||
}
|
||||
|
||||
// newCustomClient returns a Client created with custom args
|
||||
func newCustomClient(preference *preference.PreferenceInfo, telemetryFilePath string, segmentEndpoint string) (*Client, error) {
|
||||
func newCustomClient(preference preference.Client, telemetryFilePath string, segmentEndpoint string) (*Client, error) {
|
||||
// get the locale information
|
||||
tag, err := locale.Detect()
|
||||
if err != nil {
|
||||
@@ -247,16 +247,8 @@ func RunningInTerminal() bool {
|
||||
}
|
||||
|
||||
// IsTelemetryEnabled returns true if user has consented to telemetry
|
||||
func IsTelemetryEnabled(cfg *preference.PreferenceInfo) bool {
|
||||
func IsTelemetryEnabled(cfg preference.Client) bool {
|
||||
klog.V(4).Info("Checking telemetry enable status")
|
||||
var err error
|
||||
if cfg == nil {
|
||||
cfg, err = preference.New()
|
||||
if err != nil {
|
||||
klog.V(3).Info(errors.Wrap(err, "unable to read config file"))
|
||||
return false
|
||||
}
|
||||
}
|
||||
// The env variable gets precedence in this decision.
|
||||
// In case a non-bool value was passed to the env var, we ignore it
|
||||
disableTelemetry, _ := strconv.ParseBool(os.Getenv(DisableTelemetryEnv))
|
||||
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/redhat-developer/odo/pkg/kclient"
|
||||
"github.com/redhat-developer/odo/pkg/testingutil/filesystem"
|
||||
|
||||
@@ -63,15 +64,11 @@ func TestClientUploadWithoutConsent(t *testing.T) {
|
||||
body, server := mockServer()
|
||||
defer server.Close()
|
||||
defer close(body)
|
||||
falseValue := false
|
||||
|
||||
cfg := &preference.PreferenceInfo{
|
||||
Preference: preference.Preference{
|
||||
OdoSettings: preference.OdoSettings{
|
||||
ConsentTelemetry: &falseValue,
|
||||
},
|
||||
},
|
||||
}
|
||||
ctrl := gomock.NewController(t)
|
||||
cfg := preference.NewMockClient(ctrl)
|
||||
cfg.EXPECT().GetConsentTelemetry().Return(false)
|
||||
|
||||
c, err := newCustomClient(cfg, createConfigDir(t), server.URL)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
@@ -100,15 +97,6 @@ func TestClientUploadWithConsent(t *testing.T) {
|
||||
defer server.Close()
|
||||
defer close(body)
|
||||
|
||||
trueValue := true
|
||||
|
||||
cfg := &preference.PreferenceInfo{
|
||||
Preference: preference.Preference{
|
||||
OdoSettings: preference.OdoSettings{
|
||||
ConsentTelemetry: &trueValue,
|
||||
},
|
||||
},
|
||||
}
|
||||
tests := []struct {
|
||||
cmd string
|
||||
testName string
|
||||
@@ -135,6 +123,10 @@ func TestClientUploadWithConsent(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
t.Log("Running test: ", tt.testName)
|
||||
t.Run(tt.testName, func(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
cfg := preference.NewMockClient(ctrl)
|
||||
cfg.EXPECT().GetConsentTelemetry().Return(true)
|
||||
|
||||
c, err := newCustomClient(cfg, createConfigDir(t), server.URL)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
@@ -239,13 +231,10 @@ func TestIsTelemetryEnabled(t *testing.T) {
|
||||
}
|
||||
for _, tt := range tests {
|
||||
os.Setenv(DisableTelemetryEnv, tt.envVar)
|
||||
cfg := &preference.PreferenceInfo{
|
||||
Preference: preference.Preference{
|
||||
OdoSettings: preference.OdoSettings{
|
||||
ConsentTelemetry: &tt.preferenceValue,
|
||||
},
|
||||
},
|
||||
}
|
||||
ctrl := gomock.NewController(t)
|
||||
cfg := preference.NewMockClient(ctrl)
|
||||
cfg.EXPECT().GetConsentTelemetry().Return(tt.preferenceValue).AnyTimes()
|
||||
|
||||
if IsTelemetryEnabled(cfg) != tt.want {
|
||||
t.Errorf(tt.errMesssage, "%s is set to %q. %s is set to %q.", DisableTelemetryEnv, tt.envVar, preference.ConsentTelemetrySetting, tt.preferenceValue)
|
||||
}
|
||||
@@ -257,15 +246,10 @@ func TestClientUploadWithContext(t *testing.T) {
|
||||
body, server := mockServer()
|
||||
defer server.Close()
|
||||
defer close(body)
|
||||
trueValue := true
|
||||
|
||||
cfg := &preference.PreferenceInfo{
|
||||
Preference: preference.Preference{
|
||||
OdoSettings: preference.OdoSettings{
|
||||
ConsentTelemetry: &trueValue,
|
||||
},
|
||||
},
|
||||
}
|
||||
ctrl := gomock.NewController(t)
|
||||
cfg := preference.NewMockClient(ctrl)
|
||||
cfg.EXPECT().GetConsentTelemetry().Return(true).AnyTimes()
|
||||
ctx := scontext.NewContext(context.Background())
|
||||
|
||||
for k, v := range map[string]string{scontext.ComponentType: "nodejs", scontext.ClusterType: ""} {
|
||||
|
||||
@@ -1,180 +0,0 @@
|
||||
package testingutil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/ghodss/yaml"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/redhat-developer/odo/pkg/preference"
|
||||
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
|
||||
)
|
||||
|
||||
// This value can be provided to set a seperate directory for users 'homedir' resolution
|
||||
// note for mocking purpose ONLY
|
||||
var customHomeDir = os.Getenv("CUSTOM_HOMEDIR")
|
||||
|
||||
// ConfigDetails struct holds configuration details(odo and/or kube config)
|
||||
type ConfigDetails struct {
|
||||
FileName string
|
||||
Config interface{}
|
||||
ConfigPathEnv string
|
||||
}
|
||||
|
||||
// getConfFolder generates a mock config folder for the unit testing
|
||||
func getConfFolder() (string, error) {
|
||||
var confLocation string
|
||||
// If custom home dir is set, skip checking for user.Current to place config
|
||||
if len(customHomeDir) != 0 {
|
||||
confLocation = customHomeDir
|
||||
} else {
|
||||
currentUser, err := user.Current()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
confLocation = currentUser.HomeDir
|
||||
}
|
||||
|
||||
dir, err := ioutil.TempDir(confLocation, ".odo")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return dir, nil
|
||||
}
|
||||
|
||||
// setupTempConfigFile takes config file name - confFile and creates it for unit testing
|
||||
// The invocation of setupTempConfigFile puts the onus of invoking the configCleanUp as well
|
||||
func setupTempConfigFile(confFile string) (*os.File, error) {
|
||||
confFolder, err := getConfFolder()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tmpfile, err := ioutil.TempFile(confFolder, confFile)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "unable to create test config file")
|
||||
}
|
||||
return tmpfile, nil
|
||||
}
|
||||
|
||||
// setupEnv takes odoConfigFile name and sets env var ODOCONFIG to odoConfigFile
|
||||
// The config logic relies on this env var(if present) to read and/or write config
|
||||
func setupEnv(envName string, odoconfigfile string) error {
|
||||
err := os.Setenv(envName, odoconfigfile)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, fmt.Sprintf("unable to set %s to %s", envName, odoconfigfile))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetUp sets up the odo and kube config files and returns respective conf file pointers and error
|
||||
func SetUp(odoConfigDetails ConfigDetails, kubeConfigDetails ConfigDetails) (*os.File, *os.File, error) {
|
||||
odoConfigFile, err := setUpConfig(odoConfigDetails.FileName, odoConfigDetails.Config, odoConfigDetails.ConfigPathEnv)
|
||||
if err != nil {
|
||||
return odoConfigFile, nil, err
|
||||
}
|
||||
kubeConfigFile, err := setUpConfig(kubeConfigDetails.FileName, kubeConfigDetails.Config, kubeConfigDetails.ConfigPathEnv)
|
||||
return odoConfigFile, kubeConfigFile, err
|
||||
}
|
||||
|
||||
// setUpConfig sets up mock config
|
||||
// Parameters:
|
||||
// conf: the config object to write to the mock config file
|
||||
// testFile: the name of the mock config file
|
||||
// configEnvName: Name of env variable that corresponds to config file
|
||||
// Returns:
|
||||
// file handler for the mock config file
|
||||
// error if any
|
||||
|
||||
func setUpConfig(testFile string, conf interface{}, configEnvName string) (*os.File, error) {
|
||||
foundConfigType := false
|
||||
var err error
|
||||
var data []byte
|
||||
if conf, ok := conf.(preference.PreferenceInfo); ok {
|
||||
data, err = yaml.Marshal(conf.Preference)
|
||||
foundConfigType = true
|
||||
}
|
||||
if conf, ok := conf.(clientcmdapi.Config); ok {
|
||||
data, err = yaml.Marshal(conf)
|
||||
foundConfigType = true
|
||||
}
|
||||
if conf, ok := conf.(string); ok {
|
||||
data = []byte(conf)
|
||||
foundConfigType = true
|
||||
}
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "unable to create mock config file")
|
||||
}
|
||||
if !foundConfigType {
|
||||
return nil, fmt.Errorf("Config %+v not of recognisable type", conf)
|
||||
}
|
||||
configFile, err := setupTempConfigFile(testFile)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "unable to create mock config file")
|
||||
}
|
||||
if conf != nil {
|
||||
if _, err := configFile.Write(data); err != nil {
|
||||
return nil, errors.Wrapf(err, "unable to write config %+v to mock config file %s", conf, configFile.Name())
|
||||
}
|
||||
}
|
||||
return configFile, setupEnv(configEnvName, configFile.Name())
|
||||
}
|
||||
|
||||
// CleanupEnv cleans up the mock config file and anything that SetupEnv generated
|
||||
// Parameters:
|
||||
// configFile: the mock config file handler
|
||||
// t: testing pointer to log errors if any
|
||||
func CleanupEnv(confFiles []*os.File, t *testing.T) {
|
||||
for _, confFile := range confFiles {
|
||||
if confFile == nil {
|
||||
continue
|
||||
}
|
||||
if err := confFile.Close(); err != nil {
|
||||
t.Errorf("failed to cleanup the test env. Error: %v", err)
|
||||
}
|
||||
os.Remove(confFile.Name())
|
||||
os.Remove(filepath.Dir(confFile.Name()))
|
||||
}
|
||||
}
|
||||
|
||||
// FakeOdoConfig returns mock odo config
|
||||
// It takes a confPath which is the path to the config
|
||||
func FakeOdoConfig(confPath string, needNamePrefix bool, namePrefix string) preference.PreferenceInfo {
|
||||
odoConfig := preference.PreferenceInfo{
|
||||
Filename: confPath,
|
||||
Preference: preference.Preference{},
|
||||
}
|
||||
if needNamePrefix {
|
||||
odoConfig.OdoSettings = preference.OdoSettings{
|
||||
NamePrefix: &namePrefix,
|
||||
}
|
||||
}
|
||||
return odoConfig
|
||||
}
|
||||
|
||||
// FakeKubeClientConfig returns mock kube client config
|
||||
func FakeKubeClientConfig() string {
|
||||
return `apiVersion: v1
|
||||
clusters:
|
||||
- cluster:
|
||||
insecure-skip-tls-verify: true
|
||||
server: https://192.168.42.237:8443
|
||||
name: 192-168-42-237:8443
|
||||
contexts:
|
||||
- context:
|
||||
cluster: 192-168-42-237:8443
|
||||
namespace: testing
|
||||
user: developer/192-168-42-237:8443
|
||||
name: myproject/192-168-42-237:8443/developer
|
||||
current-context: myproject/192-168-42-237:8443/developer
|
||||
kind: Config
|
||||
preferences: {}
|
||||
users:
|
||||
- name: developer/192-168-42-237:8443
|
||||
user:
|
||||
token: C0E6Gkmi3n_Se2QKx6Unw3Y3Zu4mJHgzdrMVK0DsDwc`
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
package testingutil
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCustomHomeDir(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
wanterr bool
|
||||
}{
|
||||
{
|
||||
name: "Test if specifying customDir results in appropriate resolution of config dir",
|
||||
wanterr: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
oldCustomHomeDir := customHomeDir
|
||||
var err error
|
||||
customHomeDir, err = ioutil.TempDir("", "testconf")
|
||||
if err != nil {
|
||||
t.Error(err.Error())
|
||||
}
|
||||
odoConfigFile, kubeConfigFile, err := SetUp(
|
||||
ConfigDetails{
|
||||
FileName: "odo-test-config",
|
||||
Config: FakeOdoConfig("odo-test-config", false, ""),
|
||||
ConfigPathEnv: "GLOBALODOCONFIG",
|
||||
}, ConfigDetails{
|
||||
FileName: "kube-test-config",
|
||||
Config: FakeKubeClientConfig(),
|
||||
ConfigPathEnv: "KUBECONFIG",
|
||||
},
|
||||
)
|
||||
defer CleanupEnv([]*os.File{odoConfigFile, kubeConfigFile}, t)
|
||||
if err != nil {
|
||||
t.Errorf("failed to create mock odo and kube config files. Error %v", err)
|
||||
}
|
||||
customHomeDir = oldCustomHomeDir
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -34,3 +34,7 @@ mockgen -source=pkg/application/application.go \
|
||||
mockgen -source=pkg/project/project.go \
|
||||
-package project \
|
||||
-destination pkg/project/mock.go
|
||||
|
||||
mockgen -source=pkg/preference/preference.go \
|
||||
-package preference \
|
||||
-destination pkg/preference/mock.go
|
||||
|
||||
@@ -299,7 +299,7 @@ func CommonBeforeEach() CommonVar {
|
||||
commonVar.OriginalWorkingDirectory = Getwd()
|
||||
os.Setenv("GLOBALODOCONFIG", filepath.Join(commonVar.Context, "preference.yaml"))
|
||||
// Set ConsentTelemetry to false so that it does not prompt to set a preference value
|
||||
cfg, _ := preference.New()
|
||||
cfg, _ := preference.NewClient()
|
||||
err := cfg.SetConfiguration(preference.ConsentTelemetrySetting, "false")
|
||||
Expect(err).To(BeNil())
|
||||
SetDefaultDevfileRegistryAsStaging()
|
||||
|
||||
1
vendor/modules.txt
vendored
1
vendor/modules.txt
vendored
@@ -1132,6 +1132,7 @@ k8s.io/kubectl/pkg/util/templates
|
||||
k8s.io/kubectl/pkg/util/term
|
||||
k8s.io/kubectl/pkg/validation
|
||||
# k8s.io/utils v0.0.0-20210819203725-bdf08cb9a70a
|
||||
## explicit
|
||||
k8s.io/utils/buffer
|
||||
k8s.io/utils/exec
|
||||
k8s.io/utils/integer
|
||||
|
||||
Reference in New Issue
Block a user