2017-02-12 13:03:39 +03:00
|
|
|
// Copyright 2017 Microsoft. All rights reserved.
|
|
|
|
// MIT License
|
2016-05-09 14:36:59 +03:00
|
|
|
|
|
|
|
package ipam
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2019-07-04 01:41:53 +03:00
|
|
|
"errors"
|
2016-05-09 14:36:59 +03:00
|
|
|
"net"
|
2021-11-18 01:31:42 +03:00
|
|
|
"os"
|
2019-07-04 01:41:53 +03:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
2016-12-06 22:19:35 +03:00
|
|
|
|
2020-02-07 01:59:56 +03:00
|
|
|
"github.com/Azure/azure-container-networking/common"
|
2023-10-28 07:38:36 +03:00
|
|
|
"go.uber.org/zap"
|
2016-05-09 14:36:59 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-07-04 01:41:53 +03:00
|
|
|
defaultLinuxFilePath = "/etc/kubernetes/interfaces.json"
|
|
|
|
defaultWindowsFilePath = `c:\k\interfaces.json`
|
|
|
|
windows = "windows"
|
2016-05-09 14:36:59 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Microsoft Azure Stack IPAM configuration source.
|
2020-02-07 01:59:56 +03:00
|
|
|
type fileIpamSource struct {
|
2019-07-04 01:41:53 +03:00
|
|
|
name string
|
|
|
|
sink addressConfigSink
|
|
|
|
fileLoaded bool
|
|
|
|
filePath string
|
2016-05-09 14:36:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// MAS host agent JSON object format.
|
2019-07-04 01:41:53 +03:00
|
|
|
type NetworkInterfaces struct {
|
|
|
|
Interfaces []Interface
|
|
|
|
}
|
|
|
|
|
|
|
|
type Interface struct {
|
|
|
|
MacAddress string
|
|
|
|
IsPrimary bool
|
|
|
|
IPSubnets []IPSubnet
|
|
|
|
}
|
|
|
|
|
|
|
|
type IPSubnet struct {
|
|
|
|
Prefix string
|
|
|
|
IPAddresses []IPAddress
|
|
|
|
}
|
|
|
|
|
|
|
|
type IPAddress struct {
|
|
|
|
Address string
|
|
|
|
IsPrimary bool
|
2016-05-09 14:36:59 +03:00
|
|
|
}
|
|
|
|
|
2020-02-07 01:59:56 +03:00
|
|
|
// Creates the MAS/fileIpam source.
|
|
|
|
func newFileIpamSource(options map[string]interface{}) (*fileIpamSource, error) {
|
2019-07-04 01:41:53 +03:00
|
|
|
var filePath string
|
2020-02-07 01:59:56 +03:00
|
|
|
var name string
|
|
|
|
|
2019-07-04 01:41:53 +03:00
|
|
|
if runtime.GOOS == windows {
|
|
|
|
filePath = defaultWindowsFilePath
|
|
|
|
} else {
|
|
|
|
filePath = defaultLinuxFilePath
|
2016-12-06 22:19:35 +03:00
|
|
|
}
|
|
|
|
|
2020-02-07 01:59:56 +03:00
|
|
|
name = options[common.OptEnvironment].(string)
|
|
|
|
return &fileIpamSource{
|
|
|
|
name: name,
|
2019-07-04 01:41:53 +03:00
|
|
|
filePath: filePath,
|
2016-05-09 14:36:59 +03:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Starts the MAS source.
|
2020-02-07 01:59:56 +03:00
|
|
|
func (source *fileIpamSource) start(sink addressConfigSink) error {
|
2019-07-04 01:41:53 +03:00
|
|
|
source.sink = sink
|
2016-05-09 14:36:59 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stops the MAS source.
|
2020-02-07 01:59:56 +03:00
|
|
|
func (source *fileIpamSource) stop() {
|
2019-07-04 01:41:53 +03:00
|
|
|
source.sink = nil
|
2016-05-09 14:36:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Refreshes configuration.
|
2020-02-07 01:59:56 +03:00
|
|
|
func (source *fileIpamSource) refresh() error {
|
2019-07-04 01:41:53 +03:00
|
|
|
if source == nil {
|
2020-02-07 01:59:56 +03:00
|
|
|
return errors.New("fileIpamSource is nil")
|
2019-07-04 01:41:53 +03:00
|
|
|
}
|
2016-05-09 14:36:59 +03:00
|
|
|
|
2019-07-04 01:41:53 +03:00
|
|
|
if source.fileLoaded {
|
2016-05-09 14:36:59 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-04 01:41:53 +03:00
|
|
|
// Query the list of local interfaces.
|
|
|
|
localInterfaces, err := net.Interfaces()
|
2016-05-09 14:36:59 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-07-04 01:41:53 +03:00
|
|
|
// Query the list of Azure Network Interfaces
|
|
|
|
sdnInterfaces, err := getSDNInterfaces(source.filePath)
|
2016-05-09 14:36:59 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-07-04 01:41:53 +03:00
|
|
|
// Configure the local default address space.
|
|
|
|
local, err := source.sink.newAddressSpace(LocalDefaultAddressSpaceId, LocalScope)
|
2016-05-09 14:36:59 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-07-04 01:41:53 +03:00
|
|
|
if err = populateAddressSpace(local, sdnInterfaces, localInterfaces); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the local address space as active.
|
|
|
|
if err = source.sink.setAddressSpace(local); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-28 07:38:36 +03:00
|
|
|
logger.Info("Address space successfully populated from config file")
|
2019-07-04 01:41:53 +03:00
|
|
|
source.fileLoaded = true
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getSDNInterfaces(fileLocation string) (*NetworkInterfaces, error) {
|
2021-11-18 01:31:42 +03:00
|
|
|
data, err := os.ReadFile(fileLocation)
|
2019-07-04 01:41:53 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
interfaces := &NetworkInterfaces{}
|
|
|
|
if err = json.Unmarshal(data, interfaces); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return interfaces, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func populateAddressSpace(localAddressSpace *addressSpace, sdnInterfaces *NetworkInterfaces, localInterfaces []net.Interface) error {
|
2021-09-03 00:33:18 +03:00
|
|
|
// Find the interface with matching MacAddress or Name
|
2019-07-04 01:41:53 +03:00
|
|
|
for _, sdnIf := range sdnInterfaces.Interfaces {
|
|
|
|
ifName := ""
|
|
|
|
|
|
|
|
for _, localIf := range localInterfaces {
|
|
|
|
if macAddressesEqual(sdnIf.MacAddress, localIf.HardwareAddr.String()) {
|
|
|
|
ifName = localIf.Name
|
|
|
|
break
|
|
|
|
}
|
2016-05-09 14:36:59 +03:00
|
|
|
}
|
|
|
|
|
2019-07-04 01:41:53 +03:00
|
|
|
// Skip if interface is not found.
|
|
|
|
if ifName == "" {
|
2023-10-28 07:38:36 +03:00
|
|
|
logger.Info("Failed to find interface with", zap.String("MAC Address", sdnIf.MacAddress))
|
2017-08-17 23:49:36 +03:00
|
|
|
continue
|
2016-05-09 14:36:59 +03:00
|
|
|
}
|
|
|
|
|
2019-07-04 01:41:53 +03:00
|
|
|
// Prioritize secondary interfaces.
|
|
|
|
priority := 0
|
|
|
|
if !sdnIf.IsPrimary {
|
|
|
|
priority = 1
|
2016-05-09 14:36:59 +03:00
|
|
|
}
|
|
|
|
|
2019-07-04 01:41:53 +03:00
|
|
|
for _, subnet := range sdnIf.IPSubnets {
|
|
|
|
_, network, err := net.ParseCIDR(subnet.Prefix)
|
|
|
|
if err != nil {
|
2023-10-28 07:38:36 +03:00
|
|
|
logger.Error("Failed to parse subnet", zap.String("prefix", subnet.Prefix), zap.Error(err))
|
2019-07-04 01:41:53 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
addressPool, err := localAddressSpace.newAddressPool(ifName, priority, network)
|
|
|
|
if err != nil {
|
2023-10-28 07:38:36 +03:00
|
|
|
logger.Error("Failed to create pool", zap.Any("subnet", subnet), zap.String("ifName", ifName), zap.Error(err))
|
2019-07-04 01:41:53 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the IP addresses to the localAddressSpace address space.
|
|
|
|
for _, ipAddr := range subnet.IPAddresses {
|
|
|
|
// Primary addresses are reserved for the host.
|
|
|
|
if ipAddr.IsPrimary {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
address := net.ParseIP(ipAddr.Address)
|
|
|
|
|
|
|
|
_, err = addressPool.newAddressRecord(&address)
|
|
|
|
if err != nil {
|
2023-10-28 07:38:36 +03:00
|
|
|
logger.Error("Failed to create", zap.Any("address", address), zap.Error(err))
|
2019-07-04 01:41:53 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-09 14:36:59 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-07-04 01:41:53 +03:00
|
|
|
|
|
|
|
func macAddressesEqual(macAddress1 string, macAddress2 string) bool {
|
|
|
|
macAddress1 = strings.ToLower(strings.Replace(macAddress1, ":", "", -1))
|
|
|
|
macAddress2 = strings.ToLower(strings.Replace(macAddress2, ":", "", -1))
|
|
|
|
|
|
|
|
return macAddress1 == macAddress2
|
|
|
|
}
|