From 13313179644328570cf45d0fe15cc8801fc0465f Mon Sep 17 00:00:00 2001 From: Tobias Giese Date: Wed, 30 Jun 2021 16:58:51 +0200 Subject: [PATCH 1/2] floatingip.go: add description to floating ips Signed-off-by: Tobias Giese --- controllers/openstackcluster_controller.go | 5 +++-- controllers/openstackmachine_controller.go | 2 +- pkg/cloud/services/loadbalancer/loadbalancer.go | 2 +- pkg/cloud/services/networking/floatingip.go | 14 +++++++++++++- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/controllers/openstackcluster_controller.go b/controllers/openstackcluster_controller.go index e2d4fb1ef7..316f093427 100644 --- a/controllers/openstackcluster_controller.go +++ b/controllers/openstackcluster_controller.go @@ -314,7 +314,8 @@ func reconcileBastion(log logr.Logger, osProviderClient *gophercloud.ProviderCli if err != nil { return err } - fp, err := networkingService.GetOrCreateFloatingIP(openStackCluster, openStackCluster.Spec.Bastion.Instance.FloatingIP) + clusterName := fmt.Sprintf("%s-%s", cluster.Namespace, cluster.Name) + fp, err := networkingService.GetOrCreateFloatingIP(openStackCluster, clusterName, openStackCluster.Spec.Bastion.Instance.FloatingIP) if err != nil { return errors.Errorf("failed to get or create floating IP for bastion: %v", err) } @@ -403,7 +404,7 @@ func reconcileNetworkComponents(log logr.Logger, osProviderClient *gophercloud.P } else { port = int32(openStackCluster.Spec.APIServerPort) } - fp, err := networkingService.GetOrCreateFloatingIP(openStackCluster, openStackCluster.Spec.APIServerFloatingIP) + fp, err := networkingService.GetOrCreateFloatingIP(openStackCluster, clusterName, openStackCluster.Spec.APIServerFloatingIP) if err != nil { return errors.Errorf("Floating IP cannot be got or created: %v", err) } diff --git a/controllers/openstackmachine_controller.go b/controllers/openstackmachine_controller.go index d7cf491f89..646f5e65ec 100644 --- a/controllers/openstackmachine_controller.go +++ b/controllers/openstackmachine_controller.go @@ -348,7 +348,7 @@ func (r *OpenStackMachineReconciler) reconcileNormal(ctx context.Context, logger return ctrl.Result{}, nil } } else if util.IsControlPlaneMachine(machine) { - fp, err := networkingService.GetOrCreateFloatingIP(openStackCluster, openStackCluster.Spec.ControlPlaneEndpoint.Host) + fp, err := networkingService.GetOrCreateFloatingIP(openStackCluster, clusterName, openStackCluster.Spec.ControlPlaneEndpoint.Host) if err != nil { handleUpdateMachineError(logger, openStackMachine, errors.Errorf("Floating IP cannot be got or created: %v", err)) return ctrl.Result{}, nil diff --git a/pkg/cloud/services/loadbalancer/loadbalancer.go b/pkg/cloud/services/loadbalancer/loadbalancer.go index 77333d45ff..f18600efbd 100644 --- a/pkg/cloud/services/loadbalancer/loadbalancer.go +++ b/pkg/cloud/services/loadbalancer/loadbalancer.go @@ -53,7 +53,7 @@ func (s *Service) ReconcileLoadBalancer(openStackCluster *infrav1.OpenStackClust if openStackCluster.Spec.APIServerFloatingIP != "" { floatingIPAddress = openStackCluster.Spec.APIServerFloatingIP } - fp, err := s.networkingService.GetOrCreateFloatingIP(openStackCluster, floatingIPAddress) + fp, err := s.networkingService.GetOrCreateFloatingIP(openStackCluster, clusterName, floatingIPAddress) if err != nil { return err } diff --git a/pkg/cloud/services/networking/floatingip.go b/pkg/cloud/services/networking/floatingip.go index 0bcdf3412f..3613137b83 100644 --- a/pkg/cloud/services/networking/floatingip.go +++ b/pkg/cloud/services/networking/floatingip.go @@ -19,15 +19,17 @@ package networking import ( "time" + "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/attributestags" "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/floatingips" "k8s.io/apimachinery/pkg/util/wait" infrav1 "sigs.k8s.io/cluster-api-provider-openstack/api/v1alpha4" "sigs.k8s.io/cluster-api-provider-openstack/pkg/metrics" "sigs.k8s.io/cluster-api-provider-openstack/pkg/record" + "sigs.k8s.io/cluster-api-provider-openstack/pkg/utils/names" ) -func (s *Service) GetOrCreateFloatingIP(openStackCluster *infrav1.OpenStackCluster, ip string) (*floatingips.FloatingIP, error) { +func (s *Service) GetOrCreateFloatingIP(openStackCluster *infrav1.OpenStackCluster, clusterName, ip string) (*floatingips.FloatingIP, error) { var fp *floatingips.FloatingIP var err error var fpCreateOpts floatingips.CreateOpts @@ -45,6 +47,7 @@ func (s *Service) GetOrCreateFloatingIP(openStackCluster *infrav1.OpenStackClust } fpCreateOpts.FloatingNetworkID = openStackCluster.Status.ExternalNetwork.ID + fpCreateOpts.Description = names.GetDescription(clusterName) mc := metrics.NewMetricPrometheusContext("floating_ip", "create") fp, err = floatingips.Create(s.client, fpCreateOpts).Extract() @@ -53,6 +56,15 @@ func (s *Service) GetOrCreateFloatingIP(openStackCluster *infrav1.OpenStackClust return nil, err } + if len(openStackCluster.Spec.Tags) > 0 { + _, err = attributestags.ReplaceAll(s.client, "floatingips", fp.ID, attributestags.ReplaceAllOpts{ + Tags: openStackCluster.Spec.Tags, + }).Extract() + if err != nil { + return nil, err + } + } + record.Eventf(openStackCluster, "SuccessfulCreateFloatingIP", "Created floating IP %s with id %s", fp.FloatingIP, fp.ID) return fp, nil } From 011ff9cacc73551aaf7b457b3dc467e042bc019a Mon Sep 17 00:00:00 2001 From: Tobias Giese Date: Mon, 5 Jul 2021 12:01:53 +0200 Subject: [PATCH 2/2] add metric and observation call Signed-off-by: Tobias Giese --- pkg/cloud/services/networking/floatingip.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/cloud/services/networking/floatingip.go b/pkg/cloud/services/networking/floatingip.go index 3613137b83..fad4877fb5 100644 --- a/pkg/cloud/services/networking/floatingip.go +++ b/pkg/cloud/services/networking/floatingip.go @@ -57,10 +57,11 @@ func (s *Service) GetOrCreateFloatingIP(openStackCluster *infrav1.OpenStackClust } if len(openStackCluster.Spec.Tags) > 0 { + mc := metrics.NewMetricPrometheusContext("floating_ip", "update") _, err = attributestags.ReplaceAll(s.client, "floatingips", fp.ID, attributestags.ReplaceAllOpts{ Tags: openStackCluster.Spec.Tags, }).Extract() - if err != nil { + if mc.ObserveRequest(err) != nil { return nil, err } }