diff --git a/manager/Containerfile.operator b/manager/Containerfile.operator index 98479208d..4510515b1 100644 --- a/manager/Containerfile.operator +++ b/manager/Containerfile.operator @@ -11,7 +11,7 @@ COPY ./manager/ ./manager/ COPY ./operator/api ./operator/api COPY ./pkg/ ./pkg/ -RUN CGO_ENABLED=1 GOFLAGS="-p=4" go build -tags strictfipsruntime -a -v -o bin/manager ./manager/cmd/manager/main.go +RUN CGO_ENABLED=1 GOFLAGS="-p=4" go build -tags strictfipsruntime -a -v -o bin/manager ./manager/cmd/main.go # Stage 2: Copy the binaries from the image builder to the base image FROM registry.access.redhat.com/ubi9/ubi-minimal:latest @@ -30,22 +30,22 @@ ARG IMAGE_SUMMARY ARG IMAGE_OPENSHIFT_TAGS LABEL org.label-schema.vendor="Red Hat" \ - org.label-schema.name="$IMAGE_NAME_ARCH" \ - org.label-schema.description="$IMAGE_DESCRIPTION" \ - org.label-schema.vcs-ref=$VCS_REF \ - org.label-schema.vcs-url=$VCS_URL \ - org.label-schema.license="Red Hat Advanced Cluster Management for Kubernetes EULA" \ - org.label-schema.schema-version="1.0" \ - name="$IMAGE_NAME" \ - maintainer="$IMAGE_MAINTAINER" \ - vendor="$IMAGE_VENDOR" \ - version="$IMAGE_VERSION" \ - release="$IMAGE_RELEASE" \ - description="$IMAGE_DESCRIPTION" \ - summary="$IMAGE_SUMMARY" \ - io.k8s.display-name="$IMAGE_DISPLAY_NAME" \ - io.k8s.description="$IMAGE_DESCRIPTION" \ - io.openshift.tags="$IMAGE_OPENSHIFT_TAGS" + org.label-schema.name="$IMAGE_NAME_ARCH" \ + org.label-schema.description="$IMAGE_DESCRIPTION" \ + org.label-schema.vcs-ref=$VCS_REF \ + org.label-schema.vcs-url=$VCS_URL \ + org.label-schema.license="Red Hat Advanced Cluster Management for Kubernetes EULA" \ + org.label-schema.schema-version="1.0" \ + name="$IMAGE_NAME" \ + maintainer="$IMAGE_MAINTAINER" \ + vendor="$IMAGE_VENDOR" \ + version="$IMAGE_VERSION" \ + release="$IMAGE_RELEASE" \ + description="$IMAGE_DESCRIPTION" \ + summary="$IMAGE_SUMMARY" \ + io.k8s.display-name="$IMAGE_DISPLAY_NAME" \ + io.k8s.description="$IMAGE_DESCRIPTION" \ + io.openshift.tags="$IMAGE_OPENSHIFT_TAGS" ENV USER_UID=1001 ENV USER_NAME=manager @@ -54,10 +54,9 @@ ENV USER_NAME=manager COPY --from=builder /workspace/bin/manager /usr/local/bin/manager COPY ./manager/scripts/user_setup /usr/local/scripts/user_setup -RUN /usr/local/scripts/user_setup +RUN /usr/local/scripts/user_setup -RUN microdnf update -y && \ - microdnf clean all +RUN microdnf update -y && microdnf clean all USER ${USER_UID} -ENTRYPOINT ["/usr/local/bin/manager"] \ No newline at end of file +ENTRYPOINT ["/usr/local/bin/manager"] diff --git a/manager/Dockerfile b/manager/Dockerfile index 396a25c88..dc742b0a3 100644 --- a/manager/Dockerfile +++ b/manager/Dockerfile @@ -10,7 +10,7 @@ COPY ./manager/ ./manager/ COPY ./operator/api ./operator/api COPY ./pkg/ ./pkg/ -RUN go build -o bin/manager ./manager/cmd/manager/main.go +RUN go build -o bin/manager ./manager/cmd/main.go # Stage 2: Copy the binaries from the image builder to the base image FROM registry.access.redhat.com/ubi9/ubi-minimal:latest diff --git a/manager/Makefile b/manager/Makefile index 0bf4ef25c..d02c0ec12 100644 --- a/manager/Makefile +++ b/manager/Makefile @@ -36,7 +36,7 @@ manifests: controller-gen .PHONY: build ##builds the binary build: - @go build -o bin/manager ./cmd/manager/main.go + @go build -o bin/manager ./cmd/main.go .PHONY: clean ##cleans the build directories clean: diff --git a/manager/README.md b/manager/README.md new file mode 100644 index 000000000..12345b052 --- /dev/null +++ b/manager/README.md @@ -0,0 +1,22 @@ +# Multicluster Global Hub Manager + +The Global Hub Manager is responsible for distributing workloads across clusters and collecting resource status data into its database via transport. + +## Structure + +- **manager** + - **cmd**: Command-line utilities for the manager. + - **pkg**: Core logic and functionalities. + - **configs**: Holds configurations, schemas, and related assets. + - **controllers**: Includes common controllers such as migration and backup controllers. + - **processes**: Periodically running internal jobs, such as the policy compliance cronjob and managed hub cluster management job. + - **restapis**: Exposes REST APIs, such as managed clusters, policies, and subscriptions. + - **spec**: + - **specdb**: Handles database operations for synchronizing resources to the database and retrieving them for transport. + - **controllers**: Watches resources and persists them in the database. + - **syncers**: Syncs resources from the database and sends them via transport. + - **status**: + - **conflator**: Merges bundles inserted by transports and prepares them for dispatch. + - **dispactcher**: Routes bundles or events between components, from transport to conflator, and delivers bundles from the conflator to the database worker pool. + - **handlers**: Defines how transferred bundles are persisted in the database. + - **webhook**: The webhooks, such as disabling placement controllers for the global resource. \ No newline at end of file diff --git a/manager/cmd/manager/main.go b/manager/cmd/main.go similarity index 85% rename from manager/cmd/manager/main.go rename to manager/cmd/main.go index 7e4966d70..f8383df89 100644 --- a/manager/cmd/manager/main.go +++ b/manager/cmd/main.go @@ -26,14 +26,13 @@ import ( metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server" "sigs.k8s.io/controller-runtime/pkg/webhook" - "github.com/stolostron/multicluster-global-hub/manager/pkg/backup" - managerconfig "github.com/stolostron/multicluster-global-hub/manager/pkg/config" - "github.com/stolostron/multicluster-global-hub/manager/pkg/cronjob" - "github.com/stolostron/multicluster-global-hub/manager/pkg/hubmanagement" - migration "github.com/stolostron/multicluster-global-hub/manager/pkg/migration" - "github.com/stolostron/multicluster-global-hub/manager/pkg/nonk8sapi" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer" - statussyncer "github.com/stolostron/multicluster-global-hub/manager/pkg/statussyncer" + "github.com/stolostron/multicluster-global-hub/manager/pkg/configs" + "github.com/stolostron/multicluster-global-hub/manager/pkg/controllers" + "github.com/stolostron/multicluster-global-hub/manager/pkg/processes/cronjob" + "github.com/stolostron/multicluster-global-hub/manager/pkg/processes/hubmanagement" + "github.com/stolostron/multicluster-global-hub/manager/pkg/restapis" + specsyncer "github.com/stolostron/multicluster-global-hub/manager/pkg/spec" + "github.com/stolostron/multicluster-global-hub/manager/pkg/status" mgrwebhook "github.com/stolostron/multicluster-global-hub/manager/pkg/webhook" "github.com/stolostron/multicluster-global-hub/pkg/constants" "github.com/stolostron/multicluster-global-hub/pkg/database" @@ -62,23 +61,19 @@ var ( errFlagParameterEmpty = errors.New("flag parameter empty") ) -func init() { - managerconfig.RegisterMetrics() -} - -func parseFlags() *managerconfig.ManagerConfig { - managerConfig := &managerconfig.ManagerConfig{ - SyncerConfig: &managerconfig.SyncerConfig{}, - DatabaseConfig: &managerconfig.DatabaseConfig{}, +func parseFlags() *configs.ManagerConfig { + managerConfig := &configs.ManagerConfig{ + SyncerConfig: &configs.SyncerConfig{}, + DatabaseConfig: &configs.DatabaseConfig{}, TransportConfig: &transport.TransportInternalConfig{ IsManager: true, ConsumerGroupId: "global-hub-manager", EnableDatabaseOffset: true, }, - StatisticsConfig: &statistics.StatisticsConfig{}, - NonK8sAPIServerConfig: &nonk8sapi.NonK8sAPIServerConfig{}, - ElectionConfig: &commonobjects.LeaderElectionConfig{}, - LaunchJobNames: "", + StatisticsConfig: &statistics.StatisticsConfig{}, + RestAPIServerConfig: &restapis.RestApiServerConfig{}, + ElectionConfig: &commonobjects.LeaderElectionConfig{}, + LaunchJobNames: "", } // add zap flags @@ -112,11 +107,11 @@ func parseFlags() *managerconfig.ManagerConfig { "The path of CA certificate for kafka bootstrap server.") pflag.StringVar(&managerConfig.StatisticsConfig.LogInterval, "statistics-log-interval", "1m", "The log interval for statistics.") - pflag.StringVar(&managerConfig.NonK8sAPIServerConfig.ClusterAPIURL, "cluster-api-url", + pflag.StringVar(&managerConfig.RestAPIServerConfig.ClusterAPIURL, "cluster-api-url", "https://kubernetes.default.svc:443", "The cluster API URL for nonK8s API server.") - pflag.StringVar(&managerConfig.NonK8sAPIServerConfig.ClusterAPICABundlePath, "cluster-api-cabundle-path", + pflag.StringVar(&managerConfig.RestAPIServerConfig.ClusterAPICABundlePath, "cluster-api-cabundle-path", "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt", "The CA bundle path for cluster API.") - pflag.StringVar(&managerConfig.NonK8sAPIServerConfig.ServerBasePath, "server-base-path", + pflag.StringVar(&managerConfig.RestAPIServerConfig.ServerBasePath, "server-base-path", "/global-hub-api/v1", "The base path for nonK8s API server.") pflag.IntVar(&managerConfig.ElectionConfig.LeaseDuration, "lease-duration", 137, "controller leader lease duration") pflag.IntVar(&managerConfig.ElectionConfig.RenewDeadline, "renew-deadline", 107, "controller leader renew deadline") @@ -144,7 +139,7 @@ func parseFlags() *managerconfig.ManagerConfig { return managerConfig } -func completeConfig(managerConfig *managerconfig.ManagerConfig) error { +func completeConfig(managerConfig *configs.ManagerConfig) error { if managerConfig.DatabaseConfig.ProcessDatabaseURL == "" { return fmt.Errorf("database url for process user: %w", errFlagParameterEmpty) } @@ -158,14 +153,14 @@ func completeConfig(managerConfig *managerconfig.ManagerConfig) error { func createManager(ctx context.Context, restConfig *rest.Config, - managerConfig *managerconfig.ManagerConfig, + managerConfig *configs.ManagerConfig, sqlConn *sql.Conn, ) (ctrl.Manager, error) { leaseDuration := time.Duration(managerConfig.ElectionConfig.LeaseDuration) * time.Second renewDeadline := time.Duration(managerConfig.ElectionConfig.RenewDeadline) * time.Second retryPeriod := time.Duration(managerConfig.ElectionConfig.RetryPeriod) * time.Second options := ctrl.Options{ - Scheme: managerconfig.GetRuntimeScheme(), + Scheme: configs.GetRuntimeScheme(), Metrics: metricsserver.Options{ BindAddress: fmt.Sprintf("%s:%d", metricsHost, metricsPort), }, @@ -213,7 +208,6 @@ func createManager(ctx context.Context, return nil, fmt.Errorf("failed to create a new manager: %w", err) } - // TODO: refactor the manager to start the conflation manager so that it can handle the events from restful API err = controller.NewTransportCtrl(managerConfig.ManagerNamespace, constants.GHTransportConfigSecret, transportCallback(mgr, managerConfig), managerConfig.TransportConfig, @@ -231,19 +225,19 @@ func createManager(ctx context.Context, } // need lock DB for backup - backupPVC := backup.NewBackupPVCReconciler(mgr, sqlConn) + backupPVC := controllers.NewBackupPVCReconciler(mgr, sqlConn) if err := backupPVC.SetupWithManager(mgr); err != nil { return nil, err } if managerConfig.EnableGlobalResource { - if err := nonk8sapi.AddNonK8sApiServer(mgr, managerConfig.NonK8sAPIServerConfig); err != nil { + if err := restapis.AddRestApiServer(mgr, managerConfig.RestAPIServerConfig); err != nil { return nil, fmt.Errorf("failed to add non-k8s-api-server: %w", err) } } return mgr, nil } -func transportCallback(mgr ctrl.Manager, managerConfig *managerconfig.ManagerConfig) controller.TransportCallback { +func transportCallback(mgr ctrl.Manager, managerConfig *configs.ManagerConfig) controller.TransportCallback { return func(transportClient transport.TransportClient) error { if !managerConfig.WithACM { return nil @@ -251,12 +245,12 @@ func transportCallback(mgr ctrl.Manager, managerConfig *managerconfig.ManagerCon producer := transportClient.GetProducer() consumer := transportClient.GetConsumer() if managerConfig.EnableGlobalResource { - if err := specsyncer.AddGlobalResourceSpecSyncers(mgr, managerConfig, producer); err != nil { + if err := specsyncer.AddToManager(mgr, managerConfig, producer); err != nil { return fmt.Errorf("failed to add global resource spec syncers: %w", err) } } - if err := statussyncer.AddStatusSyncers(mgr, consumer, managerConfig); err != nil { + if err := status.AddStatusSyncers(mgr, consumer, managerConfig); err != nil { return fmt.Errorf("failed to add transport-to-db syncers: %w", err) } @@ -266,7 +260,7 @@ func transportCallback(mgr ctrl.Manager, managerConfig *managerconfig.ManagerCon } // start managedclustermigration controller - if err := migration.NewMigrationReconciler(mgr.GetClient(), producer, + if err := controllers.NewMigrationController(mgr.GetClient(), producer, managerConfig.ImportClusterInHosted).SetupWithManager(mgr); err != nil { return fmt.Errorf("failed to add migration controller to manager - %w", err) } diff --git a/manager/pkg/config/manager_config.go b/manager/pkg/configs/config.go similarity index 89% rename from manager/pkg/config/manager_config.go rename to manager/pkg/configs/config.go index 456620a06..a64b315fa 100644 --- a/manager/pkg/config/manager_config.go +++ b/manager/pkg/configs/config.go @@ -1,12 +1,12 @@ // Copyright (c) 2023 Red Hat, Inc. // Copyright Contributors to the Open Cluster Management project -package config +package configs import ( "time" - "github.com/stolostron/multicluster-global-hub/manager/pkg/nonk8sapi" + "github.com/stolostron/multicluster-global-hub/manager/pkg/restapis" commonobjects "github.com/stolostron/multicluster-global-hub/pkg/objects" "github.com/stolostron/multicluster-global-hub/pkg/statistics" "github.com/stolostron/multicluster-global-hub/pkg/transport" @@ -20,7 +20,7 @@ type ManagerConfig struct { DatabaseConfig *DatabaseConfig TransportConfig *transport.TransportInternalConfig StatisticsConfig *statistics.StatisticsConfig - NonK8sAPIServerConfig *nonk8sapi.NonK8sAPIServerConfig + RestAPIServerConfig *restapis.RestApiServerConfig ElectionConfig *commonobjects.LeaderElectionConfig EnableGlobalResource bool ImportClusterInHosted bool diff --git a/manager/pkg/config/manager_scheme.go b/manager/pkg/configs/scheme.go similarity index 99% rename from manager/pkg/config/manager_scheme.go rename to manager/pkg/configs/scheme.go index 51746b9c4..cd7c427c7 100644 --- a/manager/pkg/config/manager_scheme.go +++ b/manager/pkg/configs/scheme.go @@ -1,7 +1,7 @@ // Copyright (c) 2024 Red Hat, Inc. // Copyright Contributors to the Open Cluster Management project -package config +package configs import ( klusterletv1alpha1 "github.com/stolostron/cluster-lifecycle-api/klusterletconfig/v1alpha1" diff --git a/manager/pkg/backup/pvc_controller.go b/manager/pkg/controllers/backup_controller.go similarity index 99% rename from manager/pkg/backup/pvc_controller.go rename to manager/pkg/controllers/backup_controller.go index c13d5edcf..d55529268 100644 --- a/manager/pkg/backup/pvc_controller.go +++ b/manager/pkg/controllers/backup_controller.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package backup +package controllers import ( "context" diff --git a/manager/pkg/migration/migration_controller.go b/manager/pkg/controllers/migration_controller.go similarity index 93% rename from manager/pkg/migration/migration_controller.go rename to manager/pkg/controllers/migration_controller.go index 25ed715ed..76ba9f8e5 100644 --- a/manager/pkg/migration/migration_controller.go +++ b/manager/pkg/controllers/migration_controller.go @@ -1,7 +1,7 @@ // Copyright (c) 2024 Red Hat, Inc. // Copyright Contributors to the Open Cluster Management project -package migration +package controllers import ( "context" @@ -38,18 +38,18 @@ import ( "github.com/stolostron/multicluster-global-hub/pkg/utils" ) -// MigrationReconciler reconciles a ManagedClusterMigration object -type MigrationReconciler struct { +// MigrationController reconciles a ManagedClusterMigration object +type MigrationController struct { client.Client transport.Producer BootstrapSecret *corev1.Secret importClusterInHosted bool } -func NewMigrationReconciler(client client.Client, producer transport.Producer, +func NewMigrationController(client client.Client, producer transport.Producer, importClusterInHosted bool, -) *MigrationReconciler { - return &MigrationReconciler{ +) *MigrationController { + return &MigrationController{ Client: client, Producer: producer, importClusterInHosted: importClusterInHosted, @@ -62,7 +62,7 @@ const ( ) // SetupWithManager sets up the controller with the Manager. -func (m *MigrationReconciler) SetupWithManager(mgr ctrl.Manager) error { +func (m *MigrationController) SetupWithManager(mgr ctrl.Manager) error { return ctrl.NewControllerManagedBy(mgr).Named("migration-controller"). For(&migrationv1alpha1.ManagedClusterMigration{}). Watches(&v1beta1.ManagedServiceAccount{}, @@ -116,7 +116,7 @@ func (m *MigrationReconciler) SetupWithManager(mgr ctrl.Manager) error { Complete(m) } -func (m *MigrationReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { +func (m *MigrationController) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { log := log.FromContext(ctx) if req.Namespace == utils.GetDefaultNamespace() { // create managedserviceaccount @@ -197,7 +197,7 @@ func (m *MigrationReconciler) Reconcile(ctx context.Context, req ctrl.Request) ( return ctrl.Result{}, nil } -func (m *MigrationReconciler) generateKlusterConfig(req ctrl.Request) *klusterletv1alpha1.KlusterletConfig { +func (m *MigrationController) generateKlusterConfig(req ctrl.Request) *klusterletv1alpha1.KlusterletConfig { return &klusterletv1alpha1.KlusterletConfig{ ObjectMeta: metav1.ObjectMeta{ Name: klusterletConfigNamePrefix + req.Namespace, @@ -217,7 +217,7 @@ func (m *MigrationReconciler) generateKlusterConfig(req ctrl.Request) *klusterle } } -func (m *MigrationReconciler) generateBootstrapSecret(kubeconfig *clientcmdapi.Config, +func (m *MigrationController) generateBootstrapSecret(kubeconfig *clientcmdapi.Config, req ctrl.Request, ) (*corev1.Secret, error) { // Serialize the kubeconfig to YAML @@ -236,7 +236,7 @@ func (m *MigrationReconciler) generateBootstrapSecret(kubeconfig *clientcmdapi.C }, nil } -func (m *MigrationReconciler) generateKubeconfig(ctx context.Context, req ctrl.Request) (*clientcmdapi.Config, error) { +func (m *MigrationController) generateKubeconfig(ctx context.Context, req ctrl.Request) (*clientcmdapi.Config, error) { // get the secret which is generated by msa desiredSecret := &corev1.Secret{} if err := m.Client.Get(ctx, types.NamespacedName{ @@ -270,7 +270,7 @@ func (m *MigrationReconciler) generateKubeconfig(ctx context.Context, req ctrl.R return config, nil } -func (m *MigrationReconciler) ensureManagedServiceAccount(ctx context.Context, +func (m *MigrationController) ensureManagedServiceAccount(ctx context.Context, migration *migrationv1alpha1.ManagedClusterMigration, ) error { // create a desired msa @@ -306,7 +306,7 @@ func (m *MigrationReconciler) ensureManagedServiceAccount(ctx context.Context, return nil } -func (m *MigrationReconciler) deleteManagedServiceAccount(ctx context.Context, +func (m *MigrationController) deleteManagedServiceAccount(ctx context.Context, migration *migrationv1alpha1.ManagedClusterMigration, ) error { msa := &v1beta1.ManagedServiceAccount{} @@ -322,7 +322,7 @@ func (m *MigrationReconciler) deleteManagedServiceAccount(ctx context.Context, return m.Delete(ctx, msa) } -func (m *MigrationReconciler) syncMigration(ctx context.Context, +func (m *MigrationController) syncMigration(ctx context.Context, migration *migrationv1alpha1.ManagedClusterMigration, klusterletConfig *klusterletv1alpha1.KlusterletConfig, ) error { @@ -362,7 +362,7 @@ func (m *MigrationReconciler) syncMigration(ctx context.Context, return m.syncMigrationTo(ctx, migration) } -func (m *MigrationReconciler) syncMigrationFrom(ctx context.Context, +func (m *MigrationController) syncMigrationFrom(ctx context.Context, fromHub string, managedClusters []string, klusterletConfig *klusterletv1alpha1.KlusterletConfig, ) error { @@ -388,7 +388,7 @@ func (m *MigrationReconciler) syncMigrationFrom(ctx context.Context, return nil } -func (m *MigrationReconciler) syncMigrationTo(ctx context.Context, +func (m *MigrationController) syncMigrationTo(ctx context.Context, migration *migrationv1alpha1.ManagedClusterMigration, ) error { // default managedserviceaccount addon namespace diff --git a/manager/pkg/cronjob/scheduler.go b/manager/pkg/processes/cronjob/scheduler.go similarity index 88% rename from manager/pkg/cronjob/scheduler.go rename to manager/pkg/processes/cronjob/scheduler.go index 518b0e21c..82be272b0 100644 --- a/manager/pkg/cronjob/scheduler.go +++ b/manager/pkg/processes/cronjob/scheduler.go @@ -9,8 +9,8 @@ import ( "github.com/go-logr/logr" ctrl "sigs.k8s.io/controller-runtime" - "github.com/stolostron/multicluster-global-hub/manager/pkg/config" - "github.com/stolostron/multicluster-global-hub/manager/pkg/cronjob/task" + "github.com/stolostron/multicluster-global-hub/manager/pkg/configs" + "github.com/stolostron/multicluster-global-hub/manager/pkg/processes/cronjob/task" ) const ( @@ -29,6 +29,8 @@ type GlobalHubJobScheduler struct { } func NewGlobalHubScheduler(scheduler *gocron.Scheduler, launchJobs []string) *GlobalHubJobScheduler { + // register the metrics before starting the jobs + task.RegisterMetrics() return &GlobalHubJobScheduler{ log: ctrl.Log.WithName("cronjob-scheduler"), scheduler: scheduler, @@ -37,7 +39,7 @@ func NewGlobalHubScheduler(scheduler *gocron.Scheduler, launchJobs []string) *Gl } func AddSchedulerToManager(ctx context.Context, mgr ctrl.Manager, - managerConfig *config.ManagerConfig, enableSimulation bool, + managerConfig *configs.ManagerConfig, enableSimulation bool, ) error { log := ctrl.Log.WithName("cronjob-scheduler") // Scheduler timezone: @@ -85,8 +87,8 @@ func AddSchedulerToManager(ctx context.Context, mgr ctrl.Manager, func (s *GlobalHubJobScheduler) Start(ctx context.Context) error { s.log.Info("start job scheduler") // Set the status of the job to 0 (success) when the job is started. - config.GlobalHubCronJobGaugeVec.WithLabelValues(task.RetentionTaskName).Set(0) - config.GlobalHubCronJobGaugeVec.WithLabelValues(task.LocalComplianceTaskName).Set(0) + task.GlobalHubCronJobGaugeVec.WithLabelValues(task.RetentionTaskName).Set(0) + task.GlobalHubCronJobGaugeVec.WithLabelValues(task.LocalComplianceTaskName).Set(0) s.scheduler.StartAsync() if err := s.ExecJobs(); err != nil { return err diff --git a/manager/pkg/cronjob/task/data_retention.go b/manager/pkg/processes/cronjob/task/data_retention.go similarity index 96% rename from manager/pkg/cronjob/task/data_retention.go rename to manager/pkg/processes/cronjob/task/data_retention.go index d0676f80d..d8ab9db07 100644 --- a/manager/pkg/cronjob/task/data_retention.go +++ b/manager/pkg/processes/cronjob/task/data_retention.go @@ -9,8 +9,7 @@ import ( "github.com/go-co-op/gocron" ctrl "sigs.k8s.io/controller-runtime" - "github.com/stolostron/multicluster-global-hub/manager/pkg/config" - "github.com/stolostron/multicluster-global-hub/manager/pkg/hubmanagement" + "github.com/stolostron/multicluster-global-hub/manager/pkg/processes/hubmanagement" "github.com/stolostron/multicluster-global-hub/pkg/database" "github.com/stolostron/multicluster-global-hub/pkg/database/models" ) @@ -60,9 +59,9 @@ func DataRetention(ctx context.Context, retentionMonth int, job gocron.Job) { defer func() { if err != nil { - config.GlobalHubCronJobGaugeVec.WithLabelValues(RetentionTaskName).Set(1) + GlobalHubCronJobGaugeVec.WithLabelValues(RetentionTaskName).Set(1) } else { - config.GlobalHubCronJobGaugeVec.WithLabelValues(RetentionTaskName).Set(0) + GlobalHubCronJobGaugeVec.WithLabelValues(RetentionTaskName).Set(0) } }() diff --git a/manager/pkg/cronjob/task/local_compliance_history.go b/manager/pkg/processes/cronjob/task/local_compliance_history.go similarity index 94% rename from manager/pkg/cronjob/task/local_compliance_history.go rename to manager/pkg/processes/cronjob/task/local_compliance_history.go index 494f7139f..4e4aeb0df 100644 --- a/manager/pkg/cronjob/task/local_compliance_history.go +++ b/manager/pkg/processes/cronjob/task/local_compliance_history.go @@ -10,7 +10,6 @@ import ( "k8s.io/apimachinery/pkg/util/wait" ctrl "sigs.k8s.io/controller-runtime" - "github.com/stolostron/multicluster-global-hub/manager/pkg/config" "github.com/stolostron/multicluster-global-hub/pkg/database" "github.com/stolostron/multicluster-global-hub/pkg/database/models" ) @@ -39,9 +38,9 @@ func LocalComplianceHistory(ctx context.Context, job gocron.Job) { var err error defer func() { if err != nil { - config.GlobalHubCronJobGaugeVec.WithLabelValues(LocalComplianceTaskName).Set(1) + GlobalHubCronJobGaugeVec.WithLabelValues(LocalComplianceTaskName).Set(1) } else { - config.GlobalHubCronJobGaugeVec.WithLabelValues(LocalComplianceTaskName).Set(0) + GlobalHubCronJobGaugeVec.WithLabelValues(LocalComplianceTaskName).Set(0) } }() diff --git a/manager/pkg/config/monitoring_metrics.go b/manager/pkg/processes/cronjob/task/task_mestrics.go similarity index 97% rename from manager/pkg/config/monitoring_metrics.go rename to manager/pkg/processes/cronjob/task/task_mestrics.go index 1fe928635..be82cb02a 100644 --- a/manager/pkg/config/monitoring_metrics.go +++ b/manager/pkg/processes/cronjob/task/task_mestrics.go @@ -1,4 +1,4 @@ -package config +package task import ( "github.com/prometheus/client_golang/prometheus" diff --git a/manager/pkg/hubmanagement/hub_management.go b/manager/pkg/processes/hubmanagement/hub_management.go similarity index 100% rename from manager/pkg/hubmanagement/hub_management.go rename to manager/pkg/processes/hubmanagement/hub_management.go diff --git a/manager/pkg/nonk8sapi/README.md b/manager/pkg/restapis/README.md similarity index 100% rename from manager/pkg/nonk8sapi/README.md rename to manager/pkg/restapis/README.md diff --git a/manager/pkg/nonk8sapi/non_k8s_api.go b/manager/pkg/restapis/apis.go similarity index 79% rename from manager/pkg/nonk8sapi/non_k8s_api.go rename to manager/pkg/restapis/apis.go index 7785cb3a2..800df833b 100644 --- a/manager/pkg/nonk8sapi/non_k8s_api.go +++ b/manager/pkg/restapis/apis.go @@ -1,7 +1,7 @@ // Copyright (c) 2020 Red Hat, Inc. // Copyright Contributors to the Open Cluster Management project -package nonk8sapi +package restapis import ( "context" @@ -15,17 +15,17 @@ import ( "github.com/go-logr/logr" ctrl "sigs.k8s.io/controller-runtime" - "github.com/stolostron/multicluster-global-hub/manager/pkg/nonk8sapi/authentication" - "github.com/stolostron/multicluster-global-hub/manager/pkg/nonk8sapi/managedclusters" - "github.com/stolostron/multicluster-global-hub/manager/pkg/nonk8sapi/policies" - "github.com/stolostron/multicluster-global-hub/manager/pkg/nonk8sapi/subscriptions" + "github.com/stolostron/multicluster-global-hub/manager/pkg/restapis/authentication" + "github.com/stolostron/multicluster-global-hub/manager/pkg/restapis/managedclusters" + "github.com/stolostron/multicluster-global-hub/manager/pkg/restapis/policies" + "github.com/stolostron/multicluster-global-hub/manager/pkg/restapis/subscriptions" ) const secondsToFinishOnShutdown = 5 var errFailedToLoadCertificate = errors.New("failed to load certificate/key") -type NonK8sAPIServerConfig struct { +type RestApiServerConfig struct { ClusterAPIURL string ClusterAPICABundlePath string ServerBasePath string @@ -33,17 +33,17 @@ type NonK8sAPIServerConfig struct { // NeedLeaderElection implements the LeaderElectionRunnable interface, which indicates // the nonK8sApiServer doesn't need leader election. -func (*nonK8sApiServer) NeedLeaderElection() bool { +func (*restApiServer) NeedLeaderElection() bool { return false } -// nonK8sApiServer defines the non-k8s-api-server -type nonK8sApiServer struct { +// restApiServer defines the non-k8s-api-server +type restApiServer struct { log logr.Logger svr *http.Server } -func readCertificateAuthority(nonK8sAPIServerConfig *NonK8sAPIServerConfig) ([]byte, error) { +func readCertificateAuthority(nonK8sAPIServerConfig *RestApiServerConfig) ([]byte, error) { var clusterAPICABundle []byte if nonK8sAPIServerConfig.ClusterAPICABundlePath != "" { @@ -58,14 +58,14 @@ func readCertificateAuthority(nonK8sAPIServerConfig *NonK8sAPIServerConfig) ([]b return clusterAPICABundle, nil } -// AddNonK8sApiServer adds the non-k8s-api-server to the Manager. -func AddNonK8sApiServer(mgr ctrl.Manager, nonK8sAPIServerConfig *NonK8sAPIServerConfig) error { - router, err := SetupRouter(nonK8sAPIServerConfig) +// AddRestApiServer adds the non-k8s-api-server to the Manager. +func AddRestApiServer(mgr ctrl.Manager, restApiConfig *RestApiServerConfig) error { + router, err := SetupRouter(restApiConfig) if err != nil { return err } - err = mgr.Add(&nonK8sApiServer{ + err = mgr.Add(&restApiServer{ log: ctrl.Log.WithName("non-k8s-api-server"), svr: &http.Server{ Addr: ":8080", @@ -97,7 +97,7 @@ func AddNonK8sApiServer(mgr ctrl.Manager, nonK8sAPIServerConfig *NonK8sAPIServer // @in header // @name Authorization // @description Authorization with user access token -func SetupRouter(nonK8sAPIServerConfig *NonK8sAPIServerConfig) (*gin.Engine, error) { +func SetupRouter(nonK8sAPIServerConfig *RestApiServerConfig) (*gin.Engine, error) { router := gin.Default() // add aythentication eith openshift oauth // skip authentication middleware if ClusterAPIURL is empty for testing @@ -122,7 +122,7 @@ func SetupRouter(nonK8sAPIServerConfig *NonK8sAPIServerConfig) (*gin.Engine, err } // Start runs the non-k8s-api server within given context -func (s *nonK8sApiServer) Start(ctx context.Context) error { +func (s *restApiServer) Start(ctx context.Context) error { idleConnsClosed := make(chan struct{}) // initializing the shutdown process in a goroutine so that it won't block the server starting and running go func() { diff --git a/manager/pkg/nonk8sapi/authentication/authentication.go b/manager/pkg/restapis/authentication/authentication.go similarity index 100% rename from manager/pkg/nonk8sapi/authentication/authentication.go rename to manager/pkg/restapis/authentication/authentication.go diff --git a/manager/pkg/nonk8sapi/managedclusters/list.go b/manager/pkg/restapis/managedclusters/list.go similarity index 98% rename from manager/pkg/nonk8sapi/managedclusters/list.go rename to manager/pkg/restapis/managedclusters/list.go index ed3688fb7..a5a6d5df0 100644 --- a/manager/pkg/nonk8sapi/managedclusters/list.go +++ b/manager/pkg/restapis/managedclusters/list.go @@ -24,7 +24,7 @@ import ( "k8s.io/apimachinery/pkg/runtime/schema" clusterv1 "open-cluster-management.io/api/cluster/v1" - "github.com/stolostron/multicluster-global-hub/manager/pkg/nonk8sapi/util" + "github.com/stolostron/multicluster-global-hub/manager/pkg/restapis/util" "github.com/stolostron/multicluster-global-hub/pkg/database" ) @@ -146,7 +146,6 @@ func handleRowsForWatch(ginCtx *gin.Context, managedClusterListQuery string) { _, cancelContext := context.WithCancel(context.Background()) defer cancelContext() - // TODO - add deleted field to the status.managed_clusters table // instead of holding the previously added managed clusters by memory // and calculating the deleted clusters preAddedManagedClusterNames := set.NewSet() diff --git a/manager/pkg/nonk8sapi/managedclusters/patch.go b/manager/pkg/restapis/managedclusters/patch.go similarity index 100% rename from manager/pkg/nonk8sapi/managedclusters/patch.go rename to manager/pkg/restapis/managedclusters/patch.go diff --git a/manager/pkg/nonk8sapi/policies/constants.go b/manager/pkg/restapis/policies/constants.go similarity index 100% rename from manager/pkg/nonk8sapi/policies/constants.go rename to manager/pkg/restapis/policies/constants.go diff --git a/manager/pkg/nonk8sapi/policies/get.go b/manager/pkg/restapis/policies/get.go similarity index 98% rename from manager/pkg/nonk8sapi/policies/get.go rename to manager/pkg/restapis/policies/get.go index ba1ba11f6..e9ec5a167 100644 --- a/manager/pkg/nonk8sapi/policies/get.go +++ b/manager/pkg/restapis/policies/get.go @@ -19,7 +19,7 @@ import ( "k8s.io/apimachinery/pkg/runtime" policyv1 "open-cluster-management.io/governance-policy-propagator/api/v1" - "github.com/stolostron/multicluster-global-hub/manager/pkg/nonk8sapi/util" + "github.com/stolostron/multicluster-global-hub/manager/pkg/restapis/util" "github.com/stolostron/multicluster-global-hub/pkg/database" ) diff --git a/manager/pkg/nonk8sapi/policies/list.go b/manager/pkg/restapis/policies/list.go similarity index 99% rename from manager/pkg/nonk8sapi/policies/list.go rename to manager/pkg/restapis/policies/list.go index 287d229a5..204c11f68 100644 --- a/manager/pkg/nonk8sapi/policies/list.go +++ b/manager/pkg/restapis/policies/list.go @@ -24,7 +24,7 @@ import ( "k8s.io/apimachinery/pkg/runtime/schema" policyv1 "open-cluster-management.io/governance-policy-propagator/api/v1" - "github.com/stolostron/multicluster-global-hub/manager/pkg/nonk8sapi/util" + "github.com/stolostron/multicluster-global-hub/manager/pkg/restapis/util" "github.com/stolostron/multicluster-global-hub/pkg/database" "github.com/stolostron/multicluster-global-hub/pkg/database/models" ) diff --git a/manager/pkg/nonk8sapi/subscriptions/get.go b/manager/pkg/restapis/subscriptions/get.go similarity index 98% rename from manager/pkg/nonk8sapi/subscriptions/get.go rename to manager/pkg/restapis/subscriptions/get.go index ebedd1e73..6a710b6bc 100644 --- a/manager/pkg/nonk8sapi/subscriptions/get.go +++ b/manager/pkg/restapis/subscriptions/get.go @@ -17,7 +17,7 @@ import ( appsv1 "open-cluster-management.io/multicloud-operators-subscription/pkg/apis/apps/v1" appsv1alpha1 "open-cluster-management.io/multicloud-operators-subscription/pkg/apis/apps/v1alpha1" - "github.com/stolostron/multicluster-global-hub/manager/pkg/nonk8sapi/util" + "github.com/stolostron/multicluster-global-hub/manager/pkg/restapis/util" "github.com/stolostron/multicluster-global-hub/pkg/database" ) diff --git a/manager/pkg/nonk8sapi/subscriptions/list.go b/manager/pkg/restapis/subscriptions/list.go similarity index 99% rename from manager/pkg/nonk8sapi/subscriptions/list.go rename to manager/pkg/restapis/subscriptions/list.go index 18f59fa33..e257bd85f 100644 --- a/manager/pkg/nonk8sapi/subscriptions/list.go +++ b/manager/pkg/restapis/subscriptions/list.go @@ -23,7 +23,7 @@ import ( "k8s.io/apimachinery/pkg/runtime/schema" appsv1 "open-cluster-management.io/multicloud-operators-subscription/pkg/apis/apps/v1" - "github.com/stolostron/multicluster-global-hub/manager/pkg/nonk8sapi/util" + "github.com/stolostron/multicluster-global-hub/manager/pkg/restapis/util" "github.com/stolostron/multicluster-global-hub/pkg/database" ) diff --git a/manager/pkg/nonk8sapi/swagger.md b/manager/pkg/restapis/swagger.md similarity index 100% rename from manager/pkg/nonk8sapi/swagger.md rename to manager/pkg/restapis/swagger.md diff --git a/manager/pkg/nonk8sapi/swagger.yaml b/manager/pkg/restapis/swagger.yaml similarity index 100% rename from manager/pkg/nonk8sapi/swagger.yaml rename to manager/pkg/restapis/swagger.yaml diff --git a/manager/pkg/nonk8sapi/util/continue.go b/manager/pkg/restapis/util/continue.go similarity index 100% rename from manager/pkg/nonk8sapi/util/continue.go rename to manager/pkg/restapis/util/continue.go diff --git a/manager/pkg/nonk8sapi/util/crd.go b/manager/pkg/restapis/util/crd.go similarity index 92% rename from manager/pkg/nonk8sapi/util/crd.go rename to manager/pkg/restapis/util/crd.go index 6f2cde24f..782a13a68 100644 --- a/manager/pkg/nonk8sapi/util/crd.go +++ b/manager/pkg/restapis/util/crd.go @@ -18,7 +18,8 @@ import ( // GetCustomResourceColumnDefinitions return the column definitions for CRD `name`, version `version`. func GetCustomResourceColumnDefinitions(name, version string) []apiextensionsv1.CustomResourceColumnDefinition { // nolint:lll - // from https://github.com/kubernetes/apiextensions-apiserver/blob/76c7ff37eea5429706c9cfb4a0e9215e49d93930/pkg/apis/apiextensions/helpers.go#L243 + // from https://github.com/kubernetes/apiextensions-apiserver/blob/76c7ff37eea5429706c9cfb4a0e9215e49d93930/pkg/apis + // /apiextensions/helpers.go#L243 defaultColumns := []apiextensionsv1.CustomResourceColumnDefinition{ {Name: "Age", Type: "date", JSONPath: ".metadata.creationTimestamp"}, } @@ -36,7 +37,8 @@ func GetCustomResourceColumnDefinitions(name, version string) []apiextensionsv1. } // nolint:lll - // from https://github.com/kubernetes/apiextensions-apiserver/blob/76c7ff37eea5429706c9cfb4a0e9215e49d93930/test/integration/helpers.go#L84 + // from https://github.com/kubernetes/apiextensions-apiserver/blob/76c7ff37eea5429706c9cfb4a0e9215e49d93930 + // /test/integration/helpers.go#L84 crdv1, err := theClientSet.ApiextensionsV1().CustomResourceDefinitions().Get(context.TODO(), name, metav1.GetOptions{}) if err != nil { @@ -82,8 +84,8 @@ func convertColumnsToColumnsV1(columns []apiextensions.CustomResourceColumnDefin columnCopy := column.DeepCopy() // nolint:lll - err := apiextensionsv1.Convert_apiextensions_CustomResourceColumnDefinition_To_v1_CustomResourceColumnDefinition(columnCopy, - &columnv1, nil) + err := apiextensionsv1.Convert_apiextensions_CustomResourceColumnDefinition_To_v1_CustomResourceColumnDefinition( + columnCopy, &columnv1, nil) if err != nil { return nil, fmt.Errorf("unable to convert column: %w", err) } diff --git a/manager/pkg/nonk8sapi/util/label.go b/manager/pkg/restapis/util/label.go similarity index 100% rename from manager/pkg/nonk8sapi/util/label.go rename to manager/pkg/restapis/util/label.go diff --git a/manager/pkg/nonk8sapi/util/until.go b/manager/pkg/restapis/util/until.go similarity index 100% rename from manager/pkg/nonk8sapi/util/until.go rename to manager/pkg/restapis/util/until.go diff --git a/manager/pkg/specsyncer/spec2db/controller/application_spec_sync.go b/manager/pkg/spec/controllers/application_controller.go similarity index 89% rename from manager/pkg/specsyncer/spec2db/controller/application_spec_sync.go rename to manager/pkg/spec/controllers/application_controller.go index bb8af2fa0..fbdf78203 100644 --- a/manager/pkg/specsyncer/spec2db/controller/application_spec_sync.go +++ b/manager/pkg/spec/controllers/application_controller.go @@ -1,7 +1,7 @@ // Copyright (c) 2020 Red Hat, Inc. // Copyright Contributors to the Open Cluster Management project -package controller +package controllers import ( "fmt" @@ -11,18 +11,18 @@ import ( ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/db" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/specdb" "github.com/stolostron/multicluster-global-hub/pkg/constants" ) -func AddApplicationController(mgr ctrl.Manager, specDB db.SpecDB) error { +func AddApplicationController(mgr ctrl.Manager, specDB specdb.SpecDB) error { if err := ctrl.NewControllerManagedBy(mgr). For(&applicationv1beta1.Application{}). WithEventFilter(GlobalResourcePredicate()). - Complete(&genericSpecToDBReconciler{ + Complete(&genericSpecController{ client: mgr.GetClient(), specDB: specDB, - log: ctrl.Log.WithName("applications-spec-syncer"), + log: ctrl.Log.WithName("applications-spec-controller"), tableName: "applications", finalizerName: constants.GlobalHubCleanupFinalizer, createInstance: func() client.Object { return &applicationv1beta1.Application{} }, diff --git a/manager/pkg/specsyncer/db2transport/bundle/base_objects_bundle.go b/manager/pkg/spec/controllers/bundle/base_objects_bundle.go similarity index 100% rename from manager/pkg/specsyncer/db2transport/bundle/base_objects_bundle.go rename to manager/pkg/spec/controllers/bundle/base_objects_bundle.go diff --git a/manager/pkg/specsyncer/db2transport/bundle/bundle/objects_bundle.go b/manager/pkg/spec/controllers/bundle/interface.go similarity index 100% rename from manager/pkg/specsyncer/db2transport/bundle/bundle/objects_bundle.go rename to manager/pkg/spec/controllers/bundle/interface.go diff --git a/manager/pkg/specsyncer/spec2db/controller/channel_spec_sync.go b/manager/pkg/spec/controllers/channel_controller.go similarity index 90% rename from manager/pkg/specsyncer/spec2db/controller/channel_spec_sync.go rename to manager/pkg/spec/controllers/channel_controller.go index 9addc0e8e..6d6d6ad54 100644 --- a/manager/pkg/specsyncer/spec2db/controller/channel_spec_sync.go +++ b/manager/pkg/spec/controllers/channel_controller.go @@ -1,7 +1,7 @@ // Copyright (c) 2020 Red Hat, Inc. // Copyright Contributors to the Open Cluster Management project -package controller +package controllers import ( "fmt" @@ -12,11 +12,11 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/predicate" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/db" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/specdb" "github.com/stolostron/multicluster-global-hub/pkg/constants" ) -func AddChannelController(mgr ctrl.Manager, specDB db.SpecDB) error { +func AddChannelController(mgr ctrl.Manager, specDB specdb.SpecDB) error { if err := ctrl.NewControllerManagedBy(mgr). For(&channelv1.Channel{}). WithEventFilter(GlobalResourcePredicate()). @@ -29,10 +29,10 @@ func AddChannelController(mgr ctrl.Manager, specDB db.SpecDB) error { } return true })). - Complete(&genericSpecToDBReconciler{ + Complete(&genericSpecController{ client: mgr.GetClient(), specDB: specDB, - log: ctrl.Log.WithName("channels-spec-syncer"), + log: ctrl.Log.WithName("channels-spec-controller"), tableName: "channels", finalizerName: constants.GlobalHubCleanupFinalizer, createInstance: func() client.Object { return &channelv1.Channel{} }, diff --git a/manager/pkg/specsyncer/spec2db/controller/generic_spec_to_db_reconciler.go b/manager/pkg/spec/controllers/generic.go similarity index 56% rename from manager/pkg/specsyncer/spec2db/controller/generic_spec_to_db_reconciler.go rename to manager/pkg/spec/controllers/generic.go index fbea67ba6..04da9b213 100644 --- a/manager/pkg/specsyncer/spec2db/controller/generic_spec_to_db_reconciler.go +++ b/manager/pkg/spec/controllers/generic.go @@ -1,7 +1,7 @@ // Copyright (c) 2020 Red Hat, Inc. // Copyright Contributors to the Open Cluster Management project -package controller +package controllers import ( "context" @@ -18,10 +18,14 @@ import ( "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" "sigs.k8s.io/controller-runtime/pkg/predicate" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/db" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/specdb" "github.com/stolostron/multicluster-global-hub/pkg/constants" ) +const ( + requeueDuration = 5 * time.Second +) + func GlobalResourcePredicate() predicate.Predicate { p, _ := predicate.LabelSelectorPredicate(metav1.LabelSelector{ MatchExpressions: []metav1.LabelSelectorRequirement{ @@ -34,10 +38,10 @@ func GlobalResourcePredicate() predicate.Predicate { return p } -type genericSpecToDBReconciler struct { +type genericSpecController struct { client client.Client log logr.Logger - specDB db.SpecDB + specDB specdb.SpecDB tableName string finalizerName string createInstance func() client.Object @@ -45,70 +49,54 @@ type genericSpecToDBReconciler struct { areEqual func(client.Object, client.Object) bool } -const ( - requeuePeriodSeconds = 5 -) - -func (r *genericSpecToDBReconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl.Result, error) { +func (r *genericSpecController) Reconcile(ctx context.Context, request ctrl.Request) (ctrl.Result, error) { reqLogger := r.log.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name) - instanceUID, instance, err := r.processCR(ctx, request, reqLogger) - const failedMsg = "Reconciliation failed" - if err != nil { - reqLogger.Error(err, failedMsg) - return ctrl.Result{Requeue: true, RequeueAfter: requeuePeriodSeconds * time.Second}, err - } - if instance == nil { - return ctrl.Result{}, nil - } + // add/remove finalizer from the resource + instance := r.createInstance() - instanceInTheDatabase, err := r.processInstanceInTheDatabase(ctx, instance, instanceUID, reqLogger) + err := r.client.Get(ctx, request.NamespacedName, instance) if err != nil { - reqLogger.Error(err, failedMsg) - return ctrl.Result{Requeue: true, RequeueAfter: requeuePeriodSeconds * time.Second}, err + if apierrors.IsNotFound(err) { + // the instance on hub was deleted, update all the matching instances in the database as deleted + if err := r.specDB.DeleteSpecObject(ctx, r.tableName, request.Name, request.Namespace); err != nil { + return ctrl.Result{Requeue: true, RequeueAfter: requeueDuration}, err + } + return ctrl.Result{}, nil + } + return ctrl.Result{Requeue: true, RequeueAfter: requeueDuration}, fmt.Errorf("failed to get the instance: %w", err) } - if !r.areEqual(instance, instanceInTheDatabase) { - reqLogger.Info("Mismatch between hub and the database, updating the database") - - if err := r.specDB.UpdateSpecObject(ctx, r.tableName, instanceUID, &instance); err != nil { - reqLogger.Error(err, failedMsg) - return ctrl.Result{}, err + // deleting + if !instance.GetDeletionTimestamp().IsZero() { + if err := r.removeFinalizerAndDelete(ctx, instance, reqLogger); err != nil { + return ctrl.Result{Requeue: true, RequeueAfter: requeueDuration}, err } } - return ctrl.Result{}, err -} - -func (r *genericSpecToDBReconciler) processCR(ctx context.Context, request ctrl.Request, - log logr.Logger, -) (string, client.Object, error) { - instance := r.createInstance() - - err := r.client.Get(ctx, request.NamespacedName, instance) - if apierrors.IsNotFound(err) { - // the instance on hub was deleted, update all the matching instances in the database as deleted - return "", nil, r.deleteFromTheDatabase(ctx, request.Name, request.Namespace, log) + // updating + if err := r.addFinalizer(ctx, instance, reqLogger); err != nil { + return ctrl.Result{Requeue: true, RequeueAfter: requeueDuration}, fmt.Errorf("failed to add finalzier: %w", err) } + instance = r.cleanInstance(instance) + instanceInDatabase, err := r.insertInstanceToDatabase(ctx, instance, string(instance.GetUID()), reqLogger) if err != nil { - return "", nil, fmt.Errorf("failed to get the instance from hub: %w", err) + return ctrl.Result{Requeue: true, RequeueAfter: requeueDuration}, err } - if isInstanceBeingDeleted(instance) { - return "", nil, r.removeFinalizerAndDelete(ctx, instance, log) - } - - err = r.addFinalizer(ctx, instance, log) + if !r.areEqual(instance, instanceInDatabase) { + reqLogger.Info("Mismatch between hub and the database, updating the database") - return string(instance.GetUID()), r.cleanInstance(instance), err -} + if err := r.specDB.UpdateSpecObject(ctx, r.tableName, string(instance.GetUID()), &instance); err != nil { + return ctrl.Result{}, err + } + } -func isInstanceBeingDeleted(instance client.Object) bool { - return !instance.GetDeletionTimestamp().IsZero() + return ctrl.Result{}, err } -func (r *genericSpecToDBReconciler) removeFinalizerAndDelete(ctx context.Context, instance client.Object, +func (r *genericSpecController) removeFinalizerAndDelete(ctx context.Context, instance client.Object, log logr.Logger, ) error { if !controllerutil.ContainsFinalizer(instance, r.finalizerName) { @@ -118,7 +106,7 @@ func (r *genericSpecToDBReconciler) removeFinalizerAndDelete(ctx context.Context log.Info("Removing an instance from the database") // the policy is being deleted, update all the matching policies in the database as deleted - if err := r.deleteFromTheDatabase(ctx, instance.GetName(), instance.GetNamespace(), log); err != nil { + if err := r.specDB.DeleteSpecObject(ctx, r.tableName, instance.GetName(), instance.GetNamespace()); err != nil { return fmt.Errorf("failed to delete an instance from the database: %w", err) } @@ -132,7 +120,7 @@ func (r *genericSpecToDBReconciler) removeFinalizerAndDelete(ctx context.Context return nil } -func (r *genericSpecToDBReconciler) addFinalizer(ctx context.Context, instance client.Object, log logr.Logger) error { +func (r *genericSpecController) addFinalizer(ctx context.Context, instance client.Object, log logr.Logger) error { if controllerutil.ContainsFinalizer(instance, r.finalizerName) { return nil } @@ -147,7 +135,7 @@ func (r *genericSpecToDBReconciler) addFinalizer(ctx context.Context, instance c return nil } -func (r *genericSpecToDBReconciler) processInstanceInTheDatabase(ctx context.Context, instance client.Object, +func (r *genericSpecController) insertInstanceToDatabase(ctx context.Context, instance client.Object, instanceUID string, log logr.Logger, ) (client.Object, error) { instanceInTheDatabase := r.createInstance() @@ -172,7 +160,7 @@ func (r *genericSpecToDBReconciler) processInstanceInTheDatabase(ctx context.Con return instanceInTheDatabase, nil } -func (r *genericSpecToDBReconciler) cleanInstance(instance client.Object) client.Object { +func (r *genericSpecController) cleanInstance(instance client.Object) client.Object { // instance.SetUID("") instance.SetResourceVersion("") instance.SetManagedFields(nil) @@ -187,17 +175,3 @@ func (r *genericSpecToDBReconciler) cleanInstance(instance client.Object) client return instance } - -func (r *genericSpecToDBReconciler) deleteFromTheDatabase(ctx context.Context, name, namespace string, - log logr.Logger, -) error { - log.V(2).Info("Instance was deleted, update the deleted field in the database") - - if err := r.specDB.DeleteSpecObject(ctx, r.tableName, name, namespace); err != nil { - return err - } - - log.V(2).Info("Instance has been updated as deleted in the database") - - return nil -} diff --git a/manager/pkg/specsyncer/spec2db/controller/managedclusterset_spec_sync.go b/manager/pkg/spec/controllers/managedclusterset_contrller.go similarity index 92% rename from manager/pkg/specsyncer/spec2db/controller/managedclusterset_spec_sync.go rename to manager/pkg/spec/controllers/managedclusterset_contrller.go index 300ca7b0f..a5e6fce5a 100644 --- a/manager/pkg/specsyncer/spec2db/controller/managedclusterset_spec_sync.go +++ b/manager/pkg/spec/controllers/managedclusterset_contrller.go @@ -1,7 +1,7 @@ // Copyright (c) 2020 Red Hat, Inc. // Copyright Contributors to the Open Cluster Management project -package controller +package controllers import ( "fmt" @@ -11,15 +11,15 @@ import ( ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/db" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/specdb" "github.com/stolostron/multicluster-global-hub/pkg/constants" ) -func AddManagedClusterSetController(mgr ctrl.Manager, specDB db.SpecDB) error { +func AddManagedClusterSetController(mgr ctrl.Manager, specDB specdb.SpecDB) error { if err := ctrl.NewControllerManagedBy(mgr). For(&clusterv1beta2.ManagedClusterSet{}). WithEventFilter(GlobalResourcePredicate()). - Complete(&genericSpecToDBReconciler{ + Complete(&genericSpecController{ client: mgr.GetClient(), specDB: specDB, log: ctrl.Log.WithName("managedclustersets-spec-syncer"), diff --git a/manager/pkg/specsyncer/spec2db/controller/managedclustersetbinding_spec_sync.go b/manager/pkg/spec/controllers/managedclustersetbinding_controller.go similarity index 95% rename from manager/pkg/specsyncer/spec2db/controller/managedclustersetbinding_spec_sync.go rename to manager/pkg/spec/controllers/managedclustersetbinding_controller.go index 7dacf39a4..b88eea047 100644 --- a/manager/pkg/specsyncer/spec2db/controller/managedclustersetbinding_spec_sync.go +++ b/manager/pkg/spec/controllers/managedclustersetbinding_controller.go @@ -1,7 +1,7 @@ // Copyright (c) 2020 Red Hat, Inc. // Copyright Contributors to the Open Cluster Management project -package controller +package controllers import ( "fmt" @@ -11,15 +11,15 @@ import ( ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/db" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/specdb" "github.com/stolostron/multicluster-global-hub/pkg/constants" ) -func AddManagedClusterSetBindingController(mgr ctrl.Manager, specDB db.SpecDB) error { +func AddManagedClusterSetBindingController(mgr ctrl.Manager, specDB specdb.SpecDB) error { if err := ctrl.NewControllerManagedBy(mgr). For(&clusterv1beta2.ManagedClusterSetBinding{}). WithEventFilter(GlobalResourcePredicate()). - Complete(&genericSpecToDBReconciler{ + Complete(&genericSpecController{ client: mgr.GetClient(), specDB: specDB, log: ctrl.Log.WithName("managedclustersetbindings-spec-syncer"), diff --git a/manager/pkg/specsyncer/spec2db/controller/placement_spec_sync.go b/manager/pkg/spec/controllers/placement_controller.go similarity index 93% rename from manager/pkg/specsyncer/spec2db/controller/placement_spec_sync.go rename to manager/pkg/spec/controllers/placement_controller.go index dc3017a27..54464859a 100644 --- a/manager/pkg/specsyncer/spec2db/controller/placement_spec_sync.go +++ b/manager/pkg/spec/controllers/placement_controller.go @@ -1,7 +1,7 @@ // Copyright (c) 2020 Red Hat, Inc. // Copyright Contributors to the Open Cluster Management project -package controller +package controllers import ( "fmt" @@ -11,15 +11,15 @@ import ( ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/db" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/specdb" "github.com/stolostron/multicluster-global-hub/pkg/constants" ) -func AddPlacementController(mgr ctrl.Manager, specDB db.SpecDB) error { +func AddPlacementController(mgr ctrl.Manager, specDB specdb.SpecDB) error { if err := ctrl.NewControllerManagedBy(mgr). For(&clusterv1beta1.Placement{}). WithEventFilter(GlobalResourcePredicate()). - Complete(&genericSpecToDBReconciler{ + Complete(&genericSpecController{ client: mgr.GetClient(), specDB: specDB, log: ctrl.Log.WithName("placements-spec-syncer"), diff --git a/manager/pkg/specsyncer/spec2db/controller/placementbinding_spec_sync.go b/manager/pkg/spec/controllers/placementbinding_controller.go similarity index 92% rename from manager/pkg/specsyncer/spec2db/controller/placementbinding_spec_sync.go rename to manager/pkg/spec/controllers/placementbinding_controller.go index b224e6cab..9c0da35da 100644 --- a/manager/pkg/specsyncer/spec2db/controller/placementbinding_spec_sync.go +++ b/manager/pkg/spec/controllers/placementbinding_controller.go @@ -1,7 +1,7 @@ // Copyright (c) 2020 Red Hat, Inc. // Copyright Contributors to the Open Cluster Management project -package controller +package controllers import ( "fmt" @@ -11,15 +11,15 @@ import ( ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/db" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/specdb" "github.com/stolostron/multicluster-global-hub/pkg/constants" ) -func AddPlacementBindingController(mgr ctrl.Manager, specDB db.SpecDB) error { +func AddPlacementBindingController(mgr ctrl.Manager, specDB specdb.SpecDB) error { if err := ctrl.NewControllerManagedBy(mgr). For(&policyv1.PlacementBinding{}). WithEventFilter(GlobalResourcePredicate()). - Complete(&genericSpecToDBReconciler{ + Complete(&genericSpecController{ client: mgr.GetClient(), specDB: specDB, log: ctrl.Log.WithName("placementbindings-spec-syncer"), diff --git a/manager/pkg/specsyncer/spec2db/controller/placementrule_spec_sync.go b/manager/pkg/spec/controllers/placementrule_controller.go similarity index 93% rename from manager/pkg/specsyncer/spec2db/controller/placementrule_spec_sync.go rename to manager/pkg/spec/controllers/placementrule_controller.go index 844841cde..705924036 100644 --- a/manager/pkg/specsyncer/spec2db/controller/placementrule_spec_sync.go +++ b/manager/pkg/spec/controllers/placementrule_controller.go @@ -1,7 +1,7 @@ // Copyright (c) 2020 Red Hat, Inc. // Copyright Contributors to the Open Cluster Management project -package controller +package controllers import ( "fmt" @@ -11,15 +11,15 @@ import ( ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/db" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/specdb" "github.com/stolostron/multicluster-global-hub/pkg/constants" ) -func AddPlacementRuleController(mgr ctrl.Manager, specDB db.SpecDB) error { +func AddPlacementRuleController(mgr ctrl.Manager, specDB specdb.SpecDB) error { if err := ctrl.NewControllerManagedBy(mgr). For(&placementrulev1.PlacementRule{}). WithEventFilter(GlobalResourcePredicate()). - Complete(&genericSpecToDBReconciler{ + Complete(&genericSpecController{ client: mgr.GetClient(), specDB: specDB, log: ctrl.Log.WithName("placementrules-spec-syncer"), diff --git a/manager/pkg/specsyncer/spec2db/controller/policy_spec_sync.go b/manager/pkg/spec/controllers/policy_controller.go similarity index 91% rename from manager/pkg/specsyncer/spec2db/controller/policy_spec_sync.go rename to manager/pkg/spec/controllers/policy_controller.go index eac936508..3c1b52e58 100644 --- a/manager/pkg/specsyncer/spec2db/controller/policy_spec_sync.go +++ b/manager/pkg/spec/controllers/policy_controller.go @@ -1,7 +1,7 @@ // Copyright (c) 2020 Red Hat, Inc. // Copyright Contributors to the Open Cluster Management project -package controller +package controllers import ( "fmt" @@ -11,15 +11,15 @@ import ( ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/db" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/specdb" "github.com/stolostron/multicluster-global-hub/pkg/constants" ) -func AddPolicyController(mgr ctrl.Manager, specDB db.SpecDB) error { +func AddPolicyController(mgr ctrl.Manager, specDB specdb.SpecDB) error { if err := ctrl.NewControllerManagedBy(mgr). For(&policyv1.Policy{}). WithEventFilter(GlobalResourcePredicate()). - Complete(&genericSpecToDBReconciler{ + Complete(&genericSpecController{ client: mgr.GetClient(), specDB: specDB, log: ctrl.Log.WithName("policies-spec-syncer"), @@ -53,7 +53,6 @@ func arePoliciesEqual(instance1, instance2 client.Object) bool { return false } - // TODO handle Template comparison later policy1WithoutTemplates := policy1.DeepCopy() policy1WithoutTemplates.Spec.PolicyTemplates = nil diff --git a/manager/pkg/specsyncer/spec2db/controller/subscription_spec_sync.go b/manager/pkg/spec/controllers/subscription_controller.go similarity index 91% rename from manager/pkg/specsyncer/spec2db/controller/subscription_spec_sync.go rename to manager/pkg/spec/controllers/subscription_controller.go index 84d832539..95d7599ad 100644 --- a/manager/pkg/specsyncer/spec2db/controller/subscription_spec_sync.go +++ b/manager/pkg/spec/controllers/subscription_controller.go @@ -1,7 +1,7 @@ // Copyright (c) 2020 Red Hat, Inc. // Copyright Contributors to the Open Cluster Management project -package controller +package controllers import ( "fmt" @@ -12,11 +12,11 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/predicate" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/db" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/specdb" "github.com/stolostron/multicluster-global-hub/pkg/constants" ) -func AddSubscriptionController(mgr ctrl.Manager, specDB db.SpecDB) error { +func AddSubscriptionController(mgr ctrl.Manager, specDB specdb.SpecDB) error { if err := ctrl.NewControllerManagedBy(mgr). For(&subscriptionv1.Subscription{}). WithEventFilter(GlobalResourcePredicate()). @@ -29,7 +29,7 @@ func AddSubscriptionController(mgr ctrl.Manager, specDB db.SpecDB) error { } return obj.GetName() != "hive-clusterimagesets-subscription-fast-0" })). - Complete(&genericSpecToDBReconciler{ + Complete(&genericSpecController{ client: mgr.GetClient(), specDB: specDB, log: ctrl.Log.WithName("subscriptions-spec-syncer"), @@ -55,7 +55,7 @@ func cleanSubscriptionStatus(instance client.Object) { } func areSubscriptionsEqual(instance1, instance2 client.Object) bool { - // TODO: subscription come out as not equal because of package override field, check if it matters. + // Note: subscription come out as not equal because of package override field, check if it matters. subscription1, ok1 := instance1.(*subscriptionv1.Subscription) subscription2, ok2 := instance2.(*subscriptionv1.Subscription) diff --git a/manager/pkg/spec/spec.go b/manager/pkg/spec/spec.go new file mode 100644 index 000000000..65b5ca4ef --- /dev/null +++ b/manager/pkg/spec/spec.go @@ -0,0 +1,96 @@ +package spec + +import ( + "fmt" + "time" + + ctrl "sigs.k8s.io/controller-runtime" + + "github.com/stolostron/multicluster-global-hub/manager/pkg/configs" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/controllers" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/specdb" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/specdb/gorm" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/syncers" + "github.com/stolostron/multicluster-global-hub/pkg/transport" +) + +var specCtrlStarted = false + +func AddToManager(mgr ctrl.Manager, config *configs.ManagerConfig, producer transport.Producer) error { + if specCtrlStarted { + return nil + } + + // spec to db + if err := AddDBControllers(mgr); err != nil { + return fmt.Errorf("failed to add spec-to-db controllers: %w", err) + } + + // db to transport + if err := AddDatabaseSyncers(mgr, config, producer); err != nil { + return fmt.Errorf("failed to add db-to-transport syncers: %w", err) + } + if err := AddManagedClusterLabelDBSyncer(mgr, config.SyncerConfig.DeletedLabelsTrimmingInterval); err != nil { + return fmt.Errorf("failed to add status db watchers: %w", err) + } + + specCtrlStarted = true + return nil +} + +// AddControllersToDatabase adds all the spec-to-db controllers to the Manager. +func AddDBControllers(mgr ctrl.Manager) error { + addControllerFunctions := []func(ctrl.Manager, specdb.SpecDB) error{ + controllers.AddPolicyController, + controllers.AddPlacementRuleController, + controllers.AddPlacementBindingController, + controllers.AddApplicationController, + controllers.AddSubscriptionController, + controllers.AddChannelController, + controllers.AddManagedClusterSetController, + controllers.AddManagedClusterSetBindingController, + controllers.AddPlacementController, + } + specDB := gorm.NewGormSpecDB() + for _, addControllerFunction := range addControllerFunctions { + if err := addControllerFunction(mgr, specDB); err != nil { + return fmt.Errorf("failed to add controller: %w", err) + } + } + + return nil +} + +// AddDatabaseSyncers adds the controllers that send info from DB to transport layer to the Manager. +func AddDatabaseSyncers(mgr ctrl.Manager, config *configs.ManagerConfig, producer transport.Producer) error { + specSyncInterval := config.SyncerConfig.SpecSyncInterval + specDB := gorm.NewGormSpecDB() + + addDBSyncerFunctions := []func(ctrl.Manager, specdb.SpecDB, transport.Producer, time.Duration) error{ + // dbsyncer.AddHoHConfigDBToTransportSyncer, + syncers.AddPoliciesDBToTransportSyncer, + syncers.AddPlacementRulesDBToTransportSyncer, + syncers.AddPlacementBindingsDBToTransportSyncer, + syncers.AddApplicationsDBToTransportSyncer, + syncers.AddSubscriptionsDBToTransportSyncer, + syncers.AddChannelsDBToTransportSyncer, + syncers.AddManagedClusterLabelsDBToTransportSyncer, + syncers.AddPlacementsDBToTransportSyncer, + syncers.AddManagedClusterSetsDBToTransportSyncer, + syncers.AddManagedClusterSetBindingsDBToTransportSyncer, + } + for _, addDBSyncerFunction := range addDBSyncerFunctions { + if err := addDBSyncerFunction(mgr, specDB, producer, specSyncInterval); err != nil { + return fmt.Errorf("failed to add DB Syncer: %w", err) + } + } + return nil +} + +// AddManagedClusterLabelDBSyncer update the label table by the managed cluster table +func AddManagedClusterLabelDBSyncer(mgr ctrl.Manager, deletedLabelsTrimmingInterval time.Duration) error { + if err := syncers.AddManagedClusterLabelsSyncer(mgr, deletedLabelsTrimmingInterval); err != nil { + return fmt.Errorf("failed to add status watcher: %w", err) + } + return nil +} diff --git a/manager/pkg/specsyncer/db2transport/db/gorm/spec.go b/manager/pkg/spec/specdb/gorm/spec.go similarity index 99% rename from manager/pkg/specsyncer/db2transport/db/gorm/spec.go rename to manager/pkg/spec/specdb/gorm/spec.go index 37dd573f6..42c1d0c68 100644 --- a/manager/pkg/specsyncer/db2transport/db/gorm/spec.go +++ b/manager/pkg/spec/specdb/gorm/spec.go @@ -10,7 +10,7 @@ import ( "github.com/jackc/pgx/v4" "sigs.k8s.io/controller-runtime/pkg/client" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/bundle" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/controllers/bundle" "github.com/stolostron/multicluster-global-hub/pkg/database" ) diff --git a/manager/pkg/specsyncer/db2transport/db/db.go b/manager/pkg/spec/specdb/interface.go similarity index 97% rename from manager/pkg/specsyncer/db2transport/db/db.go rename to manager/pkg/spec/specdb/interface.go index a220fc884..bca45e107 100644 --- a/manager/pkg/specsyncer/db2transport/db/db.go +++ b/manager/pkg/spec/specdb/interface.go @@ -1,7 +1,7 @@ // Copyright (c) 2020 Red Hat, Inc. // Copyright Contributors to the Open Cluster Management project -package db +package specdb import ( "context" @@ -9,7 +9,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/bundle" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/controllers/bundle" ) // SpecDB is the needed interface for the spec syncer and spec transport diff --git a/manager/pkg/specsyncer/db2transport/db/postgresql/postgresql.go b/manager/pkg/spec/specdb/postgresql/postgresql.go similarity index 99% rename from manager/pkg/specsyncer/db2transport/db/postgresql/postgresql.go rename to manager/pkg/spec/specdb/postgresql/postgresql.go index f9c123498..517dcc49c 100644 --- a/manager/pkg/specsyncer/db2transport/db/postgresql/postgresql.go +++ b/manager/pkg/spec/specdb/postgresql/postgresql.go @@ -14,8 +14,8 @@ import ( "github.com/jackc/pgx/v4/pgxpool" "sigs.k8s.io/controller-runtime/pkg/client" - managerconfig "github.com/stolostron/multicluster-global-hub/manager/pkg/config" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/bundle" + managerconfig "github.com/stolostron/multicluster-global-hub/manager/pkg/configs" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/controllers/bundle" "github.com/stolostron/multicluster-global-hub/pkg/bundle/spec" "github.com/stolostron/multicluster-global-hub/pkg/database" ) @@ -339,7 +339,6 @@ func (p *PostgreSQL) GetManagedClusterLabelsStatus(ctx context.Context, tableNam } // GetManagedClusterLeafHubName returns leaf-hub name for a given managed cluster from a specific table. -// TODO: once non-k8s-restapi exposes hub names, remove line. func (p *PostgreSQL) GetManagedClusterLeafHubName(ctx context.Context, tableName string, managedClusterName string, ) (string, error) { diff --git a/manager/pkg/specsyncer/db2transport/syncer/dbsyncer/applications_db_to_transport_syncer.go b/manager/pkg/spec/syncers/applications_syncer.go similarity index 85% rename from manager/pkg/specsyncer/db2transport/syncer/dbsyncer/applications_db_to_transport_syncer.go rename to manager/pkg/spec/syncers/applications_syncer.go index c12a2f1c0..9cf0f026d 100644 --- a/manager/pkg/specsyncer/db2transport/syncer/dbsyncer/applications_db_to_transport_syncer.go +++ b/manager/pkg/spec/syncers/applications_syncer.go @@ -1,4 +1,4 @@ -package dbsyncer +package syncers import ( "context" @@ -9,9 +9,9 @@ import ( applicationv1beta1 "sigs.k8s.io/application/api/v1beta1" ctrl "sigs.k8s.io/controller-runtime" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/bundle" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/db" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/intervalpolicy" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/controllers/bundle" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/specdb" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/syncers/interval" "github.com/stolostron/multicluster-global-hub/pkg/transport" ) @@ -21,7 +21,7 @@ const ( ) // AddApplicationsDBToTransportSyncer adds applications db to transport syncer to the manager. -func AddApplicationsDBToTransportSyncer(mgr ctrl.Manager, specDB db.SpecDB, producer transport.Producer, +func AddApplicationsDBToTransportSyncer(mgr ctrl.Manager, specDB specdb.SpecDB, producer transport.Producer, specSyncInterval time.Duration, ) error { createObjFunc := func() metav1.Object { return &applicationv1beta1.Application{} } @@ -29,7 +29,7 @@ func AddApplicationsDBToTransportSyncer(mgr ctrl.Manager, specDB db.SpecDB, prod if err := mgr.Add(&genericDBToTransportSyncer{ log: ctrl.Log.WithName("db-to-transport-syncer-application"), - intervalPolicy: intervalpolicy.NewExponentialBackoffPolicy(specSyncInterval), + intervalPolicy: interval.NewExponentialBackoffPolicy(specSyncInterval), syncBundleFunc: func(ctx context.Context) (bool, error) { return syncObjectsBundle(ctx, producer, applicationsMsgKey, specDB, applicationsTableName, createObjFunc, bundle.NewBaseObjectsBundle, lastSyncTimestampPtr) diff --git a/manager/pkg/specsyncer/db2transport/syncer/dbsyncer/channels_db_to_transport_syncer.go b/manager/pkg/spec/syncers/channels_syncer.go similarity index 81% rename from manager/pkg/specsyncer/db2transport/syncer/dbsyncer/channels_db_to_transport_syncer.go rename to manager/pkg/spec/syncers/channels_syncer.go index 66ccc466e..90b06ebbe 100644 --- a/manager/pkg/specsyncer/db2transport/syncer/dbsyncer/channels_db_to_transport_syncer.go +++ b/manager/pkg/spec/syncers/channels_syncer.go @@ -1,4 +1,4 @@ -package dbsyncer +package syncers import ( "context" @@ -9,9 +9,9 @@ import ( channelv1 "open-cluster-management.io/multicloud-operators-channel/pkg/apis/apps/v1" ctrl "sigs.k8s.io/controller-runtime" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/bundle" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/db" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/intervalpolicy" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/controllers/bundle" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/specdb" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/syncers/interval" "github.com/stolostron/multicluster-global-hub/pkg/transport" ) @@ -21,7 +21,7 @@ const ( ) // AddChannelsDBToTransportSyncer adds channels db to transport syncer to the manager. -func AddChannelsDBToTransportSyncer(mgr ctrl.Manager, specDB db.SpecDB, producer transport.Producer, +func AddChannelsDBToTransportSyncer(mgr ctrl.Manager, specDB specdb.SpecDB, producer transport.Producer, specSyncInterval time.Duration, ) error { createObjFunc := func() metav1.Object { return &channelv1.Channel{} } @@ -29,7 +29,7 @@ func AddChannelsDBToTransportSyncer(mgr ctrl.Manager, specDB db.SpecDB, producer if err := mgr.Add(&genericDBToTransportSyncer{ log: ctrl.Log.WithName("db-to-transport-syncer-channels"), - intervalPolicy: intervalpolicy.NewExponentialBackoffPolicy(specSyncInterval), + intervalPolicy: interval.NewExponentialBackoffPolicy(specSyncInterval), syncBundleFunc: func(ctx context.Context) (bool, error) { return syncObjectsBundle(ctx, producer, channelsMsgKey, specDB, channelsTableName, createObjFunc, bundle.NewBaseObjectsBundle, lastSyncTimestampPtr) diff --git a/manager/pkg/specsyncer/db2transport/syncer/dbsyncer/hoh_config_db_to_transport_syncer.go b/manager/pkg/spec/syncers/configmap_syncer.go similarity index 80% rename from manager/pkg/specsyncer/db2transport/syncer/dbsyncer/hoh_config_db_to_transport_syncer.go rename to manager/pkg/spec/syncers/configmap_syncer.go index 2a122a7d9..1e4036242 100644 --- a/manager/pkg/specsyncer/db2transport/syncer/dbsyncer/hoh_config_db_to_transport_syncer.go +++ b/manager/pkg/spec/syncers/configmap_syncer.go @@ -1,4 +1,4 @@ -package dbsyncer +package syncers import ( "context" @@ -9,9 +9,9 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ctrl "sigs.k8s.io/controller-runtime" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/bundle" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/db" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/intervalpolicy" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/controllers/bundle" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/specdb" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/syncers/interval" "github.com/stolostron/multicluster-global-hub/pkg/transport" ) @@ -22,7 +22,7 @@ const ( // AddHoHConfigDBToTransportSyncer adds hub-of-hubs config db to transport syncer to the manager. // the config is synced by addon manifests -func AddHoHConfigDBToTransportSyncer(mgr ctrl.Manager, specDB db.SpecDB, producer transport.Producer, +func AddHoHConfigDBToTransportSyncer(mgr ctrl.Manager, specDB specdb.SpecDB, producer transport.Producer, specSyncInterval time.Duration, ) error { createObjFunc := func() metav1.Object { return &corev1.ConfigMap{} } @@ -30,7 +30,7 @@ func AddHoHConfigDBToTransportSyncer(mgr ctrl.Manager, specDB db.SpecDB, produce if err := mgr.Add(&genericDBToTransportSyncer{ log: ctrl.Log.WithName("db-to-transport-syncer-configmap"), - intervalPolicy: intervalpolicy.NewExponentialBackoffPolicy(specSyncInterval), + intervalPolicy: interval.NewExponentialBackoffPolicy(specSyncInterval), syncBundleFunc: func(ctx context.Context) (bool, error) { return syncObjectsBundle(ctx, producer, configMsgKey, specDB, configTableName, createObjFunc, bundle.NewBaseObjectsBundle, lastSyncTimestampPtr) diff --git a/manager/pkg/specsyncer/db2transport/syncer/dbsyncer/generic_db_to_transport_syncer.go b/manager/pkg/spec/syncers/generic_syncer.go similarity index 94% rename from manager/pkg/specsyncer/db2transport/syncer/dbsyncer/generic_db_to_transport_syncer.go rename to manager/pkg/spec/syncers/generic_syncer.go index 01517e550..b500de969 100644 --- a/manager/pkg/specsyncer/db2transport/syncer/dbsyncer/generic_db_to_transport_syncer.go +++ b/manager/pkg/spec/syncers/generic_syncer.go @@ -1,4 +1,4 @@ -package dbsyncer +package syncers import ( "context" @@ -8,9 +8,9 @@ import ( "github.com/go-logr/logr" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/bundle" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/db" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/intervalpolicy" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/controllers/bundle" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/specdb" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/syncers/interval" "github.com/stolostron/multicluster-global-hub/pkg/constants" "github.com/stolostron/multicluster-global-hub/pkg/transport" "github.com/stolostron/multicluster-global-hub/pkg/utils" @@ -18,7 +18,7 @@ import ( type genericDBToTransportSyncer struct { log logr.Logger - intervalPolicy intervalpolicy.IntervalPolicy + intervalPolicy interval.IntervalPolicy syncBundleFunc func(ctx context.Context) (bool, error) } @@ -82,7 +82,7 @@ func (syncer *genericDBToTransportSyncer) periodicSync(ctx context.Context) { // syncObjectsBundle performs the actual sync logic and returns true if bundle was committed to transport, // otherwise false. func syncObjectsBundle(ctx context.Context, producer transport.Producer, eventType string, - specDB db.SpecDB, dbTableName string, createObjFunc bundle.CreateObjectFunction, + specDB specdb.SpecDB, dbTableName string, createObjFunc bundle.CreateObjectFunction, createBundleFunc bundle.CreateBundleFunction, lastSyncTimestampPtr *time.Time, ) (bool, error) { lastUpdateTimestamp, err := specDB.GetLastUpdateTimestamp(ctx, dbTableName, true) // filter local resources diff --git a/manager/pkg/specsyncer/db2transport/intervalpolicy/exponential_backoff_policy.go b/manager/pkg/spec/syncers/interval/exponential_backoff_policy.go similarity index 98% rename from manager/pkg/specsyncer/db2transport/intervalpolicy/exponential_backoff_policy.go rename to manager/pkg/spec/syncers/interval/exponential_backoff_policy.go index f522269db..1f4397854 100644 --- a/manager/pkg/specsyncer/db2transport/intervalpolicy/exponential_backoff_policy.go +++ b/manager/pkg/spec/syncers/interval/exponential_backoff_policy.go @@ -1,4 +1,4 @@ -package intervalpolicy +package interval import ( "time" diff --git a/manager/pkg/specsyncer/db2transport/intervalpolicy/interval_policy.go b/manager/pkg/spec/syncers/interval/interval_policy.go similarity index 94% rename from manager/pkg/specsyncer/db2transport/intervalpolicy/interval_policy.go rename to manager/pkg/spec/syncers/interval/interval_policy.go index 79c8d2a70..558a8c35b 100644 --- a/manager/pkg/specsyncer/db2transport/intervalpolicy/interval_policy.go +++ b/manager/pkg/spec/syncers/interval/interval_policy.go @@ -1,4 +1,4 @@ -package intervalpolicy +package interval import "time" diff --git a/manager/pkg/specsyncer/db2transport/syncer/dbsyncer/managed_cluster_labels_db_to_transport_syncer.go b/manager/pkg/spec/syncers/managedcluster_labels_syncer.go similarity index 95% rename from manager/pkg/specsyncer/db2transport/syncer/dbsyncer/managed_cluster_labels_db_to_transport_syncer.go rename to manager/pkg/spec/syncers/managedcluster_labels_syncer.go index 81a90b640..87867caae 100644 --- a/manager/pkg/specsyncer/db2transport/syncer/dbsyncer/managed_cluster_labels_db_to_transport_syncer.go +++ b/manager/pkg/spec/syncers/managedcluster_labels_syncer.go @@ -1,4 +1,4 @@ -package dbsyncer +package syncers import ( "context" @@ -10,8 +10,8 @@ import ( "gorm.io/gorm" ctrl "sigs.k8s.io/controller-runtime" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/db" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/intervalpolicy" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/specdb" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/syncers/interval" "github.com/stolostron/multicluster-global-hub/pkg/bundle/spec" "github.com/stolostron/multicluster-global-hub/pkg/constants" "github.com/stolostron/multicluster-global-hub/pkg/database" @@ -23,14 +23,14 @@ import ( const managedClusterLabelsDBTableName = "managed_clusters_labels" // AddManagedClusterLabelsDBToTransportSyncer adds managed-cluster labels db to transport syncer to the manager. -func AddManagedClusterLabelsDBToTransportSyncer(mgr ctrl.Manager, specDB db.SpecDB, producer transport.Producer, +func AddManagedClusterLabelsDBToTransportSyncer(mgr ctrl.Manager, specDB specdb.SpecDB, producer transport.Producer, specSyncInterval time.Duration, ) error { lastSyncTimestampPtr := &time.Time{} if err := mgr.Add(&genericDBToTransportSyncer{ log: ctrl.Log.WithName("db-to-transport-syncer-managedclusterlabel"), - intervalPolicy: intervalpolicy.NewExponentialBackoffPolicy(specSyncInterval), + intervalPolicy: interval.NewExponentialBackoffPolicy(specSyncInterval), syncBundleFunc: func(ctx context.Context) (bool, error) { return syncManagedClusterLabelsBundles(ctx, producer, constants.ManagedClustersLabelsMsgKey, specDB, @@ -46,7 +46,7 @@ func AddManagedClusterLabelsDBToTransportSyncer(mgr ctrl.Manager, specDB db.Spec // syncManagedClusterLabelsBundles performs the actual sync logic and returns true if bundle was committed to transport, // otherwise false. func syncManagedClusterLabelsBundles(ctx context.Context, producer transport.Producer, transportBundleKey string, - specDB db.SpecDB, dbTableName string, lastSyncTimestampPtr *time.Time, + specDB specdb.SpecDB, dbTableName string, lastSyncTimestampPtr *time.Time, ) (bool, error) { lastUpdateTimestamp, err := specDB.GetLastUpdateTimestamp(ctx, dbTableName, false) // no resources in table if err != nil { diff --git a/manager/pkg/specsyncer/db2transport/syncer/dbsyncer/managed_cluster_labels_syncer.go b/manager/pkg/spec/syncers/managedcluster_labels_watcher.go similarity index 97% rename from manager/pkg/specsyncer/db2transport/syncer/dbsyncer/managed_cluster_labels_syncer.go rename to manager/pkg/spec/syncers/managedcluster_labels_watcher.go index b0a6e940c..605194205 100644 --- a/manager/pkg/specsyncer/db2transport/syncer/dbsyncer/managed_cluster_labels_syncer.go +++ b/manager/pkg/spec/syncers/managedcluster_labels_watcher.go @@ -1,4 +1,4 @@ -package dbsyncer +package syncers import ( "context" @@ -10,9 +10,9 @@ import ( "github.com/go-logr/logr" ctrl "sigs.k8s.io/controller-runtime" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/db" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/db/gorm" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/intervalpolicy" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/specdb" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/specdb/gorm" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/syncers/interval" "github.com/stolostron/multicluster-global-hub/pkg/bundle/spec" "github.com/stolostron/multicluster-global-hub/pkg/database" ) @@ -28,7 +28,7 @@ func AddManagedClusterLabelsSyncer(mgr ctrl.Manager, deletedLabelsTrimmingInterv if err := mgr.Add(&managedClusterLabelsStatusWatcher{ log: ctrl.Log.WithName("managed-cluster-labels-syncer"), specDB: gorm.NewGormSpecDB(), - intervalPolicy: intervalpolicy.NewExponentialBackoffPolicy(deletedLabelsTrimmingInterval), + intervalPolicy: interval.NewExponentialBackoffPolicy(deletedLabelsTrimmingInterval), }); err != nil { return fmt.Errorf("failed to add managed-cluster labels status watcher - %w", err) } @@ -40,8 +40,8 @@ func AddManagedClusterLabelsSyncer(mgr ctrl.Manager, deletedLabelsTrimmingInterv // table where required (e.g., trim deleted_label_keys). type managedClusterLabelsStatusWatcher struct { log logr.Logger - specDB db.SpecDB - intervalPolicy intervalpolicy.IntervalPolicy + specDB specdb.SpecDB + intervalPolicy interval.IntervalPolicy } func (watcher *managedClusterLabelsStatusWatcher) Start(ctx context.Context) error { diff --git a/manager/pkg/specsyncer/db2transport/syncer/dbsyncer/managed_cluster_set_bindings_db_to_transport_syncer.go b/manager/pkg/spec/syncers/managedclustersetbindings_syncer.go similarity index 87% rename from manager/pkg/specsyncer/db2transport/syncer/dbsyncer/managed_cluster_set_bindings_db_to_transport_syncer.go rename to manager/pkg/spec/syncers/managedclustersetbindings_syncer.go index 34fe434dd..91f10f37e 100644 --- a/manager/pkg/specsyncer/db2transport/syncer/dbsyncer/managed_cluster_set_bindings_db_to_transport_syncer.go +++ b/manager/pkg/spec/syncers/managedclustersetbindings_syncer.go @@ -1,4 +1,4 @@ -package dbsyncer +package syncers import ( "context" @@ -9,9 +9,9 @@ import ( clusterv1beta2 "open-cluster-management.io/api/cluster/v1beta2" ctrl "sigs.k8s.io/controller-runtime" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/bundle" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/db" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/intervalpolicy" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/controllers/bundle" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/specdb" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/syncers/interval" "github.com/stolostron/multicluster-global-hub/pkg/transport" ) @@ -22,7 +22,7 @@ const ( // AddManagedClusterSetBindingsDBToTransportSyncer adds managed-cluster-set-bindings db to transport syncer to the // manager. -func AddManagedClusterSetBindingsDBToTransportSyncer(mgr ctrl.Manager, specDB db.SpecDB, +func AddManagedClusterSetBindingsDBToTransportSyncer(mgr ctrl.Manager, specDB specdb.SpecDB, producer transport.Producer, specSyncInterval time.Duration, ) error { createObjFunc := func() metav1.Object { @@ -32,7 +32,7 @@ func AddManagedClusterSetBindingsDBToTransportSyncer(mgr ctrl.Manager, specDB db if err := mgr.Add(&genericDBToTransportSyncer{ log: ctrl.Log.WithName("db-to-transport-syncer-managedclustersetbinding"), - intervalPolicy: intervalpolicy.NewExponentialBackoffPolicy(specSyncInterval), + intervalPolicy: interval.NewExponentialBackoffPolicy(specSyncInterval), syncBundleFunc: func(ctx context.Context) (bool, error) { return syncObjectsBundle(ctx, producer, managedClusterSetBindingsMsgKey, specDB, managedClusterSetBindingsTableName, createObjFunc, bundle.NewBaseObjectsBundle, lastSyncTimestampPtr) diff --git a/manager/pkg/specsyncer/db2transport/syncer/dbsyncer/managed_cluster_sets_db_to_transport_syncer.go b/manager/pkg/spec/syncers/managedclustersets_syncer.go similarity index 85% rename from manager/pkg/specsyncer/db2transport/syncer/dbsyncer/managed_cluster_sets_db_to_transport_syncer.go rename to manager/pkg/spec/syncers/managedclustersets_syncer.go index bcfcccd43..0e79f5eae 100644 --- a/manager/pkg/specsyncer/db2transport/syncer/dbsyncer/managed_cluster_sets_db_to_transport_syncer.go +++ b/manager/pkg/spec/syncers/managedclustersets_syncer.go @@ -1,4 +1,4 @@ -package dbsyncer +package syncers import ( "context" @@ -9,9 +9,9 @@ import ( clusterv1beta2 "open-cluster-management.io/api/cluster/v1beta2" ctrl "sigs.k8s.io/controller-runtime" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/bundle" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/db" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/intervalpolicy" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/controllers/bundle" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/specdb" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/syncers/interval" "github.com/stolostron/multicluster-global-hub/pkg/transport" ) @@ -21,7 +21,7 @@ const ( ) // AddManagedClusterSetsDBToTransportSyncer adds managed-cluster-sets db to transport syncer to the manager. -func AddManagedClusterSetsDBToTransportSyncer(mgr ctrl.Manager, specDB db.SpecDB, producer transport.Producer, +func AddManagedClusterSetsDBToTransportSyncer(mgr ctrl.Manager, specDB specdb.SpecDB, producer transport.Producer, specSyncInterval time.Duration, ) error { createObjFunc := func() metav1.Object { return &clusterv1beta2.ManagedClusterSet{} } @@ -29,7 +29,7 @@ func AddManagedClusterSetsDBToTransportSyncer(mgr ctrl.Manager, specDB db.SpecDB if err := mgr.Add(&genericDBToTransportSyncer{ log: ctrl.Log.WithName("db-to-transport-syncer-managedclusterset"), - intervalPolicy: intervalpolicy.NewExponentialBackoffPolicy(specSyncInterval), + intervalPolicy: interval.NewExponentialBackoffPolicy(specSyncInterval), syncBundleFunc: func(ctx context.Context) (bool, error) { return syncObjectsBundle(ctx, producer, managedClusterSetsMsgKey, specDB, managedClusterSetsTableName, createObjFunc, bundle.NewBaseObjectsBundle, lastSyncTimestampPtr) diff --git a/manager/pkg/specsyncer/db2transport/syncer/dbsyncer/placementbindings_db_to_transport_syncer.go b/manager/pkg/spec/syncers/placementbindings_syncer.go similarity index 85% rename from manager/pkg/specsyncer/db2transport/syncer/dbsyncer/placementbindings_db_to_transport_syncer.go rename to manager/pkg/spec/syncers/placementbindings_syncer.go index bde1baaa6..7db33065e 100644 --- a/manager/pkg/specsyncer/db2transport/syncer/dbsyncer/placementbindings_db_to_transport_syncer.go +++ b/manager/pkg/spec/syncers/placementbindings_syncer.go @@ -1,4 +1,4 @@ -package dbsyncer +package syncers import ( "context" @@ -9,9 +9,9 @@ import ( policyv1 "open-cluster-management.io/governance-policy-propagator/api/v1" ctrl "sigs.k8s.io/controller-runtime" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/bundle" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/db" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/intervalpolicy" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/controllers/bundle" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/specdb" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/syncers/interval" "github.com/stolostron/multicluster-global-hub/pkg/transport" ) @@ -21,7 +21,7 @@ const ( ) // AddPlacementBindingsDBToTransportSyncer adds placement bindings db to transport syncer to the manager. -func AddPlacementBindingsDBToTransportSyncer(mgr ctrl.Manager, specDB db.SpecDB, producer transport.Producer, +func AddPlacementBindingsDBToTransportSyncer(mgr ctrl.Manager, specDB specdb.SpecDB, producer transport.Producer, specSyncInterval time.Duration, ) error { createObjFunc := func() metav1.Object { return &policyv1.PlacementBinding{} } @@ -29,7 +29,7 @@ func AddPlacementBindingsDBToTransportSyncer(mgr ctrl.Manager, specDB db.SpecDB, if err := mgr.Add(&genericDBToTransportSyncer{ log: ctrl.Log.WithName("db-to-transport-syncer-placementrulebiding"), - intervalPolicy: intervalpolicy.NewExponentialBackoffPolicy(specSyncInterval), + intervalPolicy: interval.NewExponentialBackoffPolicy(specSyncInterval), syncBundleFunc: func(ctx context.Context) (bool, error) { return syncObjectsBundle(ctx, producer, placementBindingsMsgKey, specDB, placementBindingsTableName, createObjFunc, bundle.NewBaseObjectsBundle, lastSyncTimestampPtr) diff --git a/manager/pkg/specsyncer/db2transport/syncer/dbsyncer/placementrules_db_to_transport_syncer.go b/manager/pkg/spec/syncers/placementrules_syncer.go similarity index 86% rename from manager/pkg/specsyncer/db2transport/syncer/dbsyncer/placementrules_db_to_transport_syncer.go rename to manager/pkg/spec/syncers/placementrules_syncer.go index b71654fd1..c6c7348a6 100644 --- a/manager/pkg/specsyncer/db2transport/syncer/dbsyncer/placementrules_db_to_transport_syncer.go +++ b/manager/pkg/spec/syncers/placementrules_syncer.go @@ -1,4 +1,4 @@ -package dbsyncer +package syncers import ( "context" @@ -9,9 +9,9 @@ import ( placementrulev1 "open-cluster-management.io/multicloud-operators-subscription/pkg/apis/apps/placementrule/v1" ctrl "sigs.k8s.io/controller-runtime" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/bundle" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/db" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/intervalpolicy" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/controllers/bundle" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/specdb" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/syncers/interval" "github.com/stolostron/multicluster-global-hub/pkg/transport" ) @@ -21,7 +21,7 @@ const ( ) // AddPlacementRulesDBToTransportSyncer adds placement rules db to transport syncer to the manager. -func AddPlacementRulesDBToTransportSyncer(mgr ctrl.Manager, specDB db.SpecDB, producer transport.Producer, +func AddPlacementRulesDBToTransportSyncer(mgr ctrl.Manager, specDB specdb.SpecDB, producer transport.Producer, specSyncInterval time.Duration, ) error { createObjFunc := func() metav1.Object { return &placementrulev1.PlacementRule{} } @@ -29,7 +29,7 @@ func AddPlacementRulesDBToTransportSyncer(mgr ctrl.Manager, specDB db.SpecDB, pr if err := mgr.Add(&genericDBToTransportSyncer{ log: ctrl.Log.WithName("db-to-transport-syncer-placementrule"), - intervalPolicy: intervalpolicy.NewExponentialBackoffPolicy(specSyncInterval), + intervalPolicy: interval.NewExponentialBackoffPolicy(specSyncInterval), syncBundleFunc: func(ctx context.Context) (bool, error) { return syncObjectsBundle(ctx, producer, placementRulesMsgKey, specDB, placementRulesTableName, createObjFunc, bundle.NewBaseObjectsBundle, lastSyncTimestampPtr) diff --git a/manager/pkg/specsyncer/db2transport/syncer/dbsyncer/placements_db_to_transport_syncer.go b/manager/pkg/spec/syncers/placements_syncer.go similarity index 81% rename from manager/pkg/specsyncer/db2transport/syncer/dbsyncer/placements_db_to_transport_syncer.go rename to manager/pkg/spec/syncers/placements_syncer.go index 8ccddf10d..65a4bbf73 100644 --- a/manager/pkg/specsyncer/db2transport/syncer/dbsyncer/placements_db_to_transport_syncer.go +++ b/manager/pkg/spec/syncers/placements_syncer.go @@ -1,4 +1,4 @@ -package dbsyncer +package syncers import ( "context" @@ -9,9 +9,9 @@ import ( clusterv1beta1 "open-cluster-management.io/api/cluster/v1beta1" ctrl "sigs.k8s.io/controller-runtime" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/bundle" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/db" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/intervalpolicy" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/controllers/bundle" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/specdb" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/syncers/interval" "github.com/stolostron/multicluster-global-hub/pkg/transport" ) @@ -21,7 +21,7 @@ const ( ) // AddPlacementsDBToTransportSyncer adds placement db to transport syncer to the manager. -func AddPlacementsDBToTransportSyncer(mgr ctrl.Manager, specDB db.SpecDB, producer transport.Producer, +func AddPlacementsDBToTransportSyncer(mgr ctrl.Manager, specDB specdb.SpecDB, producer transport.Producer, specSyncInterval time.Duration, ) error { createObjFunc := func() metav1.Object { return &clusterv1beta1.Placement{} } @@ -29,7 +29,7 @@ func AddPlacementsDBToTransportSyncer(mgr ctrl.Manager, specDB db.SpecDB, produc if err := mgr.Add(&genericDBToTransportSyncer{ log: ctrl.Log.WithName("db-to-transport-syncer-placements"), - intervalPolicy: intervalpolicy.NewExponentialBackoffPolicy(specSyncInterval), + intervalPolicy: interval.NewExponentialBackoffPolicy(specSyncInterval), syncBundleFunc: func(ctx context.Context) (bool, error) { return syncObjectsBundle(ctx, producer, placementsMsgKey, specDB, placementsTableName, createObjFunc, bundle.NewBaseObjectsBundle, lastSyncTimestampPtr) diff --git a/manager/pkg/specsyncer/db2transport/syncer/dbsyncer/policies_db_to_transport_syncer.go b/manager/pkg/spec/syncers/policies_syncer.go similarity index 81% rename from manager/pkg/specsyncer/db2transport/syncer/dbsyncer/policies_db_to_transport_syncer.go rename to manager/pkg/spec/syncers/policies_syncer.go index d67c78753..f3920cfea 100644 --- a/manager/pkg/specsyncer/db2transport/syncer/dbsyncer/policies_db_to_transport_syncer.go +++ b/manager/pkg/spec/syncers/policies_syncer.go @@ -1,4 +1,4 @@ -package dbsyncer +package syncers import ( "context" @@ -9,9 +9,9 @@ import ( policyv1 "open-cluster-management.io/governance-policy-propagator/api/v1" ctrl "sigs.k8s.io/controller-runtime" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/bundle" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/db" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/intervalpolicy" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/controllers/bundle" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/specdb" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/syncers/interval" "github.com/stolostron/multicluster-global-hub/pkg/transport" ) @@ -21,7 +21,7 @@ const ( ) // AddPoliciesDBToTransportSyncer adds policies db to transport syncer to the manager. -func AddPoliciesDBToTransportSyncer(mgr ctrl.Manager, specDB db.SpecDB, producer transport.Producer, +func AddPoliciesDBToTransportSyncer(mgr ctrl.Manager, specDB specdb.SpecDB, producer transport.Producer, specSyncInterval time.Duration, ) error { createObjFunc := func() metav1.Object { return &policyv1.Policy{} } @@ -29,7 +29,7 @@ func AddPoliciesDBToTransportSyncer(mgr ctrl.Manager, specDB db.SpecDB, producer if err := mgr.Add(&genericDBToTransportSyncer{ log: ctrl.Log.WithName("db-to-transport-syncer-policy"), - intervalPolicy: intervalpolicy.NewExponentialBackoffPolicy(specSyncInterval), + intervalPolicy: interval.NewExponentialBackoffPolicy(specSyncInterval), syncBundleFunc: func(ctx context.Context) (bool, error) { return syncObjectsBundle(ctx, producer, policiesMsgKey, specDB, policiesTableName, createObjFunc, bundle.NewBaseObjectsBundle, lastSyncTimestampPtr) diff --git a/manager/pkg/specsyncer/db2transport/syncer/dbsyncer/subscriptions_db_to_transport_syncer.go b/manager/pkg/spec/syncers/subscriptions_syncer.go similarity index 85% rename from manager/pkg/specsyncer/db2transport/syncer/dbsyncer/subscriptions_db_to_transport_syncer.go rename to manager/pkg/spec/syncers/subscriptions_syncer.go index 6125b95f2..a4a42948a 100644 --- a/manager/pkg/specsyncer/db2transport/syncer/dbsyncer/subscriptions_db_to_transport_syncer.go +++ b/manager/pkg/spec/syncers/subscriptions_syncer.go @@ -1,4 +1,4 @@ -package dbsyncer +package syncers import ( "context" @@ -9,9 +9,9 @@ import ( subscriptionv1 "open-cluster-management.io/multicloud-operators-subscription/pkg/apis/apps/v1" ctrl "sigs.k8s.io/controller-runtime" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/bundle" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/db" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/intervalpolicy" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/controllers/bundle" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/specdb" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/syncers/interval" "github.com/stolostron/multicluster-global-hub/pkg/transport" ) @@ -21,7 +21,7 @@ const ( ) // AddSubscriptionsDBToTransportSyncer adds subscriptions db to transport syncer to the manager. -func AddSubscriptionsDBToTransportSyncer(mgr ctrl.Manager, specDB db.SpecDB, producer transport.Producer, +func AddSubscriptionsDBToTransportSyncer(mgr ctrl.Manager, specDB specdb.SpecDB, producer transport.Producer, specSyncInterval time.Duration, ) error { createObjFunc := func() metav1.Object { return &subscriptionv1.Subscription{} } @@ -29,7 +29,7 @@ func AddSubscriptionsDBToTransportSyncer(mgr ctrl.Manager, specDB db.SpecDB, pro if err := mgr.Add(&genericDBToTransportSyncer{ log: ctrl.Log.WithName("db-to-transport-syncer-subscriptions"), - intervalPolicy: intervalpolicy.NewExponentialBackoffPolicy(specSyncInterval), + intervalPolicy: interval.NewExponentialBackoffPolicy(specSyncInterval), syncBundleFunc: func(ctx context.Context) (bool, error) { return syncObjectsBundle(ctx, producer, subscriptionMsgKey, specDB, subscriptionsTableName, createObjFunc, bundle.NewBaseObjectsBundle, lastSyncTimestampPtr) diff --git a/manager/pkg/specsyncer/db2transport/bundle/bundle/base_objects_bundle.go b/manager/pkg/specsyncer/db2transport/bundle/bundle/base_objects_bundle.go deleted file mode 100644 index 8648aee71..000000000 --- a/manager/pkg/specsyncer/db2transport/bundle/bundle/base_objects_bundle.go +++ /dev/null @@ -1,43 +0,0 @@ -package bundle - -import ( - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - - "github.com/stolostron/multicluster-global-hub/pkg/constants" -) - -// NewBaseObjectsBundle creates a new base bundle with no data in it. -func NewBaseObjectsBundle() ObjectsBundle { - return &baseObjectsBundle{ - Objects: make([]metav1.Object, 0), - DeletedObjects: make([]metav1.Object, 0), - } -} - -type baseObjectsBundle struct { - Objects []metav1.Object `json:"objects"` - DeletedObjects []metav1.Object `json:"deletedObjects"` -} - -// AddObject adds an object to the bundle. -func (b *baseObjectsBundle) AddObject(object metav1.Object, objectUID string) { - setMetaDataAnnotation(object, constants.OriginOwnerReferenceAnnotation, objectUID) - b.Objects = append(b.Objects, object) -} - -// AddDeletedObject adds a deleted object to the bundle. -func (b *baseObjectsBundle) AddDeletedObject(object metav1.Object) { - b.DeletedObjects = append(b.DeletedObjects, object) -} - -// setMetaDataAnnotation sets metadata annotation on the given object. -func setMetaDataAnnotation(object metav1.Object, key string, value string) { - annotations := object.GetAnnotations() - if annotations == nil { - annotations = make(map[string]string) - } - - annotations[key] = value - - object.SetAnnotations(annotations) -} diff --git a/manager/pkg/specsyncer/db2transport/bundle/objects_bundle.go b/manager/pkg/specsyncer/db2transport/bundle/objects_bundle.go deleted file mode 100644 index 40ad95c84..000000000 --- a/manager/pkg/specsyncer/db2transport/bundle/objects_bundle.go +++ /dev/null @@ -1,20 +0,0 @@ -package bundle - -import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - -type ( - // CreateObjectFunction is a function that specifies how to create an object. - CreateObjectFunction func() metav1.Object - // CreateBundleFunction is a function that specifies how to create a bundle. - CreateBundleFunction func() ObjectsBundle - // ExtractObjectNameFunction is a function that specifies how to extract a name from an object. - ExtractObjectNameFunction func(metav1.Object) string -) - -// ObjectsBundle bundles together a set of k8s objects to be sent to leaf hubs via transport layer. -type ObjectsBundle interface { - // AddObject adds an object to the bundle. - AddObject(object metav1.Object, objectUID string) - // AddDeletedObject adds a deleted object to the bundle. - AddDeletedObject(object metav1.Object) -} diff --git a/manager/pkg/specsyncer/db2transport/syncer/syncers.go b/manager/pkg/specsyncer/db2transport/syncer/syncers.go deleted file mode 100644 index 117393c4c..000000000 --- a/manager/pkg/specsyncer/db2transport/syncer/syncers.go +++ /dev/null @@ -1,48 +0,0 @@ -package syncer - -import ( - "fmt" - "time" - - ctrl "sigs.k8s.io/controller-runtime" - - "github.com/stolostron/multicluster-global-hub/manager/pkg/config" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/db" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/db/gorm" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/syncer/dbsyncer" - "github.com/stolostron/multicluster-global-hub/pkg/transport" -) - -// AddDB2TransportSyncers adds the controllers that send info from DB to transport layer to the Manager. -func AddDB2TransportSyncers(mgr ctrl.Manager, managerConfig *config.ManagerConfig, producer transport.Producer) error { - specSyncInterval := managerConfig.SyncerConfig.SpecSyncInterval - - addDBSyncerFunctions := []func(ctrl.Manager, db.SpecDB, transport.Producer, time.Duration) error{ - // dbsyncer.AddHoHConfigDBToTransportSyncer, - dbsyncer.AddPoliciesDBToTransportSyncer, - dbsyncer.AddPlacementRulesDBToTransportSyncer, - dbsyncer.AddPlacementBindingsDBToTransportSyncer, - dbsyncer.AddApplicationsDBToTransportSyncer, - dbsyncer.AddSubscriptionsDBToTransportSyncer, - dbsyncer.AddChannelsDBToTransportSyncer, - dbsyncer.AddManagedClusterLabelsDBToTransportSyncer, - dbsyncer.AddPlacementsDBToTransportSyncer, - dbsyncer.AddManagedClusterSetsDBToTransportSyncer, - dbsyncer.AddManagedClusterSetBindingsDBToTransportSyncer, - } - specDB := gorm.NewGormSpecDB() - for _, addDBSyncerFunction := range addDBSyncerFunctions { - if err := addDBSyncerFunction(mgr, specDB, producer, specSyncInterval); err != nil { - return fmt.Errorf("failed to add DB Syncer: %w", err) - } - } - return nil -} - -// AddManagedClusterLabelSyncer update the label table by the managed cluster table -func AddManagedClusterLabelSyncer(mgr ctrl.Manager, deletedLabelsTrimmingInterval time.Duration) error { - if err := dbsyncer.AddManagedClusterLabelsSyncer(mgr, deletedLabelsTrimmingInterval); err != nil { - return fmt.Errorf("failed to add status watcher: %w", err) - } - return nil -} diff --git a/manager/pkg/specsyncer/spec2db/controllers.go b/manager/pkg/specsyncer/spec2db/controllers.go deleted file mode 100644 index 8795bcf42..000000000 --- a/manager/pkg/specsyncer/spec2db/controllers.go +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright (c) 2020 Red Hat, Inc. -// Copyright Contributors to the Open Cluster Management project - -package spec2db - -import ( - "fmt" - - ctrl "sigs.k8s.io/controller-runtime" - - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/db" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/db/gorm" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/spec2db/controller" -) - -// AddSpec2DBControllers adds all the spec-to-db controllers to the Manager. -func AddSpec2DBControllers(mgr ctrl.Manager) error { - addControllerFunctions := []func(ctrl.Manager, db.SpecDB) error{ - controller.AddPolicyController, - controller.AddPlacementRuleController, - controller.AddPlacementBindingController, - controller.AddApplicationController, - controller.AddSubscriptionController, - controller.AddChannelController, - controller.AddManagedClusterSetController, - controller.AddManagedClusterSetBindingController, - controller.AddPlacementController, - } - specDB := gorm.NewGormSpecDB() - for _, addControllerFunction := range addControllerFunctions { - if err := addControllerFunction(mgr, specDB); err != nil { - return fmt.Errorf("failed to add controller: %w", err) - } - } - - return nil -} diff --git a/manager/pkg/specsyncer/syncers.go b/manager/pkg/specsyncer/syncers.go deleted file mode 100644 index c14029efd..000000000 --- a/manager/pkg/specsyncer/syncers.go +++ /dev/null @@ -1,39 +0,0 @@ -package specsyncer - -import ( - "fmt" - - ctrl "sigs.k8s.io/controller-runtime" - - "github.com/stolostron/multicluster-global-hub/manager/pkg/config" - specsyncer "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/syncer" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/spec2db" - "github.com/stolostron/multicluster-global-hub/pkg/transport" -) - -var specCtrlStarted = false - -func AddGlobalResourceSpecSyncers(mgr ctrl.Manager, - managerConfig *config.ManagerConfig, - producer transport.Producer, -) error { - if specCtrlStarted { - return nil - } - - if err := spec2db.AddSpec2DBControllers(mgr); err != nil { - return fmt.Errorf("failed to add spec-to-db controllers: %w", err) - } - - if err := specsyncer.AddDB2TransportSyncers(mgr, managerConfig, producer); err != nil { - return fmt.Errorf("failed to add db-to-transport syncers: %w", err) - } - - if err := specsyncer.AddManagedClusterLabelSyncer(mgr, - managerConfig.SyncerConfig.DeletedLabelsTrimmingInterval); err != nil { - return fmt.Errorf("failed to add status db watchers: %w", err) - } - - specCtrlStarted = true - return nil -} diff --git a/manager/pkg/statussyncer/conflator/conflation_committer.go b/manager/pkg/status/conflator/conflation_committer.go similarity index 100% rename from manager/pkg/statussyncer/conflator/conflation_committer.go rename to manager/pkg/status/conflator/conflation_committer.go diff --git a/manager/pkg/statussyncer/conflator/conflation_committer_test.go b/manager/pkg/status/conflator/conflation_committer_test.go similarity index 98% rename from manager/pkg/statussyncer/conflator/conflation_committer_test.go rename to manager/pkg/status/conflator/conflation_committer_test.go index 432109d16..6cd8f2112 100644 --- a/manager/pkg/statussyncer/conflator/conflation_committer_test.go +++ b/manager/pkg/status/conflator/conflation_committer_test.go @@ -5,7 +5,7 @@ import ( "github.com/stretchr/testify/assert" - "github.com/stolostron/multicluster-global-hub/manager/pkg/statussyncer/conflator/metadata" + "github.com/stolostron/multicluster-global-hub/manager/pkg/status/conflator/metadata" "github.com/stolostron/multicluster-global-hub/pkg/transport" ) diff --git a/manager/pkg/statussyncer/conflator/conflation_job.go b/manager/pkg/status/conflator/conflation_job.go similarity index 100% rename from manager/pkg/statussyncer/conflator/conflation_job.go rename to manager/pkg/status/conflator/conflation_job.go diff --git a/manager/pkg/statussyncer/conflator/conflation_manager.go b/manager/pkg/status/conflator/conflation_manager.go similarity index 99% rename from manager/pkg/statussyncer/conflator/conflation_manager.go rename to manager/pkg/status/conflator/conflation_manager.go index a4650cf42..a6bd76b65 100644 --- a/manager/pkg/statussyncer/conflator/conflation_manager.go +++ b/manager/pkg/status/conflator/conflation_manager.go @@ -7,7 +7,7 @@ import ( "github.com/go-logr/logr" ctrl "sigs.k8s.io/controller-runtime" - "github.com/stolostron/multicluster-global-hub/manager/pkg/statussyncer/conflator/metadata" + "github.com/stolostron/multicluster-global-hub/manager/pkg/status/conflator/metadata" "github.com/stolostron/multicluster-global-hub/pkg/statistics" "github.com/stolostron/multicluster-global-hub/pkg/transport/consumer" ) diff --git a/manager/pkg/statussyncer/conflator/conflation_priority.go b/manager/pkg/status/conflator/conflation_priority.go similarity index 100% rename from manager/pkg/statussyncer/conflator/conflation_priority.go rename to manager/pkg/status/conflator/conflation_priority.go diff --git a/manager/pkg/statussyncer/conflator/conflation_registration.go b/manager/pkg/status/conflator/conflation_registration.go similarity index 97% rename from manager/pkg/statussyncer/conflator/conflation_registration.go rename to manager/pkg/status/conflator/conflation_registration.go index 296b88473..df7a2d691 100644 --- a/manager/pkg/statussyncer/conflator/conflation_registration.go +++ b/manager/pkg/status/conflator/conflation_registration.go @@ -5,7 +5,7 @@ import ( cloudevents "github.com/cloudevents/sdk-go/v2" - "github.com/stolostron/multicluster-global-hub/manager/pkg/statussyncer/conflator/dependency" + "github.com/stolostron/multicluster-global-hub/manager/pkg/status/conflator/dependency" "github.com/stolostron/multicluster-global-hub/pkg/enum" ) diff --git a/manager/pkg/statussyncer/conflator/conflation_unit.go b/manager/pkg/status/conflator/conflation_unit.go similarity index 100% rename from manager/pkg/statussyncer/conflator/conflation_unit.go rename to manager/pkg/status/conflator/conflation_unit.go diff --git a/manager/pkg/statussyncer/conflator/dependency/dependency.go b/manager/pkg/status/conflator/dependency/dependency.go similarity index 100% rename from manager/pkg/statussyncer/conflator/dependency/dependency.go rename to manager/pkg/status/conflator/dependency/dependency.go diff --git a/manager/pkg/statussyncer/conflator/dependency/type.go b/manager/pkg/status/conflator/dependency/type.go similarity index 100% rename from manager/pkg/statussyncer/conflator/dependency/type.go rename to manager/pkg/status/conflator/dependency/type.go diff --git a/manager/pkg/statussyncer/conflator/element_complete.go b/manager/pkg/status/conflator/element_complete.go similarity index 99% rename from manager/pkg/statussyncer/conflator/element_complete.go rename to manager/pkg/status/conflator/element_complete.go index e74cbe23d..4d0e2703a 100644 --- a/manager/pkg/statussyncer/conflator/element_complete.go +++ b/manager/pkg/status/conflator/element_complete.go @@ -8,7 +8,7 @@ import ( "github.com/go-logr/logr" ctrl "sigs.k8s.io/controller-runtime" - "github.com/stolostron/multicluster-global-hub/manager/pkg/statussyncer/conflator/dependency" + "github.com/stolostron/multicluster-global-hub/manager/pkg/status/conflator/dependency" "github.com/stolostron/multicluster-global-hub/pkg/bundle/version" "github.com/stolostron/multicluster-global-hub/pkg/enum" ) diff --git a/manager/pkg/statussyncer/conflator/element_delta.go b/manager/pkg/status/conflator/element_delta.go similarity index 98% rename from manager/pkg/statussyncer/conflator/element_delta.go rename to manager/pkg/status/conflator/element_delta.go index 7b77e8ac0..bd8ef4603 100644 --- a/manager/pkg/statussyncer/conflator/element_delta.go +++ b/manager/pkg/status/conflator/element_delta.go @@ -8,7 +8,7 @@ import ( "github.com/go-logr/logr" ctrl "sigs.k8s.io/controller-runtime" - "github.com/stolostron/multicluster-global-hub/manager/pkg/statussyncer/conflator/dependency" + "github.com/stolostron/multicluster-global-hub/manager/pkg/status/conflator/dependency" "github.com/stolostron/multicluster-global-hub/pkg/bundle/version" "github.com/stolostron/multicluster-global-hub/pkg/enum" ) diff --git a/manager/pkg/statussyncer/conflator/interface.go b/manager/pkg/status/conflator/interface.go similarity index 89% rename from manager/pkg/statussyncer/conflator/interface.go rename to manager/pkg/status/conflator/interface.go index 88da9341f..67a5551ac 100644 --- a/manager/pkg/statussyncer/conflator/interface.go +++ b/manager/pkg/status/conflator/interface.go @@ -36,12 +36,6 @@ type ResultReporter interface { ReportResult(m ConflationMetadata, err error) } -// Handler interface for registering business logic needed for handling bundles. -type Handler interface { - // RegisterHandler registers event handler functions within the conflation manager. - RegisterHandler(conflationManager *ConflationManager) -} - type ConflationElement interface { Name() string diff --git a/manager/pkg/statussyncer/conflator/metadata/threshold_metadata.go b/manager/pkg/status/conflator/metadata/threshold_metadata.go similarity index 100% rename from manager/pkg/statussyncer/conflator/metadata/threshold_metadata.go rename to manager/pkg/status/conflator/metadata/threshold_metadata.go diff --git a/manager/pkg/statussyncer/conflator/ready_queue.go b/manager/pkg/status/conflator/ready_queue.go similarity index 100% rename from manager/pkg/statussyncer/conflator/ready_queue.go rename to manager/pkg/status/conflator/ready_queue.go diff --git a/manager/pkg/statussyncer/conflator/workerpool/worker.go b/manager/pkg/status/conflator/workerpool/worker.go similarity index 99% rename from manager/pkg/statussyncer/conflator/workerpool/worker.go rename to manager/pkg/status/conflator/workerpool/worker.go index e7dcc5aa3..20c93cf3c 100644 --- a/manager/pkg/statussyncer/conflator/workerpool/worker.go +++ b/manager/pkg/status/conflator/workerpool/worker.go @@ -7,7 +7,7 @@ import ( "github.com/go-logr/logr" "k8s.io/apimachinery/pkg/util/wait" - "github.com/stolostron/multicluster-global-hub/manager/pkg/statussyncer/conflator" + "github.com/stolostron/multicluster-global-hub/manager/pkg/status/conflator" "github.com/stolostron/multicluster-global-hub/pkg/database" "github.com/stolostron/multicluster-global-hub/pkg/statistics" ) diff --git a/manager/pkg/statussyncer/conflator/workerpool/worker_pool.go b/manager/pkg/status/conflator/workerpool/worker_pool.go similarity index 100% rename from manager/pkg/statussyncer/conflator/workerpool/worker_pool.go rename to manager/pkg/status/conflator/workerpool/worker_pool.go diff --git a/manager/pkg/statussyncer/dispatcher/conflation_dispatcher.go b/manager/pkg/status/dispatcher/conflation_dispatcher.go similarity index 96% rename from manager/pkg/statussyncer/dispatcher/conflation_dispatcher.go rename to manager/pkg/status/dispatcher/conflation_dispatcher.go index c781c28d8..3fd60555b 100644 --- a/manager/pkg/statussyncer/dispatcher/conflation_dispatcher.go +++ b/manager/pkg/status/dispatcher/conflation_dispatcher.go @@ -9,9 +9,9 @@ import ( "k8s.io/apimachinery/pkg/util/wait" ctrl "sigs.k8s.io/controller-runtime" - "github.com/stolostron/multicluster-global-hub/manager/pkg/config" - "github.com/stolostron/multicluster-global-hub/manager/pkg/statussyncer/conflator" - "github.com/stolostron/multicluster-global-hub/manager/pkg/statussyncer/conflator/workerpool" + "github.com/stolostron/multicluster-global-hub/manager/pkg/configs" + "github.com/stolostron/multicluster-global-hub/manager/pkg/status/conflator" + "github.com/stolostron/multicluster-global-hub/manager/pkg/status/conflator/workerpool" "github.com/stolostron/multicluster-global-hub/pkg/statistics" ) @@ -27,7 +27,7 @@ func NewConflationDispatcher(log logr.Logger, conflationReadyQueue *conflator.Co } func AddConflationDispatcher(mgr ctrl.Manager, conflationManager *conflator.ConflationManager, - managerConfig *config.ManagerConfig, stats *statistics.Statistics, + managerConfig *configs.ManagerConfig, stats *statistics.Statistics, ) error { // add work pool: database layer initialization - worker pool + connection pool dbWorkerPool, err := workerpool.NewDBWorkerPool(stats) diff --git a/manager/pkg/statussyncer/dispatcher/transport_dispatcher.go b/manager/pkg/status/dispatcher/transport_dispatcher.go similarity index 96% rename from manager/pkg/statussyncer/dispatcher/transport_dispatcher.go rename to manager/pkg/status/dispatcher/transport_dispatcher.go index deb7d11c6..905226351 100644 --- a/manager/pkg/statussyncer/dispatcher/transport_dispatcher.go +++ b/manager/pkg/status/dispatcher/transport_dispatcher.go @@ -7,8 +7,8 @@ import ( "github.com/go-logr/logr" ctrl "sigs.k8s.io/controller-runtime" - "github.com/stolostron/multicluster-global-hub/manager/pkg/config" - "github.com/stolostron/multicluster-global-hub/manager/pkg/statussyncer/conflator" + "github.com/stolostron/multicluster-global-hub/manager/pkg/configs" + "github.com/stolostron/multicluster-global-hub/manager/pkg/status/conflator" "github.com/stolostron/multicluster-global-hub/pkg/statistics" "github.com/stolostron/multicluster-global-hub/pkg/transport" ) @@ -21,7 +21,7 @@ type TransportDispatcher struct { statistic *statistics.Statistics } -func AddTransportDispatcher(mgr ctrl.Manager, consumer transport.Consumer, managerConfig *config.ManagerConfig, +func AddTransportDispatcher(mgr ctrl.Manager, consumer transport.Consumer, managerConfig *configs.ManagerConfig, conflationManager *conflator.ConflationManager, stats *statistics.Statistics, ) error { transportDispatcher := &TransportDispatcher{ diff --git a/manager/pkg/statussyncer/syncers/generic_object_handler.go b/manager/pkg/status/handlers/generic/generic_handler.go similarity index 78% rename from manager/pkg/statussyncer/syncers/generic_object_handler.go rename to manager/pkg/status/handlers/generic/generic_handler.go index 5c71add27..4c8f5f901 100644 --- a/manager/pkg/statussyncer/syncers/generic_object_handler.go +++ b/manager/pkg/status/handlers/generic/generic_handler.go @@ -1,4 +1,4 @@ -package dbsyncer +package generic import ( "context" @@ -11,7 +11,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ctrl "sigs.k8s.io/controller-runtime" - "github.com/stolostron/multicluster-global-hub/manager/pkg/statussyncer/conflator" + "github.com/stolostron/multicluster-global-hub/manager/pkg/status/conflator" "github.com/stolostron/multicluster-global-hub/pkg/bundle/version" "github.com/stolostron/multicluster-global-hub/pkg/constants" "github.com/stolostron/multicluster-global-hub/pkg/database" @@ -34,27 +34,25 @@ type genericObjectHandler[T metav1.Object] struct { table string } -func NewGenericHandler[T metav1.Object](eventType string, priority conflator.ConflationPriority, - syncMode enum.EventSyncMode, table string, -) *genericObjectHandler[T] { +func RegisterGenericHandler[T metav1.Object](conflationManager *conflator.ConflationManager, + eventType string, priority conflator.ConflationPriority, syncMode enum.EventSyncMode, table string, +) { logName := strings.Replace(eventType, enum.EventTypePrefix, "", -1) - return &genericObjectHandler[T]{ + h := &genericObjectHandler[T]{ log: ctrl.Log.WithName(logName), eventType: eventType, eventSyncMode: syncMode, eventPriority: priority, table: table, } -} -// RegisterHandleFunc registers bundle handler functions within the conflation manager. -// handler function need to do "diff" between objects received in the bundle and the objects in database. -// leaf hub sends only the current existing objects, and status transport bridge should understand implicitly which -// objects were deleted. -// therefore, whatever is in the db and cannot be found in the bundle has to be deleted from the database. -// for the objects that appear in both, need to check if something has changed using resourceVersion field comparison -// and if the object was changed, update the db with the current object. -func (h *genericObjectHandler[T]) RegisterHandler(conflationManager *conflator.ConflationManager) { + // register bundle handler function within the conflation manager. + // handler function need to do "diff" between objects received in the bundle and the objects in database. + // leaf hub sends only the current existing objects, and status transport bridge should understand implicitly which + // objects were deleted. + // therefore, whatever is in the db and cannot be found in the bundle has to be deleted from the database. + // for the objects that appear in both, need to check if something has changed using resourceVersion field comparison + // and if the object was changed, update the db with the current object. conflationManager.Register(conflator.NewConflationRegistration( h.eventPriority, h.eventSyncMode, diff --git a/manager/pkg/status/handlers/handlers.go b/manager/pkg/status/handlers/handlers.go new file mode 100644 index 000000000..281c689a2 --- /dev/null +++ b/manager/pkg/status/handlers/handlers.go @@ -0,0 +1,90 @@ +package handlers + +import ( + "fmt" + + clustersv1beta1 "open-cluster-management.io/api/cluster/v1beta1" + placementrulev1 "open-cluster-management.io/multicloud-operators-subscription/pkg/apis/apps/placementrule/v1" + appsv1alpha1 "open-cluster-management.io/multicloud-operators-subscription/pkg/apis/apps/v1alpha1" + + "github.com/stolostron/multicluster-global-hub/manager/pkg/status/conflator" + "github.com/stolostron/multicluster-global-hub/manager/pkg/status/handlers/generic" + "github.com/stolostron/multicluster-global-hub/manager/pkg/status/handlers/managedcluster" + "github.com/stolostron/multicluster-global-hub/manager/pkg/status/handlers/managedhub" + "github.com/stolostron/multicluster-global-hub/manager/pkg/status/handlers/policy" + "github.com/stolostron/multicluster-global-hub/manager/pkg/status/handlers/security" + "github.com/stolostron/multicluster-global-hub/pkg/database" + "github.com/stolostron/multicluster-global-hub/pkg/enum" +) + +func RegisterHandlers(cmr *conflator.ConflationManager, enableGlobalResource bool) { + // managed hub + managedhub.RegisterHubClusterHeartbeatHandler(cmr) + managedhub.RegsiterHubClusterInfoHandler(cmr) + + // managed cluster + managedcluster.RegisterManagedClusterHandler(cmr) + managedcluster.RegisterManagedClusterEventHandler(cmr) + + // local policy + policy.RegisterLocalPolicySpecHandler(cmr) + policy.RegisterLocalPolicyComplianceHandler(cmr) + policy.RegisterLocalPolicyCompleteHandler(cmr) + policy.RegisterLocalRootPolicyEventHandler(cmr) + policy.RegisterLocalReplicatedPolicyEventHandler(cmr) + + // local placementrule + generic.RegisterGenericHandler[*placementrulev1.PlacementRule]( + cmr, + string(enum.LocalPlacementRuleSpecType), + conflator.LocalPlacementRulesSpecPriority, + enum.CompleteStateMode, + fmt.Sprintf("%s.%s", database.LocalSpecSchema, database.PlacementRulesTableName)) + + // security + security.RegisterSecurityAlertCountsHandler(cmr) + + if enableGlobalResource { + // global policy + policy.RegisterPolicyComplianceHandler(cmr) + policy.RegisterPolicyCompleteHandler(cmr) + policy.RegisterPolicyDeltaComplianceHandler(cmr) + policy.RegisterPolicyMiniComplianceHandler(cmr) + + // placementRule + generic.RegisterGenericHandler[*placementrulev1.PlacementRule]( + cmr, + string(enum.PlacementRuleSpecType), + conflator.PlacementRulePriority, + enum.CompleteStateMode, + fmt.Sprintf("%s.%s", database.StatusSchema, database.PlacementRulesTableName)) + + generic.RegisterGenericHandler[*clustersv1beta1.Placement]( + cmr, + string(enum.PlacementSpecType), + conflator.PlacementPriority, + enum.CompleteStateMode, + fmt.Sprintf("%s.%s", database.StatusSchema, database.PlacementsTableName)) + + generic.RegisterGenericHandler[*clustersv1beta1.PlacementDecision]( + cmr, + string(enum.PlacementDecisionType), + conflator.PlacementDecisionPriority, + enum.CompleteStateMode, + fmt.Sprintf("%s.%s", database.StatusSchema, database.PlacementDecisionsTableName)) + + generic.RegisterGenericHandler[*appsv1alpha1.SubscriptionReport]( + cmr, + string(enum.SubscriptionReportType), + conflator.SubscriptionReportPriority, + enum.CompleteStateMode, + fmt.Sprintf("%s.%s", database.StatusSchema, database.SubscriptionReportsTableName)) + + generic.RegisterGenericHandler[*appsv1alpha1.SubscriptionStatus]( + cmr, + string(enum.SubscriptionStatusType), + conflator.SubscriptionStatusPriority, + enum.CompleteStateMode, + fmt.Sprintf("%s.%s", database.StatusSchema, database.SubscriptionStatusesTableName)) + } +} diff --git a/manager/pkg/statussyncer/syncers/managedcluster_event_handler.go b/manager/pkg/status/handlers/managedcluster/managedcluster_event_handler.go similarity index 82% rename from manager/pkg/statussyncer/syncers/managedcluster_event_handler.go rename to manager/pkg/status/handlers/managedcluster/managedcluster_event_handler.go index 3b33e88c5..0049842c8 100644 --- a/manager/pkg/statussyncer/syncers/managedcluster_event_handler.go +++ b/manager/pkg/status/handlers/managedcluster/managedcluster_event_handler.go @@ -1,4 +1,4 @@ -package dbsyncer +package managedcluster import ( "context" @@ -10,7 +10,7 @@ import ( "gorm.io/gorm/clause" ctrl "sigs.k8s.io/controller-runtime" - "github.com/stolostron/multicluster-global-hub/manager/pkg/statussyncer/conflator" + "github.com/stolostron/multicluster-global-hub/manager/pkg/status/conflator" "github.com/stolostron/multicluster-global-hub/pkg/bundle/event" eventversion "github.com/stolostron/multicluster-global-hub/pkg/bundle/version" "github.com/stolostron/multicluster-global-hub/pkg/database" @@ -24,18 +24,15 @@ type managedClusterEventHandler struct { eventPriority conflator.ConflationPriority } -func NewManagedClusterEventHandler() conflator.Handler { +func RegisterManagedClusterEventHandler(conflationManager *conflator.ConflationManager) { eventType := string(enum.ManagedClusterEventType) logName := strings.Replace(eventType, enum.EventTypePrefix, "", -1) - return &managedClusterEventHandler{ + h := &managedClusterEventHandler{ log: ctrl.Log.WithName(logName), eventType: eventType, eventSyncMode: enum.DeltaStateMode, eventPriority: conflator.ManagedClusterEventPriority, } -} - -func (h *managedClusterEventHandler) RegisterHandler(conflationManager *conflator.ConflationManager) { conflationManager.Register(conflator.NewConflationRegistration( h.eventPriority, h.eventSyncMode, @@ -47,7 +44,7 @@ func (h *managedClusterEventHandler) RegisterHandler(conflationManager *conflato func (h *managedClusterEventHandler) handleEvent(ctx context.Context, evt *cloudevents.Event) error { version := evt.Extensions()[eventversion.ExtVersion] leafHubName := evt.Source() - h.log.V(2).Info(startMessage, "type", evt.Type(), "LH", evt.Source(), "version", version) + h.log.V(2).Info("handler start", "type", evt.Type(), "LH", evt.Source(), "version", version) managedClusterEvents := event.ManagedClusterEventBundle{} if err := evt.DataAs(&managedClusterEvents); err != nil { @@ -72,6 +69,6 @@ func (h *managedClusterEventHandler) handleEvent(ctx context.Context, evt *cloud return fmt.Errorf("failed handling leaf hub LocalPolicyStatusEvent event - %w", err) } - h.log.V(2).Info(finishMessage, "type", evt.Type(), "LH", evt.Source(), "version", version) + h.log.V(2).Info("handler finished", "type", evt.Type(), "LH", evt.Source(), "version", version) return nil } diff --git a/manager/pkg/statussyncer/syncers/managedcluster_handler.go b/manager/pkg/status/handlers/managedcluster/managedcluster_handler.go similarity index 91% rename from manager/pkg/statussyncer/syncers/managedcluster_handler.go rename to manager/pkg/status/handlers/managedcluster/managedcluster_handler.go index 3bbc15dc2..fd9f7c3f2 100644 --- a/manager/pkg/statussyncer/syncers/managedcluster_handler.go +++ b/manager/pkg/status/handlers/managedcluster/managedcluster_handler.go @@ -1,4 +1,4 @@ -package dbsyncer +package managedcluster import ( "context" @@ -13,7 +13,7 @@ import ( clusterv1 "open-cluster-management.io/api/cluster/v1" ctrl "sigs.k8s.io/controller-runtime" - "github.com/stolostron/multicluster-global-hub/manager/pkg/statussyncer/conflator" + "github.com/stolostron/multicluster-global-hub/manager/pkg/status/conflator" eventversion "github.com/stolostron/multicluster-global-hub/pkg/bundle/version" "github.com/stolostron/multicluster-global-hub/pkg/database" "github.com/stolostron/multicluster-global-hub/pkg/database/models" @@ -27,18 +27,15 @@ type managedClusterHandler struct { eventPriority conflator.ConflationPriority } -func NewManagedClusterHandler() conflator.Handler { +func RegisterManagedClusterHandler(conflationManager *conflator.ConflationManager) { eventType := string(enum.ManagedClusterType) logName := strings.Replace(eventType, enum.EventTypePrefix, "", -1) - return &managedClusterHandler{ + h := &managedClusterHandler{ log: ctrl.Log.WithName(logName), eventType: eventType, eventSyncMode: enum.CompleteStateMode, eventPriority: conflator.ManagedClustersPriority, } -} - -func (h *managedClusterHandler) RegisterHandler(conflationManager *conflator.ConflationManager) { conflationManager.Register(conflator.NewConflationRegistration( h.eventPriority, h.eventSyncMode, @@ -50,7 +47,7 @@ func (h *managedClusterHandler) RegisterHandler(conflationManager *conflator.Con func (h *managedClusterHandler) handleEvent(ctx context.Context, evt *cloudevents.Event) error { version := evt.Extensions()[eventversion.ExtVersion] leafHubName := evt.Source() - h.log.V(2).Info(startMessage, "type", evt.Type(), "LH", evt.Source(), "version", version) + h.log.V(2).Info("handler start", "type", evt.Type(), "LH", evt.Source(), "version", version) var data []clusterv1.ManagedCluster if err := evt.DataAs(&data); err != nil { @@ -135,7 +132,7 @@ func (h *managedClusterHandler) handleEvent(ctx context.Context, evt *cloudevent return fmt.Errorf("failed deleting managed clusters - %w", err) } - h.log.V(2).Info(finishMessage, "type", evt.Type(), "LH", evt.Source(), "version", version) + h.log.V(2).Info("handler finished", "type", evt.Type(), "LH", evt.Source(), "version", version) return nil } diff --git a/manager/pkg/statussyncer/syncers/hub_cluster_heartbeat_handler.go b/manager/pkg/status/handlers/managedhub/hub_cluster_heartbeat_handler.go similarity index 50% rename from manager/pkg/statussyncer/syncers/hub_cluster_heartbeat_handler.go rename to manager/pkg/status/handlers/managedhub/hub_cluster_heartbeat_handler.go index eef3f3da8..2beaee126 100644 --- a/manager/pkg/statussyncer/syncers/hub_cluster_heartbeat_handler.go +++ b/manager/pkg/status/handlers/managedhub/hub_cluster_heartbeat_handler.go @@ -1,45 +1,24 @@ -package dbsyncer +package managedhub import ( "context" "fmt" - "strings" "time" cloudevents "github.com/cloudevents/sdk-go/v2" - "github.com/go-logr/logr" "gorm.io/gorm/clause" - ctrl "sigs.k8s.io/controller-runtime" - "github.com/stolostron/multicluster-global-hub/manager/pkg/statussyncer/conflator" + "github.com/stolostron/multicluster-global-hub/manager/pkg/status/conflator" "github.com/stolostron/multicluster-global-hub/pkg/database" "github.com/stolostron/multicluster-global-hub/pkg/database/models" "github.com/stolostron/multicluster-global-hub/pkg/enum" ) -type hubHeartbeatHandler struct { - log logr.Logger - eventType string - eventSyncMode enum.EventSyncMode - eventPriority conflator.ConflationPriority -} - -func NewHubClusterHeartbeatHandler() conflator.Handler { - eventType := string(enum.HubClusterHeartbeatType) - logName := strings.Replace(eventType, enum.EventTypePrefix, "", -1) - return &hubHeartbeatHandler{ - log: ctrl.Log.WithName(logName), - eventType: eventType, - eventSyncMode: enum.CompleteStateMode, - eventPriority: conflator.HubClusterHeartbeatPriority, - } -} - -func (h *hubHeartbeatHandler) RegisterHandler(conflationManager *conflator.ConflationManager) { +func RegisterHubClusterHeartbeatHandler(conflationManager *conflator.ConflationManager) { conflationManager.Register(conflator.NewConflationRegistration( - h.eventPriority, - h.eventSyncMode, - h.eventType, + conflator.HubClusterHeartbeatPriority, + enum.CompleteStateMode, + string(enum.HubClusterHeartbeatType), handleHeartbeatEvent, )) } diff --git a/manager/pkg/statussyncer/syncers/hub_cluster_info_handler.go b/manager/pkg/status/handlers/managedhub/hub_cluster_info_handler.go similarity index 72% rename from manager/pkg/statussyncer/syncers/hub_cluster_info_handler.go rename to manager/pkg/status/handlers/managedhub/hub_cluster_info_handler.go index 33f59907a..6e5c00413 100644 --- a/manager/pkg/statussyncer/syncers/hub_cluster_info_handler.go +++ b/manager/pkg/status/handlers/managedhub/hub_cluster_info_handler.go @@ -1,4 +1,4 @@ -package dbsyncer +package managedhub import ( "context" @@ -10,7 +10,7 @@ import ( "gorm.io/gorm/clause" ctrl "sigs.k8s.io/controller-runtime" - "github.com/stolostron/multicluster-global-hub/manager/pkg/statussyncer/conflator" + "github.com/stolostron/multicluster-global-hub/manager/pkg/status/conflator" "github.com/stolostron/multicluster-global-hub/pkg/bundle/cluster" eventversion "github.com/stolostron/multicluster-global-hub/pkg/bundle/version" "github.com/stolostron/multicluster-global-hub/pkg/constants" @@ -26,38 +26,28 @@ type hubClusterInfoHandler struct { eventPriority conflator.ConflationPriority } -func NewHubClusterInfoHandler() conflator.Handler { +func RegsiterHubClusterInfoHandler(conflationManager *conflator.ConflationManager) { eventType := string(enum.HubClusterInfoType) logName := strings.Replace(eventType, enum.EventTypePrefix, "", -1) - return &hubClusterInfoHandler{ + hubClusterInfo := &hubClusterInfoHandler{ log: ctrl.Log.WithName(logName), eventType: eventType, eventSyncMode: enum.CompleteStateMode, eventPriority: conflator.HubClusterInfoPriority, } -} - -func (h *hubClusterInfoHandler) RegisterHandler(conflationManager *conflator.ConflationManager) { conflationManager.Register(conflator.NewConflationRegistration( - h.eventPriority, - h.eventSyncMode, - h.eventType, - h.handleEvent, + hubClusterInfo.eventPriority, + hubClusterInfo.eventSyncMode, + hubClusterInfo.eventType, + hubClusterInfo.handleEvent, )) } -// RegisterBundleHandlerFunctions registers bundle handler functions within the conflation manager. -// handler functions need to do "diff" between objects received in the bundle and the objects in database. -// leaf hub sends only the current existing objects, and status transport bridge should understand implicitly which -// objects were deleted. -// therefore, whatever is in the db and cannot be found in the bundle has to be deleted from the database. -// for the objects that appear in both, need to check if something has changed using resourceVersion field comparison -// and if the object was changed, update the db with the current object. func (h *hubClusterInfoHandler) handleEvent(ctx context.Context, evt *cloudevents.Event, ) error { version := evt.Extensions()[eventversion.ExtVersion] - h.log.V(2).Info(startMessage, "type", evt.Type(), "LH", evt.Source(), "version", version) + h.log.V(2).Info("handler start", "type", evt.Type(), "LH", evt.Source(), "version", version) leafHubName := evt.Source() @@ -117,6 +107,6 @@ func (h *hubClusterInfoHandler) handleEvent(ctx context.Context, return err } - h.log.V(2).Info(finishMessage, "type", evt.Type(), "LH", evt.Source(), "version", version) + h.log.V(2).Info("handler finished", "type", evt.Type(), "LH", evt.Source(), "version", version) return nil } diff --git a/manager/pkg/statussyncer/syncers/local_policy_complete_handler.go b/manager/pkg/status/handlers/policy/local_complete_handler.go similarity index 92% rename from manager/pkg/statussyncer/syncers/local_policy_complete_handler.go rename to manager/pkg/status/handlers/policy/local_complete_handler.go index 5757f49c8..e159e891b 100644 --- a/manager/pkg/statussyncer/syncers/local_policy_complete_handler.go +++ b/manager/pkg/status/handlers/policy/local_complete_handler.go @@ -1,4 +1,4 @@ -package dbsyncer +package policy import ( "context" @@ -10,8 +10,8 @@ import ( "gorm.io/gorm" ctrl "sigs.k8s.io/controller-runtime" - "github.com/stolostron/multicluster-global-hub/manager/pkg/statussyncer/conflator" - "github.com/stolostron/multicluster-global-hub/manager/pkg/statussyncer/conflator/dependency" + "github.com/stolostron/multicluster-global-hub/manager/pkg/status/conflator" + "github.com/stolostron/multicluster-global-hub/manager/pkg/status/conflator/dependency" "github.com/stolostron/multicluster-global-hub/pkg/bundle/grc" eventversion "github.com/stolostron/multicluster-global-hub/pkg/bundle/version" "github.com/stolostron/multicluster-global-hub/pkg/database" @@ -27,19 +27,17 @@ type localPolicyCompleteHandler struct { eventPriority conflator.ConflationPriority } -func NewLocalPolicyCompleteHandler() conflator.Handler { +func RegisterLocalPolicyCompleteHandler(conflationManager *conflator.ConflationManager) { eventType := string(enum.LocalCompleteComplianceType) logName := strings.Replace(eventType, enum.EventTypePrefix, "", -1) - return &localPolicyCompleteHandler{ + h := &localPolicyCompleteHandler{ log: ctrl.Log.WithName(logName), eventType: eventType, dependencyType: string(enum.LocalComplianceType), eventSyncMode: enum.CompleteStateMode, eventPriority: conflator.LocalCompleteCompliancePriority, } -} -func (h *localPolicyCompleteHandler) RegisterHandler(conflationManager *conflator.ConflationManager) { registration := conflator.NewConflationRegistration( h.eventPriority, h.eventSyncMode, @@ -57,7 +55,7 @@ func (h *localPolicyCompleteHandler) handleEventWrapper(ctx context.Context, evt func handleCompleteCompliance(log logr.Logger, ctx context.Context, evt *cloudevents.Event) error { version := evt.Extensions()[eventversion.ExtVersion] leafHub := evt.Source() - log.V(2).Info(startMessage, "type", evt.Type(), "LH", evt.Source(), "version", version) + log.V(2).Info("handler start", "type", evt.Type(), "LH", evt.Source(), "version", version) db := database.GetGorm() @@ -176,6 +174,6 @@ func handleCompleteCompliance(log logr.Logger, ctx context.Context, evt *cloudev return fmt.Errorf("failed deleting compliances from local complainces - %w", err) } - log.V(2).Info(finishMessage, "type", evt.Type(), "LH", evt.Source(), "version", version) + log.V(2).Info("handler finished", "type", evt.Type(), "LH", evt.Source(), "version", version) return nil } diff --git a/manager/pkg/statussyncer/syncers/local_policy_compliance_handler.go b/manager/pkg/status/handlers/policy/local_compliance_handler.go similarity index 93% rename from manager/pkg/statussyncer/syncers/local_policy_compliance_handler.go rename to manager/pkg/status/handlers/policy/local_compliance_handler.go index c06e89e78..c687f48a2 100644 --- a/manager/pkg/statussyncer/syncers/local_policy_compliance_handler.go +++ b/manager/pkg/status/handlers/policy/local_compliance_handler.go @@ -1,4 +1,4 @@ -package dbsyncer +package policy import ( "context" @@ -12,7 +12,7 @@ import ( "gorm.io/gorm/clause" ctrl "sigs.k8s.io/controller-runtime" - "github.com/stolostron/multicluster-global-hub/manager/pkg/statussyncer/conflator" + "github.com/stolostron/multicluster-global-hub/manager/pkg/status/conflator" "github.com/stolostron/multicluster-global-hub/pkg/bundle/grc" eventversion "github.com/stolostron/multicluster-global-hub/pkg/bundle/version" "github.com/stolostron/multicluster-global-hub/pkg/database" @@ -27,18 +27,15 @@ type localPolicyComplianceHandler struct { eventPriority conflator.ConflationPriority } -func NewLocalPolicyComplianceHandler() conflator.Handler { +func RegisterLocalPolicyComplianceHandler(conflationManager *conflator.ConflationManager) { eventType := string(enum.LocalComplianceType) logName := strings.Replace(eventType, enum.EventTypePrefix, "", -1) - return &localPolicyComplianceHandler{ + h := &localPolicyComplianceHandler{ log: ctrl.Log.WithName(logName), eventType: eventType, eventSyncMode: enum.CompleteStateMode, eventPriority: conflator.LocalCompliancePriority, } -} - -func (h *localPolicyComplianceHandler) RegisterHandler(conflationManager *conflator.ConflationManager) { conflationManager.Register(conflator.NewConflationRegistration( h.eventPriority, h.eventSyncMode, @@ -54,7 +51,7 @@ func (h *localPolicyComplianceHandler) handleEventWrapper(ctx context.Context, e func handleCompliance(log logr.Logger, ctx context.Context, evt *cloudevents.Event) error { version := evt.Extensions()[eventversion.ExtVersion] leafHub := evt.Source() - log.V(2).Info(startMessage, "type", evt.Type(), "LH", evt.Source(), "version", version) + log.V(2).Info("handler start", "type", evt.Type(), "LH", evt.Source(), "version", version) data := grc.ComplianceBundle{} if err := evt.DataAs(&data); err != nil { @@ -149,7 +146,7 @@ func handleCompliance(log logr.Logger, ctx context.Context, evt *cloudevents.Eve return fmt.Errorf("failed to handle local compliance event - %w", err) } - log.V(2).Info(finishMessage, "type", evt.Type(), "LH", evt.Source(), "version", version) + log.V(2).Info("handler finished", "type", evt.Type(), "LH", evt.Source(), "version", version) return nil } diff --git a/manager/pkg/statussyncer/syncers/local_policy_spec_handler.go b/manager/pkg/status/handlers/policy/local_policy_spec_handler.go similarity index 95% rename from manager/pkg/statussyncer/syncers/local_policy_spec_handler.go rename to manager/pkg/status/handlers/policy/local_policy_spec_handler.go index e2fb78105..7417e2e86 100644 --- a/manager/pkg/statussyncer/syncers/local_policy_spec_handler.go +++ b/manager/pkg/status/handlers/policy/local_policy_spec_handler.go @@ -1,4 +1,4 @@ -package dbsyncer +package policy import ( "context" @@ -13,13 +13,18 @@ import ( policiesv1 "open-cluster-management.io/governance-policy-propagator/api/v1" ctrl "sigs.k8s.io/controller-runtime" - "github.com/stolostron/multicluster-global-hub/manager/pkg/statussyncer/conflator" + "github.com/stolostron/multicluster-global-hub/manager/pkg/status/conflator" eventversion "github.com/stolostron/multicluster-global-hub/pkg/bundle/version" "github.com/stolostron/multicluster-global-hub/pkg/database" "github.com/stolostron/multicluster-global-hub/pkg/database/models" "github.com/stolostron/multicluster-global-hub/pkg/enum" ) +const ( + startMessage = "handler start" + finishMessage = "handler finished" +) + type localPolicySpecHandler struct { log logr.Logger eventType string @@ -27,18 +32,15 @@ type localPolicySpecHandler struct { eventPriority conflator.ConflationPriority } -func NewLocalPolicySpecHandler() conflator.Handler { +func RegisterLocalPolicySpecHandler(conflationManager *conflator.ConflationManager) { eventType := string(enum.LocalPolicySpecType) logName := strings.Replace(eventType, enum.EventTypePrefix, "", -1) - return &localPolicySpecHandler{ + h := &localPolicySpecHandler{ log: ctrl.Log.WithName(logName), eventType: eventType, eventSyncMode: enum.CompleteStateMode, eventPriority: conflator.LocalPolicySpecPriority, } -} - -func (h *localPolicySpecHandler) RegisterHandler(conflationManager *conflator.ConflationManager) { conflationManager.Register(conflator.NewConflationRegistration( h.eventPriority, h.eventSyncMode, diff --git a/manager/pkg/statussyncer/syncers/local_replicated_policy_event_handler.go b/manager/pkg/status/handlers/policy/local_replicated_policy_event_handler.go similarity index 92% rename from manager/pkg/statussyncer/syncers/local_replicated_policy_event_handler.go rename to manager/pkg/status/handlers/policy/local_replicated_policy_event_handler.go index 450116165..85656c5ae 100644 --- a/manager/pkg/statussyncer/syncers/local_replicated_policy_event_handler.go +++ b/manager/pkg/status/handlers/policy/local_replicated_policy_event_handler.go @@ -1,4 +1,4 @@ -package dbsyncer +package policy import ( "context" @@ -11,7 +11,7 @@ import ( "gorm.io/gorm/clause" ctrl "sigs.k8s.io/controller-runtime" - "github.com/stolostron/multicluster-global-hub/manager/pkg/statussyncer/conflator" + "github.com/stolostron/multicluster-global-hub/manager/pkg/status/conflator" "github.com/stolostron/multicluster-global-hub/pkg/bundle/event" eventversion "github.com/stolostron/multicluster-global-hub/pkg/bundle/version" "github.com/stolostron/multicluster-global-hub/pkg/database" @@ -27,18 +27,15 @@ type localReplicatedPolicyEventHandler struct { eventPriority conflator.ConflationPriority } -func NewLocalReplicatedPolicyEventHandler() conflator.Handler { +func RegisterLocalReplicatedPolicyEventHandler(conflationManager *conflator.ConflationManager) { eventType := string(enum.LocalReplicatedPolicyEventType) logName := strings.Replace(eventType, enum.EventTypePrefix, "", -1) - return &localReplicatedPolicyEventHandler{ + h := &localReplicatedPolicyEventHandler{ log: ctrl.Log.WithName(logName), eventType: eventType, eventSyncMode: enum.DeltaStateMode, eventPriority: conflator.LocalReplicatedPolicyEventPriority, } -} - -func (h *localReplicatedPolicyEventHandler) RegisterHandler(conflationManager *conflator.ConflationManager) { conflationManager.Register(conflator.NewConflationRegistration( h.eventPriority, h.eventSyncMode, diff --git a/manager/pkg/statussyncer/syncers/local_root_policy_event_handler.go b/manager/pkg/status/handlers/policy/local_root_policy_event_handler.go similarity index 92% rename from manager/pkg/statussyncer/syncers/local_root_policy_event_handler.go rename to manager/pkg/status/handlers/policy/local_root_policy_event_handler.go index 9235a0858..fa31cc3d2 100644 --- a/manager/pkg/statussyncer/syncers/local_root_policy_event_handler.go +++ b/manager/pkg/status/handlers/policy/local_root_policy_event_handler.go @@ -1,4 +1,4 @@ -package dbsyncer +package policy import ( "context" @@ -11,7 +11,7 @@ import ( "gorm.io/gorm/clause" ctrl "sigs.k8s.io/controller-runtime" - "github.com/stolostron/multicluster-global-hub/manager/pkg/statussyncer/conflator" + "github.com/stolostron/multicluster-global-hub/manager/pkg/status/conflator" "github.com/stolostron/multicluster-global-hub/pkg/bundle/event" eventversion "github.com/stolostron/multicluster-global-hub/pkg/bundle/version" "github.com/stolostron/multicluster-global-hub/pkg/database" @@ -27,18 +27,15 @@ type localRootPolicyEventHandler struct { eventPriority conflator.ConflationPriority } -func NewLocalRootPolicyEventHandler() conflator.Handler { +func RegisterLocalRootPolicyEventHandler(conflationManager *conflator.ConflationManager) { eventType := string(enum.LocalRootPolicyEventType) logName := strings.Replace(eventType, enum.EventTypePrefix, "", -1) - return &localRootPolicyEventHandler{ + h := &localRootPolicyEventHandler{ log: ctrl.Log.WithName(logName), eventType: eventType, eventSyncMode: enum.DeltaStateMode, eventPriority: conflator.LocalEventRootPolicyPriority, } -} - -func (h *localRootPolicyEventHandler) RegisterHandler(conflationManager *conflator.ConflationManager) { conflationManager.Register(conflator.NewConflationRegistration( h.eventPriority, h.eventSyncMode, diff --git a/manager/pkg/statussyncer/syncers/policy_complete_handler.go b/manager/pkg/status/handlers/policy/policy_complete_handler.go similarity index 95% rename from manager/pkg/statussyncer/syncers/policy_complete_handler.go rename to manager/pkg/status/handlers/policy/policy_complete_handler.go index 0863e4634..0fe967e5f 100644 --- a/manager/pkg/statussyncer/syncers/policy_complete_handler.go +++ b/manager/pkg/status/handlers/policy/policy_complete_handler.go @@ -1,4 +1,4 @@ -package dbsyncer +package policy import ( "context" @@ -10,8 +10,8 @@ import ( "gorm.io/gorm" ctrl "sigs.k8s.io/controller-runtime" - "github.com/stolostron/multicluster-global-hub/manager/pkg/statussyncer/conflator" - "github.com/stolostron/multicluster-global-hub/manager/pkg/statussyncer/conflator/dependency" + "github.com/stolostron/multicluster-global-hub/manager/pkg/status/conflator" + "github.com/stolostron/multicluster-global-hub/manager/pkg/status/conflator/dependency" "github.com/stolostron/multicluster-global-hub/pkg/bundle/grc" eventversion "github.com/stolostron/multicluster-global-hub/pkg/bundle/version" "github.com/stolostron/multicluster-global-hub/pkg/database" @@ -27,19 +27,16 @@ type policyCompleteHandler struct { eventPriority conflator.ConflationPriority } -func NewPolicyCompleteHandler() conflator.Handler { +func RegisterPolicyCompleteHandler(conflationManager *conflator.ConflationManager) { eventType := string(enum.CompleteComplianceType) logName := strings.Replace(eventType, enum.EventTypePrefix, "", -1) - return &policyCompleteHandler{ + h := &policyCompleteHandler{ log: ctrl.Log.WithName(logName), eventType: eventType, eventSyncMode: enum.CompleteStateMode, eventPriority: conflator.CompleteCompliancePriority, dependencyType: string(enum.ComplianceType), } -} - -func (h *policyCompleteHandler) RegisterHandler(conflationManager *conflator.ConflationManager) { registration := conflator.NewConflationRegistration( h.eventPriority, h.eventSyncMode, diff --git a/manager/pkg/statussyncer/syncers/policy_compliance_handler.go b/manager/pkg/status/handlers/policy/policy_compliance_handler.go similarity index 97% rename from manager/pkg/statussyncer/syncers/policy_compliance_handler.go rename to manager/pkg/status/handlers/policy/policy_compliance_handler.go index 58a4a7c9e..3f1713110 100644 --- a/manager/pkg/statussyncer/syncers/policy_compliance_handler.go +++ b/manager/pkg/status/handlers/policy/policy_compliance_handler.go @@ -1,4 +1,4 @@ -package dbsyncer +package policy import ( "context" @@ -12,7 +12,7 @@ import ( "gorm.io/gorm/clause" ctrl "sigs.k8s.io/controller-runtime" - "github.com/stolostron/multicluster-global-hub/manager/pkg/statussyncer/conflator" + "github.com/stolostron/multicluster-global-hub/manager/pkg/status/conflator" "github.com/stolostron/multicluster-global-hub/pkg/bundle/grc" eventversion "github.com/stolostron/multicluster-global-hub/pkg/bundle/version" "github.com/stolostron/multicluster-global-hub/pkg/database" @@ -27,18 +27,15 @@ type policyComplianceHandler struct { eventPriority conflator.ConflationPriority } -func NewPolicyComplianceHandler() conflator.Handler { +func RegisterPolicyComplianceHandler(conflationManager *conflator.ConflationManager) { eventType := string(enum.ComplianceType) logName := strings.Replace(eventType, enum.EventTypePrefix, "", -1) - return &policyComplianceHandler{ + h := &policyComplianceHandler{ log: ctrl.Log.WithName(logName), eventType: eventType, eventSyncMode: enum.CompleteStateMode, eventPriority: conflator.CompliancePriority, } -} - -func (h *policyComplianceHandler) RegisterHandler(conflationManager *conflator.ConflationManager) { conflationManager.Register(conflator.NewConflationRegistration( h.eventPriority, h.eventSyncMode, diff --git a/manager/pkg/statussyncer/syncers/policy_delta_compliance_handler.go b/manager/pkg/status/handlers/policy/policy_delta_compliance_handler.go similarity index 92% rename from manager/pkg/statussyncer/syncers/policy_delta_compliance_handler.go rename to manager/pkg/status/handlers/policy/policy_delta_compliance_handler.go index a619f0a4e..050c4c028 100644 --- a/manager/pkg/statussyncer/syncers/policy_delta_compliance_handler.go +++ b/manager/pkg/status/handlers/policy/policy_delta_compliance_handler.go @@ -1,4 +1,4 @@ -package dbsyncer +package policy import ( "context" @@ -10,8 +10,8 @@ import ( "gorm.io/gorm" ctrl "sigs.k8s.io/controller-runtime" - "github.com/stolostron/multicluster-global-hub/manager/pkg/statussyncer/conflator" - "github.com/stolostron/multicluster-global-hub/manager/pkg/statussyncer/conflator/dependency" + "github.com/stolostron/multicluster-global-hub/manager/pkg/status/conflator" + "github.com/stolostron/multicluster-global-hub/manager/pkg/status/conflator/dependency" "github.com/stolostron/multicluster-global-hub/pkg/bundle/grc" eventversion "github.com/stolostron/multicluster-global-hub/pkg/bundle/version" "github.com/stolostron/multicluster-global-hub/pkg/database" @@ -27,19 +27,16 @@ type policyDeltaComplianceHandler struct { eventPriority conflator.ConflationPriority } -func NewPolicyDeltaComplianceHandler() conflator.Handler { +func RegisterPolicyDeltaComplianceHandler(conflationManager *conflator.ConflationManager) { eventType := string(enum.DeltaComplianceType) logName := strings.Replace(eventType, enum.EventTypePrefix, "", -1) - return &policyDeltaComplianceHandler{ + h := &policyDeltaComplianceHandler{ log: ctrl.Log.WithName(logName), eventType: eventType, dependencyType: string(enum.CompleteComplianceType), eventSyncMode: enum.DeltaStateMode, eventPriority: conflator.DeltaCompliancePriority, } -} - -func (h *policyDeltaComplianceHandler) RegisterHandler(conflationManager *conflator.ConflationManager) { registration := conflator.NewConflationRegistration( h.eventPriority, h.eventSyncMode, diff --git a/manager/pkg/statussyncer/syncers/policy_minimal_compliance_handler.go b/manager/pkg/status/handlers/policy/policy_minimal_compliance_handler.go similarity index 94% rename from manager/pkg/statussyncer/syncers/policy_minimal_compliance_handler.go rename to manager/pkg/status/handlers/policy/policy_minimal_compliance_handler.go index c360cf3a8..c917fa242 100644 --- a/manager/pkg/statussyncer/syncers/policy_minimal_compliance_handler.go +++ b/manager/pkg/status/handlers/policy/policy_minimal_compliance_handler.go @@ -1,4 +1,4 @@ -package dbsyncer +package policy import ( "context" @@ -11,7 +11,7 @@ import ( "gorm.io/gorm/clause" ctrl "sigs.k8s.io/controller-runtime" - "github.com/stolostron/multicluster-global-hub/manager/pkg/statussyncer/conflator" + "github.com/stolostron/multicluster-global-hub/manager/pkg/status/conflator" "github.com/stolostron/multicluster-global-hub/pkg/bundle/grc" eventversion "github.com/stolostron/multicluster-global-hub/pkg/bundle/version" "github.com/stolostron/multicluster-global-hub/pkg/database" @@ -26,18 +26,15 @@ type policyMiniComplianceHandler struct { eventPriority conflator.ConflationPriority } -func NewPolicyMiniComplianceHandler() conflator.Handler { +func RegisterPolicyMiniComplianceHandler(conflationManager *conflator.ConflationManager) { eventType := string(enum.MiniComplianceType) logName := strings.Replace(eventType, enum.EventTypePrefix, "", -1) - return &policyMiniComplianceHandler{ + h := &policyMiniComplianceHandler{ log: ctrl.Log.WithName(logName), eventType: eventType, eventSyncMode: enum.CompleteStateMode, eventPriority: conflator.MinimalCompliancePriority, } -} - -func (h *policyMiniComplianceHandler) RegisterHandler(conflationManager *conflator.ConflationManager) { conflationManager.Register(conflator.NewConflationRegistration( h.eventPriority, h.eventSyncMode, diff --git a/manager/pkg/statussyncer/syncers/security_alert_counts_handler.go b/manager/pkg/status/handlers/security/security_alert_counts_handler.go similarity index 84% rename from manager/pkg/statussyncer/syncers/security_alert_counts_handler.go rename to manager/pkg/status/handlers/security/security_alert_counts_handler.go index ec075615e..b136377a2 100644 --- a/manager/pkg/statussyncer/syncers/security_alert_counts_handler.go +++ b/manager/pkg/status/handlers/security/security_alert_counts_handler.go @@ -1,4 +1,4 @@ -package dbsyncer +package security import ( "context" @@ -9,7 +9,7 @@ import ( "gorm.io/gorm/clause" ctrl "sigs.k8s.io/controller-runtime" - "github.com/stolostron/multicluster-global-hub/manager/pkg/statussyncer/conflator" + "github.com/stolostron/multicluster-global-hub/manager/pkg/status/conflator" eventversion "github.com/stolostron/multicluster-global-hub/pkg/bundle/version" "github.com/stolostron/multicluster-global-hub/pkg/database" dbmodels "github.com/stolostron/multicluster-global-hub/pkg/database/models" @@ -24,18 +24,15 @@ type securityAlertCountsHandler struct { eventPriority conflator.ConflationPriority } -func NewSecurityAlertCountsHandler() conflator.Handler { +func RegisterSecurityAlertCountsHandler(conflationManager *conflator.ConflationManager) { eventType := string(enum.SecurityAlertCountsType) logName := strings.Replace(eventType, enum.EventTypePrefix, "", -1) - return &securityAlertCountsHandler{ + h := &securityAlertCountsHandler{ log: ctrl.Log.WithName(logName), eventType: eventType, eventSyncMode: enum.CompleteStateMode, eventPriority: conflator.SecurityAlertCountsPriority, } -} - -func (h *securityAlertCountsHandler) RegisterHandler(conflationManager *conflator.ConflationManager) { conflationManager.Register(conflator.NewConflationRegistration( h.eventPriority, h.eventSyncMode, @@ -47,7 +44,7 @@ func (h *securityAlertCountsHandler) RegisterHandler(conflationManager *conflato func (h *securityAlertCountsHandler) handleEvent(ctx context.Context, evt *cloudevents.Event) error { version := evt.Extensions()[eventversion.ExtVersion] leafHubName := evt.Source() - h.log.V(2).Info(startMessage, "type", evt.Type(), "LH", evt.Source(), "version", version) + h.log.V(2).Info("handler start", "type", evt.Type(), "LH", evt.Source(), "version", version) // Extract the data from the event: wireModel := &wiremodels.SecurityAlertCounts{} @@ -77,6 +74,6 @@ func (h *securityAlertCountsHandler) handleEvent(ctx context.Context, evt *cloud return err } - h.log.V(2).Info(finishMessage, "type", evt.Type(), "LH", evt.Source(), "version", version) + h.log.V(2).Info("handler finished", "type", evt.Type(), "LH", evt.Source(), "version", version) return nil } diff --git a/manager/pkg/statussyncer/syncers.go b/manager/pkg/status/status.go similarity index 50% rename from manager/pkg/statussyncer/syncers.go rename to manager/pkg/status/status.go index 9e0d7798c..996a3fd39 100644 --- a/manager/pkg/statussyncer/syncers.go +++ b/manager/pkg/status/status.go @@ -1,14 +1,14 @@ -package statussyncer +package status import ( "fmt" ctrl "sigs.k8s.io/controller-runtime" - "github.com/stolostron/multicluster-global-hub/manager/pkg/config" - "github.com/stolostron/multicluster-global-hub/manager/pkg/statussyncer/conflator" - "github.com/stolostron/multicluster-global-hub/manager/pkg/statussyncer/dispatcher" - dbsyncer "github.com/stolostron/multicluster-global-hub/manager/pkg/statussyncer/syncers" + "github.com/stolostron/multicluster-global-hub/manager/pkg/configs" + "github.com/stolostron/multicluster-global-hub/manager/pkg/status/conflator" + "github.com/stolostron/multicluster-global-hub/manager/pkg/status/dispatcher" + "github.com/stolostron/multicluster-global-hub/manager/pkg/status/handlers" "github.com/stolostron/multicluster-global-hub/pkg/statistics" "github.com/stolostron/multicluster-global-hub/pkg/transport" ) @@ -17,7 +17,7 @@ var statusCtrlStarted = false // AddStatusSyncers performs the initial setup required before starting the runtime manager. // adds controllers and/or runnables to the manager, registers handler to conflation manager -func AddStatusSyncers(mgr ctrl.Manager, consumer transport.Consumer, managerConfig *config.ManagerConfig) error { +func AddStatusSyncers(mgr ctrl.Manager, consumer transport.Consumer, managerConfig *configs.ManagerConfig) error { if statusCtrlStarted { return nil } @@ -29,7 +29,7 @@ func AddStatusSyncers(mgr ctrl.Manager, consumer transport.Consumer, managerConf // manage all Conflation Units and handlers conflationManager := conflator.NewConflationManager(stats) - registerHandler(conflationManager, managerConfig.EnableGlobalResource) + handlers.RegisterHandlers(conflationManager, managerConfig.EnableGlobalResource) // start consume message from transport to conflation manager if err := dispatcher.AddTransportDispatcher(mgr, consumer, managerConfig, conflationManager, stats); err != nil { @@ -49,30 +49,3 @@ func AddStatusSyncers(mgr ctrl.Manager, consumer transport.Consumer, managerConf statusCtrlStarted = true return nil } - -func registerHandler(cmr *conflator.ConflationManager, enableGlobalResource bool) { - dbsyncer.NewHubClusterHeartbeatHandler().RegisterHandler(cmr) - dbsyncer.NewHubClusterInfoHandler().RegisterHandler(cmr) - dbsyncer.NewManagedClusterHandler().RegisterHandler(cmr) - dbsyncer.NewManagedClusterEventHandler().RegisterHandler(cmr) - dbsyncer.NewLocalPolicySpecHandler().RegisterHandler(cmr) - dbsyncer.NewLocalPolicyComplianceHandler().RegisterHandler(cmr) - dbsyncer.NewLocalPolicyCompleteHandler().RegisterHandler(cmr) - dbsyncer.NewLocalRootPolicyEventHandler().RegisterHandler(cmr) - dbsyncer.NewLocalReplicatedPolicyEventHandler().RegisterHandler(cmr) - dbsyncer.NewLocalPlacementRuleSpecHandler().RegisterHandler(cmr) - dbsyncer.NewSecurityAlertCountsHandler().RegisterHandler(cmr) - if enableGlobalResource { - dbsyncer.NewPolicyComplianceHandler().RegisterHandler(cmr) - dbsyncer.NewPolicyCompleteHandler().RegisterHandler(cmr) - dbsyncer.NewPolicyDeltaComplianceHandler().RegisterHandler(cmr) - dbsyncer.NewPolicyMiniComplianceHandler().RegisterHandler(cmr) - - dbsyncer.NewPlacementRuleHandler().RegisterHandler(cmr) - dbsyncer.NewPlacementHandler().RegisterHandler(cmr) - dbsyncer.NewPlacementDecisionHandler().RegisterHandler(cmr) - - dbsyncer.NewSubscriptionReportHandler().RegisterHandler(cmr) - dbsyncer.NewSubscriptionStatusHandler().RegisterHandler(cmr) - } -} diff --git a/manager/pkg/statussyncer/syncers/local_placementrule_handler.go b/manager/pkg/statussyncer/syncers/local_placementrule_handler.go deleted file mode 100644 index ac1fb6d95..000000000 --- a/manager/pkg/statussyncer/syncers/local_placementrule_handler.go +++ /dev/null @@ -1,19 +0,0 @@ -package dbsyncer - -import ( - "fmt" - - placementrulev1 "open-cluster-management.io/multicloud-operators-subscription/pkg/apis/apps/placementrule/v1" - - "github.com/stolostron/multicluster-global-hub/manager/pkg/statussyncer/conflator" - "github.com/stolostron/multicluster-global-hub/pkg/database" - "github.com/stolostron/multicluster-global-hub/pkg/enum" -) - -func NewLocalPlacementRuleSpecHandler() conflator.Handler { - return NewGenericHandler[*placementrulev1.PlacementRule]( - string(enum.LocalPlacementRuleSpecType), - conflator.LocalPlacementRulesSpecPriority, - enum.CompleteStateMode, - fmt.Sprintf("%s.%s", database.LocalSpecSchema, database.PlacementRulesTableName)) -} diff --git a/manager/pkg/statussyncer/syncers/placement_decisions_handler.go b/manager/pkg/statussyncer/syncers/placement_decisions_handler.go deleted file mode 100644 index 18f17f6b3..000000000 --- a/manager/pkg/statussyncer/syncers/placement_decisions_handler.go +++ /dev/null @@ -1,19 +0,0 @@ -package dbsyncer - -import ( - "fmt" - - clustersv1beta1 "open-cluster-management.io/api/cluster/v1beta1" - - "github.com/stolostron/multicluster-global-hub/manager/pkg/statussyncer/conflator" - "github.com/stolostron/multicluster-global-hub/pkg/database" - "github.com/stolostron/multicluster-global-hub/pkg/enum" -) - -func NewPlacementDecisionHandler() conflator.Handler { - return NewGenericHandler[*clustersv1beta1.PlacementDecision]( - string(enum.PlacementDecisionType), - conflator.PlacementDecisionPriority, - enum.CompleteStateMode, - fmt.Sprintf("%s.%s", database.StatusSchema, database.PlacementDecisionsTableName)) -} diff --git a/manager/pkg/statussyncer/syncers/placement_rules_handler.go b/manager/pkg/statussyncer/syncers/placement_rules_handler.go deleted file mode 100644 index dd97f9b8b..000000000 --- a/manager/pkg/statussyncer/syncers/placement_rules_handler.go +++ /dev/null @@ -1,19 +0,0 @@ -package dbsyncer - -import ( - "fmt" - - placementrulesv1 "open-cluster-management.io/multicloud-operators-subscription/pkg/apis/apps/placementrule/v1" - - "github.com/stolostron/multicluster-global-hub/manager/pkg/statussyncer/conflator" - "github.com/stolostron/multicluster-global-hub/pkg/database" - "github.com/stolostron/multicluster-global-hub/pkg/enum" -) - -func NewPlacementRuleHandler() conflator.Handler { - return NewGenericHandler[*placementrulesv1.PlacementRule]( - string(enum.PlacementRuleSpecType), - conflator.PlacementRulePriority, - enum.CompleteStateMode, - fmt.Sprintf("%s.%s", database.StatusSchema, database.PlacementRulesTableName)) -} diff --git a/manager/pkg/statussyncer/syncers/placements_handler.go b/manager/pkg/statussyncer/syncers/placements_handler.go deleted file mode 100644 index d3902e084..000000000 --- a/manager/pkg/statussyncer/syncers/placements_handler.go +++ /dev/null @@ -1,19 +0,0 @@ -package dbsyncer - -import ( - "fmt" - - clustersv1beta1 "open-cluster-management.io/api/cluster/v1beta1" - - "github.com/stolostron/multicluster-global-hub/manager/pkg/statussyncer/conflator" - "github.com/stolostron/multicluster-global-hub/pkg/database" - "github.com/stolostron/multicluster-global-hub/pkg/enum" -) - -func NewPlacementHandler() conflator.Handler { - return NewGenericHandler[*clustersv1beta1.Placement]( - string(enum.PlacementSpecType), - conflator.PlacementPriority, - enum.CompleteStateMode, - fmt.Sprintf("%s.%s", database.StatusSchema, database.PlacementsTableName)) -} diff --git a/manager/pkg/statussyncer/syncers/subscription_reports_handler.go b/manager/pkg/statussyncer/syncers/subscription_reports_handler.go deleted file mode 100644 index 3c8fb17a5..000000000 --- a/manager/pkg/statussyncer/syncers/subscription_reports_handler.go +++ /dev/null @@ -1,19 +0,0 @@ -package dbsyncer - -import ( - "fmt" - - appsv1alpha1 "open-cluster-management.io/multicloud-operators-subscription/pkg/apis/apps/v1alpha1" - - "github.com/stolostron/multicluster-global-hub/manager/pkg/statussyncer/conflator" - "github.com/stolostron/multicluster-global-hub/pkg/database" - "github.com/stolostron/multicluster-global-hub/pkg/enum" -) - -func NewSubscriptionReportHandler() conflator.Handler { - return NewGenericHandler[*appsv1alpha1.SubscriptionReport]( - string(enum.SubscriptionReportType), - conflator.SubscriptionReportPriority, - enum.CompleteStateMode, - fmt.Sprintf("%s.%s", database.StatusSchema, database.SubscriptionReportsTableName)) -} diff --git a/manager/pkg/statussyncer/syncers/subscription_status_handler.go b/manager/pkg/statussyncer/syncers/subscription_status_handler.go deleted file mode 100644 index 965eb840a..000000000 --- a/manager/pkg/statussyncer/syncers/subscription_status_handler.go +++ /dev/null @@ -1,19 +0,0 @@ -package dbsyncer - -import ( - "fmt" - - appsv1alpha1 "open-cluster-management.io/multicloud-operators-subscription/pkg/apis/apps/v1alpha1" - - "github.com/stolostron/multicluster-global-hub/manager/pkg/statussyncer/conflator" - "github.com/stolostron/multicluster-global-hub/pkg/database" - "github.com/stolostron/multicluster-global-hub/pkg/enum" -) - -func NewSubscriptionStatusHandler() conflator.Handler { - return NewGenericHandler[*appsv1alpha1.SubscriptionStatus]( - string(enum.SubscriptionStatusType), - conflator.SubscriptionStatusPriority, - enum.CompleteStateMode, - fmt.Sprintf("%s.%s", database.StatusSchema, database.SubscriptionStatusesTableName)) -} diff --git a/test/e2e/suite_test.go b/test/e2e/suite_test.go index 9fc6589f2..bca7cf488 100644 --- a/test/e2e/suite_test.go +++ b/test/e2e/suite_test.go @@ -30,7 +30,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/log/zap" agentconfig "github.com/stolostron/multicluster-global-hub/agent/pkg/configs" - managerconfig "github.com/stolostron/multicluster-global-hub/manager/pkg/config" + managerconfig "github.com/stolostron/multicluster-global-hub/manager/pkg/configs" "github.com/stolostron/multicluster-global-hub/operator/api/operator/v1alpha4" operatorconfig "github.com/stolostron/multicluster-global-hub/operator/pkg/config" "github.com/stolostron/multicluster-global-hub/operator/pkg/constants" diff --git a/test/integration/agent/spec/generic_syncer_test.go b/test/integration/agent/spec/generic_syncer_test.go index 80ed78e74..6f28466a4 100644 --- a/test/integration/agent/spec/generic_syncer_test.go +++ b/test/integration/agent/spec/generic_syncer_test.go @@ -14,7 +14,7 @@ import ( policyv1 "open-cluster-management.io/governance-policy-propagator/api/v1" "sigs.k8s.io/controller-runtime/pkg/client" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/bundle" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec/controllers/bundle" "github.com/stolostron/multicluster-global-hub/pkg/constants" "github.com/stolostron/multicluster-global-hub/pkg/transport" "github.com/stolostron/multicluster-global-hub/pkg/utils" diff --git a/test/integration/manager/api/api_test.go b/test/integration/manager/api/api_test.go index c528e6330..d561a3139 100644 --- a/test/integration/manager/api/api_test.go +++ b/test/integration/manager/api/api_test.go @@ -18,8 +18,8 @@ import ( . "github.com/onsi/gomega" "gorm.io/gorm" - "github.com/stolostron/multicluster-global-hub/manager/pkg/nonk8sapi" - "github.com/stolostron/multicluster-global-hub/manager/pkg/nonk8sapi/util" + "github.com/stolostron/multicluster-global-hub/manager/pkg/restapis" + "github.com/stolostron/multicluster-global-hub/manager/pkg/restapis/util" "github.com/stolostron/multicluster-global-hub/pkg/database" "github.com/stolostron/multicluster-global-hub/pkg/database/models" ) @@ -65,7 +65,7 @@ var _ = Describe("Nonk8s API Server", Ordered, func() { db = database.GetGorm() By("Set up nonk8s-api server router") - router, err = nonk8sapi.SetupRouter(&nonk8sapi.NonK8sAPIServerConfig{ + router, err = restapis.SetupRouter(&restapis.RestApiServerConfig{ ServerBasePath: "/global-hub-api/v1", ClusterAPIURL: testAuthServer.URL, }) diff --git a/test/integration/manager/controller/backup_test.go b/test/integration/manager/controller/backup_test.go index 1c7662adf..8b652ebd8 100644 --- a/test/integration/manager/controller/backup_test.go +++ b/test/integration/manager/controller/backup_test.go @@ -13,7 +13,7 @@ import ( "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/reconcile" - "github.com/stolostron/multicluster-global-hub/manager/pkg/backup" + "github.com/stolostron/multicluster-global-hub/manager/pkg/controllers" "github.com/stolostron/multicluster-global-hub/pkg/constants" "github.com/stolostron/multicluster-global-hub/pkg/database" "github.com/stolostron/multicluster-global-hub/pkg/utils" @@ -65,7 +65,7 @@ var postgresPvc = &corev1.PersistentVolumeClaim{ } var _ = Describe("backup pvc", Ordered, func() { - var backupReconciler *backup.BackupPVCReconciler + var backupReconciler *controllers.BackupPVCReconciler BeforeAll(func() { By("Creating the namespace") mghSystemNamespace := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: constants.GHDefaultNamespace}} @@ -75,7 +75,7 @@ var _ = Describe("backup pvc", Ordered, func() { err = mgr.GetClient().Create(ctx, mchNamespace) Expect(err).Should(Succeed()) - backupReconciler = backup.NewBackupPVCReconciler(mgr, database.GetConn()) + backupReconciler = controllers.NewBackupPVCReconciler(mgr, database.GetConn()) err = backupReconciler.SetupWithManager(mgr) Expect(err).NotTo(HaveOccurred()) Expect(mgr.GetClient().Create(ctx, postgresPvc)).NotTo(HaveOccurred()) diff --git a/test/integration/manager/controller/compliance_history_job_test.go b/test/integration/manager/controller/compliance_history_job_test.go index b8b874a00..1b5623057 100644 --- a/test/integration/manager/controller/compliance_history_job_test.go +++ b/test/integration/manager/controller/compliance_history_job_test.go @@ -8,7 +8,7 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - "github.com/stolostron/multicluster-global-hub/manager/pkg/cronjob/task" + "github.com/stolostron/multicluster-global-hub/manager/pkg/processes/cronjob/task" "github.com/stolostron/multicluster-global-hub/pkg/database/models" ) diff --git a/test/integration/manager/controller/data_retention_job_test.go b/test/integration/manager/controller/data_retention_job_test.go index 18a1deac0..d5b3b9876 100644 --- a/test/integration/manager/controller/data_retention_job_test.go +++ b/test/integration/manager/controller/data_retention_job_test.go @@ -15,7 +15,7 @@ import ( clusterv1 "open-cluster-management.io/api/cluster/v1" policyv1 "open-cluster-management.io/governance-policy-propagator/api/v1" - "github.com/stolostron/multicluster-global-hub/manager/pkg/cronjob/task" + "github.com/stolostron/multicluster-global-hub/manager/pkg/processes/cronjob/task" "github.com/stolostron/multicluster-global-hub/pkg/database" "github.com/stolostron/multicluster-global-hub/pkg/database/models" ) diff --git a/test/integration/manager/controller/hub_management_test.go b/test/integration/manager/controller/hub_management_test.go index 4bf6ba00e..3957eec5e 100644 --- a/test/integration/manager/controller/hub_management_test.go +++ b/test/integration/manager/controller/hub_management_test.go @@ -10,7 +10,7 @@ import ( . "github.com/onsi/gomega" "gorm.io/gorm/clause" - "github.com/stolostron/multicluster-global-hub/manager/pkg/hubmanagement" + "github.com/stolostron/multicluster-global-hub/manager/pkg/processes/hubmanagement" "github.com/stolostron/multicluster-global-hub/pkg/database" "github.com/stolostron/multicluster-global-hub/pkg/database/models" "github.com/stolostron/multicluster-global-hub/pkg/transport" diff --git a/test/integration/manager/controller/scheduler_test.go b/test/integration/manager/controller/scheduler_test.go index 1a6492735..278f94667 100644 --- a/test/integration/manager/controller/scheduler_test.go +++ b/test/integration/manager/controller/scheduler_test.go @@ -7,15 +7,15 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - "github.com/stolostron/multicluster-global-hub/manager/pkg/config" - "github.com/stolostron/multicluster-global-hub/manager/pkg/cronjob" - "github.com/stolostron/multicluster-global-hub/manager/pkg/cronjob/task" + "github.com/stolostron/multicluster-global-hub/manager/pkg/configs" + "github.com/stolostron/multicluster-global-hub/manager/pkg/processes/cronjob" + "github.com/stolostron/multicluster-global-hub/manager/pkg/processes/cronjob/task" ) var _ = Describe("scheduler", func() { It("test the scheduler", func() { - managerConfig := &config.ManagerConfig{ - DatabaseConfig: &config.DatabaseConfig{ + managerConfig := &configs.ManagerConfig{ + DatabaseConfig: &configs.DatabaseConfig{ DataRetention: 18, }, } diff --git a/test/integration/manager/controller/suite_test.go b/test/integration/manager/controller/suite_test.go index 91f832d77..54d5d5d5e 100644 --- a/test/integration/manager/controller/suite_test.go +++ b/test/integration/manager/controller/suite_test.go @@ -19,8 +19,8 @@ import ( "sigs.k8s.io/controller-runtime/pkg/manager" metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server" - "github.com/stolostron/multicluster-global-hub/manager/pkg/config" - "github.com/stolostron/multicluster-global-hub/manager/pkg/migration" + "github.com/stolostron/multicluster-global-hub/manager/pkg/configs" + "github.com/stolostron/multicluster-global-hub/manager/pkg/controllers" "github.com/stolostron/multicluster-global-hub/pkg/database" "github.com/stolostron/multicluster-global-hub/pkg/transport" genericproducer "github.com/stolostron/multicluster-global-hub/pkg/transport/producer" @@ -36,7 +36,7 @@ var ( testPostgres *testpostgres.TestPostgres db *gorm.DB mgr manager.Manager - migrationReconciler *migration.MigrationReconciler + migrationReconciler *controllers.MigrationController ) func TestController(t *testing.T) { @@ -93,13 +93,13 @@ var _ = BeforeSuite(func() { Metrics: metricsserver.Options{ BindAddress: "0", // disable the metrics serving }, - Scheme: config.GetRuntimeScheme(), + Scheme: configs.GetRuntimeScheme(), }) Expect(err).NotTo(HaveOccurred()) genericProducer, err := genericproducer.NewGenericProducer(transportConfig) Expect(err).NotTo(HaveOccurred()) - migrationReconciler = migration.NewMigrationReconciler(mgr.GetClient(), genericProducer, false) + migrationReconciler = controllers.NewMigrationController(mgr.GetClient(), genericProducer, false) Expect(migrationReconciler.SetupWithManager(mgr)).To(Succeed()) go func() { diff --git a/test/integration/manager/spec/suite_test.go b/test/integration/manager/spec/suite_test.go index e28fb3cec..5873a464a 100644 --- a/test/integration/manager/spec/suite_test.go +++ b/test/integration/manager/spec/suite_test.go @@ -23,10 +23,9 @@ import ( "sigs.k8s.io/controller-runtime/pkg/log/zap" metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server" - "github.com/stolostron/multicluster-global-hub/manager/pkg/config" - "github.com/stolostron/multicluster-global-hub/manager/pkg/nonk8sapi" - specsycner "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/db2transport/syncer" - "github.com/stolostron/multicluster-global-hub/manager/pkg/specsyncer/spec2db" + "github.com/stolostron/multicluster-global-hub/manager/pkg/configs" + "github.com/stolostron/multicluster-global-hub/manager/pkg/restapis" + "github.com/stolostron/multicluster-global-hub/manager/pkg/spec" "github.com/stolostron/multicluster-global-hub/pkg/database" commonobjects "github.com/stolostron/multicluster-global-hub/pkg/objects" "github.com/stolostron/multicluster-global-hub/pkg/statistics" @@ -91,17 +90,17 @@ var _ = BeforeSuite(func() { Metrics: metricsserver.Options{ BindAddress: "0", // disable the metrics serving }, - Scheme: config.GetRuntimeScheme(), + Scheme: configs.GetRuntimeScheme(), }) Expect(err).NotTo(HaveOccurred()) By("Get kubeClient") - runtimeClient, err = client.New(cfg, client.Options{Scheme: config.GetRuntimeScheme()}) + runtimeClient, err = client.New(cfg, client.Options{Scheme: configs.GetRuntimeScheme()}) Expect(err).NotTo(HaveOccurred()) Expect(runtimeClient).NotTo(BeNil()) - managerConfig := &config.ManagerConfig{ - SyncerConfig: &config.SyncerConfig{ + managerConfig := &configs.ManagerConfig{ + SyncerConfig: &configs.SyncerConfig{ SpecSyncInterval: 1 * time.Second, DeletedLabelsTrimmingInterval: 2 * time.Second, }, @@ -113,9 +112,9 @@ var _ = BeforeSuite(func() { StatusTopic: "event", }, }, - StatisticsConfig: &statistics.StatisticsConfig{}, - NonK8sAPIServerConfig: &nonk8sapi.NonK8sAPIServerConfig{}, - ElectionConfig: &commonobjects.LeaderElectionConfig{}, + StatisticsConfig: &statistics.StatisticsConfig{}, + RestAPIServerConfig: &restapis.RestApiServerConfig{}, + ElectionConfig: &commonobjects.LeaderElectionConfig{}, } By("Create consumer/producer") @@ -128,12 +127,12 @@ var _ = BeforeSuite(func() { By("Add db to transport") Expect(mgr.Add(consumer)).Should(Succeed()) - Expect(specsycner.AddDB2TransportSyncers(mgr, managerConfig, producer)).Should(Succeed()) - Expect(specsycner.AddManagedClusterLabelSyncer(mgr, + Expect(spec.AddDatabaseSyncers(mgr, managerConfig, producer)).Should(Succeed()) + Expect(spec.AddManagedClusterLabelDBSyncer(mgr, managerConfig.SyncerConfig.DeletedLabelsTrimmingInterval)).Should(Succeed()) By("Add spec to database") - Expect(spec2db.AddSpec2DBControllers(mgr)).Should(Succeed()) + Expect(spec.AddDBControllers(mgr)).Should(Succeed()) By("Start the manager") go func() { diff --git a/test/integration/manager/status/suite_test.go b/test/integration/manager/status/suite_test.go index 4e1bc06fe..24303902d 100644 --- a/test/integration/manager/status/suite_test.go +++ b/test/integration/manager/status/suite_test.go @@ -17,8 +17,8 @@ import ( "sigs.k8s.io/controller-runtime/pkg/log/zap" metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server" - "github.com/stolostron/multicluster-global-hub/manager/pkg/config" - "github.com/stolostron/multicluster-global-hub/manager/pkg/statussyncer" + "github.com/stolostron/multicluster-global-hub/manager/pkg/configs" + "github.com/stolostron/multicluster-global-hub/manager/pkg/status" "github.com/stolostron/multicluster-global-hub/pkg/database" "github.com/stolostron/multicluster-global-hub/pkg/statistics" "github.com/stolostron/multicluster-global-hub/pkg/transport" @@ -72,17 +72,17 @@ var _ = BeforeSuite(func() { Metrics: metricsserver.Options{ BindAddress: "0", // disable the metrics serving }, - Scheme: config.GetRuntimeScheme(), + Scheme: configs.GetRuntimeScheme(), }) Expect(err).NotTo(HaveOccurred()) By("Get kubeClient") - kubeClient, err = client.New(cfg, client.Options{Scheme: config.GetRuntimeScheme()}) + kubeClient, err = client.New(cfg, client.Options{Scheme: configs.GetRuntimeScheme()}) Expect(err).NotTo(HaveOccurred()) Expect(kubeClient).NotTo(BeNil()) By("Create test postgres") - managerConfig := &config.ManagerConfig{ + managerConfig := &configs.ManagerConfig{ TransportConfig: &transport.TransportInternalConfig{ TransportType: string(transport.Chan), KafkaCredential: &transport.KafkaConfig{ @@ -107,7 +107,7 @@ var _ = BeforeSuite(func() { Expect(mgr.Add(consumer)).Should(Succeed()) By("Add controllers to manager") - err = statussyncer.AddStatusSyncers(mgr, consumer, managerConfig) + err = status.AddStatusSyncers(mgr, consumer, managerConfig) Expect(err).ToNot(HaveOccurred()) By("Start the manager")