提交 8e28925d 编写于 作者: W wanjunlei

change kubesphere-monitoring-federated to system namespace

Signed-off-by: Nwanjunlei <wanjunlei@yunify.com>
上级 3a79df0d
......@@ -128,6 +128,7 @@ const (
NotificationTag = "Notification"
NotificationSecretNamespace = "kubesphere-monitoring-federated"
NotificationManagedLabel = "notification/managed"
)
var (
......
......@@ -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)
......
......@@ -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)
......
......@@ -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",
},
},
},
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册