forked from tiswanso/examples
/
ipam.go
108 lines (91 loc) · 2.86 KB
/
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
// Copyright 2018, 2019 VMware, Inc.
// 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"
"github.com/Nordix/simple-ipam/pkg/ipam"
"github.com/golang/protobuf/ptypes/empty"
"cisco-app-networking.github.io/networkservicemesh/controlplane/api/connection"
"cisco-app-networking.github.io/networkservicemesh/controlplane/api/networkservice"
"cisco-app-networking.github.io/networkservicemesh/sdk/common"
"cisco-app-networking.github.io/networkservicemesh/sdk/endpoint"
"github.com/sirupsen/logrus"
)
// IpamEndpoint a comment
type IpamEndpoint struct {
ipam *ipam.IPAM
SelfIP net.IP
}
// Request implements the request handler
func (ice *IpamEndpoint) Request(
ctx context.Context, request *networkservice.NetworkServiceRequest) (*connection.Connection, error) {
srcIP, err := ice.ipam.Allocate()
if err != nil {
return nil, err
}
newConnection := request.GetConnection()
// Update source/dst IP's
newConnection.Context.IpContext.SrcIpAddr = (&net.IPNet{
IP: srcIP,
Mask: ice.ipam.CIDR.Mask,
}).String()
newConnection.Context.IpContext.DstIpAddr = (&net.IPNet{
IP: ice.SelfIP,
Mask: ice.ipam.CIDR.Mask,
}).String()
err = newConnection.IsComplete()
if err != nil {
logrus.Errorf("New connection is not complete: %v", err)
return nil, err
}
logrus.Infof("ipam completed on connection: %v", newConnection)
if endpoint.Next(ctx) != nil {
return endpoint.Next(ctx).Request(ctx, request)
}
return newConnection, nil
}
// Close implements the close handler
func (ice *IpamEndpoint) Close(ctx context.Context, connection *connection.Connection) (*empty.Empty, error) {
addr, _, err := net.ParseCIDR(connection.Context.IpContext.SrcIpAddr)
if err == nil {
ice.ipam.Free(addr)
}
if endpoint.Next(ctx) != nil {
if _, err := endpoint.Next(ctx).Close(ctx, connection); err != nil {
return &empty.Empty{}, nil
}
}
return &empty.Empty{}, nil
}
// Name returns the composite name
func (ice *IpamEndpoint) Name() string {
return "IPAM"
}
// NewIpamEndpoint creates a IpamEndpoint
func NewIpamEndpoint(configuration *common.NSConfiguration) *IpamEndpoint {
ipam, err := ipam.New(configuration.IPAddress)
if err != nil {
panic(err.Error())
}
selfIP, err := ipam.Allocate()
if err != nil {
panic(err.Error())
}
return &IpamEndpoint{
ipam: ipam,
SelfIP: selfIP,
}
}