sigs.k8s.io/cluster-api-provider-azure@v1.17.0/azure/services/bastionhosts/spec.go (about)

     1  /*
     2  Copyright 2021 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 bastionhosts
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"strings"
    23  
    24  	asonetworkv1 "github.com/Azure/azure-service-operator/v2/api/network/v1api20220701"
    25  	"github.com/Azure/azure-service-operator/v2/pkg/genruntime"
    26  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    27  	"k8s.io/utils/ptr"
    28  	infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
    29  	"sigs.k8s.io/cluster-api-provider-azure/azure"
    30  )
    31  
    32  // AzureBastionSpec defines the specification for azure bastion feature.
    33  type AzureBastionSpec struct {
    34  	Name            string
    35  	ResourceGroup   string
    36  	Location        string
    37  	ClusterName     string
    38  	SubnetID        string
    39  	PublicIPID      string
    40  	Sku             infrav1.BastionHostSkuName
    41  	EnableTunneling bool
    42  }
    43  
    44  // ResourceRef implements azure.ASOResourceSpecGetter.
    45  func (s *AzureBastionSpec) ResourceRef() *asonetworkv1.BastionHost {
    46  	return &asonetworkv1.BastionHost{
    47  		ObjectMeta: metav1.ObjectMeta{
    48  			Name: azure.GetNormalizedKubernetesName(s.Name),
    49  		},
    50  	}
    51  }
    52  
    53  // Parameters implements azure.ASOResourceSpecGetter.
    54  func (s *AzureBastionSpec) Parameters(ctx context.Context, existingBastionHost *asonetworkv1.BastionHost) (parameters *asonetworkv1.BastionHost, err error) {
    55  	bastionHost := &asonetworkv1.BastionHost{}
    56  	if existingBastionHost != nil {
    57  		bastionHost = existingBastionHost
    58  	}
    59  
    60  	bastionHostIPConfigName := fmt.Sprintf("%s-%s", s.Name, "bastionIP")
    61  	bastionHost.Spec.AzureName = s.Name
    62  	bastionHost.Spec.Location = ptr.To(s.Location)
    63  	bastionHost.Spec.Owner = &genruntime.KnownResourceReference{
    64  		Name: azure.GetNormalizedKubernetesName(s.ResourceGroup),
    65  	}
    66  	bastionHost.Spec.Tags = infrav1.Build(infrav1.BuildParams{
    67  		ClusterName: s.ClusterName,
    68  		Lifecycle:   infrav1.ResourceLifecycleOwned,
    69  		Name:        ptr.To(s.Name),
    70  		Role:        ptr.To("Bastion"),
    71  	})
    72  	bastionHost.Spec.Sku = &asonetworkv1.Sku{
    73  		Name: ptr.To(asonetworkv1.Sku_Name(s.Sku)),
    74  	}
    75  	bastionHost.Spec.EnableTunneling = ptr.To(s.EnableTunneling)
    76  	bastionHost.Spec.DnsName = ptr.To(fmt.Sprintf("%s-bastion", strings.ToLower(s.Name)))
    77  	bastionHost.Spec.IpConfigurations = []asonetworkv1.BastionHostIPConfiguration{
    78  		{
    79  			Name: ptr.To(bastionHostIPConfigName),
    80  			Subnet: &asonetworkv1.BastionHostSubResource{
    81  				Reference: &genruntime.ResourceReference{
    82  					ARMID: s.SubnetID,
    83  				},
    84  			},
    85  			PublicIPAddress: &asonetworkv1.BastionHostSubResource{
    86  				Reference: &genruntime.ResourceReference{
    87  					ARMID: s.PublicIPID,
    88  				},
    89  			},
    90  			PrivateIPAllocationMethod: ptr.To(asonetworkv1.IPAllocationMethod_Dynamic),
    91  		},
    92  	}
    93  
    94  	return bastionHost, nil
    95  }
    96  
    97  // WasManaged implements azure.ASOResourceSpecGetter.
    98  func (s *AzureBastionSpec) WasManaged(resource *asonetworkv1.BastionHost) bool {
    99  	// returns always returns true as CAPZ does not support BYO bastion.
   100  	return true
   101  }