diff --git a/controllers/apps/cluster_controller.go b/controllers/apps/cluster_controller.go index 7a081ecb3b5..d1f8979b4e7 100644 --- a/controllers/apps/cluster_controller.go +++ b/controllers/apps/cluster_controller.go @@ -171,7 +171,7 @@ func (r *ClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct // SetupWithManager sets up the controller with the Manager. func (r *ClusterReconciler) SetupWithManager(mgr ctrl.Manager) error { - return intctrlutil.NewNamespacedControllerManagedBy(mgr). + return intctrlutil.NewControllerManagedBy(mgr, &appsv1.Cluster{}, &appsv1.Component{}). For(&appsv1.Cluster{}). WithOptions(controller.Options{ MaxConcurrentReconciles: int(math.Ceil(viper.GetFloat64(constant.CfgKBReconcileWorkers) / 4)), diff --git a/controllers/apps/clusterdefinition_controller.go b/controllers/apps/clusterdefinition_controller.go index c44b7bdce7c..5ab3d9300f1 100644 --- a/controllers/apps/clusterdefinition_controller.go +++ b/controllers/apps/clusterdefinition_controller.go @@ -96,7 +96,7 @@ func (r *ClusterDefinitionReconciler) Reconcile(ctx context.Context, req ctrl.Re // SetupWithManager sets up the controller with the Manager. func (r *ClusterDefinitionReconciler) SetupWithManager(mgr ctrl.Manager) error { - return intctrlutil.NewNamespacedControllerManagedBy(mgr). + return intctrlutil.NewControllerManagedBy(mgr, &appsv1.ClusterDefinition{}). For(&appsv1.ClusterDefinition{}). Complete(r) } diff --git a/controllers/apps/component_controller.go b/controllers/apps/component_controller.go index ba22615ded8..f74a3a34ef3 100644 --- a/controllers/apps/component_controller.go +++ b/controllers/apps/component_controller.go @@ -196,7 +196,7 @@ func (r *ComponentReconciler) SetupWithManager(mgr ctrl.Manager, multiClusterMgr } func (r *ComponentReconciler) setupWithManager(mgr ctrl.Manager) error { - b := intctrlutil.NewNamespacedControllerManagedBy(mgr). + b := intctrlutil.NewControllerManagedBy(mgr, &appsv1.Component{}, &workloads.InstanceSet{}). For(&appsv1.Component{}). WithOptions(controller.Options{ MaxConcurrentReconciles: viper.GetInt(constant.CfgKBReconcileWorkers), @@ -220,7 +220,7 @@ func (r *ComponentReconciler) setupWithManager(mgr ctrl.Manager) error { } func (r *ComponentReconciler) setupWithMultiClusterManager(mgr ctrl.Manager, multiClusterMgr multicluster.Manager) error { - b := intctrlutil.NewNamespacedControllerManagedBy(mgr). + b := intctrlutil.NewControllerManagedBy(mgr, &appsv1.Component{}, &workloads.InstanceSet{}). For(&appsv1.Component{}). WithOptions(controller.Options{ MaxConcurrentReconciles: viper.GetInt(constant.CfgKBReconcileWorkers), diff --git a/controllers/apps/componentdefinition_controller.go b/controllers/apps/componentdefinition_controller.go index 8cd0bf896d3..ef11b36e005 100644 --- a/controllers/apps/componentdefinition_controller.go +++ b/controllers/apps/componentdefinition_controller.go @@ -86,7 +86,7 @@ func (r *ComponentDefinitionReconciler) Reconcile(ctx context.Context, req ctrl. // SetupWithManager sets up the controller with the Manager. func (r *ComponentDefinitionReconciler) SetupWithManager(mgr ctrl.Manager) error { - return intctrlutil.NewNamespacedControllerManagedBy(mgr). + return intctrlutil.NewControllerManagedBy(mgr, &appsv1.ComponentDefinition{}). For(&appsv1.ComponentDefinition{}). Complete(r) } diff --git a/controllers/apps/componentversion_controller.go b/controllers/apps/componentversion_controller.go index 57a57890a69..eaff1d6c342 100644 --- a/controllers/apps/componentversion_controller.go +++ b/controllers/apps/componentversion_controller.go @@ -91,7 +91,7 @@ func (r *ComponentVersionReconciler) Reconcile(ctx context.Context, req ctrl.Req // SetupWithManager sets up the controller with the Manager. func (r *ComponentVersionReconciler) SetupWithManager(mgr ctrl.Manager) error { - return ctrl.NewControllerManagedBy(mgr). + return intctrlutil.NewControllerManagedBy(mgr, &appsv1.ComponentVersion{}, &appsv1.ComponentDefinition{}). For(&appsv1.ComponentVersion{}). Watches(&appsv1.ComponentDefinition{}, handler.EnqueueRequestsFromMapFunc(r.compatibleCompVersion)). Complete(r) diff --git a/controllers/apps/configuration/configconstraint_controller.go b/controllers/apps/configuration/configconstraint_controller.go index 5ebd5d16065..5fc10fac609 100644 --- a/controllers/apps/configuration/configconstraint_controller.go +++ b/controllers/apps/configuration/configconstraint_controller.go @@ -117,7 +117,7 @@ func (r *ConfigConstraintReconciler) Reconcile(ctx context.Context, req ctrl.Req // SetupWithManager sets up the controller with the Manager. func (r *ConfigConstraintReconciler) SetupWithManager(mgr ctrl.Manager) error { - return intctrlutil.NewNamespacedControllerManagedBy(mgr). + return intctrlutil.NewControllerManagedBy(mgr). For(&appsv1beta1.ConfigConstraint{}). // for other resource Owns(&corev1.ConfigMap{}). // TODO(leon) diff --git a/controllers/apps/configuration/configuration_controller.go b/controllers/apps/configuration/configuration_controller.go index dcfa9b77d8e..3856465284e 100644 --- a/controllers/apps/configuration/configuration_controller.go +++ b/controllers/apps/configuration/configuration_controller.go @@ -189,7 +189,7 @@ func (r *ConfigurationReconciler) runTasks(taskCtx TaskContext, tasks []Task) (e // SetupWithManager sets up the controller with the Manager. func (r *ConfigurationReconciler) SetupWithManager(mgr ctrl.Manager, multiClusterMgr multicluster.Manager) error { - b := intctrlutil.NewNamespacedControllerManagedBy(mgr). + b := intctrlutil.NewControllerManagedBy(mgr). For(&appsv1alpha1.Configuration{}). WithOptions(controller.Options{ MaxConcurrentReconciles: int(math.Ceil(viper.GetFloat64(constant.CfgKBReconcileWorkers) / 2)), diff --git a/controllers/apps/configuration/reconfigure_controller.go b/controllers/apps/configuration/reconfigure_controller.go index 3df54e4e958..6efd1953e27 100644 --- a/controllers/apps/configuration/reconfigure_controller.go +++ b/controllers/apps/configuration/reconfigure_controller.go @@ -136,7 +136,7 @@ func (r *ReconfigureReconciler) Reconcile(ctx context.Context, req ctrl.Request) // SetupWithManager sets up the controller with the Manager. func (r *ReconfigureReconciler) SetupWithManager(mgr ctrl.Manager, multiClusterMgr multicluster.Manager) error { - b := intctrlutil.NewNamespacedControllerManagedBy(mgr). + b := intctrlutil.NewControllerManagedBy(mgr). For(&corev1.ConfigMap{}). WithOptions(controller.Options{ MaxConcurrentReconciles: int(math.Ceil(viper.GetFloat64(constant.CfgKBReconcileWorkers) / 4)), diff --git a/controllers/apps/servicedescriptor_controller.go b/controllers/apps/servicedescriptor_controller.go index 2ae0e054c4c..739147ee5b3 100644 --- a/controllers/apps/servicedescriptor_controller.go +++ b/controllers/apps/servicedescriptor_controller.go @@ -106,7 +106,7 @@ func (r *ServiceDescriptorReconciler) Reconcile(ctx context.Context, req ctrl.Re // SetupWithManager sets up the controller with the Manager. func (r *ServiceDescriptorReconciler) SetupWithManager(mgr ctrl.Manager) error { - return intctrlutil.NewNamespacedControllerManagedBy(mgr). + return intctrlutil.NewControllerManagedBy(mgr). For(&appsv1.ServiceDescriptor{}). Complete(r) } diff --git a/controllers/apps/shardingdefinition_controller.go b/controllers/apps/shardingdefinition_controller.go index c847bf22479..cbf14138dda 100644 --- a/controllers/apps/shardingdefinition_controller.go +++ b/controllers/apps/shardingdefinition_controller.go @@ -105,7 +105,7 @@ func (r *ShardingDefinitionReconciler) Reconcile(ctx context.Context, req ctrl.R // SetupWithManager sets up the controller with the Manager. func (r *ShardingDefinitionReconciler) SetupWithManager(mgr ctrl.Manager) error { - return intctrlutil.NewNamespacedControllerManagedBy(mgr). + return intctrlutil.NewControllerManagedBy(mgr). For(&appsv1.ShardingDefinition{}). Complete(r) } diff --git a/controllers/apps/sidecardefinition_controller.go b/controllers/apps/sidecardefinition_controller.go index 80cb9b57993..37465557e6b 100644 --- a/controllers/apps/sidecardefinition_controller.go +++ b/controllers/apps/sidecardefinition_controller.go @@ -108,7 +108,7 @@ func (r *SidecarDefinitionReconciler) Reconcile(ctx context.Context, req ctrl.Re // SetupWithManager sets up the controller with the Manager. func (r *SidecarDefinitionReconciler) SetupWithManager(mgr ctrl.Manager) error { - return intctrlutil.NewNamespacedControllerManagedBy(mgr). + return intctrlutil.NewControllerManagedBy(mgr). For(&appsv1.SidecarDefinition{}). Watches(&appsv1.ComponentDefinition{}, handler.EnqueueRequestsFromMapFunc(r.matchedCompDefinition)). Complete(r) diff --git a/controllers/apps/transformer_cluster_normalization.go b/controllers/apps/transformer_cluster_normalization.go index 1d643f9c819..b57dd5a8910 100644 --- a/controllers/apps/transformer_cluster_normalization.go +++ b/controllers/apps/transformer_cluster_normalization.go @@ -22,6 +22,7 @@ package apps import ( "fmt" + "golang.org/x/exp/maps" apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/sets" @@ -79,9 +80,7 @@ func (t *clusterNormalizationTransformer) Transform(ctx graph.TransformContext, } // write-back the resolved definitions and service versions to cluster spec. - t.writeBackCompNShardingSpecs(transCtx) - - return nil + return t.writeBackCompNShardingSpecs(transCtx) } func (t *clusterNormalizationTransformer) resolveCompsNShardings(transCtx *clusterTransformContext) ([]*appsv1.ClusterComponentSpec, []*appsv1.ClusterSharding, error) { @@ -393,7 +392,7 @@ func (t *clusterNormalizationTransformer) validateShardingShards(transCtx *clust return nil } -func (t *clusterNormalizationTransformer) writeBackCompNShardingSpecs(transCtx *clusterTransformContext) { +func (t *clusterNormalizationTransformer) writeBackCompNShardingSpecs(transCtx *clusterTransformContext) error { if len(transCtx.components) > 0 { comps := make([]appsv1.ClusterComponentSpec, 0) for i := range transCtx.components { @@ -401,6 +400,7 @@ func (t *clusterNormalizationTransformer) writeBackCompNShardingSpecs(transCtx * } transCtx.Cluster.Spec.ComponentSpecs = comps } + if len(transCtx.shardings) > 0 { shardings := make([]appsv1.ClusterSharding, 0) for i := range transCtx.shardings { @@ -408,4 +408,49 @@ func (t *clusterNormalizationTransformer) writeBackCompNShardingSpecs(transCtx * } transCtx.Cluster.Spec.Shardings = shardings } + + return t.patchCRDAPIVersionKey(transCtx) +} + +func (t *clusterNormalizationTransformer) patchCRDAPIVersionKey(transCtx *clusterTransformContext) error { + apiVersions := map[string][]string{} + + add := func(k, v string) { + if _, ok := apiVersions[k]; !ok { + apiVersions[k] = []string{} + } + apiVersions[k] = append(apiVersions[k], v) + } + + from := func(name string, annotations map[string]string) { + if annotations == nil { + add("", name) + } else { + add(annotations[constant.CRDAPIVersionAnnotationKey], name) + } + } + + if transCtx.clusterDef != nil { + from(transCtx.clusterDef.Name, transCtx.clusterDef.Annotations) + } else { + for _, compDef := range transCtx.componentDefs { + from(compDef.Name, compDef.Annotations) + } + for _, shardingDef := range transCtx.shardingDefs { + from(shardingDef.Name, shardingDef.Annotations) + } + } + + switch len(apiVersions) { + case 0: + return nil + case 1: + if transCtx.Cluster.Annotations == nil { + transCtx.Cluster.Annotations = make(map[string]string) + } + transCtx.Cluster.Annotations[constant.CRDAPIVersionAnnotationKey] = maps.Keys(apiVersions)[0] + return nil + default: + return fmt.Errorf("multiple CRD API versions found: %v", apiVersions) + } } diff --git a/controllers/dataprotection/actionset_controller.go b/controllers/dataprotection/actionset_controller.go index 787977e47c0..0af71c4daac 100644 --- a/controllers/dataprotection/actionset_controller.go +++ b/controllers/dataprotection/actionset_controller.go @@ -92,7 +92,7 @@ func (r *ActionSetReconciler) Reconcile(ctx context.Context, req ctrl.Request) ( // SetupWithManager sets up the controller with the Manager. func (r *ActionSetReconciler) SetupWithManager(mgr ctrl.Manager) error { - return intctrlutil.NewNamespacedControllerManagedBy(mgr). + return intctrlutil.NewControllerManagedBy(mgr). For(&dpv1alpha1.ActionSet{}). Complete(r) } diff --git a/controllers/dataprotection/backup_controller.go b/controllers/dataprotection/backup_controller.go index 3595876b8e0..31d78742043 100644 --- a/controllers/dataprotection/backup_controller.go +++ b/controllers/dataprotection/backup_controller.go @@ -141,7 +141,7 @@ func (r *BackupReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr // SetupWithManager sets up the controller with the Manager. func (r *BackupReconciler) SetupWithManager(mgr ctrl.Manager) error { - b := intctrlutil.NewNamespacedControllerManagedBy(mgr). + b := intctrlutil.NewControllerManagedBy(mgr). For(&dpv1alpha1.Backup{}). WithOptions(controller.Options{ MaxConcurrentReconciles: viper.GetInt(dptypes.CfgDataProtectionReconcileWorkers), diff --git a/controllers/dataprotection/backuppolicy_controller.go b/controllers/dataprotection/backuppolicy_controller.go index bd0bcff3f02..ec93b3721fc 100644 --- a/controllers/dataprotection/backuppolicy_controller.go +++ b/controllers/dataprotection/backuppolicy_controller.go @@ -124,7 +124,7 @@ func (r *BackupPolicyReconciler) validateBackupPolicy(backupPolicy *dpv1alpha1.B // SetupWithManager sets up the controller with the Manager. func (r *BackupPolicyReconciler) SetupWithManager(mgr ctrl.Manager) error { - return intctrlutil.NewNamespacedControllerManagedBy(mgr). + return intctrlutil.NewControllerManagedBy(mgr). For(&dpv1alpha1.BackupPolicy{}). Complete(r) } diff --git a/controllers/dataprotection/backuppolicytemplate_controller.go b/controllers/dataprotection/backuppolicytemplate_controller.go index 40d46cedcc4..3c7f8a1da94 100644 --- a/controllers/dataprotection/backuppolicytemplate_controller.go +++ b/controllers/dataprotection/backuppolicytemplate_controller.go @@ -174,7 +174,7 @@ func (r *BackupPolicyTemplateReconciler) compatibleBackupPolicyTemplate(ctx cont // SetupWithManager sets up the controller with the Manager. func (r *BackupPolicyTemplateReconciler) SetupWithManager(mgr ctrl.Manager) error { - return intctrlutil.NewNamespacedControllerManagedBy(mgr). + return intctrlutil.NewControllerManagedBy(mgr). For(&dpv1alpha1.BackupPolicyTemplate{}). Watches(&appsv1.ComponentDefinition{}, handler.EnqueueRequestsFromMapFunc(r.compatibleBackupPolicyTemplate)). Complete(r) diff --git a/controllers/dataprotection/backuprepo_controller.go b/controllers/dataprotection/backuprepo_controller.go index 08d54f13aa5..db168841b35 100644 --- a/controllers/dataprotection/backuprepo_controller.go +++ b/controllers/dataprotection/backuprepo_controller.go @@ -1497,7 +1497,7 @@ func (r *BackupRepoReconciler) SetupWithManager(mgr ctrl.Manager) error { }); err != nil { return err } - b := intctrlutil.NewNamespacedControllerManagedBy(mgr). + b := intctrlutil.NewControllerManagedBy(mgr). For(&dpv1alpha1.BackupRepo{}). Watches(&dpv1alpha1.StorageProvider{}, handler.EnqueueRequestsFromMapFunc(r.mapProviderToRepos)). Watches(&dpv1alpha1.Backup{}, handler.EnqueueRequestsFromMapFunc(r.mapBackupToRepo)). diff --git a/controllers/dataprotection/backupschedule_controller.go b/controllers/dataprotection/backupschedule_controller.go index 46bacf489d4..aaeb7ebfc71 100644 --- a/controllers/dataprotection/backupschedule_controller.go +++ b/controllers/dataprotection/backupschedule_controller.go @@ -96,7 +96,7 @@ func (r *BackupScheduleReconciler) Reconcile(ctx context.Context, req ctrl.Reque // SetupWithManager sets up the controller with the Manager. func (r *BackupScheduleReconciler) SetupWithManager(mgr ctrl.Manager) error { - b := intctrlutil.NewNamespacedControllerManagedBy(mgr). + b := intctrlutil.NewControllerManagedBy(mgr). For(&dpv1alpha1.BackupSchedule{}) // Compatible with kubernetes versions prior to K8s 1.21, only supports batch v1beta1. diff --git a/controllers/dataprotection/gc_controller.go b/controllers/dataprotection/gc_controller.go index 72dafe8c070..153b52f5217 100644 --- a/controllers/dataprotection/gc_controller.go +++ b/controllers/dataprotection/gc_controller.go @@ -62,7 +62,7 @@ func NewGCReconciler(mgr ctrl.Manager) *GCReconciler { // taken care of. Other events will be filtered to decrease the load on the controller. func (r *GCReconciler) SetupWithManager(mgr ctrl.Manager) error { s := dputils.NewPeriodicalEnqueueSource(mgr.GetClient(), &dpv1alpha1.BackupList{}, r.frequency, dputils.PeriodicalEnqueueSourceOption{}) - return intctrlutil.NewNamespacedControllerManagedBy(mgr). + return intctrlutil.NewControllerManagedBy(mgr). For(&dpv1alpha1.Backup{}, builder.WithPredicates(predicate.NewPredicateFuncs(func(client.Object) bool { return false }))). WatchesRawSource(s, nil). Complete(r) diff --git a/controllers/dataprotection/log_collection_controller.go b/controllers/dataprotection/log_collection_controller.go index a3726012ad5..5f89c407ceb 100644 --- a/controllers/dataprotection/log_collection_controller.go +++ b/controllers/dataprotection/log_collection_controller.go @@ -101,7 +101,7 @@ func (r *LogCollectionReconciler) Reconcile(ctx context.Context, req ctrl.Reques // SetupWithManager sets up the controller with the Manager. func (r *LogCollectionReconciler) SetupWithManager(mgr ctrl.Manager) error { - return intctrlutil.NewNamespacedControllerManagedBy(mgr). + return intctrlutil.NewControllerManagedBy(mgr). For(&batchv1.Job{}, builder.WithPredicates( failedJobUpdatePredicate{ Funcs: predicate.NewPredicateFuncs(func(object client.Object) bool { return false }), diff --git a/controllers/dataprotection/restore_controller.go b/controllers/dataprotection/restore_controller.go index 16842c989e5..c9c54cac98d 100644 --- a/controllers/dataprotection/restore_controller.go +++ b/controllers/dataprotection/restore_controller.go @@ -102,7 +102,7 @@ func (r *RestoreReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct // SetupWithManager sets up the controller with the Manager. func (r *RestoreReconciler) SetupWithManager(mgr ctrl.Manager) error { - return intctrlutil.NewNamespacedControllerManagedBy(mgr). + return intctrlutil.NewControllerManagedBy(mgr). For(&dpv1alpha1.Restore{}). Owns(&batchv1.Job{}). Watches(&batchv1.Job{}, handler.EnqueueRequestsFromMapFunc(r.parseRestoreJob)). diff --git a/controllers/dataprotection/storageprovider_controller.go b/controllers/dataprotection/storageprovider_controller.go index 9549efd6f6c..03c31fa5fc9 100644 --- a/controllers/dataprotection/storageprovider_controller.go +++ b/controllers/dataprotection/storageprovider_controller.go @@ -219,7 +219,7 @@ func (r *StorageProviderReconciler) deleteExternalResources( // SetupWithManager sets up the controller with the Manager. func (r *StorageProviderReconciler) SetupWithManager(mgr ctrl.Manager) error { - b := intctrlutil.NewNamespacedControllerManagedBy(mgr). + b := intctrlutil.NewControllerManagedBy(mgr). For(&dpv1alpha1.StorageProvider{}) mapCSIDriverToProvider := handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, object client.Object) []reconcile.Request { diff --git a/controllers/dataprotection/volumepopulator_controller.go b/controllers/dataprotection/volumepopulator_controller.go index f95be89d707..be41bea4c4c 100644 --- a/controllers/dataprotection/volumepopulator_controller.go +++ b/controllers/dataprotection/volumepopulator_controller.go @@ -99,7 +99,7 @@ func (r *VolumePopulatorReconciler) Reconcile(ctx context.Context, req ctrl.Requ // SetupWithManager sets up the controller with the Manager. func (r *VolumePopulatorReconciler) SetupWithManager(mgr ctrl.Manager) error { - return intctrlutil.NewNamespacedControllerManagedBy(mgr). + return intctrlutil.NewControllerManagedBy(mgr). For(&corev1.PersistentVolumeClaim{}). Owns(&batchv1.Job{}). Complete(r) diff --git a/controllers/experimental/nodecountscaler_controller.go b/controllers/experimental/nodecountscaler_controller.go index dafa1b4d6ae..f20e534a4c4 100644 --- a/controllers/experimental/nodecountscaler_controller.go +++ b/controllers/experimental/nodecountscaler_controller.go @@ -32,6 +32,7 @@ import ( appsv1 "github.com/apecloud/kubeblocks/apis/apps/v1" experimental "github.com/apecloud/kubeblocks/apis/experimental/v1alpha1" "github.com/apecloud/kubeblocks/pkg/controller/kubebuilderx" + intctrlutil "github.com/apecloud/kubeblocks/pkg/controllerutil" ) // NodeCountScalerReconciler reconciles a NodeCountScaler object @@ -69,7 +70,7 @@ func (r *NodeCountScalerReconciler) Reconcile(ctx context.Context, req ctrl.Requ // SetupWithManager sets up the controller with the Manager. func (r *NodeCountScalerReconciler) SetupWithManager(mgr ctrl.Manager) error { - return ctrl.NewControllerManagedBy(mgr). + return intctrlutil.NewControllerManagedBy(mgr). For(&experimental.NodeCountScaler{}). Watches(&corev1.Node{}, &nodeScalingHandler{r.Client}). Watches(&appsv1.Cluster{}, &clusterHandler{r.Client}). diff --git a/controllers/extensions/addon_controller.go b/controllers/extensions/addon_controller.go index d84ee588dbd..a656e7b8805 100644 --- a/controllers/extensions/addon_controller.go +++ b/controllers/extensions/addon_controller.go @@ -153,7 +153,7 @@ func (r *AddonReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl // SetupWithManager sets up the controller with the Manager. func (r *AddonReconciler) SetupWithManager(mgr ctrl.Manager) error { - return intctrlutil.NewNamespacedControllerManagedBy(mgr). + return intctrlutil.NewControllerManagedBy(mgr). For(&extensionsv1alpha1.Addon{}). Watches(&batchv1.Job{}, handler.EnqueueRequestsFromMapFunc(r.findAddonJobs)). WithOptions(controller.Options{ diff --git a/controllers/k8score/event_controller.go b/controllers/k8score/event_controller.go index 42a4e1954e4..5bf2a295dda 100644 --- a/controllers/k8score/event_controller.go +++ b/controllers/k8score/event_controller.go @@ -98,7 +98,7 @@ func (r *EventReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl // SetupWithManager sets up the controller with the Manager. func (r *EventReconciler) SetupWithManager(mgr ctrl.Manager, multiClusterMgr multicluster.Manager) error { - b := intctrlutil.NewNamespacedControllerManagedBy(mgr). + b := intctrlutil.NewControllerManagedBy(mgr). For(&corev1.Event{}) if multiClusterMgr != nil { diff --git a/controllers/operations/opsdefinition_controller.go b/controllers/operations/opsdefinition_controller.go index 2bf9bfbf8c9..42b0f17bf38 100644 --- a/controllers/operations/opsdefinition_controller.go +++ b/controllers/operations/opsdefinition_controller.go @@ -102,7 +102,7 @@ func (r *OpsDefinitionReconciler) updateStatusUnavailable(reqCtx intctrlutil.Req // SetupWithManager sets up the controller with the Manager. func (r *OpsDefinitionReconciler) SetupWithManager(mgr ctrl.Manager) error { - return intctrlutil.NewNamespacedControllerManagedBy(mgr). + return intctrlutil.NewControllerManagedBy(mgr). For(&opsv1alpha1.OpsDefinition{}). Complete(r) } diff --git a/controllers/operations/opsrequest_controller.go b/controllers/operations/opsrequest_controller.go index be4ec744e45..f54caa9839e 100644 --- a/controllers/operations/opsrequest_controller.go +++ b/controllers/operations/opsrequest_controller.go @@ -90,7 +90,7 @@ func (r *OpsRequestReconciler) Reconcile(ctx context.Context, req ctrl.Request) // SetupWithManager sets up the controller with the Manager. func (r *OpsRequestReconciler) SetupWithManager(mgr ctrl.Manager) error { - return intctrlutil.NewNamespacedControllerManagedBy(mgr). + return intctrlutil.NewControllerManagedBy(mgr). For(&opsv1alpha1.OpsRequest{}). WithOptions(controller.Options{ MaxConcurrentReconciles: int(math.Ceil(viper.GetFloat64(constant.CfgKBReconcileWorkers) / 2)), diff --git a/controllers/workloads/instanceset_controller.go b/controllers/workloads/instanceset_controller.go index c723e61245a..72463950d64 100644 --- a/controllers/workloads/instanceset_controller.go +++ b/controllers/workloads/instanceset_controller.go @@ -112,7 +112,7 @@ func (r *InstanceSetReconciler) SetupWithManager(mgr ctrl.Manager, multiClusterM func (r *InstanceSetReconciler) setupWithManager(mgr ctrl.Manager, ctx *handler.FinderContext) error { itsFinder := handler.NewLabelFinder(&workloads.InstanceSet{}, instanceset.WorkloadsManagedByLabelKey, workloads.Kind, instanceset.WorkloadsInstanceLabelKey) podHandler := handler.NewBuilder(ctx).AddFinder(itsFinder).Build() - return intctrlutil.NewNamespacedControllerManagedBy(mgr). + return intctrlutil.NewControllerManagedBy(mgr, &workloads.InstanceSet{}). For(&workloads.InstanceSet{}). WithOptions(controller.Options{ MaxConcurrentReconciles: viper.GetInt(constant.CfgKBReconcileWorkers), @@ -132,7 +132,7 @@ func (r *InstanceSetReconciler) setupWithMultiClusterManager(mgr ctrl.Manager, // TODO: modify handler.getObjectFromKey to support running Job in data clusters jobHandler := handler.NewBuilder(ctx).AddFinder(delegatorFinder).Build() - b := intctrlutil.NewNamespacedControllerManagedBy(mgr). + b := intctrlutil.NewControllerManagedBy(mgr, &workloads.InstanceSet{}). For(&workloads.InstanceSet{}). WithOptions(controller.Options{ MaxConcurrentReconciles: viper.GetInt(constant.CfgKBReconcileWorkers), diff --git a/pkg/constant/annotations.go b/pkg/constant/annotations.go index 17f73be3c34..a9b0d41c674 100644 --- a/pkg/constant/annotations.go +++ b/pkg/constant/annotations.go @@ -21,6 +21,9 @@ package constant // annotations defined by KubeBlocks const ( + // CRDAPIVersionAnnotationKey indicates the CRD API version of the object. + CRDAPIVersionAnnotationKey = "kubeblocks.io/crd-api-version" + ClusterSnapshotAnnotationKey = "kubeblocks.io/cluster-snapshot" // ClusterSnapshotAnnotationKey saves the snapshot of cluster. EncryptedSystemAccountsAnnotationKey = "kubeblocks.io/encrypted-system-accounts" // EncryptedSystemAccountsAnnotationKey saves the encrypted system accounts. OpsRequestAnnotationKey = "kubeblocks.io/ops-request" // OpsRequestAnnotationKey OpsRequest annotation key in Cluster diff --git a/pkg/controller/component/component.go b/pkg/controller/component/component.go index 43d2400990f..9678d516481 100644 --- a/pkg/controller/component/component.go +++ b/pkg/controller/component/component.go @@ -62,6 +62,7 @@ func BuildComponent(cluster *appsv1.Cluster, compSpec *appsv1.ClusterComponentSp } compBuilder := builder.NewComponentBuilder(cluster.Namespace, FullName(cluster.Name, compSpec.Name), compSpec.ComponentDef). AddAnnotations(constant.KubeBlocksGenerationKey, strconv.FormatInt(cluster.Generation, 10)). + AddAnnotations(constant.CRDAPIVersionAnnotationKey, appsv1.GroupVersion.String()). AddAnnotations(constant.KBAppClusterUIDKey, string(cluster.UID)). AddAnnotationsInMap(inheritedAnnotations(cluster)). AddAnnotationsInMap(annotations). // annotations added by the cluster controller diff --git a/pkg/controller/factory/builder.go b/pkg/controller/factory/builder.go index 424a630c438..61b4d4fea46 100644 --- a/pkg/controller/factory/builder.go +++ b/pkg/controller/factory/builder.go @@ -66,6 +66,7 @@ func BuildInstanceSet(synthesizedComp *component.SynthesizedComponent, component AddLabelsInMap(constant.GetCompLabels(clusterName, compName)). AddLabelsInMap(synthesizedComp.StaticLabels). AddAnnotations(constant.KubeBlocksGenerationKey, synthesizedComp.Generation). + AddAnnotations(constant.CRDAPIVersionAnnotationKey, workloads.GroupVersion.String()). AddAnnotationsInMap(map[string]string{ constant.AppComponentLabelKey: compDefName, constant.KBAppServiceVersionKey: synthesizedComp.ServiceVersion, diff --git a/pkg/controllerutil/predicate.go b/pkg/controllerutil/predicate.go index 360e86e27ce..adfada3d2d8 100644 --- a/pkg/controllerutil/predicate.go +++ b/pkg/controllerutil/predicate.go @@ -20,6 +20,7 @@ along with this program. If not, see . package controllerutil import ( + "reflect" "strings" "k8s.io/apimachinery/pkg/util/sets" @@ -29,17 +30,88 @@ import ( "sigs.k8s.io/controller-runtime/pkg/manager" "sigs.k8s.io/controller-runtime/pkg/predicate" + appsv1 "github.com/apecloud/kubeblocks/apis/apps/v1" + workloadsv1 "github.com/apecloud/kubeblocks/apis/workloads/v1" "github.com/apecloud/kubeblocks/pkg/constant" viper "github.com/apecloud/kubeblocks/pkg/viperx" ) var ( - managedNamespaces *sets.Set[string] + // pkg reconciler resource sub-resources operation + // experimentalv1alpha1 NodeCountScalerReconciler NodeCountScaler corev1.Node w + // appsv1alpha1.Cluster w + // extensionsv1alpha1 AddonReconciler Addon batchv1.Job w + // corev1 EventReconciler Event + // workloadsv1alpha1 InstanceSetReconciler InstanceSet corev1.Pod w + // corev1.PersistentVolumeClaim o + // batchv1.Job o + // corev1.Service o + // corev1.ConfigMap o + // appsv1beta1 ConfigConstraintReconciler ConfigConstraint corev1.ConfigMap o + // appsv1alpha1 OpsRequestReconciler OpsRequest appsv1alpha1.Cluster w + // workloadsv1alpha1.InstanceSet w + // dpv1alpha1.Backup w + // corev1.PersistentVolumeClaim w + // corev1.Pod w + // batchv1.Job o + // dpv1alpha1.Restore o + // ReconfigureReconciler corev1.ConfigMap + // ConfigurationReconciler Configuration corev1.ConfigMap o + // ClusterReconciler Cluster appsv1alpha1.Component o + // corev1.Service o + // corev1.Secret o + // dpv1alpha1.BackupPolicy o + // dpv1alpha1.BackupSchedule o + // SystemAccountReconciler Cluster corev1.Secret o + // batchv1.Job w + // ComponentReconciler Component workloads.InstanceSet o + // corev1.Service o + // corev1.Secret o + // corev1.ConfigMap o + // dpv1alpha1.Backup o + // dpv1alpha1.Restore o + // corev1.PersistentVolumeClaim w + // batchv1.Job o + // appsv1alpha1.Configuration w + // rbacv1.ClusterRoleBinding o/w + // rbacv1.RoleBinding o/w + // corev1.ServiceAccount o/w + // BackupPolicyTemplateReconciler BackupPolicyTemplate appsv1alpha1.ComponentDefinition w + // ComponentClassReconciler ComponentClassDefinition + // ClusterVersionReconciler ClusterVersion + // ServiceDescriptorReconciler ServiceDescriptor + // ClusterDefinitionReconciler ClusterDefinition + // OpsDefinitionReconciler OpsDefinition + // ComponentDefinitionReconciler ComponentDefinition + // ComponentVersionReconciler ComponentVersion appsv1alpha1.ComponentDefinition w + // + // has new version: - filter by api version label/annotation + // addon: ClusterDefinition, ComponentDefinition, ComponentVersion, BackupPolicyTemplate + // user:ServiceDescriptor, Cluster + // controller: Component, InstanceSet + // unchanged:NodeCountScaler, Addon - the new operator will be responsible for these + // deleted:ClusterVersion, ComponentClassDefinition - nothing to do + // group changed:OpsRequest, OpsDefinition, ConfigConstraint, Configuration - nothing to do + // TODO: + // EventReconciler.Event + + managedNamespaces *sets.Set[string] + supportedCRDAPIVersions = sets.New[string]( + // ClusterDefinition, ComponentDefinition, ComponentVersion, Cluster, Component + appsv1.GroupVersion.String(), + // InstanceSet + workloadsv1.GroupVersion.String(), + // TODO: corev1.Event + ) ) -func NewNamespacedControllerManagedBy(mgr manager.Manager) *builder.Builder { - return ctrl.NewControllerManagedBy(mgr). +func NewControllerManagedBy(mgr manager.Manager, objs ...client.Object) *builder.Builder { + b := ctrl.NewControllerManagedBy(mgr). WithEventFilter(predicate.NewPredicateFuncs(namespacePredicateFilter)) + if len(objs) > 0 { + b.WithEventFilter(predicate.NewPredicateFuncs(newAPIVersionPredicateFilter(objs))) + } + return b } func namespacePredicateFilter(object client.Object) bool { @@ -56,3 +128,25 @@ func namespacePredicateFilter(object client.Object) bool { } return managedNamespaces.Has(object.GetNamespace()) } + +func newAPIVersionPredicateFilter(objs []client.Object) func(client.Object) bool { + return func(obj client.Object) bool { + annotations := obj.GetAnnotations() + if annotations != nil { + apiVersion, ok := annotations[constant.CRDAPIVersionAnnotationKey] + if ok { + return supportedCRDAPIVersions.Has(apiVersion) + } + } + switch reflect.TypeOf(obj) { + case reflect.TypeOf(&appsv1.Cluster{}): + return true + case reflect.TypeOf(&appsv1.ClusterDefinition{}), reflect.TypeOf(&appsv1.ComponentDefinition{}), + reflect.TypeOf(&appsv1.ComponentVersion{}), reflect.TypeOf(&appsv1.Component{}), + reflect.TypeOf(&workloadsv1.InstanceSet{}): + return false + default: + return true + } + } +}