mirror of
https://github.com/kubeshark/kubeshark.git
synced 2025-07-19 10:48:59 +00:00
fixed panic when using invalid kube config path (#231)
This commit is contained in:
parent
a37d1f4aeb
commit
7f9fd82c0e
@ -19,8 +19,9 @@ var logsCmd = &cobra.Command{
|
|||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
go telemetry.ReportRun("logs", config.Config.Logs)
|
go telemetry.ReportRun("logs", config.Config.Logs)
|
||||||
|
|
||||||
kubernetesProvider, err := kubernetes.NewProvider(config.Config.KubeConfigPath)
|
kubernetesProvider, err := kubernetes.NewProvider(config.Config.KubeConfigPath())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
logger.Log.Error(err)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
ctx, _ := context.WithCancel(context.Background())
|
ctx, _ := context.WithCancel(context.Background())
|
||||||
|
@ -62,7 +62,7 @@ func RunMizuTap() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
kubernetesProvider, err := kubernetes.NewProvider(config.Config.KubeConfigPath)
|
kubernetesProvider, err := kubernetes.NewProvider(config.Config.KubeConfigPath())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Log.Error(err)
|
logger.Log.Error(err)
|
||||||
return
|
return
|
||||||
|
@ -13,7 +13,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func runMizuView() {
|
func runMizuView() {
|
||||||
kubernetesProvider, err := kubernetes.NewProvider(config.Config.KubeConfigPath)
|
kubernetesProvider, err := kubernetes.NewProvider(config.Config.KubeConfigPath())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Log.Error(err)
|
logger.Log.Error(err)
|
||||||
return
|
return
|
||||||
|
@ -5,6 +5,9 @@ import (
|
|||||||
"github.com/up9inc/mizu/cli/config/configStructs"
|
"github.com/up9inc/mizu/cli/config/configStructs"
|
||||||
"github.com/up9inc/mizu/cli/mizu"
|
"github.com/up9inc/mizu/cli/mizu"
|
||||||
v1 "k8s.io/api/core/v1"
|
v1 "k8s.io/api/core/v1"
|
||||||
|
"k8s.io/client-go/util/homedir"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -23,7 +26,7 @@ type ConfigStruct struct {
|
|||||||
MizuResourcesNamespace string `yaml:"mizu-resources-namespace" default:"mizu"`
|
MizuResourcesNamespace string `yaml:"mizu-resources-namespace" default:"mizu"`
|
||||||
Telemetry bool `yaml:"telemetry" default:"true"`
|
Telemetry bool `yaml:"telemetry" default:"true"`
|
||||||
DumpLogs bool `yaml:"dump-logs" default:"false"`
|
DumpLogs bool `yaml:"dump-logs" default:"false"`
|
||||||
KubeConfigPath string `yaml:"kube-config-path"`
|
KubeConfigPathStr string `yaml:"kube-config-path"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (config *ConfigStruct) SetDefaults() {
|
func (config *ConfigStruct) SetDefaults() {
|
||||||
@ -37,3 +40,17 @@ func (config *ConfigStruct) ImagePullPolicy() v1.PullPolicy {
|
|||||||
func (config *ConfigStruct) IsNsRestrictedMode() bool {
|
func (config *ConfigStruct) IsNsRestrictedMode() bool {
|
||||||
return config.MizuResourcesNamespace != "mizu" // Notice "mizu" string must match the default MizuResourcesNamespace
|
return config.MizuResourcesNamespace != "mizu" // Notice "mizu" string must match the default MizuResourcesNamespace
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (config *ConfigStruct) KubeConfigPath() string {
|
||||||
|
if config.KubeConfigPathStr != "" {
|
||||||
|
return config.KubeConfigPathStr
|
||||||
|
}
|
||||||
|
|
||||||
|
envKubeConfigPath := os.Getenv("KUBECONFIG")
|
||||||
|
if envKubeConfigPath != "" {
|
||||||
|
return envKubeConfigPath
|
||||||
|
}
|
||||||
|
|
||||||
|
home := homedir.HomeDir()
|
||||||
|
return filepath.Join(home, ".kube", "config")
|
||||||
|
}
|
||||||
|
@ -9,7 +9,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"github.com/up9inc/mizu/cli/config/configStructs"
|
"github.com/up9inc/mizu/cli/config/configStructs"
|
||||||
"github.com/up9inc/mizu/cli/logger"
|
"github.com/up9inc/mizu/cli/logger"
|
||||||
"os"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
@ -38,7 +37,6 @@ import (
|
|||||||
"k8s.io/client-go/tools/clientcmd"
|
"k8s.io/client-go/tools/clientcmd"
|
||||||
_ "k8s.io/client-go/tools/portforward"
|
_ "k8s.io/client-go/tools/portforward"
|
||||||
watchtools "k8s.io/client-go/tools/watch"
|
watchtools "k8s.io/client-go/tools/watch"
|
||||||
"k8s.io/client-go/util/homedir"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Provider struct {
|
type Provider struct {
|
||||||
@ -57,13 +55,23 @@ func NewProvider(kubeConfigPath string) (*Provider, error) {
|
|||||||
restClientConfig, err := kubernetesConfig.ClientConfig()
|
restClientConfig, err := kubernetesConfig.ClientConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if clientcmd.IsEmptyConfig(err) {
|
if clientcmd.IsEmptyConfig(err) {
|
||||||
return nil, fmt.Errorf("Couldn't find the kube config file, or file is empty. Try adding '--kube-config=<path to kube config file>'\n")
|
return nil, fmt.Errorf("couldn't find the kube config file, or file is empty (%s)\n" +
|
||||||
|
"you can set alternative kube config file path by adding the kube-config-path field to the mizu config file, err: %w", kubeConfigPath, err)
|
||||||
}
|
}
|
||||||
if clientcmd.IsConfigurationInvalid(err) {
|
if clientcmd.IsConfigurationInvalid(err) {
|
||||||
return nil, fmt.Errorf("Invalid kube config file. Try using a different config with '--kube-config=<path to kube config file>'\n")
|
return nil, fmt.Errorf("invalid kube config file (%s)\n" +
|
||||||
|
"you can set alternative kube config file path by adding the kube-config-path field to the mizu config file, err: %w", kubeConfigPath, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return nil, fmt.Errorf("error while using kube config (%s)\n" +
|
||||||
|
"you can set alternative kube config file path by adding the kube-config-path field to the mizu config file, err: %w", kubeConfigPath, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
clientSet, err := getClientSet(restClientConfig)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error while using kube config (%s)\n" +
|
||||||
|
"you can set alternative kube config file path by adding the kube-config-path field to the mizu config file, err: %w", kubeConfigPath, err)
|
||||||
}
|
}
|
||||||
clientSet := getClientSet(restClientConfig)
|
|
||||||
|
|
||||||
return &Provider{
|
return &Provider{
|
||||||
clientSet: clientSet,
|
clientSet: clientSet,
|
||||||
@ -731,24 +739,16 @@ func (provider *Provider) GetPodLogs(namespace string, podName string, ctx conte
|
|||||||
return str, nil
|
return str, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getClientSet(config *restclient.Config) *kubernetes.Clientset {
|
func getClientSet(config *restclient.Config) (*kubernetes.Clientset, error) {
|
||||||
clientSet, err := kubernetes.NewForConfig(config)
|
clientSet, err := kubernetes.NewForConfig(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err.Error())
|
return nil, err
|
||||||
}
|
}
|
||||||
return clientSet
|
|
||||||
|
return clientSet, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadKubernetesConfiguration(kubeConfigPath string) clientcmd.ClientConfig {
|
func loadKubernetesConfiguration(kubeConfigPath string) clientcmd.ClientConfig {
|
||||||
if kubeConfigPath == "" {
|
|
||||||
kubeConfigPath = os.Getenv("KUBECONFIG")
|
|
||||||
}
|
|
||||||
|
|
||||||
if kubeConfigPath == "" {
|
|
||||||
home := homedir.HomeDir()
|
|
||||||
kubeConfigPath = filepath.Join(home, ".kube", "config")
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.Log.Debugf("Using kube config %s", kubeConfigPath)
|
logger.Log.Debugf("Using kube config %s", kubeConfigPath)
|
||||||
configPathList := filepath.SplitList(kubeConfigPath)
|
configPathList := filepath.SplitList(kubeConfigPath)
|
||||||
configLoadingRules := &clientcmd.ClientConfigLoadingRules{}
|
configLoadingRules := &clientcmd.ClientConfigLoadingRules{}
|
||||||
|
Loading…
Reference in New Issue
Block a user