-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathk8s.go
72 lines (63 loc) · 2.39 KB
/
k8s.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
70
71
72
// Package k8s provides a PodLister and PodDeleter that talks to
// a real Kubernetes
package k8s
import (
"github.com/pkg/errors"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
// Client is a wrapper around a Kubernetes cluster
type Client struct {
client *kubernetes.Clientset
}
// New creates and returns a new client. If kubeconfig is not define, then
// an in-cluster client is created. context is only used if kubeconfig
// is specified and sets the k8s context - if blank, current context from the
// config file is used.
func New(kubeconfig string, context string) (*Client, error) {
if kubeconfig == "" {
config, err := rest.InClusterConfig()
if err != nil {
return nil, errors.Wrap(err, "failed to create an in-cluster config")
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, errors.Wrap(err, "failed to create an in-cluster client")
}
return &Client{clientset}, nil
}
config, err := k8sConfig(kubeconfig, context)
if err != nil {
return nil, errors.Wrapf(err, "failed to create a config from %q", kubeconfig)
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, errors.Wrapf(err, "failed to create a client from %q", kubeconfig)
}
return &Client{clientset}, nil
}
func k8sConfig(kubeconfig string, context string) (*rest.Config, error) {
return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeconfig},
&clientcmd.ConfigOverrides{CurrentContext: context},
).ClientConfig()
}
// ListPods will return a list of Pods in a namespace, optionally using a label selector.
// Empty namespace means all namespaces
func (c *Client) ListPods(namespace string, selector string) ([]v1.Pod, error) {
pods, err := c.client.CoreV1().Pods(namespace).List(metav1.ListOptions{LabelSelector: selector})
if err != nil {
return nil, errors.Wrap(err, "failed to list pods")
}
return pods.Items, nil
}
// DeletePod attempts to delete a single pod
func (c *Client) DeletePod(namespace string, name string) error {
// XXX: Do we need any delete options?
// https://godoc.org/k8s.io/apimachinery/pkg/apis/meta/v1#DeleteOptions
// we do not wrap the error here, as the caller may need to check it directly
return c.client.CoreV1().Pods(namespace).Delete(name, nil)
}