This repository has been archived by the owner on Oct 21, 2019. It is now read-only.
/
app.go
165 lines (138 loc) · 4.61 KB
/
app.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
160
161
162
163
164
165
package app
import (
"context"
"flag"
"math"
"net"
"os"
"time"
"github.com/ash2k/iam4kube/pkg/ip2service"
"github.com/ash2k/iam4kube/pkg/util/logz"
"github.com/ash2k/stager"
"github.com/coreos/go-iptables/iptables"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
core_v1inf "k8s.io/client-go/informers/core/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
)
const (
defaultResyncPeriod = 20 * time.Minute
ipTablesChainPrefix = "IP2SVC"
)
type PrometheusRegistry interface {
prometheus.Registerer
prometheus.Gatherer
}
type App struct {
Logger *zap.Logger
RestConfig *rest.Config
PrometheusRegistry PrometheusRegistry
ServiceNamespace string
ServiceName string
ServiceTargetPortName string
InterceptIP string
InterceptPort int32
ResyncPeriod time.Duration
AuxListenOn string
Debug bool
}
func (a *App) Run(ctx context.Context) error {
defer a.Logger.Sync()
ipt, err := iptables.New()
if err != nil {
return err
}
// Clients
mainClient, err := kubernetes.NewForConfig(a.RestConfig)
if err != nil {
return err
}
// Informers
endpointsInf := core_v1inf.NewEndpointsInformer(mainClient, a.ServiceNamespace, a.ResyncPeriod, cache.Indexers{})
endpointsInf.AddEventHandler(&ip2service.EndpointsEventHandler{
Logger: a.Logger,
ServiceName: a.ServiceName,
ServiceTargetPortName: a.ServiceTargetPortName,
Router: &ip2service.Router{
Logger: a.Logger,
Prefix: ipTablesChainPrefix,
InterceptIP: a.InterceptIP,
InterceptPort: a.InterceptPort,
IPTables: ipt,
},
})
// Auxiliary server
auxSrv := &AuxServer{
Logger: a.Logger,
Addr: a.AuxListenOn,
Gatherer: a.PrometheusRegistry,
Debug: a.Debug,
}
// ==== Lets start it all ====
// Stager will perform ordered, graceful shutdown. Stage by stage in reverse startup order.
stgr := stager.New()
defer stgr.Shutdown()
stage := stgr.NextStage()
stage.StartWithChannel(endpointsInf.Run)
return auxSrv.Run(ctx)
}
func NewFromFlags(flagset *flag.FlagSet, arguments []string) (*App, error) {
a := App{}
flagset.DurationVar(&a.ResyncPeriod, "resync-period", defaultResyncPeriod, "Resync period for informers.")
flagset.BoolVar(&a.Debug, "debug", false, "Enables pprof endpoint.")
flagset.StringVar(&a.AuxListenOn, "aux-listen-on", ":9090", "Auxiliary address to listen on. Used for Prometheus metrics server and pprof endpoints.")
flagset.StringVar(&a.ServiceNamespace, "service-namespace", meta_v1.NamespaceDefault, "Namespace of the Service object to route traffic to.")
flagset.StringVar(&a.ServiceName, "service-name", "iam4kube", "Name of the Service object to route traffic to.")
flagset.StringVar(&a.ServiceTargetPortName, "service-target-port", "http", "Target port name of the Service object to route traffic to.")
flagset.StringVar(&a.InterceptIP, "intercept-ip", "169.254.169.254", "IP address to intercept traffic for.")
interceptPort := flagset.Int("intercept-port", 80, "Port on the IP to intercept traffic for.")
logEncoding := flagset.String("log-encoding", "json", `Sets the logger's encoding. Valid values are "json" and "console".`)
loggingLevel := flagset.String("log-level", "info", `Sets the logger's output level.`)
err := flagset.Parse(arguments)
if err != nil {
return nil, err
}
if a.ServiceNamespace == "" {
return nil, errors.New("Service namespace must be specified")
}
if a.ServiceName == "" {
return nil, errors.New("Service name must be specified")
}
if a.ServiceTargetPortName == "" {
return nil, errors.New("Service target port name must be specified")
}
if !isValidIP(a.InterceptIP) {
return nil, errors.New("Intercept ip is invalid")
}
if !isValidPort(*interceptPort) {
return nil, errors.New("Intercept port is invalid")
}
a.InterceptPort = int32(*interceptPort)
a.RestConfig, err = rest.InClusterConfig()
if err != nil {
return nil, errors.WithStack(err)
}
a.RestConfig.UserAgent = "ip2service"
a.Logger = logz.Logger(*loggingLevel, *logEncoding, os.Stderr)
// Metrics
a.PrometheusRegistry = prometheus.NewPedanticRegistry()
err = a.PrometheusRegistry.Register(prometheus.NewProcessCollector(os.Getpid(), ""))
if err != nil {
return nil, err
}
err = a.PrometheusRegistry.Register(prometheus.NewGoCollector())
if err != nil {
return nil, err
}
return &a, nil
}
func isValidPort(port int) bool {
return port > 0 && port <= math.MaxUint16
}
func isValidIP(ip string) bool {
return net.ParseIP(ip) != nil
}