forked from cloudwatt/kube2consul
-
Notifications
You must be signed in to change notification settings - Fork 0
/
endpoint.go
52 lines (45 loc) · 1.17 KB
/
endpoint.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
package main
import (
"fmt"
"k8s.io/client-go/pkg/api/v1"
)
// Endpoint is a summary of kubernetes endpoint
type Endpoint struct {
Name string
Address string
Port int32
RefName string
}
// NewEndpoint allows to create Endpoint
func NewEndpoint(name, address string, port int32, refName string) Endpoint {
return Endpoint{name, address, port, refName}
}
func generateEntries(endpoint *v1.Endpoints) []Endpoint {
var (
eps []Endpoint
refName string
)
for _, subset := range endpoint.Subsets {
for _, addr := range subset.Addresses {
if addr.TargetRef != nil {
refName = addr.TargetRef.Name
}
for _, port := range subset.Ports {
eps = append(eps, NewEndpoint(endpoint.Name, addr.IP, port.Port, refName))
}
}
}
return eps
}
func (k2c *kube2consul) updateEndpoints(ep *v1.Endpoints) error {
endpoints := generateEntries(ep)
for _, e := range endpoints {
if err := k2c.registerEndpoint(e); err != nil {
return fmt.Errorf("Error updating endpoints %v: %v", ep.Name, err)
}
}
if err := k2c.removeDeletedEndpoints(ep.Name, endpoints); err != nil {
return fmt.Errorf("Error removing possible deleted endpoints: %v", err)
}
return nil
}