This repository has been archived by the owner on Dec 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathkubernetes.go
163 lines (136 loc) · 4.01 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
package pprofserver
import (
"context"
"fmt"
"net"
"sort"
"strings"
"time"
"github.com/segmentio/events"
apiv1 "k8s.io/api/core/v1"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
)
// KubernetesRegistry is a registry which discovers PODs running
// on a Kubernetes cluster.
//
// TODO: give the ability to configure multiple Kubernetes clusters.
type KubernetesRegistry struct {
Namespace string
client kubernetes.Interface
store cache.Store
}
func NewKubernetesRegistry(client *kubernetes.Clientset) *KubernetesRegistry {
return &KubernetesRegistry{
client: client,
}
}
// Name implements the Registry interface.
func (k *KubernetesRegistry) String() string {
return "kubernetes"
}
// Init initialize the watcher and store configuration for the registry.
func (k *KubernetesRegistry) Init(ctx context.Context) {
p := k.client.CoreV1().Pods(k.Namespace)
listWatch := &cache.ListWatch{
ListFunc: func(options metaV1.ListOptions) (runtime.Object, error) {
return p.List(context.TODO(), options)
},
WatchFunc: func(options metaV1.ListOptions) (watch.Interface, error) {
return p.Watch(context.TODO(), options)
},
}
queue := workqueue.New()
informer := cache.NewSharedInformer(listWatch, &apiv1.Pod{}, 10*time.Second)
informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
k.handleObj(queue, obj)
},
DeleteFunc: func(obj interface{}) {
k.handleObj(queue, obj)
},
UpdateFunc: func(_, obj interface{}) {
k.handleObj(queue, obj)
},
})
go informer.Run(ctx.Done())
k.store = informer.GetStore()
}
func (k *KubernetesRegistry) handleObj(q *workqueue.Type, o interface{}) {
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(o)
if err != nil {
events.Log("failed to handle object: %{error}s", err)
return
}
q.Add(key)
}
func toPod(o interface{}) (*apiv1.Pod, error) {
pod, ok := o.(*apiv1.Pod)
if ok {
return pod, nil
}
return nil, fmt.Errorf("received unexpected object: %v", o)
}
func (k *KubernetesRegistry) ListServices(ctx context.Context) ([]string, error) {
podnames, err := k.client.CoreV1().Pods(k.Namespace).List(ctx, metaV1.ListOptions{})
if err != nil {
return nil, err
}
list := make([]string, 0, len(podnames.Items))
for _, pod := range podnames.Items {
list = append(list, joinNamespacePodName(pod.Namespace, pod.Name))
}
sort.Strings(list)
return list, nil
}
// LookupService implements the Registry interface. The returned Service will contain
// one Host entry per POD IP+container exposed port.
func (k *KubernetesRegistry) LookupService(ctx context.Context, name string) (Service, error) {
svc := Service{
Name: "kubernetes",
}
namespace, podName := splitNamespacePodName(name)
hosts := []Host{}
for _, obj := range k.store.List() {
pod, err := toPod(obj)
if err != nil {
events.Log("failed to convert data to pod: %{error}s", err)
continue
}
// filtering pods based on podname, even if they are diff namepsaces for now, since the route for namespaces isnt made yet
if pod.Namespace == namespace && pod.Name == podName {
for _, container := range pod.Spec.Containers {
// adding container name to display
tags := []string{pod.Name + "-" + container.Name}
for _, port := range container.Ports {
if port.Name == "http" {
hosts = append(hosts, Host{
Addr: &net.TCPAddr{
IP: net.ParseIP(pod.Status.PodIP),
Port: int(port.ContainerPort),
},
Tags: append(tags, port.Name), // port name must be specified in the pod spec as http
})
}
}
}
}
}
svc.Hosts = hosts
return svc, nil
}
func joinNamespacePodName(namespace, podName string) string {
return namespace + "/" + podName
}
func splitNamespacePodName(name string) (namespace, podName string) {
if i := strings.IndexByte(name, '/'); i < 0 {
podName = name
} else {
namespace, podName = name[:i], name[i+1:]
}
return
}