mirror of
https://github.com/kubeshark/kubeshark.git
synced 2025-06-24 07:14:15 +00:00
Making kube config errors more user friendly (#132)
* Making kube config errors more user friendly
This commit is contained in:
parent
2996c1a4bc
commit
ac358be877
@ -9,6 +9,7 @@ import (
|
|||||||
"github.com/up9inc/mizu/shared"
|
"github.com/up9inc/mizu/shared"
|
||||||
"github.com/up9inc/mizu/shared/debounce"
|
"github.com/up9inc/mizu/shared/debounce"
|
||||||
core "k8s.io/api/core/v1"
|
core "k8s.io/api/core/v1"
|
||||||
|
"k8s.io/client-go/tools/clientcmd"
|
||||||
"k8s.io/apimachinery/pkg/util/wait"
|
"k8s.io/apimachinery/pkg/util/wait"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -36,7 +37,17 @@ func RunMizuTap(podRegexQuery *regexp.Regexp, tappingOptions *MizuTapOptions) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
kubernetesProvider := kubernetes.NewProvider(tappingOptions.KubeConfigPath)
|
kubernetesProvider, err := kubernetes.NewProvider(tappingOptions.KubeConfigPath)
|
||||||
|
if err != nil {
|
||||||
|
if clientcmd.IsEmptyConfig(err) {
|
||||||
|
fmt.Printf(mizu.Red, "Couldn't find the kube config file, or file is empty. Try adding '--kube-config=<path to kube config file>'\n")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if clientcmd.IsConfigurationInvalid(err) {
|
||||||
|
fmt.Printf(mizu.Red, "Invalid kube config file. Try using a different config with '--kube-config=<path to kube config file>'\n")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
defer cleanUpMizuResources(kubernetesProvider)
|
defer cleanUpMizuResources(kubernetesProvider)
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
@ -6,7 +6,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type MizuViewOptions struct {
|
type MizuViewOptions struct {
|
||||||
GuiPort uint16
|
GuiPort uint16
|
||||||
|
KubeConfigPath string
|
||||||
}
|
}
|
||||||
|
|
||||||
var mizuViewOptions = &MizuViewOptions{}
|
var mizuViewOptions = &MizuViewOptions{}
|
||||||
@ -15,8 +16,8 @@ var viewCmd = &cobra.Command{
|
|||||||
Use: "view",
|
Use: "view",
|
||||||
Short: "Open GUI in browser",
|
Short: "Open GUI in browser",
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
go mizu.ReportRun("view", mizuFetchOptions)
|
go mizu.ReportRun("view", mizuViewOptions)
|
||||||
if isCompatible, err := mizu.CheckVersionCompatibility(mizuFetchOptions.MizuPort); err != nil {
|
if isCompatible, err := mizu.CheckVersionCompatibility(mizuViewOptions.GuiPort); err != nil {
|
||||||
return err
|
return err
|
||||||
} else if !isCompatible {
|
} else if !isCompatible {
|
||||||
return nil
|
return nil
|
||||||
@ -30,5 +31,5 @@ func init() {
|
|||||||
rootCmd.AddCommand(viewCmd)
|
rootCmd.AddCommand(viewCmd)
|
||||||
|
|
||||||
viewCmd.Flags().Uint16VarP(&mizuViewOptions.GuiPort, "gui-port", "p", 8899, "Provide a custom port for the web interface webserver")
|
viewCmd.Flags().Uint16VarP(&mizuViewOptions.GuiPort, "gui-port", "p", 8899, "Provide a custom port for the web interface webserver")
|
||||||
|
viewCmd.Flags().StringVarP(&mizuViewOptions.KubeConfigPath, "kube-config", "k", "", "Path to kube-config file")
|
||||||
}
|
}
|
||||||
|
@ -5,11 +5,22 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"github.com/up9inc/mizu/cli/kubernetes"
|
"github.com/up9inc/mizu/cli/kubernetes"
|
||||||
"github.com/up9inc/mizu/cli/mizu"
|
"github.com/up9inc/mizu/cli/mizu"
|
||||||
|
"k8s.io/client-go/tools/clientcmd"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
func runMizuView(mizuViewOptions *MizuViewOptions) {
|
func runMizuView(mizuViewOptions *MizuViewOptions) {
|
||||||
kubernetesProvider := kubernetes.NewProvider("")
|
kubernetesProvider, err := kubernetes.NewProvider(mizuViewOptions.KubeConfigPath)
|
||||||
|
if err != nil {
|
||||||
|
if clientcmd.IsEmptyConfig(err) {
|
||||||
|
fmt.Printf(mizu.Red, "Couldn't find the kube config file, or file is empty. Try adding '--kube-config=<path to kube config file>'\n")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if clientcmd.IsConfigurationInvalid(err) {
|
||||||
|
fmt.Printf(mizu.Red, "Invalid kube config file. Try using a different config with '--kube-config=<path to kube config file>'\n")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
@ -6,6 +6,8 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
|
"k8s.io/client-go/tools/cache"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
@ -17,7 +19,6 @@ import (
|
|||||||
k8serrors "k8s.io/apimachinery/pkg/api/errors"
|
k8serrors "k8s.io/apimachinery/pkg/api/errors"
|
||||||
resource "k8s.io/apimachinery/pkg/api/resource"
|
resource "k8s.io/apimachinery/pkg/api/resource"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
|
||||||
"k8s.io/apimachinery/pkg/util/intstr"
|
"k8s.io/apimachinery/pkg/util/intstr"
|
||||||
"k8s.io/apimachinery/pkg/watch"
|
"k8s.io/apimachinery/pkg/watch"
|
||||||
applyconfapp "k8s.io/client-go/applyconfigurations/apps/v1"
|
applyconfapp "k8s.io/client-go/applyconfigurations/apps/v1"
|
||||||
@ -29,7 +30,6 @@ import (
|
|||||||
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
|
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
|
||||||
_ "k8s.io/client-go/plugin/pkg/client/auth/openstack"
|
_ "k8s.io/client-go/plugin/pkg/client/auth/openstack"
|
||||||
restclient "k8s.io/client-go/rest"
|
restclient "k8s.io/client-go/rest"
|
||||||
"k8s.io/client-go/tools/cache"
|
|
||||||
"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"
|
||||||
@ -44,17 +44,14 @@ type Provider struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
fieldManagerName = "mizu-manager"
|
fieldManagerName = "mizu-manager"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewProvider(kubeConfigPath string) *Provider {
|
func NewProvider(kubeConfigPath string) (*Provider, error) {
|
||||||
if kubeConfigPath == "" {
|
|
||||||
kubeConfigPath = os.Getenv("KUBECONFIG")
|
|
||||||
}
|
|
||||||
kubernetesConfig := loadKubernetesConfiguration(kubeConfigPath)
|
kubernetesConfig := loadKubernetesConfiguration(kubeConfigPath)
|
||||||
restClientConfig, err := kubernetesConfig.ClientConfig()
|
restClientConfig, err := kubernetesConfig.ClientConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err.Error())
|
return nil, err
|
||||||
}
|
}
|
||||||
clientSet := getClientSet(restClientConfig)
|
clientSet := getClientSet(restClientConfig)
|
||||||
|
|
||||||
@ -62,7 +59,7 @@ func NewProvider(kubeConfigPath string) *Provider {
|
|||||||
clientSet: clientSet,
|
clientSet: clientSet,
|
||||||
kubernetesConfig: kubernetesConfig,
|
kubernetesConfig: kubernetesConfig,
|
||||||
clientConfig: *restClientConfig,
|
clientConfig: *restClientConfig,
|
||||||
}
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (provider *Provider) CurrentNamespace() string {
|
func (provider *Provider) CurrentNamespace() string {
|
||||||
@ -301,8 +298,7 @@ func (provider *Provider) CreateMizuRBAC(ctx context.Context, namespace string,
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (provider *Provider) RemoveNamespace(ctx context.Context, name string) error {
|
func (provider *Provider) RemoveNamespace(ctx context.Context, name string) error {
|
||||||
if isFound, err := provider.CheckNamespaceExists(ctx, name);
|
if isFound, err := provider.CheckNamespaceExists(ctx, name); err != nil {
|
||||||
err != nil {
|
|
||||||
return err
|
return err
|
||||||
} else if !isFound {
|
} else if !isFound {
|
||||||
return nil
|
return nil
|
||||||
@ -324,8 +320,7 @@ func (provider *Provider) RemoveNonNamespacedResources(ctx context.Context, clus
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (provider *Provider) RemoveClusterRole(ctx context.Context, name string) error {
|
func (provider *Provider) RemoveClusterRole(ctx context.Context, name string) error {
|
||||||
if isFound, err := provider.CheckClusterRoleExists(ctx, name);
|
if isFound, err := provider.CheckClusterRoleExists(ctx, name); err != nil {
|
||||||
err != nil {
|
|
||||||
return err
|
return err
|
||||||
} else if !isFound {
|
} else if !isFound {
|
||||||
return nil
|
return nil
|
||||||
@ -335,8 +330,7 @@ func (provider *Provider) RemoveClusterRole(ctx context.Context, name string) er
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (provider *Provider) RemoveClusterRoleBinding(ctx context.Context, name string) error {
|
func (provider *Provider) RemoveClusterRoleBinding(ctx context.Context, name string) error {
|
||||||
if isFound, err := provider.CheckClusterRoleBindingExists(ctx, name);
|
if isFound, err := provider.CheckClusterRoleBindingExists(ctx, name); err != nil {
|
||||||
err != nil {
|
|
||||||
return err
|
return err
|
||||||
} else if !isFound {
|
} else if !isFound {
|
||||||
return nil
|
return nil
|
||||||
@ -378,7 +372,7 @@ func (provider *Provider) RemoveDaemonSet(ctx context.Context, namespace string,
|
|||||||
func (provider *Provider) CheckNamespaceExists(ctx context.Context, name string) (bool, error) {
|
func (provider *Provider) CheckNamespaceExists(ctx context.Context, name string) (bool, error) {
|
||||||
listOptions := metav1.ListOptions{
|
listOptions := metav1.ListOptions{
|
||||||
FieldSelector: fmt.Sprintf("metadata.name=%s", name),
|
FieldSelector: fmt.Sprintf("metadata.name=%s", name),
|
||||||
Limit: 1,
|
Limit: 1,
|
||||||
}
|
}
|
||||||
resourceList, err := provider.clientSet.CoreV1().Namespaces().List(ctx, listOptions)
|
resourceList, err := provider.clientSet.CoreV1().Namespaces().List(ctx, listOptions)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -395,7 +389,7 @@ func (provider *Provider) CheckNamespaceExists(ctx context.Context, name string)
|
|||||||
func (provider *Provider) CheckClusterRoleExists(ctx context.Context, name string) (bool, error) {
|
func (provider *Provider) CheckClusterRoleExists(ctx context.Context, name string) (bool, error) {
|
||||||
listOptions := metav1.ListOptions{
|
listOptions := metav1.ListOptions{
|
||||||
FieldSelector: fmt.Sprintf("metadata.name=%s", name),
|
FieldSelector: fmt.Sprintf("metadata.name=%s", name),
|
||||||
Limit: 1,
|
Limit: 1,
|
||||||
}
|
}
|
||||||
resourceList, err := provider.clientSet.RbacV1().ClusterRoles().List(ctx, listOptions)
|
resourceList, err := provider.clientSet.RbacV1().ClusterRoles().List(ctx, listOptions)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -412,7 +406,7 @@ func (provider *Provider) CheckClusterRoleExists(ctx context.Context, name strin
|
|||||||
func (provider *Provider) CheckClusterRoleBindingExists(ctx context.Context, name string) (bool, error) {
|
func (provider *Provider) CheckClusterRoleBindingExists(ctx context.Context, name string) (bool, error) {
|
||||||
listOptions := metav1.ListOptions{
|
listOptions := metav1.ListOptions{
|
||||||
FieldSelector: fmt.Sprintf("metadata.name=%s", name),
|
FieldSelector: fmt.Sprintf("metadata.name=%s", name),
|
||||||
Limit: 1,
|
Limit: 1,
|
||||||
}
|
}
|
||||||
resourceList, err := provider.clientSet.RbacV1().ClusterRoleBindings().List(ctx, listOptions)
|
resourceList, err := provider.clientSet.RbacV1().ClusterRoleBindings().List(ctx, listOptions)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -615,6 +609,10 @@ func getClientSet(config *restclient.Config) *kubernetes.Clientset {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func loadKubernetesConfiguration(kubeConfigPath string) clientcmd.ClientConfig {
|
func loadKubernetesConfiguration(kubeConfigPath string) clientcmd.ClientConfig {
|
||||||
|
if kubeConfigPath == "" {
|
||||||
|
kubeConfigPath = os.Getenv("KUBECONFIG")
|
||||||
|
}
|
||||||
|
|
||||||
if kubeConfigPath == "" {
|
if kubeConfigPath == "" {
|
||||||
home := homedir.HomeDir()
|
home := homedir.HomeDir()
|
||||||
kubeConfigPath = filepath.Join(home, ".kube", "config")
|
kubeConfigPath = filepath.Join(home, ".kube", "config")
|
||||||
|
Loading…
Reference in New Issue
Block a user