From 28ef41b65f9cd2b299efed540800fd1646a280bc Mon Sep 17 00:00:00 2001 From: Jooho Lee Date: Thu, 28 Sep 2023 18:40:42 -0400 Subject: [PATCH] fix: namespace-scoped mode by service_controller (#435) Add ClusterScope condition statement to list namespace that is only allowed for cluster-scope. Resolves #433 --------- Signed-off-by: jooho --- controllers/service_controller.go | 30 ++++++++++++++++-------------- controllers/util.go | 6 +++++- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/controllers/service_controller.go b/controllers/service_controller.go index 54e7be56..4514b19d 100644 --- a/controllers/service_controller.go +++ b/controllers/service_controller.go @@ -238,25 +238,27 @@ func (r *ServiceReconciler) reconcileService(ctx context.Context, mms *mmesh.MMS return nil, errors.New("unexpected state - MMService uninitialized"), false } - sl := &corev1.ServiceList{} - if err := r.List(ctx, sl, client.HasLabels{"modelmesh-service"}, client.InNamespace(namespace)); err != nil { - return nil, err, false + if r.ClusterScope { + namespaceObj := &corev1.Namespace{} + // Get the namespace object to check label and state of the namespace + if err := r.Client.Get(ctx, types.NamespacedName{Name: namespace}, namespaceObj); err != nil { + return nil, err, false + } + // This will remove the goroutine when modelmesh is not enabled for a namespace. + // - when the namespace does not have the annotation modelmesh-enabled + // - when the namespace is under a Terminating state. + if !modelMeshEnabled(namespaceObj, r.ControllerDeployment.Namespace) { + r.ModelEventStream.RemoveWatchedService(serviceName, namespace) + r.Log.V(1).Info("Deleted Watched Service", "name", serviceName, "namespace", namespace) + return nil, nil, false + } } - //This will remove the goroutine when modelmesh is not enabled for a namespace. - // - when the namespace does not have the annotation modelmesh-enabled - // - when the namespace is under a Terminating state. - n := &corev1.Namespace{} - if err := r.Client.Get(ctx, types.NamespacedName{Name: namespace}, n); err != nil { + sl := &corev1.ServiceList{} + if err := r.List(ctx, sl, client.HasLabels{"modelmesh-service"}, client.InNamespace(namespace)); err != nil { return nil, err, false } - if !modelMeshEnabled(n, r.ControllerDeployment.Namespace) { - r.ModelEventStream.RemoveWatchedService(serviceName, namespace) - r.Log.Info("Deleted Watched Service", "name", serviceName, "namespace", namespace) - return nil, nil, false - } - var s *corev1.Service for i := range sl.Items { ss := &sl.Items[i] diff --git a/controllers/util.go b/controllers/util.go index e2b0aea5..e0fdabc4 100644 --- a/controllers/util.go +++ b/controllers/util.go @@ -18,6 +18,7 @@ import ( "context" corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/client" ) @@ -42,7 +43,10 @@ func modelMeshEnabled2(ctx context.Context, namespace, controllerNamespace strin } n := &corev1.Namespace{} if err := client.Get(ctx, types.NamespacedName{Name: namespace}, n); err != nil { - return false, err + if errors.IsNotFound(err) { + // If the namespace has already been deleted, it can not be modelmesh namespace + return false, nil + } } return modelMeshEnabled(n, controllerNamespace), nil }