-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkube.go
48 lines (38 loc) · 1.31 KB
/
kube.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
package main
import (
"context"
"fmt"
"github.com/rs/zerolog/log"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
type kubeClient struct {
clientset *kubernetes.Clientset
}
// newKubeClient creates a new kuberentes client
func newKubeClient() *kubeClient {
log.Info().Msg("creating kubernetes client")
cfg, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
clientcmd.NewDefaultClientConfigLoadingRules(),
&clientcmd.ConfigOverrides{},
).ClientConfig()
if err != nil {
log.Fatal().Err(err).Msg("")
}
log.Debug().Msg("creating kubernetes clientset")
clientset, err := kubernetes.NewForConfig(cfg)
if err != nil {
log.Fatal().Err(err).Msg("")
}
return &kubeClient{clientset: clientset}
}
// getDriverPod returns the driver pod for the given name and namespace
func (k *kubeClient) getDriverPod(name, ns string) (*corev1.Pod, error) {
return k.clientset.CoreV1().Pods(ns).Get(context.TODO(), fmt.Sprintf("%s-driver", name), metav1.GetOptions{})
}
// deleteDriverPod deletes the driver pod for the given name and namespace
func (k *kubeClient) deleteDriverPod(name, ns string) error {
return k.clientset.CoreV1().Pods(ns).Delete(context.TODO(), fmt.Sprintf("%s-driver", name), metav1.DeleteOptions{})
}