-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
145 lines (130 loc) · 2.86 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
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
package main
import (
"context"
"flag"
"fmt"
"net"
"os"
"os/signal"
"strconv"
"syscall"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
const envNamePrefixEndpointOverride = "AWS_ENDPOINT_OVERRIDES_"
var rootCtx = context.Background()
var debugLevel int = 0
var connTimeout time.Duration
var cacheTtl time.Duration
func populateConfigFromEnvVars() {
{
v, err := strconv.Atoi(os.Getenv("CLOUDMAP_PROXY_DEBUG"))
if err == nil {
debugLevel = v
}
}
{
v, err := time.ParseDuration(os.Getenv("CLOUDMAP_PROXY_CONN_TIMEOUT"))
if err == nil {
connTimeout = v
}
}
{
v, err := time.ParseDuration(os.Getenv("CLOUDMAP_PROXY_CACHE_TTL"))
if err == nil {
cacheTtl = v
}
}
}
func parseListenAddr(addr string) (listenAddr net.TCPAddr, err error) {
if addr[0] == ':' {
listenAddr.IP = make(net.IP, 4)
listenAddr.Port, err = strconv.Atoi(addr[1:])
if err != nil {
err = fmt.Errorf("fail to parse listen address", err)
}
} else {
var _listenAddr *net.TCPAddr
_listenAddr, err = net.ResolveTCPAddr("tcp", addr)
if err != nil {
err = fmt.Errorf("fail to resolve listen address: %w", err)
}
listenAddr = *_listenAddr
}
return
}
func main() {
ctx, cancel := context.WithCancel(rootCtx)
defer cancel()
var debug bool
{
flag.BoolVar(&debug, "debug", false, "debug mode")
flag.DurationVar(&connTimeout, "conn-timeout", 10*time.Second, "connection timeout")
flag.DurationVar(&cacheTtl, "cache-ttl", 60*time.Second, "io timeout")
}
populateConfigFromEnvVars()
flag.Parse()
args := flag.Args()
if len(args) != 2 {
fmt.Fprintf(os.Stderr, "%s: too few arguments\n", os.Args[0])
flag.PrintDefaults()
os.Exit(255)
}
if debug {
debugLevel = 2
}
if debugLevel > 0 {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
} else {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
}
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
config, err := getAwsConfig()
if err != nil {
log.Error().Err(err).Msg("fail to fetch aws configuration")
os.Exit(1)
}
if len(args[1]) == 0 {
log.Error().Err(err).Msg("listen address must be specified")
os.Exit(1)
}
listenAddr, err := parseListenAddr(args[1])
if err != nil {
log.Error().Err(err).Msg(err.Error())
os.Exit(1)
}
s, err := NewServer(
ctx,
NewUplookerCache(
NewCloudMapServiceUplooker(config),
cacheTtl,
), listenAddr,
args[0],
connTimeout,
)
if err != nil {
log.Error().Err(err).Msg("fail to resolve listen address")
os.Exit(1)
}
{
sigCh := make(chan os.Signal)
go func() {
outer:
for {
select {
case <-ctx.Done():
break outer
case <-sigCh:
log.Info().Msg("shutdown initiated")
s.Close()
break outer
}
}
}()
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
}
log.Info().Msgf("cloudmap proxy started listening on %s", listenAddr.String())
s.WaitForTermination()
log.Info().Msg("cloudmap proxy stopped")
}