-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
121 lines (109 loc) · 2.82 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package gk8s
import (
"encoding/base64"
"path/filepath"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/eks"
"sigs.k8s.io/aws-iam-authenticator/pkg/token"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
)
type Config struct {
RestConfig *rest.Config
APIClient kubernetes.Interface
}
// FromRestConfig - Init K8s Client with rest configuration
func FromRestConfig(config *rest.Config) (*Config, error) {
apiClient, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}
return &Config{
RestConfig: config,
APIClient: apiClient,
}, nil
}
// InCluster - Init K8s Client with in cluster credentials
func InCluster() (*Config, error) {
config, err := rest.InClusterConfig()
if err != nil {
return nil, err
}
return FromRestConfig(config)
}
// FromKubeConfig - Init K8s Client with kubeconfig file $USER/.kube/config
func FromKubeConfig() (*Config, error) {
home := homedir.HomeDir()
path := filepath.Join(home, ".kube", "config")
return FromKubeConfigPath(path)
}
// FromKubeConfigPath - Init K8s Client with specific kubeconfig file
func FromKubeConfigPath(path string) (*Config, error) {
config, err := clientcmd.BuildConfigFromFlags("", path)
if err != nil {
return nil, err
}
return FromRestConfig(config)
}
// FromEKS - Init K8s Client with AWS shared-credentials-file and cluster name
func FromEKS(clusterName string) (*Config, error) {
sess, err := getSession()
if err != nil {
return nil, err
}
config, err := getEKSConfig(sess, clusterName)
if err != nil {
return nil, err
}
return FromRestConfig(config)
}
func getSession() (*session.Session, error) {
sess, err := session.NewSession()
if err != nil {
return nil, err
}
return sess, nil
}
func getEKSCluster(sess *session.Session, clusterName string) (*eks.Cluster, error) {
svc := eks.New(sess)
input := eks.DescribeClusterInput{
Name: aws.String(clusterName),
}
output, err := svc.DescribeCluster(&input)
if err != nil {
return nil, err
}
return output.Cluster, nil
}
func getEKSConfig(sess *session.Session, clusterName string) (*rest.Config, error) {
cluster, err := getEKSCluster(sess, clusterName)
if err != nil {
return nil, err
}
gen, err := token.NewGenerator(true, false)
if err != nil {
return nil, err
}
opts := &token.GetTokenOptions{
Session: sess,
ClusterID: aws.StringValue(cluster.Name),
}
tok, err := gen.GetWithOptions(opts)
if err != nil {
return nil, err
}
ca, err := base64.StdEncoding.DecodeString(aws.StringValue(cluster.CertificateAuthority.Data))
if err != nil {
return nil, err
}
return &rest.Config{
Host: aws.StringValue(cluster.Endpoint),
BearerToken: tok.Token,
TLSClientConfig: rest.TLSClientConfig{
CAData: ca,
},
}, nil
}