Skip to content

Commit c3f782c

Browse files
committed
feat: allow to preserve removed ips
1 parent 5082919 commit c3f782c

2 files changed

Lines changed: 61 additions & 43 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ The following annotations on services of type `LoadBalancer` are supported:
1313
- `linkyard.ch/slb-controller-id`: this service must be processed by a controller with the given id
1414
- `linkyard.ch/existing-floating-ip`: use the already existing floating IP on cloudscale.ch for this
1515
service; note that no additional checks (e.g. port collisions) are performed
16+
- `linkyard.ch/preserve-floating-ip`: don't delete the floating IP on cloudscale.ch when the service is deleted (or otherwise changed in a way that the IP no longer belongs to it). Default is `false`
1617

1718
## configuration
1819

internal/event_processor.go

Lines changed: 60 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
)
1414

1515
const ExistingIpAnnotation = "linkyard.ch/existing-floating-ip"
16+
const PreserveIpAnnotation = "linkyard.ch/preserve-floating-ip"
1617

1718
type EventProcessor struct {
1819
cloudscaleClient CloudscaleClient
@@ -84,56 +85,72 @@ func (processor *EventProcessor) CreateIp(svc *v1.Service) error {
8485
}
8586

8687
func (processor *EventProcessor) DeleteIp(svc *v1.Service, wasDeleted bool) error {
87-
err := processor.deleteIp(svc)
88-
if err != nil {
89-
log.WithFields(log.Fields{
90-
"svc": getKey(svc),
91-
"action": "DeleteIp",
92-
"error": err,
93-
}).Error("unable to delete ip")
88+
preserveIp := svc.Annotations[PreserveIpAnnotation]
89+
if preserveIp == "true" {
90+
// do not delete the ip, because the user has chosen so
91+
log.Infof("load balancer ip for %v/%v (%v) was not deleted, because the preserve annotation has been set", svc.Namespace, svc.Name, getServiceLbIp(svc))
9492
processor.emitEvent(
95-
"Warning",
96-
"FailedToDeleteFloatingIP",
97-
fmt.Sprintf("Failed to delete IP %v: %v", getServiceLbIp(svc), err.Error()),
98-
"delete-ip-failed",
93+
"Normal",
94+
"FloatingIPPreserved",
95+
"FloatingIP preserved (put back to pool)",
96+
"preserved",
9997
getServiceLbIp(svc),
10098
svc)
101-
return err
10299
} else {
103-
log.Infof("successfully deleted ip %v for service %v/%v", getServiceLbIp(svc), svc.Namespace, svc.Name)
104-
if !wasDeleted {
105-
err = processor.updateLoadBalancerIngress(svc, "")
106-
if err != nil {
107-
log.WithFields(log.Fields{
108-
"svc": getKey(svc),
109-
"action": "DeleteIp",
110-
"error": err,
111-
}).Error("unable to update load balancer ip")
112-
processor.emitEvent(
113-
"Warning",
114-
"DeleteLoadBalancerIpFailed",
115-
fmt.Sprintf("Failed to update load balancer IP: %v", err),
116-
"delete-lb-ip-failed",
117-
getServiceLbIp(svc),
118-
svc)
119-
return err
120-
} else {
121-
log.Infof("load balancer ingress for service %v/%v updated successfully", svc.Namespace, svc.Name)
122-
processor.emitEvent(
123-
"Normal",
124-
"FloatingIPDeleted",
125-
"FloatingIP deleted",
126-
"deleted",
127-
getServiceLbIp(svc),
128-
svc)
129-
return nil
130-
}
100+
// delete the ip (from cloudscale)
101+
err := processor.deleteIp(svc)
102+
if err != nil {
103+
log.WithFields(log.Fields{
104+
"svc": getKey(svc),
105+
"action": "DeleteIp",
106+
"error": err,
107+
}).Error("unable to delete ip")
108+
processor.emitEvent(
109+
"Warning",
110+
"FailedToDeleteFloatingIP",
111+
fmt.Sprintf("Failed to delete IP %v: %v", getServiceLbIp(svc), err.Error()),
112+
"delete-ip-failed",
113+
getServiceLbIp(svc),
114+
svc)
115+
return err
116+
} else {
117+
log.Infof("successfully deleted ip %v for service %v/%v", getServiceLbIp(svc), svc.Namespace, svc.Name)
118+
processor.emitEvent(
119+
"Normal",
120+
"FloatingIPDeleted",
121+
"FloatingIP deleted",
122+
"deleted",
123+
getServiceLbIp(svc),
124+
svc)
125+
}
126+
}
127+
if wasDeleted {
128+
//service was deleted, so we don't need to update it
129+
return nil
130+
} else {
131+
// remove the ip from the service
132+
err := processor.updateLoadBalancerIngress(svc, "")
133+
if err != nil {
134+
log.WithFields(log.Fields{
135+
"svc": getKey(svc),
136+
"action": "DeleteIp",
137+
"error": err,
138+
}).Error("unable to update load balancer ip")
139+
processor.emitEvent(
140+
"Warning",
141+
"DeleteLoadBalancerIpFailed",
142+
fmt.Sprintf("Failed to update load balancer IP: %v", err),
143+
"delete-lb-ip-failed",
144+
getServiceLbIp(svc),
145+
svc)
146+
return err
131147
}
148+
log.Infof("load balancer ingress for service %v/%v updated successfully", svc.Namespace, svc.Name)
132149
processor.emitEvent(
133150
"Normal",
134-
"FloatingIPDeleted",
135-
"FloatingIP deleted",
136-
"deleted",
151+
"FloatingIPDetached",
152+
"FloatingIP removed from service",
153+
"detached",
137154
getServiceLbIp(svc),
138155
svc)
139156
return nil

0 commit comments

Comments
 (0)