generated from networkservicemesh/cmd-template
-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.go
352 lines (307 loc) · 10.5 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// Copyright (c) 2021-2022 Doc.ai and/or its affiliates.
//
// Copyright (c) 2023 Cisco and/or its affiliates.
//
// Copyright (c) 2024 OpenInfra Foundation Europe. All rights reserved.
//
// SPDX-License-Identifier: Apache-2.0
//
// 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"
"net"
"os"
"os/signal"
"syscall"
"time"
nested "github.com/antonfisher/nested-logrus-formatter"
"github.com/kelseyhightower/envconfig"
"gopkg.in/yaml.v2"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"github.com/networkservicemesh/cmd-map-ip-k8s/internal/mapipwriter"
"github.com/networkservicemesh/sdk/pkg/tools/log"
"github.com/networkservicemesh/sdk/pkg/tools/log/logruslogger"
"github.com/networkservicemesh/sdk/pkg/tools/opentelemetry"
"github.com/networkservicemesh/sdk/pkg/tools/pprofutils"
)
// Config represents the configuration for cmd-map-ip-k8s application
type Config struct {
OutputPath string `default:"external_ips.yaml" desc:"Path to writing map of internal to extenrnal ips" split_words:"true"`
NodeName string `default:"" desc:"The name of node where application is running" split_words:"true"`
LogLevel string `default:"INFO" desc:"Log level" split_words:"true"`
Namespace string `default:"default" desc:"Namespace where is mapip running" split_words:"true"`
FromConfigMap string `default:"" desc:"If it's not empty then gets entries from the configmap" split_words:"true"`
OpenTelemetryEndpoint string `default:"otel-collector.observability.svc.cluster.local:4317" desc:"OpenTelemetry Collector Endpoint" split_words:"true"`
MetricsExportInterval time.Duration `default:"10s" desc:"interval between mertics exports" split_words:"true"`
PprofEnabled bool `default:"false" desc:"is pprof enabled" split_words:"true"`
PprofListenOn string `default:"localhost:6060" desc:"pprof URL to ListenAndServe" split_words:"true"`
}
func main() {
// ********************************************************************************
// Configure signal handling context
// ********************************************************************************
ctx, cancel := signal.NotifyContext(
context.Background(),
os.Interrupt,
// More Linux signals here
syscall.SIGHUP,
syscall.SIGTERM,
syscall.SIGQUIT,
)
defer cancel()
// ********************************************************************************
// Setup logger
// ********************************************************************************
log.EnableTracing(true)
logrus.Info("Starting NetworkServiceMesh Client ...")
logrus.SetFormatter(&nested.Formatter{})
ctx = log.WithLog(ctx, logruslogger.New(ctx, map[string]interface{}{"cmd": os.Args[:1]}))
logger := log.FromContext(ctx)
// ********************************************************************************
// Get config from environment
// ********************************************************************************
conf := &Config{}
if err := envconfig.Usage("nsm", conf); err != nil {
logger.Fatal(err)
}
if err := envconfig.Process("nsm", conf); err != nil {
logger.Fatalf("error processing rootConf from env: %+v", err)
}
level, err := logrus.ParseLevel(conf.LogLevel)
if err != nil {
logrus.Fatalf("invalid log level %s", conf.LogLevel)
}
logrus.SetLevel(level)
logruslogger.SetupLevelChangeOnSignal(ctx, map[os.Signal]logrus.Level{
syscall.SIGUSR1: logrus.TraceLevel,
syscall.SIGUSR2: level,
})
// ********************************************************************************
// Configure Open Telemetry
// ********************************************************************************
if opentelemetry.IsEnabled() {
collectorAddress := conf.OpenTelemetryEndpoint
spanExporter := opentelemetry.InitSpanExporter(ctx, collectorAddress)
metricExporter := opentelemetry.InitOPTLMetricExporter(ctx, collectorAddress, conf.MetricsExportInterval)
o := opentelemetry.Init(ctx, spanExporter, metricExporter, "map-ip-k8s")
defer func() {
if err = o.Close(); err != nil {
logger.Error(err.Error())
}
}()
}
// ********************************************************************************
// Configure pprof
// ********************************************************************************
if conf.PprofEnabled {
go pprofutils.ListenAndServe(ctx, conf.PprofListenOn)
}
// ********************************************************************************
// Create client-go
// ********************************************************************************
kubeConfig, err := rest.InClusterConfig()
if err != nil {
logger.Fatalf("can't get Kubernetes config. Are you running this app inside Kubernetes pod")
}
c, err := kubernetes.NewForConfig(kubeConfig)
if err != nil {
logger.Fatal(err.Error())
}
<-Start(ctx, conf, c)
}
func getPublicIP(ctx context.Context) string {
addrs, err := net.InterfaceAddrs()
if err != nil {
log.FromContext(ctx).Errorf("InterfaceAddrs: %v", err.Error())
return ""
}
for _, a := range addrs {
if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ip := ipnet.IP.To4(); ip != nil {
return ip.String()
}
if ip := ipnet.IP.To16(); ip != nil {
return ip.String()
}
}
}
log.FromContext(ctx).Warn("not found public ip")
return ""
}
// Start starts main application
func Start(ctx context.Context, conf *Config, c kubernetes.Interface) <-chan struct{} {
logger := log.FromContext(ctx)
var mapWriter = mapipwriter.MapIPWriter{
OutputPath: conf.OutputPath,
}
list, err := c.CoreV1().Nodes().List(ctx, v1.ListOptions{})
if err != nil {
logger.Fatal(err.Error())
}
var eventsCh = make(chan mapipwriter.Event, 64)
if conf.FromConfigMap != "" {
cm, err := c.CoreV1().ConfigMaps(conf.Namespace).Get(ctx, conf.FromConfigMap, v1.GetOptions{})
if err == nil {
for _, event := range translateFromConfigmap(watch.Event{
Type: watch.Added,
Object: cm,
}) {
eventsCh <- event
}
}
}
for i := 0; i < len(list.Items); i++ {
for _, event := range translationFromNode(watch.Event{
Type: watch.Added,
Object: &list.Items[i],
}) {
eventsCh <- event
}
}
go mapWriter.Start(ctx, eventsCh)
go monitorEvents(ctx, eventsCh, func() watch.Interface {
r, _ := c.CoreV1().Nodes().Watch(ctx, v1.ListOptions{})
return r
}, func(e watch.Event) []mapipwriter.Event {
var result = translationFromNode(e)
var podEvent = translationFromPodToNode(ctx, e, conf.NodeName)
if podEvent != nil {
result = append(result, *podEvent)
}
return result
})
if conf.FromConfigMap != "" {
go monitorEvents(ctx, eventsCh, func() watch.Interface {
r, _ := c.CoreV1().ConfigMaps(conf.FromConfigMap).Watch(ctx, v1.ListOptions{FieldSelector: "meta.name=" + conf.FromConfigMap})
return r
}, translateFromConfigmap)
}
return ctx.Done()
}
func monitorEvents(ctx context.Context, out chan<- mapipwriter.Event, getWatchFn func() watch.Interface, translateFn func(watch.Event) []mapipwriter.Event) {
w := getWatchFn()
defer func() {
if w != nil {
w.Stop()
}
}()
for ctx.Err() == nil {
if w == nil {
log.FromContext(ctx).Errorf("cant supply watcher")
time.Sleep(time.Second / 2)
w = getWatchFn()
continue
}
select {
case e, ok := <-w.ResultChan():
if !ok {
w.Stop()
w = getWatchFn()
continue
}
events := translateFn(e)
for _, event := range events {
out <- event
}
case <-ctx.Done():
return
}
}
}
func translateFromConfigmap(e watch.Event) []mapipwriter.Event {
var res []mapipwriter.Event
var c = e.Object.(*corev1.ConfigMap)
for _, v := range c.Data {
var m map[string]string
if err := yaml.Unmarshal([]byte(v), &m); err == nil {
for from, to := range m {
res = append(res, mapipwriter.Event{
Type: e.Type,
Translation: mapipwriter.Translation{
From: from,
To: to,
},
})
}
}
}
return res
}
func translationFromPodToNode(ctx context.Context, e watch.Event, currentNodeName string) *mapipwriter.Event {
var node = e.Object.(*corev1.Node)
if node.Name != currentNodeName || e.Type == watch.Deleted {
return nil
}
var result = &mapipwriter.Event{
Type: watch.Added,
Translation: mapipwriter.Translation{
From: getPublicIP(ctx),
},
}
for i := 0; i < len(node.Status.Addresses); i++ {
if node.Status.Addresses[i].Type == corev1.NodeInternalIP {
result.To = node.Status.Addresses[i].Address
}
}
for i := 0; i < len(node.Status.Addresses); i++ {
if node.Status.Addresses[i].Type == corev1.NodeExternalIP {
result.To = node.Status.Addresses[i].Address
}
}
return result
}
func translationFromNode(e watch.Event) []mapipwriter.Event {
var result []mapipwriter.Event
var node = e.Object.(*corev1.Node)
// map internal ip on itself, in case we don't have an external IP
for i := 0; i < len(node.Status.Addresses); i++ {
if node.Status.Addresses[i].Type == corev1.NodeInternalIP {
result = append(result, mapipwriter.Event{
Type: e.Type,
Translation: mapipwriter.Translation{
From: node.Status.Addresses[i].Address,
To: node.Status.Addresses[i].Address,
},
})
result[0].To = node.Status.Addresses[i].Address
}
}
// if we have external IPs, instead map internal IP to external
for i := 0; i < len(node.Status.Addresses); i++ {
if node.Status.Addresses[i].Type == corev1.NodeExternalIP {
for j := 0; j < len(result); j++ {
result[j].To = node.Status.Addresses[i].Address
}
break
}
}
// map external IP to itself, in case we want to send data from external IP
for i := 0; i < len(node.Status.Addresses); i++ {
if node.Status.Addresses[i].Type == corev1.NodeExternalIP {
result = append(result, mapipwriter.Event{
Type: e.Type,
Translation: mapipwriter.Translation{
From: node.Status.Addresses[i].Address,
To: node.Status.Addresses[i].Address,
},
})
}
}
return result
}