Skip to content

Commit

Permalink
fix: namespace-scoped mode by service_controller (#435)
Browse files Browse the repository at this point in the history
Add ClusterScope condition statement to list namespace that is only
allowed for cluster-scope.

Resolves #433

---------

Signed-off-by: jooho <[email protected]>
  • Loading branch information
Jooho authored Sep 28, 2023
1 parent 1773d7d commit 28ef41b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
30 changes: 16 additions & 14 deletions controllers/service_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
6 changes: 5 additions & 1 deletion controllers/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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
}

0 comments on commit 28ef41b

Please sign in to comment.