-
Notifications
You must be signed in to change notification settings - Fork 24
/
client.go
94 lines (76 loc) · 2.81 KB
/
client.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package client
import (
"context"
"github.com/dtan4/k8sec/version"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/clientcmd/api"
)
// Client represents Kubernetes client and calculated namespace
type Client interface {
DefaultNamespace() string
CreateSecret(ctx context.Context, namespace string, secret *v1.Secret) (*v1.Secret, error)
GetSecret(ctx context.Context, namespace, name string) (*v1.Secret, error)
ListSecrets(ctx context.Context, namespace string) (*v1.SecretList, error)
UpdateSecret(ctx context.Context, namespace string, secret *v1.Secret) (*v1.Secret, error)
}
type clientImpl struct {
clientset kubernetes.Interface
rawConfig api.Config
}
// New creates new Kubernetes API client
func New(kubeconfig, context string) (*clientImpl, error) {
if kubeconfig == "" {
kubeconfig = clientcmd.RecommendedHomeFile
}
clientConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeconfig},
&clientcmd.ConfigOverrides{CurrentContext: context},
)
config, err := clientConfig.ClientConfig()
if err != nil {
return nil, err
}
config.UserAgent = version.UserAgent()
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}
rawConfig, err := clientConfig.RawConfig()
if err != nil {
return nil, err
}
return &clientImpl{
clientset: clientset,
rawConfig: rawConfig,
}, nil
}
// DefaultNamespace returns the default namespace in kubeconfig
func (c *clientImpl) DefaultNamespace() string {
var namespace string
if c.rawConfig.Contexts[c.rawConfig.CurrentContext].Namespace == "" {
namespace = v1.NamespaceDefault
} else {
namespace = c.rawConfig.Contexts[c.rawConfig.CurrentContext].Namespace
}
return namespace
}
// CreateSecret creates new Secret
func (c *clientImpl) CreateSecret(ctx context.Context, namespace string, secret *v1.Secret) (*v1.Secret, error) {
return c.clientset.CoreV1().Secrets(namespace).Create(ctx, secret, metav1.CreateOptions{})
}
// GetSecret returns secret with the given name
func (c *clientImpl) GetSecret(ctx context.Context, namespace, name string) (*v1.Secret, error) {
return c.clientset.CoreV1().Secrets(namespace).Get(ctx, name, metav1.GetOptions{})
}
// ListSecrets returns the list of Secrets
func (c *clientImpl) ListSecrets(ctx context.Context, namespace string) (*v1.SecretList, error) {
return c.clientset.CoreV1().Secrets(namespace).List(ctx, metav1.ListOptions{})
}
// UpdateSecret updates the existed secret
func (c *clientImpl) UpdateSecret(ctx context.Context, namespace string, secret *v1.Secret) (*v1.Secret, error) {
return c.clientset.CoreV1().Secrets(namespace).Update(ctx, secret, metav1.UpdateOptions{})
}