Skip to content

Commit 1816a25

Browse files
committed
feat: add function to delete obsolete service if exists
Signed-off-by: mishraprafful <[email protected]>
1 parent 49c30f8 commit 1816a25

File tree

1 file changed

+49
-6
lines changed

1 file changed

+49
-6
lines changed

components/notebook-controller/controllers/notebook_controller.go

+49-6
Original file line numberDiff line numberDiff line change
@@ -106,18 +106,18 @@ func (r *NotebookReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
106106
return ctrl.Result{}, err
107107
}
108108

109-
// Make sure the prefix doesn't cause the derived resource names to get too long
110-
if len(nbName)+len(namePrefix) > maxNameLength {
111-
return reconcile.Result{},
112-
fmt.Errorf("notebook name must not be longer than %d characters", maxNameLength-len(namePrefix))
113-
}
114-
115109
involvedNotebookKey := types.NamespacedName{Name: nbName, Namespace: req.Namespace}
116110
if err := r.Get(ctx, involvedNotebookKey, involvedNotebook); err != nil {
117111
log.Error(err, "unable to fetch Notebook by looking at event")
118112
return ctrl.Result{}, ignoreNotFound(err)
119113
}
120114

115+
// Make sure the prefix doesn't cause the derived resource names to get too long
116+
if len(nbName)+len(involvedNotebookKey.Namespace)+len(namePrefix) > maxNameLength {
117+
return reconcile.Result{},
118+
fmt.Errorf("notebook name must not be longer than %d characters as notebook resources are prefixed with %s%s", maxNameLength-len(namePrefix), namePrefix, involvedNotebookKey.Namespace)
119+
}
120+
121121
// re-emit the event in the Notebook CR
122122
log.Info("Emitting Notebook Event.", "Event", event)
123123
r.EventRecorder.Eventf(involvedNotebook, event.Type, event.Reason,
@@ -181,6 +181,7 @@ func (r *NotebookReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
181181
if err := ctrl.SetControllerReference(instance, service, r.Scheme); err != nil {
182182
return ctrl.Result{}, err
183183
}
184+
184185
// Check if the Service already exists
185186
foundService := &corev1.Service{}
186187
justCreated = false
@@ -206,6 +207,12 @@ func (r *NotebookReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
206207
return ctrl.Result{}, err
207208
}
208209
}
210+
211+
err = deleteObsoleteService(ctx, r, instance)
212+
if err != nil {
213+
log.Error(err, "unable to delete obsolete Service")
214+
return ctrl.Result{}, err
215+
}
209216

210217
// Reconcile virtual service if we use ISTIO.
211218
if os.Getenv("USE_ISTIO") == "true" {
@@ -619,6 +626,42 @@ func (r *NotebookReconciler) reconcileVirtualService(instance *v1beta1.Notebook)
619626
return nil
620627
}
621628

629+
func deleteObsoleteService(ctx context.Context, r *NotebookReconciler, instance *v1beta1.Notebook) error {
630+
log := r.Log.WithValues("notebook", instance.Namespace)
631+
obsoleteServiceName := instance.Name
632+
obsoleteService := &corev1.Service{}
633+
634+
err := r.Get(ctx, client.ObjectKey{Name: obsoleteServiceName, Namespace: instance.Namespace}, obsoleteService)
635+
if apierrs.IsNotFound(err) {
636+
log.Info("Obsolete Service not found; nothing to delete", "namespace", instance.Namespace, "name", obsoleteServiceName)
637+
return nil
638+
} else if err != nil {
639+
log.Error(err, "error getting obsolete service", "namespace", instance.Namespace, "name", obsoleteServiceName)
640+
return err
641+
}
642+
643+
log.Info("Found obsolete Service", "namespace", obsoleteService.Namespace, "name", obsoleteService.Name)
644+
645+
// Remove owner references
646+
obsoleteService.OwnerReferences = []metav1.OwnerReference{}
647+
err = r.Update(ctx, obsoleteService)
648+
if err != nil {
649+
log.Error(err, "unable to update owner reference for obsolete Service", "namespace", obsoleteService.Namespace, "name", obsoleteService.Name)
650+
return err
651+
}
652+
653+
// Delete the obsolete service
654+
err = r.Delete(ctx, obsoleteService)
655+
if err != nil {
656+
log.Error(err, "unable to delete obsolete Service", "namespace", obsoleteService.Namespace, "name", obsoleteService.Name)
657+
return err
658+
}
659+
660+
log.Info("Deleted obsolete Service", "namespace", obsoleteService.Namespace, "name", obsoleteService.Name)
661+
return nil
662+
663+
}
664+
622665
func isStsOrPodEvent(event *corev1.Event) bool {
623666
return event.InvolvedObject.Kind == "Pod" || event.InvolvedObject.Kind == "StatefulSet"
624667
}

0 commit comments

Comments
 (0)