From a88f7ccd14bc65839c46c2a56dfbcc94b60bba33 Mon Sep 17 00:00:00 2001 From: Stephen Augustus Date: Thu, 7 Mar 2019 15:00:13 -0500 Subject: [PATCH] Update public IP creation/reconcile (#124) * deployer: Improve FQDN retrieval in GetIP/GetKubeConfig Signed-off-by: Stephen Augustus * network: Update GetPublicIPName usage Signed-off-by: Stephen Augustus * network: Update public IP reconcile checks Signed-off-by: Stephen Augustus --- pkg/cloud/azure/actuators/machine/actuator.go | 2 +- .../azure/services/network/interfaces.go | 7 +++--- .../azure/services/network/publicipaddress.go | 23 +++++++++++++++++-- pkg/deployer/deployer.go | 19 +++++++-------- 4 files changed, 34 insertions(+), 17 deletions(-) diff --git a/pkg/cloud/azure/actuators/machine/actuator.go b/pkg/cloud/azure/actuators/machine/actuator.go index 57c7c33b9ef..60f51096cd2 100644 --- a/pkg/cloud/azure/actuators/machine/actuator.go +++ b/pkg/cloud/azure/actuators/machine/actuator.go @@ -190,7 +190,7 @@ func (a *Actuator) Create(ctx context.Context, cluster *clusterv1.Cluster, machi } } - pip, err := networkSvc.CreateOrUpdatePublicIPAddress(scope.ClusterConfig.ResourceGroup, scope.Machine.Name, networkSvc.GetDefaultPublicIPZone()) + pip, err := networkSvc.CreateOrUpdatePublicIPAddress(scope.ClusterConfig.ResourceGroup, networkSvc.GetPublicIPName(machine), networkSvc.GetDefaultPublicIPZone()) if err != nil { klog.Errorf("Unable to create public IP: %+v", err) return &controllerError.RequeueAfterError{ diff --git a/pkg/cloud/azure/services/network/interfaces.go b/pkg/cloud/azure/services/network/interfaces.go index eb240be49ca..de73fe55d18 100644 --- a/pkg/cloud/azure/services/network/interfaces.go +++ b/pkg/cloud/azure/services/network/interfaces.go @@ -81,7 +81,7 @@ func (s *Service) ReconcileNICBackendPool(networkInterfaceName, backendPoolID st if len(ipConfigs) > 0 { ipConfig := ipConfigs[0] - if ipConfig.LoadBalancerBackendAddressPools != nil { + if ipConfig.LoadBalancerBackendAddressPools != (*[]network.BackendAddressPool)(nil) { backendPool := (*ipConfig.LoadBalancerBackendAddressPools)[0] if *backendPool.ID != backendPoolID { klog.V(2).Infof("Could not attach NIC to load balancer backend pool (%q). NIC is already attached to %q.", backendPoolID, *backendPool.ID) @@ -123,9 +123,8 @@ func (s *Service) ReconcileNICPublicIP(networkInterfaceName string, publicIP net ipConfigs := (*nic.IPConfigurations) if len(ipConfigs) > 0 { ipConfig := ipConfigs[0] - pip := ipConfig.PublicIPAddress - if pip != nil { + if ipConfig.PublicIPAddress != (*network.PublicIPAddress)(nil) { pipID := *ipConfig.PublicIPAddress.ID if pipID != *publicIP.ID { klog.V(2).Infof("Could not associate NIC to public IP (%q). NIC is already associated with %q.", *publicIP.ID, pipID) @@ -173,3 +172,5 @@ func (s *Service) getDefaultVMNetworkInterfaceConfig() network.Interface { func (s *Service) GetNetworkInterfaceName(machine *clusterv1.Machine) string { return fmt.Sprintf("%s-nic", machine.Name) } + +// TODO: Add method for retrieving a network interface's primary IP config diff --git a/pkg/cloud/azure/services/network/publicipaddress.go b/pkg/cloud/azure/services/network/publicipaddress.go index 8def6f45782..63d18427bbc 100644 --- a/pkg/cloud/azure/services/network/publicipaddress.go +++ b/pkg/cloud/azure/services/network/publicipaddress.go @@ -21,12 +21,31 @@ import ( "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-12-01/network" "github.com/Azure/go-autorest/autorest/to" + "github.com/pkg/errors" "k8s.io/klog" clusterv1 "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1" ) -// CreateOrUpdatePublicIPAddress retrieves the Public IP address resource. +// GetPublicIPAddress retrieves the Public IP address resource. +func (s *Service) GetPublicIPAddress(resourceGroup, IPName string) (network.PublicIPAddress, error) { + klog.V(2).Info("Attempting to get public IP") + pip, err := s.scope.PublicIPAddresses.Get( + s.scope.Context, + resourceGroup, + IPName, + "", + ) + + if err != nil { + return pip, errors.Wrapf(err, "Failed to get public IP %q", IPName) + } + + return pip, nil +} + +// CreateOrUpdatePublicIPAddress updates a Public IP address resource or creates one, if it doesn't exist. func (s *Service) CreateOrUpdatePublicIPAddress(resourceGroup, IPName, zone string) (pip network.PublicIPAddress, err error) { + klog.V(2).Info("Attempting to create or update public IP") publicIP := network.PublicIPAddress{ Name: to.StringPtr(IPName), Location: to.StringPtr(s.scope.Location()), @@ -78,7 +97,7 @@ func (s *Service) DeletePublicIPAddress(resourceGroup string, IPName string) (er // GetPublicIPName returns the public IP resource name of the machine. func (s *Service) GetPublicIPName(machine *clusterv1.Machine) string { - return fmt.Sprintf("%s-pip", machine.Name) + return fmt.Sprintf("%s", machine.Name) } // GetDefaultPublicIPZone returns the public IP resource name of the machine. diff --git a/pkg/deployer/deployer.go b/pkg/deployer/deployer.go index 9a5cd18bb58..b5112831e0e 100644 --- a/pkg/deployer/deployer.go +++ b/pkg/deployer/deployer.go @@ -58,15 +58,17 @@ func (d *Deployer) GetIP(cluster *clusterv1.Cluster, machine *clusterv1.Machine) networkSvc := network.NewService(scope) - pip, err := networkSvc.CreateOrUpdatePublicIPAddress(scope.ClusterConfig.ResourceGroup, networkSvc.GetPublicIPName(machine), "") + // TODO: Consider moving FQDN retrieval into its' own method. + pip, err := networkSvc.GetPublicIPAddress(scope.ClusterConfig.ResourceGroup, networkSvc.GetPublicIPName(machine)) if err != nil { return "", err } - return *pip.IPAddress, nil + + return *pip.DNSSettings.Fqdn, nil } // GetKubeConfig returns the kubeconfig after the bootstrap process is complete. -func (d *Deployer) GetKubeConfig(cluster *clusterv1.Cluster, machine *clusterv1.Machine) (string, error) { +func (d *Deployer) GetKubeConfig(cluster *clusterv1.Cluster, _ *clusterv1.Machine) (string, error) { // Load provider config. config, err := providerv1.ClusterConfigFromProviderSpec(cluster.Spec.ProviderSpec) @@ -88,14 +90,9 @@ func (d *Deployer) GetKubeConfig(cluster *clusterv1.Cluster, machine *clusterv1. return "", errors.New("key not found in status") } - // TODO: Unwrap this once load balancer is implemented - dnsName := "null" - - if machine != nil { - dnsName, err = d.GetIP(cluster, nil) - if err != nil { - return "", errors.Wrap(err, "failed to get DNS address") - } + dnsName, err := d.GetIP(cluster, nil) + if err != nil { + return "", errors.Wrap(err, "failed to get DNS address") } server := fmt.Sprintf("https://%s:6443", dnsName)