kubesphere.io/api@v0.0.0-20231107125330-c9a03957060c/network/calicov3/ipamblock_types.go (about)

     1  // Copyright (c) 2019-2020 Tigera, Inc. All rights reserved.
     2  
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package calicov3
    16  
    17  import (
    18  	"strings"
    19  
    20  	v3 "github.com/projectcalico/calico/libcalico-go/lib/apis/v3"
    21  	cnet "github.com/projectcalico/calico/libcalico-go/lib/net"
    22  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    23  )
    24  
    25  // +genclient
    26  // +kubebuilder:object:root=true
    27  // +genclient:nonNamespaced
    28  // +k8s:openapi-gen=true
    29  // +kubebuilder:resource:scope=Cluster
    30  type IPAMBlock struct {
    31  	metav1.TypeMeta   `json:",inline"`
    32  	metav1.ObjectMeta `json:"metadata,omitempty"`
    33  	Spec              v3.IPAMBlockSpec `json:"spec,omitempty"`
    34  }
    35  
    36  // +kubebuilder:object:root=true
    37  
    38  // IPAMBlockList contains a list of IPAMBlock resources.
    39  type IPAMBlockList struct {
    40  	metav1.TypeMeta `json:",inline"`
    41  	metav1.ListMeta `json:"metadata"`
    42  	Items           []IPAMBlock `json:"items"`
    43  }
    44  
    45  func (b *IPAMBlock) NumReservedAddresses() int {
    46  	sum := 0
    47  	for _, attrIdx := range b.Spec.Allocations {
    48  		if attrIdx == nil {
    49  			continue
    50  		}
    51  		attrs := b.Spec.Attributes[*attrIdx]
    52  		if attrs.AttrPrimary == nil || strings.ToLower(*attrs.AttrPrimary) == WindowsReservedHandle {
    53  			sum += 1
    54  		}
    55  	}
    56  	return sum
    57  }
    58  
    59  // Get number of addresses covered by the block
    60  func (b *IPAMBlock) NumAddresses() int {
    61  	_, cidr, _ := cnet.ParseCIDR(b.Spec.CIDR)
    62  	ones, size := cidr.Mask.Size()
    63  	numAddresses := 1 << uint(size-ones)
    64  	return numAddresses
    65  }
    66  
    67  func (b *IPAMBlock) NumFreeAddresses() int {
    68  	return len(b.Spec.Unallocated)
    69  }
    70  
    71  // windwowsReservedHandle is the handle used to reserve addresses required for Windows
    72  // networking so that workloads do not get assigned these addresses.
    73  const WindowsReservedHandle = "windows-reserved-ipam-handle"
    74  
    75  func (b *IPAMBlock) Empty() bool {
    76  	for _, attrIdx := range b.Spec.Allocations {
    77  		if attrIdx == nil {
    78  			continue
    79  		}
    80  		attrs := b.Spec.Attributes[*attrIdx]
    81  		if attrs.AttrPrimary == nil || strings.ToLower(*attrs.AttrPrimary) != WindowsReservedHandle {
    82  			return false
    83  		}
    84  	}
    85  	return true
    86  }