Skip to content

Commit

Permalink
Merge pull request #34 from navilg/32-not-printing-proper-error-messa…
Browse files Browse the repository at this point in the history
…ge-if-no-kubeconfig-file-or-kubeconfig-env-variable-found-with-kubectl-auto

fix #32: Not printing proper error message if no .kube/config file or KUBECONFIG env variable found with kubectl auto
  • Loading branch information
navilg authored Nov 2, 2024
2 parents bdb42ad + d038ab7 commit cdf24db
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 8 deletions.
11 changes: 5 additions & 6 deletions internal/ikubernetes/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package ikubernetes
import (
"context"
"errors"
"log"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -37,12 +36,12 @@ func GetK8sVersion() (*string, error) {

clientset, err := kubernetes.NewForConfig(kubeconfig)
if err != nil {
log.Fatalf("Error setting up K8s client")
return nil, err
}

k8sVersion, err := clientset.ServerVersion()
if err != nil {
log.Fatalf("Error getting K8s server version: %v", err)
return nil, err
}

k8sSemversion := k8sVersion.String()
Expand Down Expand Up @@ -72,15 +71,15 @@ func GetVeleroVersion() (*string, error) {

clientset, err := kubernetes.NewForConfig(kubeconfig)
if err != nil {
log.Fatalf("Error setting up K8s client")
return nil, err
}

namespace := "velero"
deploymentName := "velero"

deployment, err := clientset.AppsV1().Deployments(namespace).Get(context.TODO(), deploymentName, metav1.GetOptions{})
if err != nil {
return nil, errors.New("Error retrieving velero deployment from velero namespace")
return nil, errors.New("error retrieving velero deployment from velero namespace")
}

veleroImageName := deployment.Spec.Template.Spec.Containers[0].Image
Expand All @@ -90,7 +89,7 @@ func GetVeleroVersion() (*string, error) {
semVersionRegex := regexp.MustCompile(`^v?(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z-.]+))?(?:\+([0-9A-Za-z-.]+))?$`)

if !semVersionRegex.MatchString(veleroVersion) {
return nil, errors.New("Velero image used in velero deployment is not semantic version: " + veleroVersion)
return nil, errors.New("velero image used in velero deployment is not semantic version: " + veleroVersion)
}

return &veleroVersion, nil
Expand Down
2 changes: 1 addition & 1 deletion internal/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func InstallKubectl(version string, overwrite bool, timeout int, proxy string) e
fmt.Println("Fetching Kubernetes server version")
k8sVersion, err := ikubernetes.GetK8sVersion()
if err != nil {
fmt.Println("Error getting Kubernetes server version")
fmt.Println("Error getting Kubernetes server version. Error:", err.Error())
return err
}
version = *k8sVersion
Expand Down
2 changes: 1 addition & 1 deletion internal/use/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func UseVersion(toolname, version string) error {
fmt.Println("Fetching Kubernetes server version")
k8sVersion, err := ikubernetes.GetK8sVersion()
if err != nil {
fmt.Println("Error getting Kubernetes server version")
fmt.Println("Error getting Kubernetes server version. Error:", err.Error())
return err
}
version = *k8sVersion
Expand Down

0 comments on commit cdf24db

Please sign in to comment.