Skip to content

Commit

Permalink
Merge pull request #232 from Nordix/ipam
Browse files Browse the repository at this point in the history
Release IP on connection close
  • Loading branch information
LionelJouin authored Jun 10, 2022
2 parents 9178e20 + cd6121e commit 5add698
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 3 deletions.
30 changes: 29 additions & 1 deletion pkg/ipam/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,35 @@ func (is *IpamServer) Allocate(ctx context.Context, child *ipamAPI.Child) (*ipam
}

func (is *IpamServer) Release(ctx context.Context, child *ipamAPI.Child) (*emptypb.Empty, error) {
return &emptypb.Empty{}, nil
logrus.Infof("Release: %v", child)
trench, exists := is.Trenches[child.GetSubnet().GetIpFamily()]
if !exists {
return &emptypb.Empty{}, nil
}
if child.GetSubnet().GetConduit() == nil {
return &emptypb.Empty{}, nil
}
if child.GetSubnet().GetConduit().GetTrench() == nil {
return &emptypb.Empty{}, nil
}
if trench.GetName() != getTrenchName(child.GetSubnet().GetConduit().GetTrench().GetName(), child.GetSubnet().GetIpFamily()) {
return &emptypb.Empty{}, nil
}
conduit, err := trench.GetConduit(ctx, child.GetSubnet().GetConduit().GetName())
if err != nil {
return &emptypb.Empty{}, err
}
if conduit == nil {
return &emptypb.Empty{}, nil
}
node, err := conduit.GetNode(ctx, child.GetSubnet().GetNode())
if err != nil {
return &emptypb.Empty{}, err
}
if node == nil {
return &emptypb.Empty{}, nil
}
return &emptypb.Empty{}, node.Release(ctx, child.GetName())
}

func getTrenchName(trenchName string, ipFamily ipamAPI.IPFamily) string {
Expand Down
5 changes: 4 additions & 1 deletion pkg/nsm/ipcontext/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ func (icc *ipcontextClient) Request(ctx context.Context, request *networkservice

// Close
func (icc *ipcontextClient) Close(ctx context.Context, conn *networkservice.Connection, opts ...grpc.CallOption) (*empty.Empty, error) {
// TODO: free IPs
err := icc.ics.UnsetIPContext(conn, networking.NSC)
if err != nil {
return nil, err
}
return next.Client(ctx).Close(ctx, conn, opts...)
}
1 change: 1 addition & 0 deletions pkg/nsm/ipcontext/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ import (

type ipContextSetter interface {
SetIPContext(conn *networkservice.Connection, interfaceType networking.InterfaceType) error
UnsetIPContext(conn *networkservice.Connection, interfaceType networking.InterfaceType) error
}
5 changes: 4 additions & 1 deletion pkg/nsm/ipcontext/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ func (ics *ipcontextServer) Request(ctx context.Context, request *networkservice

// Close
func (ics *ipcontextServer) Close(ctx context.Context, conn *networkservice.Connection) (*empty.Empty, error) {
// TODO: free IPs
err := ics.ics.UnsetIPContext(conn, networking.NSE)
if err != nil {
return nil, err
}
return next.Server(ctx).Close(ctx, conn)
}
14 changes: 14 additions & 0 deletions pkg/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,20 @@ func (p *Proxy) SetIPContext(conn *networkservice.Connection, interfaceType netw
return nil
}

func (p *Proxy) UnsetIPContext(conn *networkservice.Connection, interfaceType networking.InterfaceType) error {
for _, subnet := range p.Subnets {
child := &ipamAPI.Child{
Name: fmt.Sprintf("%s-src", conn.Id),
Subnet: subnet,
}
_, err := p.ipamClient.Release(context.TODO(), child)
if err != nil {
return err
}
}
return nil
}

func (p *Proxy) setBridgeIP(prefix string) error {
err := p.bridge.AddLocalPrefix(prefix)
if err != nil {
Expand Down

0 comments on commit 5add698

Please sign in to comment.