-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
Copy pathmain.go
82 lines (67 loc) · 1.76 KB
/
main.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
package main
// #include <stdio.h>
// #include <stdlib.h>
import "C"
import (
"context"
"encoding/json"
"log"
"strings"
"unicode"
"unsafe"
"github.com/ericchiang/k8s"
corev1 "github.com/ericchiang/k8s/apis/core/v1"
)
type endpoints struct {
IPs []string `json:"ips"`
}
// GetEndpoints searches the endpoints of a service returning a list of IP addresses.
//export GetEndpoints
func GetEndpoints(namespace, service, defSeeds *C.char) *C.char {
ns := C.GoString(namespace)
svc := C.GoString(service)
seeds := C.GoString(defSeeds)
s := strings.Map(func(r rune) rune {
if unicode.IsSpace(r) {
return -1
}
return r
}, seeds)
nseeds := strings.Split(s, ",")
client, err := k8s.NewInClusterClient()
if err != nil {
log.Printf("unexpected error opening a connection against API server: %v\n", err)
log.Printf("returning default seeds: %v\n", nseeds)
return buildEndpoints(nseeds)
}
ips := make([]string, 0)
var endpoints corev1.Endpoints
err = client.Get(context.Background(), ns, svc, &endpoints)
if err != nil {
log.Printf("unexpected error obtaining information about service endpoints: %v\n", err)
log.Printf("returning default seeds: %v\n", nseeds)
return buildEndpoints(nseeds)
}
for _, endpoint := range endpoints.Subsets {
for _, address := range endpoint.Addresses {
ips = append(ips, *address.Ip)
}
}
if len(ips) == 0 {
return buildEndpoints(nseeds)
}
return buildEndpoints(ips)
}
func buildEndpoints(ips []string) *C.char {
b, err := json.Marshal(&endpoints{ips})
if err != nil {
log.Printf("unexpected error serializing JSON response: %v\n", err)
rc := C.CString(`{"ips":[]}`)
defer C.free(unsafe.Pointer(rc))
return rc
}
rc := C.CString(string(b))
defer C.free(unsafe.Pointer(rc))
return rc
}
func main() {}