Skip to content

Commit

Permalink
add support for "auto" as kubectl version with use subcommand
Browse files Browse the repository at this point in the history
Signed-off-by: Navratan Lal Gupta <navilg0409@gmail.com>
  • Loading branch information
navilg committed Oct 27, 2024
1 parent 73d5448 commit ce3be33
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ k8senv [VERB] [CLIENT] [VERSION]

# [VERB] can be install, use, list or remove
# [CLIENT] can be kubectl, velero or helm
# [VERSION] can be any client's version in format v*.*.* or latest or auto (only supported for kubectl and velero)
# [VERSION] can be any client's version in format v*.*.* or latest or auto (auto is only supported for kubectl install and use subcommands)

examples:

Expand Down Expand Up @@ -120,6 +120,7 @@ Any of below commands can be used to switch kubectl version to 1.26.2. If versio
k8senv use kubectl v1.26.2
k8senv use kubectl 1.26.2
k8senv kubectl use v1.26.2
k8senv use kubectl auto
```

If you were using system installed version of `kubectl` in same shell session, you will need to clear the cached path of the kubectl using below command.
Expand Down
12 changes: 12 additions & 0 deletions internal/use/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"github.com/navilg/k8senv/internal/config"
"github.com/navilg/k8senv/internal/ikubernetes"
"github.com/navilg/k8senv/internal/install"
)

Expand All @@ -19,6 +20,17 @@ func UseVersion(toolname, version string) error {
return errors.New(".k8senv/bin is not added in PATH environment variable")
}

if toolname == "kubectl" && version == "auto" {
fmt.Println("Fetching Kubernetes server version")
k8sVersion, err := ikubernetes.GetK8sVersion()
if err != nil {
fmt.Println("Error getting Kubernetes server version")
return err
}
version = *k8sVersion
fmt.Println("Kubernetes server version is", version)
}

major_minor_patch_vers := strings.Split(version, ".")

if !strings.HasPrefix(major_minor_patch_vers[0], "v") {
Expand Down

0 comments on commit ce3be33

Please sign in to comment.