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

     1  /*
     2  Copyright 2020 The KubeSphere authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package v1alpha1
    18  
    19  import (
    20  	"fmt"
    21  	"strconv"
    22  	"strings"
    23  
    24  	"github.com/projectcalico/calico/libcalico-go/lib/names"
    25  	cnet "github.com/projectcalico/calico/libcalico-go/lib/net"
    26  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    27  )
    28  
    29  const (
    30  	ResourceKindIPAMHandle     = "IPAMHandle"
    31  	ResourceSingularIPAMHandle = "ipamhandle"
    32  	ResourcePluralIPAMHandle   = "ipamhandles"
    33  )
    34  
    35  // +genclient
    36  // +genclient:nonNamespaced
    37  // +kubebuilder:object:root=true
    38  // +k8s:openapi-gen=true
    39  // +kubebuilder:resource:scope=Cluster
    40  type IPAMHandle struct {
    41  	metav1.TypeMeta   `json:",inline"`
    42  	metav1.ObjectMeta `json:"metadata,omitempty"`
    43  	// Specification of the IPAMHandle.
    44  	Spec IPAMHandleSpec `json:"spec,omitempty"`
    45  }
    46  
    47  // IPAMHandleSpec contains the specification for an IPAMHandle resource.
    48  type IPAMHandleSpec struct {
    49  	HandleID string         `json:"handleID"`
    50  	Block    map[string]int `json:"block"`
    51  	Deleted  bool           `json:"deleted"`
    52  }
    53  
    54  // +kubebuilder:object:root=true
    55  // +genclient:nonNamespaced
    56  type IPAMHandleList struct {
    57  	metav1.TypeMeta `json:",inline"`
    58  	metav1.ListMeta `json:"metadata"`
    59  	Items           []IPAMHandle `json:"items"`
    60  }
    61  
    62  func (h *IPAMHandle) IncrementBlock(block *IPAMBlock, num int) int {
    63  	newNum := num
    64  	if val, ok := h.Spec.Block[block.String()]; ok {
    65  		// An entry exists for this block, increment the number
    66  		// of allocations.
    67  		newNum = val + num
    68  	}
    69  	h.Spec.Block[block.String()] = newNum
    70  	return newNum
    71  }
    72  
    73  func (h *IPAMHandle) Empty() bool {
    74  	return len(h.Spec.Block) == 0
    75  }
    76  
    77  func (h *IPAMHandle) MarkDeleted() {
    78  	h.Spec.Deleted = true
    79  }
    80  
    81  func (h *IPAMHandle) IsDeleted() bool {
    82  	return h.Spec.Deleted
    83  }
    84  
    85  func (h *IPAMHandle) DecrementBlock(block *IPAMBlock, num int) (*int, error) {
    86  	if current, ok := h.Spec.Block[block.String()]; !ok {
    87  		// This entry doesn't exist.
    88  		return nil, fmt.Errorf("Tried to decrement block %s by %v but it isn't linked to handle %s", block.BlockName(), num, h.Spec.HandleID)
    89  	} else {
    90  		newNum := current - num
    91  		if newNum < 0 {
    92  			return nil, fmt.Errorf("Tried to decrement block %s by %v but it only has %v addresses on handle %s", block.BlockName(), num, current, h.Spec.HandleID)
    93  		}
    94  
    95  		if newNum == 0 {
    96  			delete(h.Spec.Block, block.String())
    97  		} else {
    98  			h.Spec.Block[block.String()] = newNum
    99  		}
   100  		return &newNum, nil
   101  	}
   102  }
   103  
   104  func ConvertToBlockName(k string) string {
   105  	strs := strings.SplitN(k, "-", 2)
   106  	id, _ := strconv.Atoi(strs[0])
   107  	_, blockCIDR, _ := cnet.ParseCIDR(strs[1])
   108  
   109  	return fmt.Sprintf("%d-%s", id, names.CIDRToName(*blockCIDR))
   110  }