Skip to content

Commit 11cca09

Browse files
committed
Delete floating IP if not specified explicitly
Currently floating IP remains after cluster deleted. This commit deletes floating IP when deleting cluster if it is not in the specs. Both Non HA and HA uses common parameters to set openStackCluster.Spec.ControlPlaneEndpoint. - APIServerFloatingIP - APIServerPort
1 parent bc5d11a commit 11cca09

File tree

6 files changed

+59
-35
lines changed

6 files changed

+59
-35
lines changed

api/v1alpha3/openstackcluster_types.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,18 @@ type OpenStackClusterSpec struct {
6868

6969
// ManagedAPIServerLoadBalancer defines whether a LoadBalancer for the
7070
// APIServer should be created. If set to true the following properties are
71-
// mandatory: APIServerLoadBalancerFloatingIP, APIServerLoadBalancerPort
71+
// mandatory: APIServerFloatingIP, APIServerPort
7272
// +optional
7373
ManagedAPIServerLoadBalancer bool `json:"managedAPIServerLoadBalancer"`
7474

75-
// APIServerLoadBalancerFloatingIP is the floatingIP which will be associated
76-
// to the APIServer loadbalancer. The floatingIP will be created if it not
75+
// APIServerFloatingIP is the floatingIP which will be associated
76+
// to the APIServer. The floatingIP will be created if it not
7777
// already exists.
78-
APIServerLoadBalancerFloatingIP string `json:"apiServerLoadBalancerFloatingIP,omitempty"`
78+
APIServerFloatingIP string `json:"apiServerFloatingIP,omitempty"`
7979

80-
// APIServerLoadBalancerPort is the port on which the listener on the APIServer
81-
// loadbalancer will be created
82-
APIServerLoadBalancerPort int `json:"apiServerLoadBalancerPort,omitempty"`
80+
// APIServerPort is the port on which the listener on the APIServer
81+
// will be created
82+
APIServerPort int `json:"apiServerPort,omitempty"`
8383

8484
// APIServerLoadBalancerAdditionalPorts adds additional ports to the APIServerLoadBalancer
8585
APIServerLoadBalancerAdditionalPorts []int `json:"apiServerLoadBalancerAdditionalPorts,omitempty"`

config/crd/bases/infrastructure.cluster.x-k8s.io_openstackclusters.yaml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,20 +64,20 @@ spec:
6464
spec:
6565
description: OpenStackClusterSpec defines the desired state of OpenStackCluster
6666
properties:
67+
apiServerFloatingIP:
68+
description: APIServerFloatingIP is the floatingIP which will be associated
69+
to the APIServer. The floatingIP will be created if it not already
70+
exists.
71+
type: string
6772
apiServerLoadBalancerAdditionalPorts:
6873
description: APIServerLoadBalancerAdditionalPorts adds additional
6974
ports to the APIServerLoadBalancer
7075
items:
7176
type: integer
7277
type: array
73-
apiServerLoadBalancerFloatingIP:
74-
description: APIServerLoadBalancerFloatingIP is the floatingIP which
75-
will be associated to the APIServer loadbalancer. The floatingIP
76-
will be created if it not already exists.
77-
type: string
78-
apiServerLoadBalancerPort:
79-
description: APIServerLoadBalancerPort is the port on which the listener
80-
on the APIServer loadbalancer will be created
78+
apiServerPort:
79+
description: APIServerPort is the port on which the listener on the
80+
APIServer will be created
8181
type: integer
8282
bastion:
8383
description: Bastion is the OpenStack instance to login the nodes
@@ -361,7 +361,7 @@ spec:
361361
managedAPIServerLoadBalancer:
362362
description: 'ManagedAPIServerLoadBalancer defines whether a LoadBalancer
363363
for the APIServer should be created. If set to true the following
364-
properties are mandatory: APIServerLoadBalancerFloatingIP, APIServerLoadBalancerPort'
364+
properties are mandatory: APIServerFloatingIP, APIServerPort'
365365
type: boolean
366366
managedSecurityGroups:
367367
description: 'ManagedSecurityGroups defines that kubernetes manages

controllers/openstackcluster_controller.go

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,12 @@ func (r *OpenStackClusterReconciler) reconcileDelete(ctx context.Context, log lo
134134
return reconcile.Result{}, errors.Errorf("failed to delete bastion: %v", err)
135135
}
136136
r.Recorder.Eventf(openStackCluster, corev1.EventTypeNormal, "SuccessfulDeleteServer", "Deleted server %s with id %s", bastion.Name, bastion.ID)
137+
if openStackCluster.Spec.Bastion.FloatingIP == "" {
138+
if err = networkingService.DeleteFloatingIP(bastion.FloatingIP); err != nil {
139+
return reconcile.Result{}, errors.Errorf("failed to delete floating IP: %v", err)
140+
}
141+
r.Recorder.Eventf(openStackCluster, corev1.EventTypeNormal, "SuccessfulDeleteFloatingIP", "Deleted floating IP %s", bastion.FloatingIP)
142+
}
137143
}
138144

139145
if bastionSecGroup := openStackCluster.Status.BastionSecurityGroup; bastionSecGroup != nil {
@@ -142,7 +148,6 @@ func (r *OpenStackClusterReconciler) reconcileDelete(ctx context.Context, log lo
142148
return reconcile.Result{}, errors.Errorf("failed to delete security group: %v", err)
143149
}
144150
r.Recorder.Eventf(openStackCluster, corev1.EventTypeNormal, "SuccessfulDeleteSecurityGroup", "Deleted security group %s with id %s", bastionSecGroup.Name, bastionSecGroup.ID)
145-
146151
}
147152
}
148153

@@ -158,6 +163,12 @@ func (r *OpenStackClusterReconciler) reconcileDelete(ctx context.Context, log lo
158163
}
159164
r.Recorder.Eventf(openStackCluster, corev1.EventTypeNormal, "SuccessfulDeleteLoadBalancer", "Deleted load balancer %s with id %s", apiLb.Name, apiLb.ID)
160165

166+
if openStackCluster.Spec.APIServerFloatingIP == "" {
167+
if err = networkingService.DeleteFloatingIP(apiLb.IP); err != nil {
168+
return reconcile.Result{}, errors.Errorf("failed to delete floating IP: %v", err)
169+
}
170+
r.Recorder.Eventf(openStackCluster, corev1.EventTypeNormal, "SuccessfulDeleteFloatingIP", "Deleted floating IP %s", apiLb.IP)
171+
}
161172
}
162173
}
163174

@@ -389,26 +400,14 @@ func (r *OpenStackClusterReconciler) reconcileNetworkComponents(log logr.Logger,
389400
return errors.Errorf("failed to reconcile router: %v", err)
390401
}
391402
}
392-
393-
if openStackCluster.Spec.ControlPlaneEndpoint.IsZero() {
394-
var controlPlaneEndpointHost string
403+
if !openStackCluster.Spec.ControlPlaneEndpoint.IsValid() {
395404
var port int32
396-
if openStackCluster.Spec.ManagedAPIServerLoadBalancer {
397-
controlPlaneEndpointHost = openStackCluster.Spec.APIServerLoadBalancerFloatingIP
398-
if openStackCluster.Spec.APIServerLoadBalancerPort == 0 {
399-
port = 6443
400-
} else {
401-
port = int32(openStackCluster.Spec.APIServerLoadBalancerPort)
402-
}
405+
if openStackCluster.Spec.APIServerPort == 0 {
406+
port = 6443
403407
} else {
404-
controlPlaneEndpointHost = openStackCluster.Spec.ControlPlaneEndpoint.Host
405-
if openStackCluster.Spec.ControlPlaneEndpoint.Port == 0 {
406-
port = 6443
407-
} else {
408-
port = openStackCluster.Spec.ControlPlaneEndpoint.Port
409-
}
408+
port = int32(openStackCluster.Spec.APIServerPort)
410409
}
411-
fp, err := networkingService.GetOrCreateFloatingIP(openStackCluster, controlPlaneEndpointHost)
410+
fp, err := networkingService.GetOrCreateFloatingIP(openStackCluster, openStackCluster.Spec.APIServerFloatingIP)
412411
if err != nil {
413412
return errors.Errorf("Floating IP cannot be got or created: %v", err)
414413
}

controllers/openstackmachine_controller.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,11 @@ func (r *OpenStackMachineReconciler) reconcileDelete(ctx context.Context, logger
202202
return ctrl.Result{}, err
203203
}
204204

205+
networkingService, err := networking.NewService(osProviderClient, clientOpts, logger)
206+
if err != nil {
207+
return ctrl.Result{}, err
208+
}
209+
205210
loadBalancerService, err := loadbalancer.NewService(osProviderClient, clientOpts, logger, openStackCluster.Spec.UseOctavia)
206211
if err != nil {
207212
return ctrl.Result{}, err
@@ -236,6 +241,15 @@ func (r *OpenStackMachineReconciler) reconcileDelete(ctx context.Context, logger
236241
logger.Info("OpenStack machine deleted successfully")
237242
r.Recorder.Eventf(openStackMachine, corev1.EventTypeNormal, "SuccessfulDeleteServer", "Deleted server %s with id %s", instance.Name, instance.ID)
238243

244+
if util.IsControlPlaneMachine(machine) && openStackCluster.Spec.APIServerFloatingIP == "" {
245+
if err = networkingService.DeleteFloatingIP(instance.FloatingIP); err != nil {
246+
handleUpdateMachineError(logger, openStackMachine, errors.Errorf("error deleting Openstack floating IP: %v", err))
247+
return ctrl.Result{}, nil
248+
}
249+
logger.Info("OpenStack floating IP deleted successfully", "Floating IP", instance.FloatingIP)
250+
r.Recorder.Eventf(openStackMachine, corev1.EventTypeNormal, "SuccessfulDeleteFloatingIP", "Deleted floating IP %s", instance.FloatingIP)
251+
}
252+
239253
// Instance is deleted so remove the finalizer.
240254
controllerutil.RemoveFinalizer(openStackMachine, infrav1.MachineFinalizer)
241255
logger.Info("Reconciled Machine delete successfully")

docs/configuration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ Note: If your openstack cluster does not already have a public network, you shou
114114

115115
## Floating IP
116116

117-
A floating IP is automatically created and associated with the load balancer or controller node, but you can specify the floating IP explicitly. When `managedAPIServerLoadBalancer: true`, `spec.apiServerLoadBalancerFlotingIP` of `OpenStackCluster` is used. When `managedAPIServerLoadBalancer: false`, `spec.controlPlaneEndpoint.host` of `OpenStackCluster` is used.
117+
A floating IP is automatically created and associated with the load balancer or controller node, but you can specify the floating IP explicitly by `spec.apiServerFloatingIP` of `OpenStackCluster`.
118118

119119
You have to be able to create a floating IP in your OpenStack in advance. You can create one using,
120120

pkg/cloud/services/networking/floatingip.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,14 @@ func checkIfFloatingIPExists(client *gophercloud.ServiceClient, ip string) (*flo
6666
}
6767
return &fpList[0], nil
6868
}
69+
70+
func (s *Service) DeleteFloatingIP(ip string) error {
71+
fip, err := checkIfFloatingIPExists(s.client, ip)
72+
if err != nil {
73+
return err
74+
}
75+
if fip != nil {
76+
return floatingips.Delete(s.client, fip.ID).ExtractErr()
77+
}
78+
return nil
79+
}

0 commit comments

Comments
 (0)