Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ add description and tag to floating ips #925

Merged
merged 2 commits into from
Jul 5, 2021
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
5 changes: 3 additions & 2 deletions controllers/openstackcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion controllers/openstackmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/cloud/services/loadbalancer/loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
15 changes: 14 additions & 1 deletion pkg/cloud/services/networking/floatingip.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -53,6 +56,16 @@ func (s *Service) GetOrCreateFloatingIP(openStackCluster *infrav1.OpenStackClust
return nil, err
}

if len(openStackCluster.Spec.Tags) > 0 {
tobiasgiese marked this conversation as resolved.
Show resolved Hide resolved
mc := metrics.NewMetricPrometheusContext("floating_ip", "update")
_, err = attributestags.ReplaceAll(s.client, "floatingips", fp.ID, attributestags.ReplaceAllOpts{
tobiasgiese marked this conversation as resolved.
Show resolved Hide resolved
Tags: openStackCluster.Spec.Tags,
}).Extract()
if mc.ObserveRequest(err) != nil {
return nil, err
}
}

record.Eventf(openStackCluster, "SuccessfulCreateFloatingIP", "Created floating IP %s with id %s", fp.FloatingIP, fp.ID)
return fp, nil
}
Expand Down