diff --git a/internal/cmd/alpha/hana/check.go b/internal/cmd/alpha/hana/check.go index 3d0619a5f..1d639a203 100644 --- a/internal/cmd/alpha/hana/check.go +++ b/internal/cmd/alpha/hana/check.go @@ -71,7 +71,12 @@ func runCheck(config *hanaCheckConfig) clierror.Error { } func checkHanaInstance(config *hanaCheckConfig) clierror.Error { - instance, err := config.KubeClient.Btp().GetServiceInstance(config.Ctx, config.namespace, config.name) + client, clientErr := config.GetKubeClientWithClierr() + if clientErr != nil { + return clientErr + } + + instance, err := client.Btp().GetServiceInstance(config.Ctx, config.namespace, config.name) if err != nil { return clierror.New(err.Error()) } @@ -80,7 +85,12 @@ func checkHanaInstance(config *hanaCheckConfig) clierror.Error { } func checkHanaBinding(config *hanaCheckConfig) clierror.Error { - binding, err := config.KubeClient.Btp().GetServiceBinding(config.Ctx, config.namespace, config.name) + client, clientErr := config.GetKubeClientWithClierr() + if clientErr != nil { + return clientErr + } + + binding, err := client.Btp().GetServiceBinding(config.Ctx, config.namespace, config.name) if err != nil { return clierror.New(err.Error()) } @@ -89,8 +99,13 @@ func checkHanaBinding(config *hanaCheckConfig) clierror.Error { } func checkHanaBindingURL(config *hanaCheckConfig) clierror.Error { + client, clientErr := config.GetKubeClientWithClierr() + if clientErr != nil { + return clientErr + } + urlName := hanaBindingURLName(config.name) - binding, err := config.KubeClient.Btp().GetServiceBinding(config.Ctx, config.namespace, urlName) + binding, err := client.Btp().GetServiceBinding(config.Ctx, config.namespace, urlName) if err != nil { return clierror.New(err.Error()) } diff --git a/internal/cmd/alpha/hana/credentials.go b/internal/cmd/alpha/hana/credentials.go index 2d397f2e0..364a6a59e 100644 --- a/internal/cmd/alpha/hana/credentials.go +++ b/internal/cmd/alpha/hana/credentials.go @@ -71,7 +71,12 @@ func printCredentials(config *hanaCredentialsConfig, credentials credentials) { } func getHanaCredentials(config *hanaCredentialsConfig) (credentials, clierror.Error) { - secret, err := config.KubeClient.Static().CoreV1().Secrets(config.namespace).Get(config.Ctx, config.name, metav1.GetOptions{}) + client, clientErr := config.GetKubeClientWithClierr() + if clientErr != nil { + return credentials{}, clientErr + } + + secret, err := client.Static().CoreV1().Secrets(config.namespace).Get(config.Ctx, config.name, metav1.GetOptions{}) if err != nil { return handleGetHanaCredentialsError(err) } diff --git a/internal/cmd/alpha/hana/delete.go b/internal/cmd/alpha/hana/delete.go index 1303905b7..7be89cf3f 100644 --- a/internal/cmd/alpha/hana/delete.go +++ b/internal/cmd/alpha/hana/delete.go @@ -62,22 +62,37 @@ func runDelete(config *hanaDeleteConfig) clierror.Error { } func deleteHanaInstance(config *hanaDeleteConfig) clierror.Error { - err := config.KubeClient.Dynamic().Resource(btp.GVRServiceInstance). + client, clientErr := config.GetKubeClientWithClierr() + if clientErr != nil { + return clientErr + } + + err := client.Dynamic().Resource(btp.GVRServiceInstance). Namespace(config.namespace). Delete(config.Ctx, config.name, metav1.DeleteOptions{}) return handleDeleteResponse(err, "Hana instance", config.namespace, config.name) } func deleteHanaBinding(config *hanaDeleteConfig) clierror.Error { - err := config.KubeClient.Dynamic().Resource(btp.GVRServiceBinding). + client, clientErr := config.GetKubeClientWithClierr() + if clientErr != nil { + return clientErr + } + + err := client.Dynamic().Resource(btp.GVRServiceBinding). Namespace(config.namespace). Delete(config.Ctx, config.name, metav1.DeleteOptions{}) return handleDeleteResponse(err, "Hana binding", config.namespace, config.name) } func deleteHanaBindingURL(config *hanaDeleteConfig) clierror.Error { + client, clientErr := config.GetKubeClientWithClierr() + if clientErr != nil { + return clientErr + } + urlName := hanaBindingURLName(config.name) - err := config.KubeClient.Dynamic().Resource(btp.GVRServiceBinding). + err := client.Dynamic().Resource(btp.GVRServiceBinding). Namespace(config.namespace). Delete(config.Ctx, urlName, metav1.DeleteOptions{}) return handleDeleteResponse(err, "Hana URL binding", config.namespace, urlName) diff --git a/internal/cmd/alpha/hana/map.go b/internal/cmd/alpha/hana/map.go index 30f1de765..13aa5de46 100644 --- a/internal/cmd/alpha/hana/map.go +++ b/internal/cmd/alpha/hana/map.go @@ -62,8 +62,13 @@ func runMap(config *hanaCheckConfig) clierror.Error { } func createHanaAPIInstanceIfNeeded(config *hanaCheckConfig) clierror.Error { + client, clientErr := config.GetKubeClientWithClierr() + if clientErr != nil { + return clientErr + } + // check if instance exists, skip API instance creation if it does - instance, err := config.KubeClient.Btp().GetServiceInstance(config.Ctx, config.namespace, hanaBindingAPIName(config.name)) + instance, err := client.Btp().GetServiceInstance(config.Ctx, config.namespace, hanaBindingAPIName(config.name)) if err == nil && instance != nil { fmt.Printf("Hana API instance already exists (%s/%s)\n", config.namespace, hanaBindingAPIName(config.name)) return nil @@ -72,8 +77,13 @@ func createHanaAPIInstanceIfNeeded(config *hanaCheckConfig) clierror.Error { } func createHanaAPIBindingIfNeeded(config *hanaCheckConfig) clierror.Error { + client, clientErr := config.GetKubeClientWithClierr() + if clientErr != nil { + return clientErr + } + //check if binding exists, skip API binding creation if it does - instance, err := config.KubeClient.Btp().GetServiceBinding(config.Ctx, config.namespace, hanaBindingAPIName(config.name)) + instance, err := client.Btp().GetServiceBinding(config.Ctx, config.namespace, hanaBindingAPIName(config.name)) if err == nil && instance != nil { fmt.Printf("Hana API instance already exists (%s/%s)\n", config.namespace, hanaBindingAPIName(config.name)) return nil @@ -84,16 +94,26 @@ func createHanaAPIBindingIfNeeded(config *hanaCheckConfig) clierror.Error { } func createHanaAPIInstance(config *hanaCheckConfig) clierror.Error { + client, clientErr := config.GetKubeClientWithClierr() + if clientErr != nil { + return clientErr + } + instance := hanaAPIInstance(config) - err := config.KubeClient.Btp().CreateServiceInstance(config.Ctx, instance) + err := client.Btp().CreateServiceInstance(config.Ctx, instance) return handleProvisionResponse(err, "Hana API instance", config.namespace, hanaBindingAPIName(config.name)) } func createHanaAPIBinding(config *hanaCheckConfig) clierror.Error { + client, clientErr := config.GetKubeClientWithClierr() + if clientErr != nil { + return clientErr + } + binding := hanaAPIBinding(config) - err := config.KubeClient.Btp().CreateServiceBinding(config.Ctx, binding) + err := client.Btp().CreateServiceBinding(config.Ctx, binding) return handleProvisionResponse(err, "Hana API binding", config.namespace, hanaBindingAPIName(config.name)) } @@ -172,7 +192,12 @@ func createHanaInstanceMapping(config *hanaCheckConfig) clierror.Error { } func getClusterID(config *hanaCheckConfig) (string, clierror.Error) { - cm, err := config.KubeClient.Static().CoreV1().ConfigMaps("kyma-system").Get(config.Ctx, "sap-btp-operator-config", metav1.GetOptions{}) + client, clientErr := config.GetKubeClientWithClierr() + if clientErr != nil { + return "", clientErr + } + + cm, err := client.Static().CoreV1().ConfigMaps("kyma-system").Get(config.Ctx, "sap-btp-operator-config", metav1.GetOptions{}) if err != nil { return "", clierror.Wrap(err, clierror.New("failed to get cluster ID")) } @@ -180,9 +205,14 @@ func getClusterID(config *hanaCheckConfig) (string, clierror.Error) { } func getHanaID(config *hanaCheckConfig) (string, clierror.Error) { + client, clientErr := config.GetKubeClientWithClierr() + if clientErr != nil { + return "", clientErr + } + // wait for until Hana instance is ready, for default setting it should take 5 minutes fmt.Print("waiting for Hana instance to be ready... ") - instanceReadyCheck := config.KubeClient.Btp().IsInstanceReady(config.Ctx, config.namespace, config.name) + instanceReadyCheck := client.Btp().IsInstanceReady(config.Ctx, config.namespace, config.name) err := wait.PollUntilContextTimeout(config.Ctx, 10*time.Second, config.timeout, true, instanceReadyCheck) if err != nil { fmt.Println("Failed") @@ -192,7 +222,7 @@ func getHanaID(config *hanaCheckConfig) (string, clierror.Error) { } fmt.Println("done") - instance, err := config.KubeClient.Btp().GetServiceInstance(config.Ctx, config.namespace, config.name) + instance, err := client.Btp().GetServiceInstance(config.Ctx, config.namespace, config.name) if err != nil { return "", clierror.Wrap(err, clierror.New("failed to get Hana instance")) } @@ -201,8 +231,13 @@ func getHanaID(config *hanaCheckConfig) (string, clierror.Error) { } func readHanaAPISecret(config *hanaCheckConfig) (string, *auth.UAA, clierror.Error) { + client, clientErr := config.GetKubeClientWithClierr() + if clientErr != nil { + return "", nil, clientErr + } + fmt.Print("waiting for Hana API instance to be ready... ") - instanceReadyCheck := config.KubeClient.Btp().IsInstanceReady(config.Ctx, config.namespace, hanaBindingAPIName(config.name)) + instanceReadyCheck := client.Btp().IsInstanceReady(config.Ctx, config.namespace, hanaBindingAPIName(config.name)) err := wait.PollUntilContextTimeout(config.Ctx, 5*time.Second, 2*time.Minute, true, instanceReadyCheck) if err != nil { fmt.Println("Failed") @@ -213,14 +248,14 @@ func readHanaAPISecret(config *hanaCheckConfig) (string, *auth.UAA, clierror.Err fmt.Println("done") fmt.Print("waiting for Hana API binding to be ready... ") - bindingReadyCheck := config.KubeClient.Btp().IsBindingReady(config.Ctx, config.namespace, hanaBindingAPIName(config.name)) + bindingReadyCheck := client.Btp().IsBindingReady(config.Ctx, config.namespace, hanaBindingAPIName(config.name)) err = wait.PollUntilContextTimeout(config.Ctx, 5*time.Second, 2*time.Minute, true, bindingReadyCheck) if err != nil { fmt.Println("Failed") return "", nil, clierror.Wrap(err, clierror.New("timeout while waiting for Hana API binding")) } fmt.Println("done") - secret, err := config.KubeClient.Static().CoreV1().Secrets(config.namespace).Get(config.Ctx, hanaBindingAPIName(config.name), metav1.GetOptions{}) + secret, err := client.Static().CoreV1().Secrets(config.namespace).Get(config.Ctx, hanaBindingAPIName(config.name), metav1.GetOptions{}) if err != nil { return "", nil, clierror.Wrap(err, clierror.New("failed to get secret")) } diff --git a/internal/cmd/alpha/hana/provision.go b/internal/cmd/alpha/hana/provision.go index 3c0306a1c..1545c8a80 100644 --- a/internal/cmd/alpha/hana/provision.go +++ b/internal/cmd/alpha/hana/provision.go @@ -72,21 +72,36 @@ func runProvision(config *hanaProvisionConfig) clierror.Error { func createHanaInstance(config *hanaProvisionConfig) clierror.Error { instance := hanaInstance(config) - err := config.KubeClient.Btp().CreateServiceInstance(config.Ctx, instance) + client, clientErr := config.GetKubeClientWithClierr() + if clientErr != nil { + return clientErr + } + + err := client.Btp().CreateServiceInstance(config.Ctx, instance) return handleProvisionResponse(err, "Hana instance", config.namespace, config.name) } func createHanaBinding(config *hanaProvisionConfig) clierror.Error { binding := hanaBinding(config) - err := config.KubeClient.Btp().CreateServiceBinding(config.Ctx, binding) + client, clientErr := config.GetKubeClientWithClierr() + if clientErr != nil { + return clientErr + } + + err := client.Btp().CreateServiceBinding(config.Ctx, binding) return handleProvisionResponse(err, "Hana binding", config.namespace, config.name) } func createHanaBindingURL(config *hanaProvisionConfig) clierror.Error { bindingURL := hanaBindingURL(config) - err := config.KubeClient.Btp().CreateServiceBinding(config.Ctx, bindingURL) + client, clientErr := config.GetKubeClientWithClierr() + if clientErr != nil { + return clientErr + } + + err := client.Btp().CreateServiceBinding(config.Ctx, bindingURL) return handleProvisionResponse(err, "Hana URL binding", config.namespace, hanaBindingURLName(config.name)) } diff --git a/internal/cmd/alpha/modules/modules.go b/internal/cmd/alpha/modules/modules.go index 0693f259e..1891d364b 100644 --- a/internal/cmd/alpha/modules/modules.go +++ b/internal/cmd/alpha/modules/modules.go @@ -82,11 +82,18 @@ func collectiveView(cfg *modulesConfig) clierror.Error { if err != nil { return clierror.WrapE(err, clierror.New("failed to get all Kyma catalog")) } - installedWith, err := communitymodules.InstalledModules(cfg.Ctx, cfg.KubeClient) + + client, err := cfg.GetKubeClientWithClierr() + if err != nil { + return err + } + + installedWith, err := communitymodules.InstalledModules(cfg.Ctx, client) if err != nil { return clierror.WrapE(err, clierror.New("failed to get installed Kyma modules")) } - managedWith, err := communitymodules.ManagedModules(cfg.Ctx, cfg.KubeClient) + + managedWith, err := communitymodules.ManagedModules(cfg.Ctx, client) if err != nil { return clierror.WrapE(err, clierror.New("failed to get managed Kyma modules")) } @@ -99,7 +106,12 @@ func collectiveView(cfg *modulesConfig) clierror.Error { // listInstalledModules lists all installed modules func listInstalledModules(cfg *modulesConfig) clierror.Error { - installed, err := communitymodules.InstalledModules(cfg.Ctx, cfg.KubeClient) + client, err := cfg.GetKubeClientWithClierr() + if err != nil { + return err + } + + installed, err := communitymodules.InstalledModules(cfg.Ctx, client) if err != nil { return clierror.WrapE(err, clierror.New("failed to get installed Kyma modules")) } @@ -110,7 +122,12 @@ func listInstalledModules(cfg *modulesConfig) clierror.Error { // listManagedModules lists all managed modules func listManagedModules(cfg *modulesConfig) clierror.Error { - managed, err := communitymodules.ManagedModules(cfg.Ctx, cfg.KubeClient) + client, err := cfg.GetKubeClientWithClierr() + if err != nil { + return err + } + + managed, err := communitymodules.ManagedModules(cfg.Ctx, client) if err != nil { return clierror.WrapE(err, clierror.New("failed to get managed Kyma modules")) } diff --git a/internal/cmd/alpha/oidc/oidc.go b/internal/cmd/alpha/oidc/oidc.go index 014226cd9..e949ccfd3 100644 --- a/internal/cmd/alpha/oidc/oidc.go +++ b/internal/cmd/alpha/oidc/oidc.go @@ -117,7 +117,12 @@ func runOIDC(cfg *oidcConfig) clierror.Error { return clierror.WrapE(clierr, clierror.New("failed to get kubeconfig from CIS")) } } else { - kubeconfig = cfg.KubeClient.APIConfig() + client, err := cfg.GetKubeClientWithClierr() + if err != nil { + return err + } + + kubeconfig = client.APIConfig() } enrichedKubeconfig := createKubeconfig(kubeconfig, token) diff --git a/internal/cmd/alpha/referenceinstance/referenceinstance.go b/internal/cmd/alpha/referenceinstance/referenceinstance.go index a2a433559..7aef7539b 100644 --- a/internal/cmd/alpha/referenceinstance/referenceinstance.go +++ b/internal/cmd/alpha/referenceinstance/referenceinstance.go @@ -59,7 +59,12 @@ func NewReferenceInstanceCMD(kymaConfig *cmdcommon.KymaConfig) *cobra.Command { func runReferenceInstance(config referenceInstanceConfig) error { requestData := fillRequestData(config) - return config.KubeClient.Btp().CreateServiceInstance(config.Ctx, &requestData) + client, err := config.GetKubeClient() + if err != nil { + return err + } + + return client.Btp().CreateServiceInstance(config.Ctx, &requestData) } func fillRequestData(config referenceInstanceConfig) btp.ServiceInstance { diff --git a/internal/cmd/alpha/registry/config/config.go b/internal/cmd/alpha/registry/config/config.go index 0b2a1ec86..330a6c5e7 100644 --- a/internal/cmd/alpha/registry/config/config.go +++ b/internal/cmd/alpha/registry/config/config.go @@ -38,7 +38,12 @@ func NewConfigCMD(kymaConfig *cmdcommon.KymaConfig) *cobra.Command { } func runConfig(cfg *cfgConfig) clierror.Error { - registryConfig, err := registry.GetExternalConfig(cfg.Ctx, cfg.KubeClient) + client, err := cfg.GetKubeClientWithClierr() + if err != nil { + return err + } + + registryConfig, err := registry.GetExternalConfig(cfg.Ctx, client) if err != nil { return clierror.WrapE(err, clierror.New("failed to load in-cluster registry configuration")) } diff --git a/internal/cmd/alpha/registry/imageimport/imageimport.go b/internal/cmd/alpha/registry/imageimport/imageimport.go index 891087a0a..bf8fe008a 100644 --- a/internal/cmd/alpha/registry/imageimport/imageimport.go +++ b/internal/cmd/alpha/registry/imageimport/imageimport.go @@ -53,8 +53,13 @@ func (pc *provisionConfig) complete(args []string) { } func runImageImport(config *provisionConfig) clierror.Error { + client, err := config.GetKubeClientWithClierr() + if err != nil { + return err + } + // TODO: Add "serverless is not installed" error message - registryConfig, err := registry.GetInternalConfig(config.Ctx, config.KubeClient) + registryConfig, err := registry.GetInternalConfig(config.Ctx, client) if err != nil { return clierror.WrapE(err, clierror.New("failed to load in-cluster registry configuration")) } @@ -65,7 +70,7 @@ func runImageImport(config *provisionConfig) clierror.Error { config.Ctx, config.image, registry.ImportOptions{ - ClusterAPIRestConfig: config.KubeClient.RestConfig(), + ClusterAPIRestConfig: client.RestConfig(), RegistryAuth: registry.NewBasicAuth(registryConfig.SecretData.Username, registryConfig.SecretData.Password), RegistryPullHost: registryConfig.SecretData.PullRegAddr, RegistryPodName: registryConfig.PodMeta.Name, diff --git a/internal/cmd/alpha/remove/managed/managed.go b/internal/cmd/alpha/remove/managed/managed.go index 6cc39777a..7cbc0a083 100644 --- a/internal/cmd/alpha/remove/managed/managed.go +++ b/internal/cmd/alpha/remove/managed/managed.go @@ -32,5 +32,10 @@ func NewManagedCMD(kymaConfig *cmdcommon.KymaConfig) *cobra.Command { } func runRemoveManaged(config *managedConfig) error { - return config.KubeClient.Kyma().DisableModule(config.Ctx, config.module) + client, err := config.GetKubeClient() + if err != nil { + return err + } + + return client.Kyma().DisableModule(config.Ctx, config.module) } diff --git a/internal/cmd/alpha/remove/remove.go b/internal/cmd/alpha/remove/remove.go index bbd0c8402..61d96d64c 100644 --- a/internal/cmd/alpha/remove/remove.go +++ b/internal/cmd/alpha/remove/remove.go @@ -38,5 +38,10 @@ func NewRemoveCMD(kymaConfig *cmdcommon.KymaConfig) *cobra.Command { func runRemove(cfg *removeConfig) clierror.Error { modules := cluster.ParseModules(cfg.modules) - return cluster.RemoveSpecifiedModules(cfg.Ctx, cfg.KubeClient.RootlessDynamic(), modules) + client, err := cfg.GetKubeClientWithClierr() + if err != nil { + return err + } + + return cluster.RemoveSpecifiedModules(cfg.Ctx, client.RootlessDynamic(), modules) } diff --git a/internal/cmdcommon/extension.go b/internal/cmdcommon/extension.go index 2726c1a63..ee086f8c1 100644 --- a/internal/cmdcommon/extension.go +++ b/internal/cmdcommon/extension.go @@ -10,7 +10,6 @@ import ( "github.com/spf13/cobra" "gopkg.in/yaml.v3" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/client-go/kubernetes" ) func BuildExtensions(config *KymaConfig, availableTemplateCommands *TemplateCommandsList, availableCoreCommands CoreCommandsMap) []*cobra.Command { @@ -66,9 +65,15 @@ func addCoreCommands(cmd *cobra.Command, config *KymaConfig, extensionCoreComman } } -func ListExtensions(ctx context.Context, client kubernetes.Interface) (ExtensionList, error) { +func listExtensions(ctx context.Context, clientConfig *KubeClientConfig) (ExtensionList, error) { + client, clientErr := clientConfig.GetKubeClient() + if clientErr != nil { + // skip becuase can't connect to the cluster + return nil, nil + } + labelSelector := fmt.Sprintf("%s==%s", ExtensionLabelKey, ExtensionResourceLabelValue) - cms, err := client.CoreV1().ConfigMaps("").List(ctx, metav1.ListOptions{ + cms, err := client.Static().CoreV1().ConfigMaps("").List(ctx, metav1.ListOptions{ LabelSelector: labelSelector, }) if err != nil { diff --git a/internal/cmdcommon/extension_test.go b/internal/cmdcommon/extension_test.go index 3175bcc13..ce9fee587 100644 --- a/internal/cmdcommon/extension_test.go +++ b/internal/cmdcommon/extension_test.go @@ -5,6 +5,7 @@ import ( "fmt" "testing" + "github.com/kyma-project/cli.v3/internal/kube/fake" "github.com/stretchr/testify/require" corev1 "k8s.io/api/core/v1" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -13,11 +14,15 @@ import ( func TestListFromCluster(t *testing.T) { t.Run("list extensions from cluster", func(t *testing.T) { - client := k8s_fake.NewSimpleClientset( - fixTestExtensionConfigmap("test-1"), - fixTestExtensionConfigmap("test-2"), - fixTestExtensionConfigmap("test-3"), - ) + client := &KubeClientConfig{ + KubeClient: &fake.FakeKubeClient{ + TestKubernetesInterface: k8s_fake.NewSimpleClientset( + fixTestExtensionConfigmap("test-1"), + fixTestExtensionConfigmap("test-2"), + fixTestExtensionConfigmap("test-3"), + ), + }, + } want := ExtensionList{ fixTestExtension("test-1"), @@ -25,47 +30,55 @@ func TestListFromCluster(t *testing.T) { fixTestExtension("test-3"), } - got, err := ListExtensions(context.Background(), client) + got, err := listExtensions(context.Background(), client) require.NoError(t, err) require.Equal(t, want, got) }) t.Run("missing rootCommand error", func(t *testing.T) { - client := k8s_fake.NewSimpleClientset( - &corev1.ConfigMap{ - ObjectMeta: v1.ObjectMeta{ - Name: "bad-data", - Labels: map[string]string{ - ExtensionLabelKey: ExtensionResourceLabelValue, + client := &KubeClientConfig{ + KubeClient: &fake.FakeKubeClient{ + TestKubernetesInterface: k8s_fake.NewSimpleClientset( + &corev1.ConfigMap{ + ObjectMeta: v1.ObjectMeta{ + Name: "bad-data", + Labels: map[string]string{ + ExtensionLabelKey: ExtensionResourceLabelValue, + }, + }, + Data: map[string]string{}, }, - }, - Data: map[string]string{}, + ), }, - ) + } - got, err := ListExtensions(context.Background(), client) + got, err := listExtensions(context.Background(), client) require.ErrorContains(t, err, "failed to parse configmap '/bad-data': missing .data.rootCommand field") require.Nil(t, got) }) t.Run("skip optional fields", func(t *testing.T) { - client := k8s_fake.NewSimpleClientset( - &corev1.ConfigMap{ - ObjectMeta: v1.ObjectMeta{ - Name: "bad-data", - Labels: map[string]string{ - ExtensionLabelKey: ExtensionResourceLabelValue, - }, - }, - Data: map[string]string{ - ExtensionRootCommandKey: ` + client := &KubeClientConfig{ + KubeClient: &fake.FakeKubeClient{ + TestKubernetesInterface: k8s_fake.NewSimpleClientset( + &corev1.ConfigMap{ + ObjectMeta: v1.ObjectMeta{ + Name: "bad-data", + Labels: map[string]string{ + ExtensionLabelKey: ExtensionResourceLabelValue, + }, + }, + Data: map[string]string{ + ExtensionRootCommandKey: ` name: test-command description: test-description descriptionLong: test-description-long `, - }, + }, + }, + ), }, - ) + } want := ExtensionList{ { @@ -77,7 +90,7 @@ descriptionLong: test-description-long }, } - got, err := ListExtensions(context.Background(), client) + got, err := listExtensions(context.Background(), client) require.NoError(t, err) require.Equal(t, want, got) }) diff --git a/internal/cmdcommon/kubeconfig.go b/internal/cmdcommon/kubeconfig.go index e54a71061..142e9476b 100644 --- a/internal/cmdcommon/kubeconfig.go +++ b/internal/cmdcommon/kubeconfig.go @@ -10,14 +10,30 @@ import ( // KubeClientConfig allows to setup kubeconfig flag and use it to create kube.Client type KubeClientConfig struct { - KubeClient kube.Client + KubeClient kube.Client + KubeClientErr error } -func newKubeClientConfig(cmd *cobra.Command) (*KubeClientConfig, clierror.Error) { +func newKubeClientConfig(cmd *cobra.Command) *KubeClientConfig { cfg := &KubeClientConfig{} cfg.addFlag(cmd) + cfg.complete() - return cfg, cfg.complete() + return cfg +} + +func (kcc *KubeClientConfig) GetKubeClient() (kube.Client, error) { + return kcc.KubeClient, kcc.KubeClientErr +} + +func (kcc *KubeClientConfig) GetKubeClientWithClierr() (kube.Client, clierror.Error) { + if kcc.KubeClientErr != nil { + return nil, clierror.Wrap(kcc.KubeClientErr, + clierror.New("failed to create cluster connection", "Make sure that kubeconfig is proper."), + ) + } + + return kcc.KubeClient, nil } func (kcc *KubeClientConfig) addFlag(cmd *cobra.Command) { @@ -25,13 +41,10 @@ func (kcc *KubeClientConfig) addFlag(cmd *cobra.Command) { _ = cmd.PersistentFlags().String("kubeconfig", "", "Path to the Kyma kubeconfig file.") } -func (kcc *KubeClientConfig) complete() clierror.Error { +func (kcc *KubeClientConfig) complete() { kubeconfigPath := getKubeconfigPath() - var err clierror.Error - kcc.KubeClient, err = kube.NewClient(kubeconfigPath) - - return err + kcc.KubeClient, kcc.KubeClientErr = kube.NewClient(kubeconfigPath) } // search os.Args manually to find if user pass --kubeconfig path and return its value diff --git a/internal/cmdcommon/kymaconfig.go b/internal/cmdcommon/kymaconfig.go index 150485bd7..5c5314e88 100644 --- a/internal/cmdcommon/kymaconfig.go +++ b/internal/cmdcommon/kymaconfig.go @@ -19,12 +19,9 @@ type KymaConfig struct { func NewKymaConfig(cmd *cobra.Command) (*KymaConfig, clierror.Error) { ctx := context.Background() - kubeClient, kubeClientErr := newKubeClientConfig(cmd) - if kubeClientErr != nil { - return nil, kubeClientErr - } + kubeClient := newKubeClientConfig(cmd) - extensions, err := ListExtensions(ctx, kubeClient.KubeClient.Static()) + extensions, err := listExtensions(ctx, kubeClient) if err != nil { fmt.Printf("DEBUG ERROR: %s\n", err.Error()) // TODO: think about handling error later diff --git a/internal/kube/client.go b/internal/kube/client.go index ab77ee395..92d22775c 100644 --- a/internal/kube/client.go +++ b/internal/kube/client.go @@ -1,10 +1,10 @@ package kube import ( - "github.com/kyma-project/cli.v3/internal/clierror" "github.com/kyma-project/cli.v3/internal/kube/btp" "github.com/kyma-project/cli.v3/internal/kube/kyma" "github.com/kyma-project/cli.v3/internal/kube/rootlessdynamic" + "github.com/pkg/errors" "k8s.io/client-go/discovery" "k8s.io/client-go/dynamic" "k8s.io/client-go/kubernetes" @@ -35,12 +35,10 @@ type client struct { restClient *rest.RESTClient } -func NewClient(kubeconfig string) (Client, clierror.Error) { +func NewClient(kubeconfig string) (Client, error) { client, err := newClient(kubeconfig) if err != nil { - return nil, clierror.Wrap(err, - clierror.New("failed to initialise kubernetes client", "Make sure that kubeconfig is proper."), - ) + return nil, errors.Wrap(err, "failed to initialise kubernetes client") } return client, nil }