Skip to content

Commit 5bfb355

Browse files
committed
feat: cleanup DNS on FQDN change
1 parent 6f28048 commit 5bfb355

File tree

1 file changed

+50
-7
lines changed

1 file changed

+50
-7
lines changed

internal/controller/tunnelbinding_controller.go

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,13 @@ type TunnelBindingReconciler struct {
5454

5555
// Custom data for ease of (re)use
5656

57-
ctx context.Context
58-
log logr.Logger
59-
binding *networkingv1alpha1.TunnelBinding
60-
configmap *corev1.ConfigMap
61-
fallbackTarget string
62-
cfAPI *cf.API
57+
ctx context.Context
58+
log logr.Logger
59+
binding *networkingv1alpha1.TunnelBinding
60+
configmap *corev1.ConfigMap
61+
fallbackTarget string
62+
cfAPI *cf.API
63+
removedHostnames []string
6364
}
6465

6566
// labelsForBinding returns the labels for selecting the Bindings served by a Tunnel.
@@ -75,6 +76,7 @@ func labelsForBinding(binding networkingv1alpha1.TunnelBinding) map[string]strin
7576
func (r *TunnelBindingReconciler) initStruct(ctx context.Context, tunnelBinding *networkingv1alpha1.TunnelBinding) error {
7677
r.ctx = ctx
7778
r.binding = tunnelBinding
79+
r.removedHostnames = []string{}
7880

7981
var err error
8082
var namespacedName apitypes.NamespacedName
@@ -195,12 +197,24 @@ func (r *TunnelBindingReconciler) Reconcile(ctx context.Context, req ctrl.Reques
195197
if err := r.creationLogic(); err != nil {
196198
return ctrl.Result{}, err
197199
}
200+
201+
if err := r.cleanupDNSLogic(); err != nil {
202+
return ctrl.Result{}, err
203+
}
204+
198205
return ctrl.Result{}, nil
199206
}
200207

201208
func (r *TunnelBindingReconciler) setStatus() error {
202209
status := make([]networkingv1alpha1.ServiceInfo, 0, len(r.binding.Subjects))
203210
var hostnames string
211+
shouldRemoveHostnames := map[string]bool{}
212+
213+
for _, svc := range r.binding.Status.Services {
214+
// Mark hostname for cleanup
215+
shouldRemoveHostnames[svc.Hostname] = true
216+
}
217+
204218
for _, sub := range r.binding.Subjects {
205219
hostname, target, err := r.getConfigForSubject(sub)
206220
if err != nil {
@@ -210,6 +224,15 @@ func (r *TunnelBindingReconciler) setStatus() error {
210224
}
211225
status = append(status, networkingv1alpha1.ServiceInfo{Hostname: hostname, Target: target})
212226
hostnames += hostname + ","
227+
228+
// Keep / Insert / Update hostname
229+
shouldRemoveHostnames[hostname] = false
230+
}
231+
232+
for hostname, shouldRemove := range shouldRemoveHostnames {
233+
if shouldRemove {
234+
r.removedHostnames = append(r.removedHostnames, hostname)
235+
}
213236
}
214237

215238
r.binding.Status.Services = status
@@ -256,8 +279,28 @@ func (r *TunnelBindingReconciler) deletionLogic() error {
256279
return nil
257280
}
258281

259-
func (r *TunnelBindingReconciler) creationLogic() error {
282+
func (r *TunnelBindingReconciler) cleanupDNSLogic() error {
283+
if r.binding.TunnelRef.DisableDNSUpdates || len(r.removedHostnames) < 1 {
284+
return nil
285+
}
260286

287+
errors := false
288+
var err error
289+
for _, hostname := range r.removedHostnames {
290+
if err = r.deleteDNSLogic(hostname); err != nil {
291+
errors = true
292+
}
293+
}
294+
295+
if errors {
296+
r.Recorder.Event(r.binding, corev1.EventTypeWarning, "FailedDNSCleanupPartial", "Some DNS entries failed to cleanup")
297+
return err
298+
}
299+
300+
return nil
301+
}
302+
303+
func (r *TunnelBindingReconciler) creationLogic() error {
261304
// Add labels for TunnelBinding
262305
if r.binding.Labels == nil {
263306
r.binding.Labels = make(map[string]string)

0 commit comments

Comments
 (0)