-
Notifications
You must be signed in to change notification settings - Fork 0
/
clusterConfig.go
69 lines (53 loc) · 1.5 KB
/
clusterConfig.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright 2022 Anton Mamonov <hi@antonmamonov.com> GNU GENERAL PUBLIC LICENSE
package k8sutils
import (
"context"
"fmt"
"os"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
type K8sClusterConfig struct {
ClientSet *kubernetes.Clientset
}
func GetK8sClusterConfig() (*K8sClusterConfig, error) {
// get the home directory path
homeDir, homeDirError := os.UserHomeDir()
if homeDirError != nil {
return nil, homeDirError
}
fmt.Println("homeDir", homeDir)
// use the current context in kubeconfig
config, err := clientcmd.BuildConfigFromFlags("", homeDir+"/.kube/config")
if err != nil {
return nil, err
}
// create the clientset
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}
k8sClusterConfig := &K8sClusterConfig{
ClientSet: clientset,
}
return k8sClusterConfig, nil
}
func CheckK8sClusterConfigConnection() (bool, error) {
k8sClusterConfig, err := GetK8sClusterConfig()
if err != nil {
return false, err
}
// get pods in all the namespaces by omitting namespace
// Or specify namespace to get pods in particular namespace
pods, err := k8sClusterConfig.ClientSet.CoreV1().Pods("").List(context.TODO(), metav1.ListOptions{})
if err != nil {
return false, err
}
fmt.Printf("[CheckK8sClusterConfigConnection] There are %d pods in the cluster\n", len(pods.Items))
// how can a cluster have 0 pods overall?
if len(pods.Items) == 0 {
return false, nil
}
return true, nil
}