diff --git a/pkg/constants/constants.go b/pkg/constants/constants.go index 456782591..a24caed98 100644 --- a/pkg/constants/constants.go +++ b/pkg/constants/constants.go @@ -128,6 +128,7 @@ const ( NotificationTag = "Notification" NotificationSecretNamespace = "kubesphere-monitoring-federated" + NotificationManagedLabel = "notification.kubesphere.io/managed" ) var ( diff --git a/pkg/controller/notification/notification_controller.go b/pkg/controller/notification/notification_controller.go index 3877fa999..59e1fc919 100644 --- a/pkg/controller/notification/notification_controller.go +++ b/pkg/controller/notification/notification_controller.go @@ -214,14 +214,11 @@ func (c *Controller) reconcile(obj interface{}) error { // Only reconcile the secret which created by notification manager. if secret, ok := obj.(*corev1.Secret); ok { - if secret.Namespace != constants.NotificationSecretNamespace { + if secret.Namespace != constants.NotificationSecretNamespace || + secret.Labels == nil || secret.Labels[constants.NotificationManagedLabel] != "true" { klog.V(8).Infof("No need to reconcile secret %s/%s", accessor.GetNamespace(), accessor.GetName()) return nil } - - if err := c.ensureNotificationNamespaceExist(); err != nil { - return err - } } name := accessor.GetName() @@ -444,50 +441,6 @@ func (c *Controller) syncFederatedSecret(obj *corev1.Secret) error { return nil } -func (c *Controller) ensureNotificationNamespaceExist() error { - - ns := corev1.Namespace{} - if err := c.Get(context.Background(), client.ObjectKey{Name: constants.NotificationSecretNamespace}, &ns); err != nil { - return err - } - - fedNs := v1beta1.FederatedNamespace{} - if err := c.Get(context.Background(), client.ObjectKey{Name: constants.NotificationSecretNamespace, Namespace: constants.NotificationSecretNamespace}, &fedNs); err != nil { - if errors.IsAlreadyExists(err) { - return nil - } - - if errors.IsNotFound(err) { - fedNs = v1beta1.FederatedNamespace{ - TypeMeta: metav1.TypeMeta{ - Kind: v1beta1.FederatedNamespaceKind, - APIVersion: v1beta1.SchemeGroupVersion.String(), - }, - ObjectMeta: metav1.ObjectMeta{ - Name: constants.NotificationSecretNamespace, - Namespace: constants.NotificationSecretNamespace, - }, - Spec: v1beta1.FederatedNamespaceSpec{ - Placement: v1beta1.GenericPlacementFields{ - ClusterSelector: &metav1.LabelSelector{}, - }, - }, - } - - if err := controllerutil.SetControllerReference(&ns, &fedNs, scheme.Scheme); err != nil { - return err - } - - return c.Create(context.Background(), &fedNs) - } - - return err - } - - return nil - -} - func (c *Controller) ensureNotControlledByKubefed(ctx context.Context, obj runtime.Object) error { accessor, err := meta.Accessor(obj) diff --git a/pkg/controller/notification/notification_controller_test.go b/pkg/controller/notification/notification_controller_test.go index d3d317114..9609c6a35 100644 --- a/pkg/controller/notification/notification_controller_test.go +++ b/pkg/controller/notification/notification_controller_test.go @@ -47,6 +47,9 @@ var ( ObjectMeta: metav1.ObjectMeta{ Name: "foo", Namespace: constants.NotificationSecretNamespace, + Labels: map[string]string{ + constants.NotificationManagedLabel: "true", + }, }, } diff --git a/pkg/models/notification/notification.go b/pkg/models/notification/notification.go index 69bdc3228..70542290a 100644 --- a/pkg/models/notification/notification.go +++ b/pkg/models/notification/notification.go @@ -20,6 +20,10 @@ import ( "reflect" ) +const ( + Secret = "secrets" +) + type Operator interface { List(user, resource, subresource string, query *query.Query) (*api.ListResult, error) Get(user, resource, name, subresource string) (runtime.Object, error) @@ -74,12 +78,17 @@ func (o *operator) List(user, resource, subresource string, q *query.Query) (*ap q.LabelSelector = q.LabelSelector + filter - res, err := o.resourceGetter.List(resource, constants.NotificationSecretNamespace, q) + ns := "" + if resource == Secret { + ns = constants.NotificationSecretNamespace + } + + res, err := o.resourceGetter.List(resource, ns, q) if err != nil { return nil, err } - if subresource == "" || resource == "secrets" { + if subresource == "" || resource == Secret { return res, nil } @@ -98,7 +107,13 @@ func (o *operator) List(user, resource, subresource string, q *query.Query) (*ap // Get the specified object, if you want to get a global object, the user must be nil. // If you want to get a tenant object, the user must equal to the tenant specified in labels of the object. func (o *operator) Get(user, resource, name, subresource string) (runtime.Object, error) { - obj, err := o.resourceGetter.Get(resource, constants.NotificationSecretNamespace, name) + + ns := "" + if resource == Secret { + ns = constants.NotificationSecretNamespace + } + + obj, err := o.resourceGetter.Get(resource, ns, name) if err != nil { return nil, err } @@ -107,7 +122,7 @@ func (o *operator) Get(user, resource, name, subresource string) (runtime.Object return nil, err } - if subresource == "" || resource == "secrets" { + if subresource == "" || resource == Secret { return obj, nil } @@ -198,7 +213,7 @@ func (o *operator) GetObject(resource string) runtime.Object { return &v2beta1.Config{} case v2beta1.ResourcesPluralReceiver: return &v2beta1.Receiver{} - case "secrets": + case Secret: return &corev1.Secret{} default: return nil @@ -290,15 +305,20 @@ func appendLabel(user string, obj runtime.Object) error { labels = make(map[string]string) } - if user == "" { - if isConfig(obj) { - labels["type"] = "default" + switch obj.(type) { + case *corev1.Secret: + labels[constants.NotificationManagedLabel] = "true" + default: + if user == "" { + if isConfig(obj) { + labels["type"] = "default" + } else { + labels["type"] = "global" + } } else { - labels["type"] = "global" + labels["type"] = "tenant" + labels["user"] = user } - } else { - labels["type"] = "tenant" - labels["user"] = user } accessor.SetLabels(labels) diff --git a/pkg/models/notification/notification_test.go b/pkg/models/notification/notification_test.go index a4152b3af..2e66afcf9 100644 --- a/pkg/models/notification/notification_test.go +++ b/pkg/models/notification/notification_test.go @@ -111,7 +111,7 @@ func TestOperator_Create(t *testing.T) { Name: "test", Namespace: constants.NotificationSecretNamespace, Labels: map[string]string{ - "type": "global", + constants.NotificationManagedLabel: "true", }, }, }, @@ -119,7 +119,7 @@ func TestOperator_Create(t *testing.T) { ObjectMeta: metav1.ObjectMeta{ Name: "test", Labels: map[string]string{ - "type": "global", + constants.NotificationManagedLabel: "true", }, }, },