Skip to content

Commit

Permalink
πŸ“ Refactor the structure of the global hub manager (stolostron#1166)
Browse files Browse the repository at this point in the history
* refactor manager

Signed-off-by: myan <[email protected]>

* add update

Signed-off-by: myan <[email protected]>

* add loading

Signed-off-by: myan <[email protected]>

* fix the error

Signed-off-by: myan <[email protected]>

* format code

Signed-off-by: myan <[email protected]>

* fix the build error

Signed-off-by: myan <[email protected]>

* test ut and integration

Signed-off-by: myan <[email protected]>

* add document

Signed-off-by: myan <[email protected]>

* fix the sonar

Signed-off-by: myan <[email protected]>

* add rename

Signed-off-by: myan <[email protected]>

* update readme

Signed-off-by: myan <[email protected]>

* fix the build

Signed-off-by: myan <[email protected]>

* fix the dir

Signed-off-by: myan <[email protected]>

* update docker file

Signed-off-by: myan <[email protected]>

---------

Signed-off-by: myan <[email protected]>
  • Loading branch information
yanmxa authored Oct 21, 2024
1 parent c215eac commit c98be62
Show file tree
Hide file tree
Showing 118 changed files with 640 additions and 864 deletions.
41 changes: 20 additions & 21 deletions manager/Containerfile.operator
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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"]
ENTRYPOINT ["/usr/local/bin/manager"]
2 changes: 1 addition & 1 deletion manager/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion manager/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
22 changes: 22 additions & 0 deletions manager/README.md
Original file line number Diff line number Diff line change
@@ -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.
60 changes: 27 additions & 33 deletions manager/cmd/manager/main.go β†’ manager/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
}
Expand All @@ -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),
},
Expand Down Expand Up @@ -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,
Expand All @@ -231,32 +225,32 @@ 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
}
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)
}

Expand All @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package backup
package controllers

import (
"context"
Expand Down
Loading

0 comments on commit c98be62

Please sign in to comment.