Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions api/v1beta1/azurecluster_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,19 +309,15 @@ func (c *AzureCluster) setBastionDefaults() {
c.Spec.BastionSpec.AzureBastion.Name = generateAzureBastionName(c.ObjectMeta.Name)
}
// Ensure defaults for the Subnet settings.
{
if c.Spec.BastionSpec.AzureBastion.Subnet.Name == "" {
c.Spec.BastionSpec.AzureBastion.Subnet.Name = DefaultAzureBastionSubnetName
}
if len(c.Spec.BastionSpec.AzureBastion.Subnet.CIDRBlocks) == 0 {
c.Spec.BastionSpec.AzureBastion.Subnet.CIDRBlocks = []string{DefaultAzureBastionSubnetCIDR}
}
if c.Spec.BastionSpec.AzureBastion.Subnet.Name == "" {
c.Spec.BastionSpec.AzureBastion.Subnet.Name = DefaultAzureBastionSubnetName
}
if len(c.Spec.BastionSpec.AzureBastion.Subnet.CIDRBlocks) == 0 {
c.Spec.BastionSpec.AzureBastion.Subnet.CIDRBlocks = []string{DefaultAzureBastionSubnetCIDR}
}
// Ensure defaults for the PublicIP settings.
{
if c.Spec.BastionSpec.AzureBastion.PublicIP.Name == "" {
c.Spec.BastionSpec.AzureBastion.PublicIP.Name = generateAzureBastionPublicIPName(c.ObjectMeta.Name)
}
if c.Spec.BastionSpec.AzureBastion.PublicIP.Name == "" {
c.Spec.BastionSpec.AzureBastion.PublicIP.Name = generateAzureBastionPublicIPName(c.ObjectMeta.Name)
}
}
}
Expand Down
41 changes: 29 additions & 12 deletions azure/scope/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (

infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-azure/azure"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/bastionhosts"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/groups"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/loadbalancers"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/natgateways"
Expand Down Expand Up @@ -290,7 +291,7 @@ func (s *ClusterScope) NSGSpecs() []azure.NSGSpec {
// SubnetSpecs returns the subnets specs.
func (s *ClusterScope) SubnetSpecs() []azure.SubnetSpec {
numberOfSubnets := len(s.AzureCluster.Spec.NetworkSpec.Subnets)
if s.AzureCluster.Spec.BastionSpec.AzureBastion != nil {
if s.IsAzureBastionEnabled() {
numberOfSubnets++
}

Expand All @@ -308,7 +309,7 @@ func (s *ClusterScope) SubnetSpecs() []azure.SubnetSpec {
subnetSpecs = append(subnetSpecs, subnetSpec)
}

if s.AzureCluster.Spec.BastionSpec.AzureBastion != nil {
if s.IsAzureBastionEnabled() {
azureBastionSubnet := s.AzureCluster.Spec.BastionSpec.AzureBastion.Subnet
subnetSpecs = append(subnetSpecs, azure.SubnetSpec{
Name: azureBastionSubnet.Name,
Expand Down Expand Up @@ -401,19 +402,33 @@ func (s *ClusterScope) PrivateDNSSpec() *azure.PrivateDNSSpec {
return specs
}

// BastionSpec returns the bastion spec.
func (s *ClusterScope) BastionSpec() azure.BastionSpec {
var ret azure.BastionSpec
if s.AzureCluster.Spec.BastionSpec.AzureBastion != nil {
ret.AzureBastion = &azure.AzureBastionSpec{
Name: s.AzureCluster.Spec.BastionSpec.AzureBastion.Name,
SubnetSpec: s.AzureCluster.Spec.BastionSpec.AzureBastion.Subnet,
PublicIPName: s.AzureCluster.Spec.BastionSpec.AzureBastion.PublicIP.Name,
VNetName: s.Vnet().Name,
// IsAzureBastionEnabled returns true if the azure bastion is enabled.
func (s *ClusterScope) IsAzureBastionEnabled() bool {
return s.AzureCluster.Spec.BastionSpec.AzureBastion != nil
}

// AzureBastion returns the cluster AzureBastion.
func (s *ClusterScope) AzureBastion() *infrav1.AzureBastion {
return s.AzureCluster.Spec.BastionSpec.AzureBastion
}

// AzureBastionSpec returns the bastion spec.
func (s *ClusterScope) AzureBastionSpec() azure.ResourceSpecGetter {
if s.IsAzureBastionEnabled() {
subnetID := azure.SubnetID(s.SubscriptionID(), s.ResourceGroup(), s.Vnet().Name, s.AzureBastion().Subnet.Name)
publicIPID := azure.PublicIPID(s.SubscriptionID(), s.ResourceGroup(), s.AzureBastion().PublicIP.Name)

return &bastionhosts.AzureBastionSpec{
Name: s.AzureBastion().Name,
ResourceGroup: s.ResourceGroup(),
Location: s.Location(),
ClusterName: s.ClusterName(),
SubnetID: subnetID,
PublicIPID: publicIPID,
}
}

return ret
return nil
}

// Vnet returns the cluster Vnet.
Expand Down Expand Up @@ -646,6 +661,7 @@ func (s *ClusterScope) PatchObject(ctx context.Context) error {
infrav1.DisksReadyCondition,
infrav1.NATGatewaysReadyCondition,
infrav1.LoadBalancersReadyCondition,
infrav1.BastionHostReadyCondition,
),
)

Expand All @@ -661,6 +677,7 @@ func (s *ClusterScope) PatchObject(ctx context.Context) error {
infrav1.DisksReadyCondition,
infrav1.NATGatewaysReadyCondition,
infrav1.LoadBalancersReadyCondition,
infrav1.BastionHostReadyCondition,
}})
}

Expand Down
108 changes: 0 additions & 108 deletions azure/services/bastionhosts/azurebastion.go

This file was deleted.

101 changes: 101 additions & 0 deletions azure/services/bastionhosts/azurebastion_spec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
Copyright 2021 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package bastionhosts

import (
"fmt"
"strings"

"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2021-02-01/network"
"github.com/Azure/go-autorest/autorest/to"
"github.com/pkg/errors"
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-azure/azure/converters"
)

// AzureBastionSpec defines the specification for azure bastion feature.
type AzureBastionSpec struct {
Name string
ResourceGroup string
Location string
ClusterName string
SubnetID string
PublicIPID string
}

// AzureBastionSpecInput defines the required inputs to construct an azure bastion spec.
type AzureBastionSpecInput struct {
SubnetName string
PublicIPName string
VNetName string
}

// ResourceName returns the name of the bastion host.
func (s *AzureBastionSpec) ResourceName() string {
return s.Name
}

// ResourceGroupName returns the name of the resource group.
func (s *AzureBastionSpec) ResourceGroupName() string {
return s.ResourceGroup
}

// OwnerResourceName is a no-op for bastion hosts.
func (s *AzureBastionSpec) OwnerResourceName() string {
return ""
}

// Parameters returns the parameters for the bastion host.
func (s *AzureBastionSpec) Parameters(existing interface{}) (paramteres interface{}, err error) {
if existing != nil {
if _, ok := existing.(network.BastionHost); !ok {
return nil, errors.Errorf("%T is not a network.BastionHost", existing)
}
// bastion host already exists
return nil, nil
}

bastionHostIPConfigName := fmt.Sprintf("%s-%s", s.Name, "bastionIP")

return network.BastionHost{
Name: to.StringPtr(s.Name),
Location: to.StringPtr(s.Location),
Tags: converters.TagsToMap(infrav1.Build(infrav1.BuildParams{
ClusterName: s.ClusterName,
Lifecycle: infrav1.ResourceLifecycleOwned,
Name: to.StringPtr(s.Name),
Role: to.StringPtr("Bastion"),
})),
BastionHostPropertiesFormat: &network.BastionHostPropertiesFormat{
DNSName: to.StringPtr(fmt.Sprintf("%s-bastion", strings.ToLower(s.Name))),
IPConfigurations: &[]network.BastionHostIPConfiguration{
{
Name: to.StringPtr(bastionHostIPConfigName),
BastionHostIPConfigurationPropertiesFormat: &network.BastionHostIPConfigurationPropertiesFormat{
Subnet: &network.SubResource{
ID: &s.SubnetID,
},
PublicIPAddress: &network.SubResource{
ID: &s.PublicIPID,
},
PrivateIPAllocationMethod: network.IPAllocationMethodDynamic,
},
},
},
},
}, nil
}
Loading