sigs.k8s.io/cluster-api-provider-azure@v1.14.3/api/v1beta1/types_template.go (about)

     1  /*
     2  Copyright 2022 The Kubernetes 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 v1beta1
    18  
    19  import (
    20  	"github.com/pkg/errors"
    21  	"k8s.io/utils/net"
    22  )
    23  
    24  // AzureManagedControlPlaneTemplateResourceSpec specifies an Azure managed control plane template resource.
    25  type AzureManagedControlPlaneTemplateResourceSpec struct {
    26  	AzureManagedControlPlaneClassSpec `json:",inline"`
    27  }
    28  
    29  // AzureManagedControlPlaneTemplateMachineTemplate is only used to fulfill the CAPI contract which expects a
    30  // MachineTemplate field for any controlplane ref in a topology.
    31  type AzureManagedControlPlaneTemplateMachineTemplate struct{}
    32  
    33  // AzureManagedMachinePoolTemplateResourceSpec specifies an Azure managed control plane template resource.
    34  type AzureManagedMachinePoolTemplateResourceSpec struct {
    35  	AzureManagedMachinePoolClassSpec `json:",inline"`
    36  }
    37  
    38  // AzureManagedClusterTemplateResourceSpec specifies an Azure managed cluster template resource.
    39  type AzureManagedClusterTemplateResourceSpec struct{}
    40  
    41  // AzureClusterTemplateResourceSpec specifies an Azure cluster template resource.
    42  type AzureClusterTemplateResourceSpec struct {
    43  	AzureClusterClassSpec `json:",inline"`
    44  
    45  	// NetworkSpec encapsulates all things related to Azure network.
    46  	// +optional
    47  	NetworkSpec NetworkTemplateSpec `json:"networkSpec,omitempty"`
    48  
    49  	// BastionSpec encapsulates all things related to the Bastions in the cluster.
    50  	// +optional
    51  	BastionSpec BastionTemplateSpec `json:"bastionSpec,omitempty"`
    52  }
    53  
    54  // NetworkTemplateSpec specifies a network template.
    55  type NetworkTemplateSpec struct {
    56  	NetworkClassSpec `json:",inline"`
    57  
    58  	// Vnet is the configuration for the Azure virtual network.
    59  	// +optional
    60  	Vnet VnetTemplateSpec `json:"vnet,omitempty"`
    61  
    62  	// Subnets is the configuration for the control-plane subnet and the node subnet.
    63  	// +optional
    64  	Subnets SubnetTemplatesSpec `json:"subnets,omitempty"`
    65  
    66  	// APIServerLB is the configuration for the control-plane load balancer.
    67  	// +optional
    68  	APIServerLB LoadBalancerClassSpec `json:"apiServerLB,omitempty"`
    69  
    70  	// NodeOutboundLB is the configuration for the node outbound load balancer.
    71  	// +optional
    72  	NodeOutboundLB *LoadBalancerClassSpec `json:"nodeOutboundLB,omitempty"`
    73  
    74  	// ControlPlaneOutboundLB is the configuration for the control-plane outbound load balancer.
    75  	// This is different from APIServerLB, and is used only in private clusters (optionally) for enabling outbound traffic.
    76  	// +optional
    77  	ControlPlaneOutboundLB *LoadBalancerClassSpec `json:"controlPlaneOutboundLB,omitempty"`
    78  }
    79  
    80  // GetSubnetTemplate returns the subnet template based on the subnet role.
    81  func (n *NetworkTemplateSpec) GetSubnetTemplate(role SubnetRole) (SubnetTemplateSpec, error) {
    82  	for _, sn := range n.Subnets {
    83  		if sn.Role == role {
    84  			return sn, nil
    85  		}
    86  	}
    87  	return SubnetTemplateSpec{}, errors.Errorf("no subnet template found with role %s", role)
    88  }
    89  
    90  // UpdateSubnetTemplate updates the subnet template based on subnet role.
    91  func (n *NetworkTemplateSpec) UpdateSubnetTemplate(subnet SubnetTemplateSpec, role SubnetRole) {
    92  	for i, sn := range n.Subnets {
    93  		if sn.Role == role {
    94  			n.Subnets[i] = subnet
    95  		}
    96  	}
    97  }
    98  
    99  // VnetTemplateSpec defines the desired state of a virtual network.
   100  type VnetTemplateSpec struct {
   101  	VnetClassSpec `json:",inline"`
   102  
   103  	// Peerings defines a list of peerings of the newly created virtual network with existing virtual networks.
   104  	// +optional
   105  	Peerings VnetPeeringsTemplateSpec `json:"peerings,omitempty"`
   106  }
   107  
   108  // VnetPeeringsTemplateSpec defines a list of peerings of the newly created virtual network with existing virtual networks.
   109  type VnetPeeringsTemplateSpec []VnetPeeringClassSpec
   110  
   111  // SubnetTemplateSpec specifies a template for a subnet.
   112  type SubnetTemplateSpec struct {
   113  	SubnetClassSpec `json:",inline"`
   114  
   115  	// SecurityGroup defines the NSG (network security group) that should be attached to this subnet.
   116  	// +optional
   117  	SecurityGroup SecurityGroupClass `json:"securityGroup,omitempty"`
   118  
   119  	// NatGateway associated with this subnet.
   120  	// +optional
   121  	NatGateway NatGatewayClassSpec `json:"natGateway,omitempty"`
   122  }
   123  
   124  // IsNatGatewayEnabled returns true if the NAT gateway is enabled.
   125  func (s SubnetTemplateSpec) IsNatGatewayEnabled() bool {
   126  	return s.NatGateway.Name != ""
   127  }
   128  
   129  // IsIPv6Enabled returns whether or not IPv6 is enabled on the subnet.
   130  func (s SubnetTemplateSpec) IsIPv6Enabled() bool {
   131  	for _, cidr := range s.CIDRBlocks {
   132  		if net.IsIPv6CIDRString(cidr) {
   133  			return true
   134  		}
   135  	}
   136  	return false
   137  }
   138  
   139  // SubnetTemplatesSpec specifies a list of subnet templates.
   140  // +listType=map
   141  // +listMapKey=name
   142  type SubnetTemplatesSpec []SubnetTemplateSpec
   143  
   144  // BastionTemplateSpec specifies a template for a bastion host.
   145  type BastionTemplateSpec struct {
   146  	// +optional
   147  	AzureBastion *AzureBastionTemplateSpec `json:"azureBastion,omitempty"`
   148  }
   149  
   150  // AzureBastionTemplateSpec specifies a template for an Azure Bastion host.
   151  type AzureBastionTemplateSpec struct {
   152  	// +optional
   153  	Subnet SubnetTemplateSpec `json:"subnet,omitempty"`
   154  }