-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: updated IP controller for new ipam
- Loading branch information
Showing
25 changed files
with
435 additions
and
227 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright 2019-2024 The Liqo Authors | ||
// | ||
// 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 utils contain utility functions for the IPAM package. | ||
package utils |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// Copyright 2019-2024 The Liqo Authors | ||
// | ||
// 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 utils | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"net/netip" | ||
|
||
"go4.org/netipx" | ||
utilruntime "k8s.io/apimachinery/pkg/util/runtime" | ||
|
||
"github.com/liqotech/liqo/pkg/consts" | ||
) | ||
|
||
// MapIPToNetwork creates a new IP address obtained by means of the old IP address and the new network. | ||
func MapIPToNetwork(newNetwork, oldIP string) (newIP string, err error) { | ||
if newNetwork == consts.DefaultCIDRValue { | ||
return oldIP, nil | ||
} | ||
// Parse newNetwork | ||
ip, network, err := net.ParseCIDR(newNetwork) | ||
if err != nil { | ||
return "", err | ||
} | ||
// Get mask | ||
mask := network.Mask | ||
// Get slice of bytes for newNetwork | ||
// Type net.IP has underlying type []byte | ||
parsedNewIP := ip.To4() | ||
// Get oldIP as slice of bytes | ||
parsedOldIP := net.ParseIP(oldIP) | ||
if parsedOldIP == nil { | ||
return "", fmt.Errorf("cannot parse oldIP") | ||
} | ||
parsedOldIP = parsedOldIP.To4() | ||
// Substitute the last 32-mask bits of newNetwork with bits taken by the old ip | ||
for i := 0; i < len(mask); i++ { | ||
// Step 1: NOT(mask[i]) = mask[i] ^ 0xff. They are the 'host' bits | ||
// Step 2: BITWISE AND between the host bits and parsedOldIP[i] zeroes the network bits in parsedOldIP[i] | ||
// Step 3: BITWISE OR copies the result of step 2 in newIP[i] | ||
parsedNewIP[i] |= (mask[i] ^ 0xff) & parsedOldIP[i] | ||
} | ||
newIP = parsedNewIP.String() | ||
return | ||
} | ||
|
||
// GetMask retrieves the mask from a CIDR. | ||
func GetMask(network string) uint8 { | ||
_, subnet, err := net.ParseCIDR(network) | ||
utilruntime.Must(err) | ||
ones, _ := subnet.Mask.Size() | ||
return uint8(ones) | ||
} | ||
|
||
// SetMask forges a new cidr from a network cidr and a mask. | ||
func SetMask(network string, mask uint8) string { | ||
_, n, err := net.ParseCIDR(network) | ||
utilruntime.Must(err) | ||
newMask := net.CIDRMask(int(mask), 32) | ||
n.Mask = newMask | ||
return n.String() | ||
} | ||
|
||
// Next used to get the second half of a given network. | ||
func Next(network string) string { | ||
prefix, err := netip.ParsePrefix(network) | ||
utilruntime.Must(err) | ||
// Step 1: Get last IP address of network | ||
// Step 2: Get next IP address | ||
firstIP := netipx.RangeOfPrefix(prefix).To().Next() | ||
prefix = netip.PrefixFrom(firstIP, prefix.Bits()) | ||
return prefix.String() | ||
} | ||
|
||
// IsValidCIDR returns an error if the received CIDR is invalid. | ||
func IsValidCIDR(cidr string) error { | ||
_, _, err := net.ParseCIDR(cidr) | ||
return err | ||
} | ||
|
||
// SplitNetwork returns the two halves that make up a given network. | ||
func SplitNetwork(network string) []string { | ||
halves := make([]string, 2) | ||
|
||
// Get halves mask length. | ||
mask := GetMask(network) | ||
mask++ | ||
|
||
// Get first half CIDR. | ||
halves[0] = SetMask(network, mask) | ||
|
||
// Get second half CIDR. | ||
halves[1] = Next(halves[0]) | ||
|
||
return halves | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright 2019-2024 The Liqo Authors | ||
// | ||
// 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 utils_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestUtils(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Utils Suite") | ||
} |
Oops, something went wrong.