This repository has been archived by the owner on Mar 30, 2023. It is now read-only.
/
main.go
215 lines (187 loc) · 5.16 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package main
import (
"context"
"errors"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/bufbuild/connect-go"
grpchealth "github.com/bufbuild/connect-grpchealth-go"
grpcreflect "github.com/bufbuild/connect-grpcreflect-go"
// compress "github.com/klauspost/connect-compress"
"github.com/urfave/cli/v2"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
goipam "github.com/cicdteam/go-ipam"
goipamapiv1 "github.com/cicdteam/go-ipam/api/v1"
"github.com/cicdteam/go-ipam/api/v1/apiv1connect"
"github.com/cicdteam/go-ipam/pkg/service"
)
func main() {
app := &cli.App{
Name: "vxlan ipam server",
Usage: "grpc server for vxlan ipam",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "grpc-server-endpoint",
Value: ":9090",
Usage: "gRPC server endpoint",
EnvVars: []string{"GOIPAM_GRPC_SERVER_ENDPOINT"},
},
&cli.StringFlag{
Name: "cidr",
Value: "10.10.10.0/24",
Usage: "CIDR for IP address management",
EnvVars: []string{"GOIPAM_CIDR"},
},
&cli.StringFlag{
Name: "log-level",
Value: "info",
Usage: "log-level can be one of error|warn|info|debug",
EnvVars: []string{"GOIPAM_LOG_LEVEL"},
},
&cli.StringFlag{
Name: "configmap",
Value: "vxlan-ipam",
Usage: "k8s ConfigMap name where ipam data stored",
EnvVars: []string{"GOIPAM_CONFIGMAP_NAME"},
},
&cli.BoolFlag{
Name: "cache",
Usage: "cache queries to k8s ConfigMap",
Value: true,
EnvVars: []string{"GOIPAM_CONFIGMAP_CACHE"},
},
},
Action: func(ctx *cli.Context) error {
c := getConfig(ctx)
configmap := ctx.String("configmap")
cache := ctx.Bool("cache")
c.Storage = goipam.NewConfigmap(configmap, cache)
s := newServer(c)
return s.Run()
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatalf("Error in cli: %v", err)
}
}
type config struct {
GrpcServerEndpoint string
Log *zap.SugaredLogger
Storage goipam.Storage
ConfigMapName string
ConfigMapCache bool
Cidr string
}
func getConfig(ctx *cli.Context) config {
cfg := zap.NewProductionConfig()
level, err := zap.ParseAtomicLevel(ctx.String("log-level"))
if err != nil {
log.Fatal(err)
}
cfg.Level = level
cfg.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
zlog, err := cfg.Build()
if err != nil {
log.Fatal(err)
}
return config{
GrpcServerEndpoint: ctx.String("grpc-server-endpoint"),
Cidr: ctx.String("cidr"),
Log: zlog.Sugar(),
ConfigMapName: ctx.String("configmap"),
ConfigMapCache: ctx.Bool("cache"),
}
}
type server struct {
c config
ipamer goipam.Ipamer
storage goipam.Storage
log *zap.SugaredLogger
}
func newServer(c config) *server {
return &server{
c: c,
ipamer: goipam.NewWithStorage(c.Storage),
storage: c.Storage,
log: c.Log,
}
}
func (s *server) Run() error {
s.log.Infow("starting vxlan ipam server", "cidr", s.c.Cidr, "backend", s.storage.Name(), "configmap name", s.c.ConfigMapName, "configmap cache", s.c.ConfigMapCache, "endpoint", s.c.GrpcServerEndpoint)
mux := http.NewServeMux()
compress1KB := connect.WithCompressMinBytes(1024)
// The generated constructors return a path and a plain net/http
// handler.
mux.Handle(apiv1connect.NewIpamServiceHandler(
service.New(s.log, s.ipamer),
compress1KB,
))
mux.Handle(grpchealth.NewHandler(
grpchealth.NewStaticChecker(apiv1connect.IpamServiceName),
compress1KB,
))
mux.Handle(grpcreflect.NewHandlerV1(
grpcreflect.NewStaticReflector(apiv1connect.IpamServiceName),
compress1KB,
))
server := http.Server{
Addr: s.c.GrpcServerEndpoint,
Handler: h2c.NewHandler(
mux,
&http2.Server{},
),
ReadHeaderTimeout: 30 * time.Second,
ReadTimeout: 1 * time.Minute,
WriteTimeout: 1 * time.Minute,
MaxHeaderBytes: 8 * 1024, // 8KiB
}
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt, syscall.SIGTERM)
go func() error {
if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Printf("HTTP listen and serve: %v", err)
return err
}
return nil
}()
s.log.Infow("creating prefix", "cidr", s.c.Cidr)
c := apiv1connect.NewIpamServiceClient(
http.DefaultClient,
"http://"+s.c.GrpcServerEndpoint,
connect.WithGRPC(),
)
prefixes, err := c.ListPrefixes(context.Background(), connect.NewRequest(&goipamapiv1.ListPrefixesRequest{}))
if err != nil {
return err
}
prefixAlreadyCreated := false
for _, p := range prefixes.Msg.Prefixes {
if p.Cidr == s.c.Cidr {
prefixAlreadyCreated = true
s.log.Infow("prefix already present, skip prefix creation", "cidr", p.Cidr)
}
}
if !prefixAlreadyCreated {
result, err := c.CreatePrefix(context.Background(), connect.NewRequest(&goipamapiv1.CreatePrefixRequest{Cidr: s.c.Cidr}))
if err != nil {
return err
}
s.log.Infow("prefix created", "cidr", result.Msg.Prefix.Cidr)
}
<-signals
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
log.Printf("HTTP shutdown: %v", err) //nolint:gocritic
return err
}
return nil
}