Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions hack/e2e-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ set -x

E2E_TYPE=${1:-"pullrequest"}
KUBE_CONF=${2:-"${HOME}/.kube/config"}
export KUBE_CONF

REPO_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
cd "${REPO_ROOT}"
Expand Down
9 changes: 7 additions & 2 deletions test/e2e/node/test_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,20 @@ import (
"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"

"github.com/Project-HAMi/HAMi/test/utils"
)

var _ = ginkgo.Describe("[Node] Node E2E Tests", ginkgo.Ordered, func() {
var clientSet = utils.GetClientSet()
var nodeName string
var (
clientSet *kubernetes.Clientset
nodeName string
)

ginkgo.BeforeAll(func() {
clientSet = utils.GetClientSet()

// Get all nodes in the cluster
nodes, err := utils.GetNodes(clientSet)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
Expand Down
4 changes: 3 additions & 1 deletion test/e2e/pod/test_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ var _ = ginkgo.Describe("Pod E2E Tests", ginkgo.Ordered, func() {
)

var (
clientSet = utils.GetClientSet()
clientSet *kubernetes.Clientset
newPod *corev1.Pod
nodeName string
)

ginkgo.BeforeAll(func() {
clientSet = utils.GetClientSet()

var err error
nodeName, err = utils.GetGPUNode(clientSet)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
Expand Down
40 changes: 25 additions & 15 deletions test/utils/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"math/rand"
"os"
"os/exec"
"path/filepath"
"strconv"
"time"

Expand All @@ -33,34 +34,43 @@ import (
var kubeConfig string

func init() {
flag.StringVar(&kubeConfig, "kubeconfig", defaultKubeConfigPath(), "Path to the kubeConfig file")
flag.StringVar(&kubeConfig, "kubeconfig", "", "Path to the kubeConfig file")
}

func defaultKubeConfigPath() string {
configPath := os.Getenv("KUBE_CONF")
if configPath == "" {
klog.Fatalf("Environment variable KUBE_CONF is not set or empty. Please set it to a valid kubeconfig file path.")
// resolveKubeConfigPath picks kubeconfig in order: --kubeconfig flag, KUBE_CONF, ~/.kube/config.
func resolveKubeConfigPath() string {
if kubeConfig != "" {
return validateKubeConfigPath(kubeConfig)
}
if configPath := os.Getenv("KUBE_CONF"); configPath != "" {
kubeConfig = validateKubeConfigPath(configPath)
return kubeConfig
}
home, err := os.UserHomeDir()
if err == nil {
defaultPath := filepath.Join(home, ".kube", "config")
if _, err := os.Stat(defaultPath); err == nil {
kubeConfig = defaultPath
return kubeConfig
}
}
klog.Fatalf("kubeconfig not set: pass --kubeconfig, set KUBE_CONF, or place config at ~/.kube/config")
return ""
}

func validateKubeConfigPath(configPath string) string {
if _, err := os.Stat(configPath); os.IsNotExist(err) {
klog.Fatalf("Kubeconfig file does not exist at path: %s", configPath)
}
return configPath
}

func DefaultKubeConfigPath() string {
configPath := os.Getenv("KUBE_CONF")
if configPath == "" {
klog.Fatalf("Environment variable KUBE_CONF is not set or empty. Please set it to a valid kubeconfig file path.")
}

if _, err := os.Stat(configPath); os.IsNotExist(err) {
klog.Fatalf("lalala Kubeconfig file does not exist at path: %s, error is %s", configPath, err)
}
return configPath
return resolveKubeConfigPath()
}

func GetClientSet() *kubernetes.Clientset {
config, err := clientcmd.BuildConfigFromFlags("", kubeConfig)
config, err := clientcmd.BuildConfigFromFlags("", resolveKubeConfigPath())
if err != nil {
klog.Fatalf("Failed to load kubeConfig: %v", err)
}
Expand Down
Loading