-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkube.go
213 lines (174 loc) · 5.38 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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package kube
import (
"context"
"errors"
"fmt"
core "k8s.io/api/core/v1"
apixv1beta1client "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/typed/apiextensions/v1beta1"
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/rest"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)
type (
Kube interface {
GetNamespaces() ([]string, error)
GetClientSet() *kubernetes.Clientset
GetCrdClientSet() *apixv1beta1client.ApiextensionsV1beta1Client
GetArgoServerSvc(string) (core.Service, error)
GetLoadBalancerHost(svc core.Service) (string, error)
GetService(string) (*core.Service, error)
GetAutogeneratedPassword() (string, error)
GetArgoServerHost() (string, error)
}
kube struct {
contextName string
namespace string
pathToKubeConfig string
inCluster bool
clientSet *kubernetes.Clientset
crdClientSet *apixv1beta1client.ApiextensionsV1beta1Client
}
Options struct {
ContextName string
Namespace string
PathToKubeConfig string
InCluster bool
FailFast bool
}
)
func New(o *Options) (Kube, error) {
client := &kube{
contextName: o.ContextName,
namespace: o.Namespace,
pathToKubeConfig: o.PathToKubeConfig,
inCluster: o.InCluster,
}
clientSet, crdClientSet, err := client.buildClient()
if err != nil && !o.FailFast {
return nil, err
}
client.clientSet = clientSet
client.crdClientSet = crdClientSet
return client, nil
}
func (k *kube) GetAutogeneratedPassword() (string, error) {
opts := metav1.ListOptions{LabelSelector: "app.kubernetes.io/name=argocd-server"}
podsList, err := k.clientSet.CoreV1().Pods(k.namespace).List(context.Background(), opts)
if err != nil {
return "", err
}
return podsList.Items[0].ObjectMeta.Name, nil
}
func (k *kube) GetService(labelSelector string) (*core.Service, error) {
var argoServerSvc core.Service
opts := metav1.ListOptions{LabelSelector: labelSelector}
svcs, err := k.clientSet.CoreV1().Services(k.namespace).List(context.Background(), opts)
if err != nil {
return &argoServerSvc, err
}
if svcs == nil || len(svcs.Items) == 0 {
return &argoServerSvc, errors.New(fmt.Sprint("Invalid svcs"))
}
return &svcs.Items[0], nil
}
func IsLoadBalancer(svc core.Service) bool {
return svc.Spec.Type == "LoadBalancer"
}
func (k *kube) GetArgoServerHost() (string, error) {
svc, err := k.GetArgoServerSvc(k.namespace)
if err != nil {
return "", err
}
return k.GetLoadBalancerHost(svc)
}
func (k *kube) GetLoadBalancerHost(svc core.Service) (string, error) {
if svc.Status.LoadBalancer.Ingress == nil || len(svc.Status.LoadBalancer.Ingress) == 0 {
return "", errors.New(fmt.Sprint("Failed to resolve Load Balancer Hostname or IP"))
}
ingress := svc.Status.LoadBalancer.Ingress[0]
if ingress.Hostname != "" {
return "https://" + ingress.Hostname, nil
}
if ingress.IP != "" {
return "https://" + ingress.IP, nil
}
return "", errors.New(fmt.Sprint("Failed to retrieve Load Balancer Hostname or IP"))
}
func (k *kube) buildClient() (*kubernetes.Clientset, *apixv1beta1client.ApiextensionsV1beta1Client, error) {
var config *rest.Config
var err error
if k.inCluster {
config, err = rest.InClusterConfig()
} else {
config, err = clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: k.pathToKubeConfig},
&clientcmd.ConfigOverrides{
CurrentContext: k.contextName,
Context: clientcmdapi.Context{
Namespace: k.namespace,
},
}).ClientConfig()
}
if err != nil {
return nil, nil, err
}
clientSet, err := kubernetes.NewForConfig(config)
apixClient, err := apixv1beta1client.NewForConfig(config)
return clientSet, apixClient, err
}
func (k *kube) GetArgoServerSvc(namespace string) (core.Service, error) {
var argoServerSvc core.Service
opts := metav1.ListOptions{LabelSelector: "app.kubernetes.io/name=argocd-server"}
svcs, err := k.clientSet.CoreV1().Services(namespace).List(context.Background(), opts)
if err != nil {
return argoServerSvc, err
}
if svcs == nil || len(svcs.Items) == 0 {
return argoServerSvc, errors.New(fmt.Sprint("Invalid svcs"))
}
return svcs.Items[0], nil
}
func (k *kube) GetNamespaces() ([]string, error) {
namespaces, err := k.clientSet.CoreV1().Namespaces().List(context.Background(), metav1.ListOptions{})
if err != nil {
return nil, err
}
var result []string
for _, value := range namespaces.Items {
if value.Name == "default" {
result = append([]string{"default"}, result...)
continue
}
result = append(result, value.Name)
}
return result, nil
}
func (k *kube) GetClientSet() *kubernetes.Clientset {
return k.clientSet
}
func (k *kube) GetCrdClientSet() *apixv1beta1client.ApiextensionsV1beta1Client {
return k.crdClientSet
}
func GetAllContexts(pathToKubeConfig string) ([]string, error) {
var result []string
k8scmd := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: pathToKubeConfig},
&clientcmd.ConfigOverrides{})
config, err := k8scmd.RawConfig()
if err != nil {
return result, err
}
if config.CurrentContext != "" {
result = append(result, config.CurrentContext)
}
for k, _ := range config.Contexts {
if k == config.CurrentContext {
continue
}
result = append(result, k)
}
return result, err
}