Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding KUBECONFIG checks in odo catalog list components #4756

Merged
4 changes: 3 additions & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

### Bug Fixes

- Adding KUBECONFIG checks in odo catalog list components ([#4756](https://github.com/openshift/odo/pull/4756))

### Tests

### Documentation
Expand Down Expand Up @@ -37,4 +39,4 @@

- Adds a document regarding the odo.dev.push.path attributes in the devfile ([#4681](https://github.com/openshift/odo/pull/4681))
- Add --s2i conversion related breaking changes ([#4683](https://github.com/openshift/odo/pull/4683))
- Fix OCI-based registry migration ([#4702](https://github.com/openshift/odo/pull/4702))
- Fix OCI-based registry migration ([#4702](https://github.com/openshift/odo/pull/4702))
41 changes: 21 additions & 20 deletions pkg/odo/cli/catalog/list/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ package list

import (
"fmt"
catalogutil "github.com/openshift/odo/pkg/odo/cli/catalog/util"
"io"
"k8s.io/klog"
"os"
"strings"
"text/tabwriter"

"github.com/openshift/odo/pkg/catalog"
"github.com/openshift/odo/pkg/log"
"github.com/openshift/odo/pkg/machineoutput"
catalogutil "github.com/openshift/odo/pkg/odo/cli/catalog/util"
"github.com/openshift/odo/pkg/odo/genericclioptions"
"github.com/openshift/odo/pkg/util"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog"
)

const componentsRecommendedCommandName = "components"
Expand All @@ -40,27 +40,28 @@ func NewListComponentsOptions() *ListComponentsOptions {

// Complete completes ListComponentsOptions after they've been created
func (o *ListComponentsOptions) Complete(name string, cmd *cobra.Command, args []string) (err error) {

tasks := util.NewConcurrentTasks(2)

o.Context, err = genericclioptions.NewContext(cmd)
if err != nil {
return err
}
supported, err := o.Client.IsImageStreamSupported()
if err != nil {
klog.V(4).Info("ignoring error while checking imagestream support:", err.Error())
}
if util.IsValidKubeConfigPath() {
o.Context, err = genericclioptions.NewContext(cmd)
if err != nil {
return err
}
supported, err := o.Client.IsImageStreamSupported()
if err != nil {
klog.V(4).Info("ignoring error while checking imagestream support:", err.Error())
}

if supported {
tasks.Add(util.ConcurrentTask{ToRun: func(errChannel chan error) {
o.catalogList, err = catalog.ListComponents(o.Client)
if err != nil {
errChannel <- err
} else {
o.catalogList.Items = catalogutil.FilterHiddenComponents(o.catalogList.Items)
}
}})
if supported {
tasks.Add(util.ConcurrentTask{ToRun: func(errChannel chan error) {
o.catalogList, err = catalog.ListComponents(o.Client)
if err != nil {
errChannel <- err
} else {
o.catalogList.Items = catalogutil.FilterHiddenComponents(o.catalogList.Items)
}
}})
}
}

tasks.Add(util.ConcurrentTask{ToRun: func(errChannel chan error) {
Expand Down
26 changes: 26 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"io"
"io/ioutil"
"k8s.io/client-go/util/homedir"
"math/big"
"net"
"net/http"
Expand Down Expand Up @@ -1479,3 +1480,28 @@ func GetEnvWithDefault(key string, defaultval string) string {
}
return val
}

//IsValidKubeConfigPath checks if specified `KUBECONFIG` value is a valid file i.e the path should exist and be a file
//if env `KUBECONFIG` is set then that is used or default `KUBECONFIG` is checked
func IsValidKubeConfigPath() bool {
v := os.Getenv("KUBECONFIG")
if v == "" {
if home := homedir.HomeDir(); home != "" {
v = filepath.Join(home, ".kube", "config")
klog.V(4).Infof("using default kubeconfig path %s", v)
} else {
klog.V(4).Infof("no KUBECONFIG provided and cannot fallback to default")
return false
}
}
f1, err := os.Stat(v)
if os.IsNotExist(err) {
klog.V(4).Infof("invalid kubeconfig path set, KUBECONFIG env was set to %s which does no exist", v)
return false
}
if f1.IsDir() {
klog.V(4).Infof("invalid kubeconfig path set, KUBECONFIG env was set to %s which is a directory", v)
return false
}
return true
}
12 changes: 12 additions & 0 deletions tests/integration/devfile/cmd_devfile_catalog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package devfile

import (
"encoding/json"
"os"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -46,6 +47,17 @@ var _ = Describe("odo devfile catalog command tests", func() {
}
helper.MatchAllInOutput(output, wantOutput)
})
It("should list components successfully even with an invalid kubeconfig path or path points to existing directory", func() {
originalKC := os.Getenv("KUBECONFIG")
err := os.Setenv("KUBECONFIG", "/idonotexist")
Expect(err).ToNot(HaveOccurred())
helper.CmdShouldPass("odo", "catalog", "list", "components")
err = os.Setenv("KUBECONFIG", commonVar.Context)
Expect(err).ToNot(HaveOccurred())
helper.CmdShouldPass("odo", "catalog", "list", "components")
err = os.Setenv("KUBECONFIG", originalKC)
Expect(err).ToNot(HaveOccurred())
})
})

Context("When executing catalog list components with -o json flag", func() {
Expand Down