-
Notifications
You must be signed in to change notification settings - Fork 20
/
main.go
159 lines (137 loc) · 4.37 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"flag"
"fmt"
"os"
"strings"
log "github.com/sirupsen/logrus"
haproxy "github.com/haproxytech/haproxy-consul-connect/haproxy"
"github.com/haproxytech/haproxy-consul-connect/haproxy/haproxy_cmd"
"github.com/haproxytech/haproxy-consul-connect/lib"
"github.com/hashicorp/consul/api"
"github.com/haproxytech/haproxy-consul-connect/consul"
)
// Version is set by Travis build
var Version string = "v0.1.9-Dev"
// BuildTime is set by Travis
var BuildTime string = "2020-01-01T00:00:00Z"
// GitHash The last reference Hash from Git
var GitHash string = "unknown"
type consulLogger struct{}
// Debugf Display debug message
func (consulLogger) Debugf(format string, args ...interface{}) {
log.Debugf(format, args...)
}
// Infof Display info message
func (consulLogger) Infof(format string, args ...interface{}) {
log.Infof(format, args...)
}
// Warnf Display warning message
func (consulLogger) Warnf(format string, args ...interface{}) {
log.Infof(format, args...)
}
// Errorf Display error message
func (consulLogger) Errorf(format string, args ...interface{}) {
log.Errorf(format, args...)
}
// validateRequirements Checks that dependencies are present
func validateRequirements(dataplaneBin, haproxyBin string) error {
err := haproxy_cmd.CheckEnvironment(dataplaneBin, haproxyBin)
if err != nil {
msg := fmt.Sprintf("Some external dependencies are missing: %s", err.Error())
os.Stderr.WriteString(fmt.Sprintf("%s\n", msg))
return err
}
return nil
}
func main() {
versionFlag := flag.Bool("version", false, "Show version and exit")
logLevel := flag.String("log-level", "INFO", "Log level")
consulAddr := flag.String("http-addr", "127.0.0.1:8500", "Consul agent address")
service := flag.String("sidecar-for", "", "The consul service id to proxy")
serviceTag := flag.String("sidecar-for-tag", "", "The consul service id to proxy")
haproxyBin := flag.String("haproxy", haproxy_cmd.DefaultHAProxyBin, "Haproxy binary path")
dataplaneBin := flag.String("dataplane", haproxy_cmd.DefaultDataplaneBin, "Dataplane binary path")
haproxyCfgBasePath := flag.String("haproxy-cfg-base-path", "/tmp", "Haproxy binary path")
statsListenAddr := flag.String("stats-addr", "", "Listen addr for stats server")
statsServiceRegister := flag.Bool("stats-service-register", false, "Register a consul service for connect stats")
enableIntentions := flag.Bool("enable-intentions", false, "Enable Connect intentions")
token := flag.String("token", "", "Consul ACL token")
flag.Parse()
if versionFlag != nil && *versionFlag {
fmt.Printf("Version: %s ; BuildTime: %s ; GitHash: %s\n", Version, BuildTime, GitHash)
status := 0
if err := validateRequirements(*dataplaneBin, *haproxyBin); err != nil {
fmt.Printf("ERROR: dataplane API / HAProxy dependencies are not satisfied: %s\n", err)
status = 4
}
os.Exit(status)
}
ll, err := log.ParseLevel(*logLevel)
if err != nil {
log.Fatal(err)
}
log.SetLevel(ll)
sd := lib.NewShutdown()
consulConfig := &api.Config{
Address: *consulAddr,
}
if token != nil {
consulConfig.Token = *token
}
consulClient, err := api.NewClient(consulConfig)
if err != nil {
}
var serviceID string
if *serviceTag != "" {
svcs, err := consulClient.Agent().Services()
if err != nil {
log.Fatal(err)
}
OUTER:
for _, s := range svcs {
if strings.HasSuffix(s.Service, "sidecar-proxy") {
continue
}
for _, t := range s.Tags {
if t == *serviceTag {
serviceID = s.ID
break OUTER
}
}
}
if serviceID == "" {
log.Fatalf("No sidecar proxy found for service with tag %s", *serviceTag)
}
} else if *service != "" {
serviceID = *service
} else {
log.Fatalf("Please specify -sidecar-for or -sidecar-for-tag")
}
consulLogger := &consulLogger{}
watcher := consul.New(serviceID, consulClient, consulLogger)
go func() {
if err := watcher.Run(); err != nil {
log.Error(err)
sd.Shutdown(err.Error())
}
}()
hap := haproxy.New(consulClient, watcher.C, haproxy.Options{
HAProxyBin: *haproxyBin,
DataplaneBin: *dataplaneBin,
ConfigBaseDir: *haproxyCfgBasePath,
EnableIntentions: *enableIntentions,
StatsListenAddr: *statsListenAddr,
StatsRegisterService: *statsServiceRegister,
LogRequests: ll == log.TraceLevel,
})
sd.Add(1)
go func() {
defer sd.Done()
if err := hap.Run(sd); err != nil {
log.Error(err)
sd.Shutdown(err.Error())
}
}()
sd.Wait()
}