forked from cisco-app-networking/nsm-nse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_ipam.go
157 lines (141 loc) · 4.21 KB
/
config_ipam.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
package config
import (
"bytes"
"context"
"fmt"
"os"
"strconv"
"time"
"cisco-app-networking.github.io/nsm-nse/api/ipam/ipprovider"
"cisco-app-networking.github.io/nsm-nse/pkg/nseconfig"
"cisco-app-networking.github.io/networkservicemesh/pkg/tools"
"github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
type IpamService interface {
AllocateSubnet(ucnfEndpoint *nseconfig.Endpoint) (string, error)
}
type IpamServiceImpl struct {
IpamAllocator ipprovider.AllocatorClient
RegisteredSubnets chan *ipprovider.Subnet
}
func (i *IpamServiceImpl) AllocateSubnet(ucnfEndpoint *nseconfig.Endpoint) (string, error) {
var subnet *ipprovider.Subnet
for j := 0; j < 6; j++ {
var err error
subnet, err = i.IpamAllocator.AllocateSubnet(context.Background(), &ipprovider.SubnetRequest{
Identifier: &ipprovider.Identifier{
Fqdn: ucnfEndpoint.NseControl.Address,
Name: GetEndpointName(),
ConnectivityDomain: ucnfEndpoint.NseControl.ConnectivityDomain,
},
AddrFamily: &ipprovider.IpFamily{Family: ipprovider.IpFamily_IPV4},
PrefixLen: uint32(ucnfEndpoint.VL3.IPAM.PrefixLength),
})
if err != nil {
if j == 5 {
return "", fmt.Errorf("ipam allocation not successful: %v", err)
}
logrus.Errorf("ipam allocation not successful: %v \n waiting 60 seconds before retrying \n", err)
time.Sleep(60 * time.Second)
} else {
break
}
}
i.RegisteredSubnets <- subnet
return subnet.Prefix.Subnet, nil
}
func (i *IpamServiceImpl) Renew(ctx context.Context, errorHandler func(err error)) error {
g, ctx := errgroup.WithContext(ctx)
var subnets = make(map[string]*ipprovider.Subnet)
for {
select {
case subnet := <-i.RegisteredSubnets:
g.Go(func() error {
for range time.Tick(time.Duration(subnet.LeaseTimeout-1) * time.Hour) {
_, err := i.IpamAllocator.RenewSubnetLease(ctx, subnet)
if err != nil {
errorHandler(err)
}
subnets[subnet.Identifier.Name] = subnet
}
return nil
})
case <-ctx.Done():
logrus.Info("Cleaning registered subnets")
close(i.RegisteredSubnets)
err := i.Cleanup(subnets)
if err != nil {
errorHandler(err)
}
return g.Wait()
}
}
}
func (i *IpamServiceImpl) Cleanup(subnets map[string]*ipprovider.Subnet) error {
var errs errors
for _, v := range subnets {
_, err := i.IpamAllocator.FreeSubnet(context.Background(), v)
if err != nil {
errs = append(errs, err)
}
}
if len(errs) > 0 {
return errs
}
return nil
}
type errors []error
func (es errors) Error() string {
buff := bytes.NewBufferString("multiple errors: \n")
for _, e := range es {
_, _ = fmt.Fprintf(buff, "\t%s\n", e)
}
return buff.String()
}
func NewIpamService(ctx context.Context, addr string) (IpamService, error) {
insecure, err := strconv.ParseBool(os.Getenv(tools.InsecureEnv))
if err != nil {
logrus.Info("Missing INSECURE env variable. Continuing with insecure mode enabled.")
insecure = true
}
var opts []grpc.DialOption
// If we have a service provider and we want to run in secure mode
if !insecure && tools.GetConfig().SecurityProvider != nil {
if tlsConfig, err := tools.GetConfig().SecurityProvider.GetTLSConfig(ctx); err != nil {
logrus.Errorf(
"Failed getting tls config with error: %v. GRPC connection will be insecure.",
err,
)
opts = append(opts, grpc.WithInsecure())
} else {
logrus.Info("GRPC connection will be secured.")
opts = append(opts, grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig)))
}
} else {
logrus.Info("GRPC connection will be insecure.")
opts = append(opts, grpc.WithInsecure())
}
conn, err := grpc.Dial(addr, opts...)
if err != nil {
return &IpamServiceImpl{}, fmt.Errorf("unable to connect to ipam server: %v", err)
}
ipamAllocator := ipprovider.NewAllocatorClient(conn)
ipamService := IpamServiceImpl{
IpamAllocator: ipamAllocator,
RegisteredSubnets: make(chan *ipprovider.Subnet),
}
go func() {
logrus.Info("begin the ipam leased subnet renew process")
if err := ipamService.Renew(ctx, func(err error) {
if err != nil {
logrus.Error("unable to renew the subnet", err)
}
}); err != nil {
logrus.Error(err)
}
}()
return &ipamService, nil
}