Skip to content

Commit f75782c

Browse files
authored
Merge pull request #336 from asmit27rai/feature/list-contexts
✨ feature: Add kflex command to list available contexts
2 parents 781b2fc + 31cb4ab commit f75782c

File tree

5 files changed

+127
-12
lines changed

5 files changed

+127
-12
lines changed

cmd/kflex/ctx/ctx.go

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ func (c *CPCtx) Context(chattyStatus, failIfNone, overwriteExistingCtx, setCurre
5858
}
5959
fmt.Println(currentContext)
6060
return
61+
case "list":
62+
c.ListContexts()
63+
return
6164
case "":
6265
if setCurrentCtxAsHosting { // set hosting cluster context unconditionally to the current context
6366
kubeconfig.SetHostingClusterContextPreference(kconf, nil)
@@ -190,10 +193,33 @@ func (c *CPCtx) isCurrentContextHostingClusterContext() bool {
190193
}
191194

192195
func (c *CPCtx) GetCurrentContext() {
193-
currentContext, err := kubeconfig.GetCurrentContext(c.Ctx)
194-
if err != nil {
195-
fmt.Fprintf(os.Stderr, "Error retrieving current context: %s\n", err)
196-
os.Exit(1)
197-
}
198-
fmt.Println(currentContext)
196+
currentContext, err := kubeconfig.GetCurrentContext(c.Ctx)
197+
if err != nil {
198+
fmt.Fprintf(os.Stderr, "Error retrieving current context: %s\n", err)
199+
os.Exit(1)
200+
}
201+
fmt.Println(currentContext)
202+
}
203+
204+
func (c *CPCtx) ListContexts() {
205+
kconf, err := kubeconfig.LoadKubeconfig(c.Ctx)
206+
if err != nil {
207+
fmt.Fprintf(os.Stderr, "Error loading kubeconfig: %s\n", err)
208+
os.Exit(1)
209+
}
210+
211+
if len(kconf.Contexts) == 0 {
212+
fmt.Println("No contexts found in kubeconfig")
213+
return
214+
}
215+
216+
currentContext := kconf.CurrentContext
217+
fmt.Println("Available Contexts:")
218+
for name := range kconf.Contexts {
219+
prefix := " "
220+
if name == currentContext {
221+
prefix = "*"
222+
}
223+
fmt.Printf("%s %s\n", prefix, name)
224+
}
199225
}

cmd/kflex/ctx/list.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
Copyright 2023 The KubeStellar Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package ctx
18+
19+
import (
20+
"fmt"
21+
"os"
22+
23+
"github.com/kubestellar/kubeflex/cmd/kflex/common"
24+
"k8s.io/client-go/tools/clientcmd"
25+
)
26+
27+
type CPContextList struct {
28+
CP common.CP
29+
}
30+
31+
func (cp *CPContextList) ListContexts() {
32+
config, err := clientcmd.LoadFromFile(cp.CP.Kubeconfig)
33+
if err != nil {
34+
fmt.Printf("Error loading kubeconfig: %s\n", err)
35+
os.Exit(1)
36+
}
37+
38+
if len(config.Contexts) == 0 {
39+
fmt.Println("No contexts found.")
40+
return
41+
}
42+
43+
currentContext := config.CurrentContext
44+
fmt.Println("Available Contexts:")
45+
for name := range config.Contexts {
46+
prefix := " "
47+
if name == currentContext {
48+
prefix = "*"
49+
}
50+
fmt.Printf("%s %s\n", prefix, name)
51+
}
52+
}

cmd/kflex/main.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ import (
3232
cont "github.com/kubestellar/kubeflex/cmd/kflex/ctx"
3333
del "github.com/kubestellar/kubeflex/cmd/kflex/delete"
3434
in "github.com/kubestellar/kubeflex/cmd/kflex/init"
35-
"github.com/kubestellar/kubeflex/cmd/kflex/list"
3635
cluster "github.com/kubestellar/kubeflex/cmd/kflex/init/cluster"
36+
"github.com/kubestellar/kubeflex/cmd/kflex/list"
3737
"github.com/kubestellar/kubeflex/pkg/client"
3838
"github.com/kubestellar/kubeflex/pkg/util"
3939
"github.com/spf13/cobra"
@@ -240,6 +240,22 @@ var listCmd = &cobra.Command{
240240
},
241241
}
242242

243+
var listCtxCmd = &cobra.Command{
244+
Use: "list",
245+
Short: "List all available contexts",
246+
Long: `List all available contexts in the kubeconfig file`,
247+
Args: cobra.ExactArgs(0),
248+
Run: func(cmd *cobra.Command, args []string) {
249+
cp := cont.CPCtx{
250+
CP: common.CP{
251+
Ctx: createContext(),
252+
Kubeconfig: kubeconfig,
253+
},
254+
}
255+
cp.ListContexts()
256+
},
257+
}
258+
243259
func init() {
244260
versionCmd.Flags().StringVarP(&kubeconfig, "kubeconfig", "k", "", "path to the kubeconfig file for the KubeFlex hosting cluster. If not specified, and $KUBECONFIG is set, it uses the value in $KUBECONFIG, otherwise it falls back to ${HOME}/.kube/configg")
245261
versionCmd.Flags().BoolVarP(&chattyStatus, "chatty-status", "s", true, "chatty status indicator")
@@ -281,6 +297,7 @@ func init() {
281297
ctxCmd.Flags().BoolVarP(&setCurrentCtxAsHosting, "set-current-for-hosting", "c", false, "Set current context as hosting cluster context")
282298

283299
ctxCmd.AddCommand(ctxGetCmd)
300+
ctxCmd.AddCommand(listCtxCmd)
284301

285302
rootCmd.AddCommand(versionCmd)
286303
rootCmd.AddCommand(initCmd)

docs/users.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,3 +908,10 @@ helm delete -n kubeflex-system postgres
908908
kubectl delete pvc data-postgres-postgresql-0
909909
kubectl delete ns kubeflex-system
910910
```
911+
912+
## Listing Available Contexts
913+
914+
To list all available contexts in your kubeconfig file, use the following command:
915+
916+
```shell
917+
kflex ctx list

pkg/kubeconfig/kubeconfig.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,22 @@ func renameKey(m interface{}, oldKey string, newKey string) interface{} {
221221
}
222222

223223
func GetCurrentContext(ctx context.Context) (string, error) {
224-
kconf, err := LoadKubeconfig(ctx)
225-
if err != nil {
226-
return "", fmt.Errorf("error loading kubeconfig: %v", err)
227-
}
228-
return kconf.CurrentContext, nil
224+
config, err := LoadKubeconfig(ctx)
225+
if err != nil {
226+
return "", err
227+
}
228+
return config.CurrentContext, nil
229+
}
230+
231+
func ListContexts(ctx context.Context) ([]string, error) {
232+
config, err := LoadKubeconfig(ctx)
233+
if err != nil {
234+
return nil, err
235+
}
236+
237+
contexts := make([]string, 0, len(config.Contexts))
238+
for name := range config.Contexts {
239+
contexts = append(contexts, name)
240+
}
241+
return contexts, nil
229242
}

0 commit comments

Comments
 (0)