From 28259927a9b3a68fe435f660db8db06bb1bd8711 Mon Sep 17 00:00:00 2001 From: niusmallnan Date: Wed, 9 Feb 2022 13:48:33 +0800 Subject: [PATCH] K-EXPLORER: format cluster name with kube context --- pkg/resources/cluster/cluster.go | 15 ++++++++++++++- pkg/server/cli/clicontext.go | 4 ++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/pkg/resources/cluster/cluster.go b/pkg/resources/cluster/cluster.go index a37f9f6..cfb6706 100644 --- a/pkg/resources/cluster/cluster.go +++ b/pkg/resources/cluster/cluster.go @@ -2,7 +2,9 @@ package cluster import ( "context" + "fmt" "net/http" + "strings" "time" "github.com/rancher/apiserver/pkg/store/empty" @@ -21,6 +23,10 @@ import ( "k8s.io/client-go/discovery" ) +var ( + CurrentKubeContext string +) + const ( shellPodImage = "rancher/shell:v0.1.6" shellPodNS = "kube-system" @@ -160,7 +166,7 @@ func (s *Store) getLocal() types.APIObject { Name: "local", }, Spec: Spec{ - DisplayName: "Local Cluster", + DisplayName: getDisplayNameWithContext(), Internal: true, }, Status: Status{ @@ -207,3 +213,10 @@ func (s *Store) Watch(apiOp *types.APIRequest, schema *types.APISchema, w types. return result, nil } + +func getDisplayNameWithContext() string { + if CurrentKubeContext != "" { + return fmt.Sprintf("%s Cluster", strings.Title(CurrentKubeContext)) + } + return "Local Cluster" +} diff --git a/pkg/server/cli/clicontext.go b/pkg/server/cli/clicontext.go index 08fee60..ec098bf 100644 --- a/pkg/server/cli/clicontext.go +++ b/pkg/server/cli/clicontext.go @@ -5,6 +5,7 @@ import ( steveauth "github.com/rancher/steve/pkg/auth" authcli "github.com/rancher/steve/pkg/auth/cli" + "github.com/rancher/steve/pkg/resources/cluster" "github.com/rancher/steve/pkg/server" "github.com/rancher/steve/pkg/ui" "github.com/rancher/wrangler/pkg/kubeconfig" @@ -48,6 +49,9 @@ func (c *Config) ToServer(ctx context.Context) (*server.Server, error) { } } + // K-EXPLORER + cluster.CurrentKubeContext = c.Context + return server.New(ctx, restConfig, &server.Options{ AuthMiddleware: auth, Next: ui.New(c.UIPath),