forked from uswitch/kiam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent.go
136 lines (116 loc) · 3.93 KB
/
agent.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
// Copyright 2017 uSwitch
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"os"
"os/signal"
"syscall"
log "github.com/sirupsen/logrus"
http "github.com/uswitch/kiam/pkg/aws/metadata"
kiamserver "github.com/uswitch/kiam/pkg/server"
)
type agentCommand struct {
logOptions
telemetryOptions
tlsOptions
clientOptions
*http.ServerOptions
iptables bool
iptablesRemove bool
hostIP string
hostInterface string
}
func (cmd *agentCommand) Bind(parser parser) {
cmd.logOptions.bind(parser)
cmd.telemetryOptions.bind(parser)
cmd.tlsOptions.bind(parser)
cmd.clientOptions.bind(parser)
cmd.ServerOptions = http.DefaultOptions()
parser.Flag("port", "HTTP port").Default("3100").IntVar(&cmd.ListenPort)
parser.Flag("allow-ip-query", "Allow client IP to be specified with ?ip. Development use only.").Default("false").BoolVar(&cmd.AllowIPQuery)
parser.Flag("allow-route-regexp", "Only routes matching this regular expression will be proxied").Default("^$").RegexpVar(&cmd.AllowRouteRegexp)
parser.Flag("iptables", "Add IPTables rules").Default("false").BoolVar(&cmd.iptables)
parser.Flag("iptables-remove", "Remove iptables rules at shutdown").Default("true").BoolVar(&cmd.iptablesRemove)
parser.Flag("host", "Host IP address.").Envar("HOST_IP").Required().StringVar(&cmd.hostIP)
parser.Flag("host-interface", "Network interface for pods to configure IPTables.").Default("docker0").StringVar(&cmd.hostInterface)
}
// run is the actual run implementation.
func (opts *agentCommand) run() error {
opts.configureLogger()
if opts.iptables {
log.Infof("configuring iptables")
rules := newIPTablesRules(opts.hostIP, opts.ListenPort, opts.hostInterface)
err := rules.Add()
if err != nil {
log.Errorf("error configuring iptables: %s", err.Error())
return err
}
if opts.iptablesRemove {
defer func() {
log.Infof("undoing iptables changes")
rules.Remove()
}()
}
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go opts.telemetryOptions.start(ctx, "agent")
stopChan := make(chan os.Signal, 8)
signal.Notify(stopChan, os.Interrupt, syscall.SIGTERM)
ctxGateway, cancelCtxGateway := context.WithTimeout(context.Background(), opts.timeoutKiamGateway)
defer cancelCtxGateway()
b := kiamserver.NewKiamGatewayBuilder().WithAddress(opts.serverAddress).WithKeepAlive(opts.keepaliveParams)
_, err := b.WithTLS(opts.certificatePath, opts.keyPath, opts.caPath)
if err != nil {
log.Errorf("error configuring TLS: ", err.Error())
return err
}
gateway, err := b.Build(ctxGateway)
if err != nil {
log.Errorf("error creating server gateway: %s", err.Error())
return err
}
defer gateway.Close()
server, err := http.NewWebServer(opts.ServerOptions, gateway)
if err != nil {
log.Errorf("error creating agent http server: %s", err.Error())
return err
}
errCh := make(chan error, 1)
go func() {
errCh <- server.Serve()
}()
select {
case err := <-errCh:
if err != nil {
log.Errorf("error running server: %s", err.Error())
return err
}
case sig := <-stopChan:
log.Infof("received signal (%s): starting server shutdown", sig.String())
if err := server.Stop(ctx); err != nil {
log.Errorf("error shutting down server: %s", err.Error())
return err
}
log.Infoln("gracefully shutdown server")
}
log.Infoln("stopped")
return nil
}
func (opts *agentCommand) Run() {
if err := opts.run(); err != nil {
log.Fatalf("fatal error: %s", err.Error())
}
}