This repository has been archived by the owner on Sep 16, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
kubernetes.go
317 lines (258 loc) · 8.46 KB
/
kubernetes.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package kubernetes
import (
"context"
"fmt"
"net/http"
"net/url"
"os"
"regexp"
"strings"
"github.com/ckotzbauer/chekr/pkg/prometheus"
"github.com/ckotzbauer/chekr/pkg/util"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
pfwd "k8s.io/client-go/tools/portforward"
"k8s.io/client-go/transport/spdy"
"k8s.io/kubectl/pkg/cmd/portforward"
)
type KubeClient struct {
Client *kubernetes.Clientset
Config *rest.Config
}
type defaultPortForwarder struct {
genericclioptions.IOStreams
prometheus *prometheus.Prometheus
}
type PodQuery struct {
Namespace string
LabelSelector string
AnnotationSelector string
Names []string
}
func IsInCluster() bool {
_, err := rest.InClusterConfig()
return err == nil
}
func BindFlags(flags *pflag.FlagSet) *clientcmd.ConfigOverrides {
cmd := &clientcmd.ConfigOverrides{}
overrides := clientcmd.RecommendedConfigOverrideFlags("")
clientcmd.BindOverrideFlags(cmd, flags, overrides)
viper.BindPFlags(flags)
return cmd
}
func NewClient(cmd *cobra.Command, configOverrides *clientcmd.ConfigOverrides) *KubeClient {
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
configFile, err := cmd.Root().PersistentFlags().GetString(clientcmd.RecommendedConfigPathFlag)
if err != nil {
logrus.WithError(err).Fatalf("kubeconfig flag could not be retrieved!")
}
if configFile != "" {
loadingRules.Precedence = []string{configFile}
}
var config *rest.Config
kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, configOverrides)
config, err = kubeConfig.ClientConfig()
if err != nil {
logrus.WithError(err).Fatalf("kubeconfig file could not be found!")
}
client, err := kubernetes.NewForConfig(config)
if err != nil {
logrus.WithError(err).Fatalf("Could not create Kubernetes client from config!")
}
return &KubeClient{Client: client, Config: config}
}
func (kubeClient *KubeClient) ListPods(query PodQuery) []corev1.Pod {
listOptions := metav1.ListOptions{}
if len(query.LabelSelector) > 0 {
listOptions.LabelSelector = query.LabelSelector
}
list, err := kubeClient.Client.CoreV1().Pods(query.Namespace).List(context.Background(), listOptions)
pods := []corev1.Pod{}
if err != nil {
logrus.WithError(
err).WithField(
"namespace", query.Namespace).WithField(
"labelSelector", query.LabelSelector).Fatalf(
"Could list pods!")
}
for _, p := range list.Items {
if len(query.Names) > 0 && !util.Contains(query.Names, p.Name) {
continue
}
if len(query.AnnotationSelector) > 0 {
selectors := util.ParseSelector(query.AnnotationSelector)
matched := true
for _, selector := range selectors {
potentialValue := p.Annotations[selector.Key]
if selector.Operator == "=" {
matched = matched && potentialValue == selector.Value
} else if selector.Operator == "!=" {
matched = matched && potentialValue != selector.Value
} else if selector.Operator == "" {
matched = matched && potentialValue != ""
}
}
if !matched {
continue
}
}
pods = append(pods, p)
}
return pods
}
func (kubeClient *KubeClient) GetReplicaSet(pod corev1.Pod) (*appsv1.ReplicaSet, error) {
var rsName string
for _, ref := range pod.OwnerReferences {
if ref.Kind == "ReplicaSet" {
rsName = ref.Name
}
}
if rsName == "" {
return nil, nil
}
return kubeClient.Client.AppsV1().ReplicaSets(pod.Namespace).Get(context.Background(), rsName, metav1.GetOptions{})
}
func (kubeClient *KubeClient) GetDeployment(namespace string, name string) (*appsv1.Deployment, error) {
return kubeClient.Client.AppsV1().Deployments(namespace).Get(context.Background(), name, metav1.GetOptions{})
}
func (kubeClient *KubeClient) GetDaemonSet(namespace string, name string) (*appsv1.DaemonSet, error) {
return kubeClient.Client.AppsV1().DaemonSets(namespace).Get(context.Background(), name, metav1.GetOptions{})
}
func (kubeClient *KubeClient) GetStatefulSet(namespace string, name string) (*appsv1.StatefulSet, error) {
return kubeClient.Client.AppsV1().StatefulSets(namespace).Get(context.Background(), name, metav1.GetOptions{})
}
func (kubeClient *KubeClient) GetPersistentVolumeClaim(namespace string, name string) (*corev1.PersistentVolumeClaim, error) {
return kubeClient.Client.CoreV1().PersistentVolumeClaims(namespace).Get(context.Background(), name, metav1.GetOptions{})
}
func (kubeClient *KubeClient) DiscoverResourceNameAndPreferredGV() KindVersions {
kv := make(KindVersions)
groups, resources, err := kubeClient.Client.DiscoveryClient.ServerGroupsAndResources()
if err != nil {
if apierrors.IsNotFound(err) {
return kv
}
if apierrors.IsForbidden(err) {
logrus.WithError(err).Fatalf("Failed to list objects for Name discovery. Permission denied! Please check if you have the proper authorization")
}
logrus.WithError(err).Fatalf("Failed communicating with k8s while discovering the object preferred name and gv")
}
for _, resourceGroup := range resources {
parts := strings.Split(resourceGroup.GroupVersion, "/")
var group, version string
if len(parts) == 1 {
group = ""
version = parts[0]
} else {
group = parts[0]
version = parts[1]
}
for _, resource := range resourceGroup.APIResources {
if strings.Contains(resource.Name, "/") {
continue
}
kindVersion := KindVersion{
Group: group,
Version: version,
Name: resource.Name,
Preferred: isVersionPreferred(group, version, groups),
}
if _, ok := kv[resource.Kind]; !ok {
kv[resource.Kind] = []KindVersion{kindVersion}
} else {
kv[resource.Kind] = append(kv[resource.Kind], kindVersion)
}
}
}
return kv
}
func isVersionPreferred(group, currentVersion string, allGroups []*metav1.APIGroup) bool {
for _, supportedGroup := range allGroups {
if supportedGroup.Name != group {
continue
}
return supportedGroup.PreferredVersion.Version == currentVersion
}
return false
}
func ParseResourceName(url string) (string, string, string) {
r := regexp.MustCompile(`^([a-z-_0-9A-Z\.]+)\/([a-z-_0-9A-Z\.]+):?([0-9]{0,4})$`)
if r.MatchString(url) {
groups := r.FindStringSubmatch(url)
namespace := groups[1]
name := groups[2]
var port string
if len(groups) > 3 && groups[3] != "" {
port = groups[3]
} else {
port = "9090"
}
return namespace, name, port
} else {
return "", "", ""
}
}
func (kubeClient *KubeClient) ForwardResource(prometheus *prometheus.Prometheus, readyChannel, stopChannel chan struct{}) {
namespace, name, port := ParseResourceName(prometheus.Url)
opts := portforward.PortForwardOptions{
Namespace: namespace,
PodName: name,
PodClient: kubeClient.Client.CoreV1(),
Config: kubeClient.Config,
RESTClient: kubeClient.Client.RESTClient().(*rest.RESTClient),
StopChannel: stopChannel,
ReadyChannel: readyChannel,
Ports: []string{fmt.Sprintf("%s:%s", "0", port)},
Address: []string{"127.0.0.1"},
PortForwarder: &defaultPortForwarder{
prometheus: prometheus,
IOStreams: genericclioptions.IOStreams{
In: os.Stdin,
Out: logrus.New().WriterLevel(logrus.DebugLevel),
ErrOut: logrus.New().WriterLevel(logrus.ErrorLevel),
},
},
}
err := opts.Validate()
if err != nil {
logrus.WithError(err).Fatal("Validation for port-forward has failed!")
}
err = opts.RunPortForward()
if err != nil {
logrus.WithError(err).Fatal("Port-forward has failed!")
}
}
func (f *defaultPortForwarder) ForwardPorts(method string, url *url.URL, opts portforward.PortForwardOptions) error {
transport, upgrader, err := spdy.RoundTripperFor(opts.Config)
if err != nil {
return err
}
pfwUrl := opts.RESTClient.Post().
Prefix("api/v1").
Resource("pods").
Namespace(opts.Namespace).
Name(opts.PodName).
SubResource("portforward").URL()
readyChannel := make(chan struct{})
dialer := spdy.NewDialer(upgrader, &http.Client{Transport: transport}, method, pfwUrl)
fw, err := pfwd.NewOnAddresses(dialer, opts.Address, opts.Ports, opts.StopChannel, readyChannel, f.Out, f.ErrOut)
if err != nil {
return err
}
go func() {
<-readyChannel
ports, _ := fw.GetPorts()
f.prometheus.Url = fmt.Sprintf("http://%v:%v", opts.Address[0], ports[0].Local)
close(opts.ReadyChannel)
}()
return fw.ForwardPorts()
}