forked from GauntletWizard/targetgroupcontroller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
104 lines (88 loc) · 2.63 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/elbv2"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
type IPGroupController struct {
targetGroupARN string
}
var region = flag.String("region", "", "AWS Region")
var targetARN = flag.String("targetgrouparn", "", "ARN of the Target group to update")
var service = flag.String("service", "", "Name of the service to watch")
var port = flag.Int64("port", 0, "Port number")
var httpAddr = flag.String("http", ":8080", "Address to bind for metrics server")
func main() {
flag.Parse()
if *region == "" || *targetARN == "" || *service == "" || *port == 0 {
flag.PrintDefaults()
os.Exit(2)
}
// Start metrics server
http.Handle("/metrics", promhttp.Handler())
go func() { log.Fatal(http.ListenAndServe(*httpAddr, nil)) }()
// Setup AWS
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
elbSvc := elbv2.New(sess, &aws.Config{Region: region})
log.Println("Port is custom")
metadata := ec2metadata.New(sess)
currentVpc, err := getVpcIDFromEC2Metadata(metadata)
if err != nil {
log.Panic(err)
}
log.Printf("Current vpc is %s\n", currentVpc)
targetVpc, err := getTargetGroupVpcID(elbSvc, targetARN)
if err != nil {
log.Panic(err)
}
log.Printf("targetVpc vpc is %s\n", targetVpc)
isOtherVpc := currentVpc != targetVpc
tg := TargetGroup{
ARN: targetARN,
connection: elbSvc,
Port: *port,
}
tg.UpdateKnown()
watcher := Watcher{Service: *service, port: *port, tg: &tg, isOtherVpc: isOtherVpc}
// Setup k8s
client, ns := NewK8sClient()
watchOptions := meta.ListOptions{}
for {
watch, err := client.CoreV1().Endpoints(ns).Watch(watchOptions)
if err != nil {
log.Fatal("Opening watch failed,", err)
}
watchChan := watch.ResultChan()
watcher.Watch(watchChan)
}
}
func getTargetGroupVpcID(elbSvc *elbv2.ELBV2, targetARN *string) (string, error) {
tgs := []*string{targetARN}
input := elbv2.DescribeTargetGroupsInput{TargetGroupArns: tgs}
out, err := elbSvc.DescribeTargetGroups(&input)
if err != nil {
return "", err
}
return *out.TargetGroups[0].VpcId, err
}
func getVpcIDFromEC2Metadata(metadata *ec2metadata.EC2Metadata) (string, error) {
mac, err := metadata.GetMetadata("mac")
if err != nil {
return "", err
}
vpcID, err := metadata.GetMetadata(fmt.Sprintf("network/interfaces/macs/%s/vpc-id", mac))
if err != nil {
return "", err
}
return vpcID, nil
}