diff --git a/pkg/controller/cluster/cluster_controller.go b/pkg/controller/cluster/cluster_controller.go index c16e1c2f9..86983b232 100644 --- a/pkg/controller/cluster/cluster_controller.go +++ b/pkg/controller/cluster/cluster_controller.go @@ -57,6 +57,7 @@ import ( clusterlister "kubesphere.io/kubesphere/pkg/client/listers/cluster/v1alpha1" iamv1alpha2listers "kubesphere.io/kubesphere/pkg/client/listers/iam/v1alpha2" "kubesphere.io/kubesphere/pkg/constants" + clusterutils "kubesphere.io/kubesphere/pkg/controller/cluster/utils" "kubesphere.io/kubesphere/pkg/simple/client/multicluster" "kubesphere.io/kubesphere/pkg/utils/k8sutil" "kubesphere.io/kubesphere/pkg/version" @@ -352,7 +353,7 @@ func (c *clusterController) syncCluster(key string) error { return err } - if cluster.Annotations[NotificationCleanup] == "true" { + if clusterutils.IsClusterReady(cluster) && cluster.Annotations[NotificationCleanup] == "true" { if err := c.cleanupNotification(cluster); err != nil { klog.Errorf("Failed to cleanup notification config in cluster %s: %v", name, err) return err diff --git a/pkg/controller/cluster/utils/utils.go b/pkg/controller/cluster/utils/utils.go new file mode 100644 index 000000000..1332fef55 --- /dev/null +++ b/pkg/controller/cluster/utils/utils.go @@ -0,0 +1,32 @@ +/* +Copyright 2022 KubeSphere Authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package utils + +import ( + corev1 "k8s.io/api/core/v1" + + clusterv1alpha1 "kubesphere.io/api/cluster/v1alpha1" +) + +func IsClusterReady(cluster *clusterv1alpha1.Cluster) bool { + for _, condition := range cluster.Status.Conditions { + if condition.Type == clusterv1alpha1.ClusterReady && condition.Status == corev1.ConditionTrue { + return true + } + } + return false +} diff --git a/pkg/utils/clusterclient/clusterclient.go b/pkg/utils/clusterclient/clusterclient.go index 23b2063ff..b36a492ac 100644 --- a/pkg/utils/clusterclient/clusterclient.go +++ b/pkg/utils/clusterclient/clusterclient.go @@ -22,7 +22,6 @@ import ( "reflect" "sync" - corev1 "k8s.io/api/core/v1" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" "k8s.io/client-go/tools/cache" @@ -34,6 +33,7 @@ import ( kubesphere "kubesphere.io/kubesphere/pkg/client/clientset/versioned" clusterinformer "kubesphere.io/kubesphere/pkg/client/informers/externalversions/cluster/v1alpha1" clusterlister "kubesphere.io/kubesphere/pkg/client/listers/cluster/v1alpha1" + clusterutils "kubesphere.io/kubesphere/pkg/controller/cluster/utils" ) type innerCluster struct { @@ -172,12 +172,7 @@ func (c *clusterClients) GetInnerCluster(name string) *innerCluster { } func (c *clusterClients) IsClusterReady(cluster *clusterv1alpha1.Cluster) bool { - for _, condition := range cluster.Status.Conditions { - if condition.Type == clusterv1alpha1.ClusterReady && condition.Status == corev1.ConditionTrue { - return true - } - } - return false + return clusterutils.IsClusterReady(cluster) } func (c *clusterClients) IsHostCluster(cluster *clusterv1alpha1.Cluster) bool {