-
Notifications
You must be signed in to change notification settings - Fork 431
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cloud/services: add bastion host service
- Loading branch information
Showing
6 changed files
with
777 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
/* | ||
Copyright 2020 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 ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2019-06-01/network" | ||
"github.com/Azure/go-autorest/autorest/to" | ||
"github.com/pkg/errors" | ||
"k8s.io/klog" | ||
|
||
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1alpha3" | ||
azure "sigs.k8s.io/cluster-api-provider-azure/cloud" | ||
"sigs.k8s.io/cluster-api-provider-azure/cloud/converters" | ||
) | ||
|
||
// Spec specification for bastion host | ||
type Spec struct { | ||
Name string | ||
SubnetName string | ||
PublicIPName string | ||
VnetName string | ||
} | ||
|
||
// Reconcile gets/creates/updates a bastion host. | ||
func (s *Service) Reconcile(ctx context.Context, spec interface{}) error { | ||
bastionHostSpec, ok := spec.(*Spec) | ||
if !ok { | ||
return errors.New("invalid Bastion Host specification") | ||
} | ||
|
||
klog.V(2).Infof("getting subnet %s", bastionHostSpec.SubnetName) | ||
subnet, err := s.SubnetsClient.Get(ctx, s.Scope.ResourceGroup(), bastionHostSpec.VnetName, bastionHostSpec.SubnetName) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to get subnet") | ||
} | ||
|
||
klog.V(2).Infof("successfully got subnet %s", bastionHostSpec.SubnetName) | ||
|
||
klog.V(2).Infof("checking if public ip %s exist otherwise will try to create", bastionHostSpec.PublicIPName) | ||
publicIP := network.PublicIPAddress{} | ||
publicIP, err = s.PublicIPsClient.Get(ctx, s.Scope.ResourceGroup(), bastionHostSpec.PublicIPName) | ||
if err != nil && azure.ResourceNotFound(err) { | ||
iperr := s.createBastionPublicIP(ctx, bastionHostSpec.PublicIPName) | ||
if iperr != nil { | ||
return errors.Wrap(iperr, "failed to create bastion public IP") | ||
} | ||
var errPublicIP error | ||
publicIP, errPublicIP = s.PublicIPsClient.Get(ctx, s.Scope.ResourceGroup(), bastionHostSpec.PublicIPName) | ||
if errPublicIP != nil { | ||
return errors.Wrap(errPublicIP, "failed to get public IP") | ||
} | ||
} else if err != nil { | ||
return errors.Wrap(err, "failed to look for existing public IP") | ||
} | ||
klog.V(2).Infof("successfully got public ip %s", bastionHostSpec.PublicIPName) | ||
|
||
bastionHostName := bastionHostSpec.Name | ||
klog.V(2).Infof("creating bastion host %s", bastionHostName) | ||
|
||
bastionHostIPConfigName := fmt.Sprintf("%s-%s", bastionHostSpec.Name, "bastionIP") | ||
|
||
err = s.Client.CreateOrUpdate( | ||
ctx, | ||
s.Scope.ResourceGroup(), | ||
bastionHostName, | ||
network.BastionHost{ | ||
Name: to.StringPtr(bastionHostName), | ||
Location: to.StringPtr(s.Scope.Location()), | ||
Tags: converters.TagsToMap(infrav1.Build(infrav1.BuildParams{ | ||
ClusterName: s.Scope.Name(), | ||
Lifecycle: infrav1.ResourceLifecycleOwned, | ||
Name: to.StringPtr(bastionHostName), | ||
Role: to.StringPtr("Bastion"), | ||
})), | ||
BastionHostPropertiesFormat: &network.BastionHostPropertiesFormat{ | ||
DNSName: to.StringPtr(fmt.Sprintf("%s-bastion", strings.ToLower(bastionHostName))), | ||
IPConfigurations: &[]network.BastionHostIPConfiguration{ | ||
{ | ||
Name: to.StringPtr(bastionHostIPConfigName), | ||
BastionHostIPConfigurationPropertiesFormat: &network.BastionHostIPConfigurationPropertiesFormat{ | ||
Subnet: &network.SubResource{ | ||
ID: subnet.ID, | ||
}, | ||
PublicIPAddress: &network.SubResource{ | ||
ID: publicIP.ID, | ||
}, | ||
PrivateIPAllocationMethod: network.Static, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
) | ||
if err != nil { | ||
return errors.Wrap(err, "cannot create bastion host") | ||
} | ||
|
||
klog.V(2).Infof("successfully created bastion host %s", bastionHostName) | ||
return nil | ||
} | ||
|
||
// Delete deletes the bastion host with the provided scope. | ||
func (s *Service) Delete(ctx context.Context, spec interface{}) error { | ||
bastionHostSpec, ok := spec.(*Spec) | ||
if !ok { | ||
return errors.New("invalid Bastion Host specification") | ||
} | ||
|
||
klog.V(2).Infof("deleting Bastion Host %s", bastionHostSpec.Name) | ||
err := s.Client.Delete(ctx, s.Scope.ResourceGroup(), bastionHostSpec.Name) | ||
if err != nil && azure.ResourceNotFound(err) { | ||
// already deleted | ||
return nil | ||
} | ||
if err != nil { | ||
return errors.Wrapf(err, "failed to delete Bastion Host %s in resource group %s", bastionHostSpec.Name, s.Scope.ResourceGroup()) | ||
} | ||
|
||
klog.V(2).Infof("deleted Bastion Host %s", bastionHostSpec.Name) | ||
return err | ||
} | ||
|
||
func (s *Service) createBastionPublicIP(ctx context.Context, ipName string) error { | ||
klog.V(2).Infof("creating bastion public IP %s", ipName) | ||
|
||
return s.PublicIPsClient.CreateOrUpdate( | ||
ctx, | ||
s.Scope.ResourceGroup(), | ||
ipName, | ||
network.PublicIPAddress{ | ||
Sku: &network.PublicIPAddressSku{Name: network.PublicIPAddressSkuNameStandard}, | ||
Name: to.StringPtr(ipName), | ||
Location: to.StringPtr(s.Scope.Location()), | ||
PublicIPAddressPropertiesFormat: &network.PublicIPAddressPropertiesFormat{ | ||
PublicIPAddressVersion: network.IPv4, | ||
PublicIPAllocationMethod: network.Static, | ||
DNSSettings: &network.PublicIPAddressDNSSettings{ | ||
DomainNameLabel: to.StringPtr(strings.ToLower(ipName)), | ||
}, | ||
}, | ||
}, | ||
) | ||
} |
Oops, something went wrong.