github.com/cilium/cilium@v1.16.2/pkg/azure/types/types.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package types
     5  
     6  import (
     7  	"strings"
     8  
     9  	"github.com/cilium/cilium/pkg/ipam/types"
    10  )
    11  
    12  const (
    13  	// ProviderPrefix is the prefix used to indicate that a k8s ProviderID
    14  	// represents an Azure resource
    15  	ProviderPrefix = "azure://"
    16  
    17  	// InterfaceAddressLimit is the maximum number of addresses on an interface
    18  	//
    19  	//
    20  	// For more information:
    21  	// https://docs.microsoft.com/en-us/azure/azure-resource-manager/management/azure-subscription-service-limits?toc=%2fazure%2fvirtual-network%2ftoc.json#networking-limits
    22  	InterfaceAddressLimit = 256
    23  
    24  	// StateSucceeded is the address state for a successfully provisioned address
    25  	StateSucceeded = "succeeded"
    26  )
    27  
    28  // AzureSpec is the Azure specification of a node running via the Azure IPAM
    29  //
    30  // The Azure specification can either be provided explicitly by the user or the
    31  // cilium agent running on the node can be instructed to create the CiliumNode
    32  // custom resource along with an Azure specification when the node registers
    33  // itself to the Kubernetes cluster.
    34  // This struct is embedded into v2.CiliumNode
    35  //
    36  // +k8s:deepcopy-gen=true
    37  type AzureSpec struct {
    38  	// InterfaceName is the name of the interface the cilium-operator
    39  	// will use to allocate all the IPs on
    40  	//
    41  	// +kubebuilder:validation:Optional
    42  	InterfaceName string `json:"interface-name,omitempty"`
    43  }
    44  
    45  // AzureStatus is the status of Azure addressing of the node.
    46  // This struct is embedded into v2.CiliumNode
    47  //
    48  // +k8s:deepcopy-gen=true
    49  type AzureStatus struct {
    50  	// Interfaces is the list of interfaces on the node
    51  	//
    52  	// +optional
    53  	Interfaces []AzureInterface `json:"interfaces,omitempty"`
    54  }
    55  
    56  // AzureAddress is an IP address assigned to an AzureInterface
    57  type AzureAddress struct {
    58  	// IP is the ip address of the address
    59  	IP string `json:"ip,omitempty"`
    60  
    61  	// Subnet is the subnet the address belongs to
    62  	Subnet string `json:"subnet,omitempty"`
    63  
    64  	// State is the provisioning state of the address
    65  	State string `json:"state,omitempty"`
    66  }
    67  
    68  // AzureInterface represents an Azure Interface
    69  //
    70  // +k8s:deepcopy-gen=true
    71  type AzureInterface struct {
    72  	// ID is the identifier
    73  	//
    74  	// +optional
    75  	ID string `json:"id,omitempty"`
    76  
    77  	// Name is the name of the interface
    78  	//
    79  	// +optional
    80  	Name string `json:"name,omitempty"`
    81  
    82  	// MAC is the mac address
    83  	//
    84  	// +optional
    85  	MAC string `json:"mac,omitempty"`
    86  
    87  	// State is the provisioning state
    88  	//
    89  	// +optional
    90  	State string `json:"state,omitempty"`
    91  
    92  	// Addresses is the list of all IPs associated with the interface,
    93  	// including all secondary addresses
    94  	//
    95  	// +optional
    96  	Addresses []AzureAddress `json:"addresses,omitempty"`
    97  
    98  	// SecurityGroup is the security group associated with the interface
    99  	SecurityGroup string `json:"security-group,omitempty"`
   100  
   101  	// GatewayIP is the interface's subnet's default route
   102  	//
   103  	// OBSOLETE: This field is obsolete, please use Gateway field instead.
   104  	//
   105  	// +optional
   106  	GatewayIP string `json:"GatewayIP"`
   107  
   108  	// Gateway is the interface's subnet's default route
   109  	//
   110  	// +optional
   111  	Gateway string `json:"gateway"`
   112  
   113  	// CIDR is the range that the interface belongs to.
   114  	//
   115  	// +optional
   116  	CIDR string `json:"cidr,omitempty"`
   117  
   118  	// vmssName is the name of the virtual machine scale set. This field is
   119  	// set by extractIDs()
   120  	vmssName string `json:"-"`
   121  
   122  	// vmID is the ID of the virtual machine
   123  	vmID string `json:"-"`
   124  
   125  	// resourceGroup is the resource group the interface belongs to
   126  	resourceGroup string `json:"-"`
   127  }
   128  
   129  func (a *AzureInterface) DeepCopyInterface() types.Interface {
   130  	return a.DeepCopy()
   131  }
   132  
   133  // SetID sets the Azure interface ID, as well as extracting other fields from
   134  // the ID itself.
   135  func (a *AzureInterface) SetID(id string) {
   136  	a.ID = id
   137  	a.extractIDs()
   138  }
   139  
   140  // InterfaceID returns the identifier of the interface
   141  func (a *AzureInterface) InterfaceID() string {
   142  	return a.ID
   143  }
   144  
   145  func (a *AzureInterface) extractIDs() {
   146  	switch {
   147  	// Interface from a VMSS instance:
   148  	// //subscriptions/xxx/resourceGroups/yyy/providers/Microsoft.Compute/virtualMachineScaleSets/ssss/virtualMachines/vvv/networkInterfaces/iii
   149  	case strings.Contains(a.ID, "virtualMachineScaleSets"):
   150  		segs := strings.Split(a.ID, "/")
   151  		if len(segs) >= 5 {
   152  			a.resourceGroup = segs[4]
   153  		}
   154  		if len(segs) >= 9 {
   155  			a.vmssName = segs[8]
   156  		}
   157  		if len(segs) >= 11 {
   158  			a.vmID = segs[10]
   159  		}
   160  	// Interface from a standalone instance:
   161  	// //subscriptions/xxx/resourceGroups/yyy/providers/Microsoft.Network/networkInterfaces/iii
   162  	case strings.Contains(a.ID, "/Microsoft.Network/"):
   163  		segs := strings.Split(a.ID, "/")
   164  		if len(segs) >= 5 {
   165  			a.resourceGroup = segs[4]
   166  		}
   167  	}
   168  }
   169  
   170  // GetResourceGroup returns the resource group the interface belongs to
   171  func (a *AzureInterface) GetResourceGroup() string {
   172  	return a.resourceGroup
   173  }
   174  
   175  // GetVMScaleSetName returns the VM scale set name the interface belongs to
   176  func (a *AzureInterface) GetVMScaleSetName() string {
   177  	return a.vmssName
   178  }
   179  
   180  // GetVMID returns the VM ID the interface belongs to
   181  func (a *AzureInterface) GetVMID() string {
   182  	return a.vmID
   183  }
   184  
   185  // ForeachAddress iterates over all addresses and calls fn
   186  func (a *AzureInterface) ForeachAddress(id string, fn types.AddressIterator) error {
   187  	for _, address := range a.Addresses {
   188  		if err := fn(id, a.ID, address.IP, address.Subnet, address); err != nil {
   189  			return err
   190  		}
   191  	}
   192  
   193  	return nil
   194  }