github.phpd.cn/cilium/cilium@v1.6.12/pkg/aws/types/types.go (about)

     1  // Copyright 2019 Authors of Cilium
     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 types
    16  
    17  import (
    18  	"github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2"
    19  )
    20  
    21  // Tags implements generic key value tags used by AWS
    22  type Tags map[string]string
    23  
    24  // Match returns true if the required tags are all found
    25  func (t Tags) Match(required Tags) bool {
    26  	for k, neededvalue := range required {
    27  		haveValue, ok := t[k]
    28  		if !ok || (ok && neededvalue != haveValue) {
    29  			return false
    30  		}
    31  	}
    32  	return true
    33  }
    34  
    35  // Subnet is a representation of an AWS subnet
    36  type Subnet struct {
    37  	// ID is the subnet ID
    38  	ID string
    39  
    40  	// Name is the subnet name
    41  	Name string
    42  
    43  	// CIDR is the CIDR associated with the subnet
    44  	CIDR string
    45  
    46  	// AvailabilityZone is the availability zone of the subnet
    47  	AvailabilityZone string
    48  
    49  	// VpcID is the VPC the subnet is in
    50  	VpcID string
    51  
    52  	// AvailableAddresses is the number of addresses available for
    53  	// allocation
    54  	AvailableAddresses int
    55  
    56  	// Tags is the tags of the subnet
    57  	Tags Tags
    58  }
    59  
    60  // Vpc is the representation of an AWS VPC
    61  type Vpc struct {
    62  	// ID is the VPC ID
    63  	ID string
    64  
    65  	// PrimaryCIDR is the primary IPv4 CIDR
    66  	PrimaryCIDR string
    67  }
    68  
    69  // instance is the minimal representation of an AWS instance as needed by the
    70  // ENI allocator
    71  type instance struct {
    72  	// enis is a map of all ENIs attached to the instance indexed by the
    73  	// ENI ID
    74  	enis map[string]*v2.ENI
    75  }
    76  
    77  // InstanceMap is the list of all instances indexed by instance ID
    78  type InstanceMap map[string]*instance
    79  
    80  // Add adds an instance definition to the instance map. instanceMap may not be
    81  // subject to concurrent access while add() is used.
    82  func (m InstanceMap) Add(instanceID string, eni *v2.ENI) {
    83  	i, ok := m[instanceID]
    84  	if !ok {
    85  		i = &instance{}
    86  		m[instanceID] = i
    87  	}
    88  
    89  	if i.enis == nil {
    90  		i.enis = map[string]*v2.ENI{}
    91  	}
    92  
    93  	i.enis[eni.ID] = eni
    94  }
    95  
    96  // Update updates the ENI definition of an ENI for a particular instance. If
    97  // the ENI is already known, the definition is updated, otherwise the ENI is
    98  // added to the instance.
    99  func (m InstanceMap) Update(instanceID string, eni *v2.ENI) {
   100  	if i, ok := m[instanceID]; ok {
   101  		i.enis[eni.ID] = eni
   102  	} else {
   103  		m.Add(instanceID, eni)
   104  	}
   105  }
   106  
   107  // Get returns the list of ENIs for a particular instance ID
   108  func (m InstanceMap) Get(instanceID string) (enis []*v2.ENI) {
   109  	if instance, ok := m[instanceID]; ok {
   110  		for _, e := range instance.enis {
   111  			enis = append(enis, e.DeepCopy())
   112  		}
   113  	}
   114  
   115  	return
   116  }
   117  
   118  // SubnetMap indexes AWS subnets by subnet ID
   119  type SubnetMap map[string]*Subnet
   120  
   121  // VpcMap indexes AWS VPCs by VPC ID
   122  type VpcMap map[string]*Vpc