Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EVEREST-1811 | dynamically configure watches for controllers #630

Merged
merged 9 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions api/v1alpha1/databaseclusterbackup_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ const (
// BackupState is used to represent the backup's state.
type BackupState string

// Known Backup states.
const (
BackupNew BackupState = "" //nolint:revive
BackupStarting BackupState = "Starting" //nolint:revive
BackupNew BackupState = ""
BackupStarting BackupState = "Starting"
BackupRunning BackupState = "Running"
BackupFailed BackupState = "Failed"
BackupSucceeded BackupState = "Succeeded"
Expand Down
5 changes: 3 additions & 2 deletions api/v1alpha1/databaseclusterrestore_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ import (
// RestoreState represents state of restoration.
type RestoreState string

// Known Restore states.
const (
RestoreNew RestoreState = "" //nolint:revive
RestoreStarting RestoreState = "Starting" //nolint:revive
RestoreNew RestoreState = ""
RestoreStarting RestoreState = "Starting"
RestoreRunning RestoreState = "Restoring"
RestoreFailed RestoreState = "Failed"
RestoreSucceeded RestoreState = "Succeeded"
Expand Down
39 changes: 26 additions & 13 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ import (
"strings"

vmv1beta1 "github.com/VictoriaMetrics/operator/api/operator/v1beta1"
pgv2 "github.com/percona/percona-postgresql-operator/pkg/apis/pgv2.percona.com/v2"
psmdbv1 "github.com/percona/percona-server-mongodb-operator/pkg/apis/psmdb/v1"
pxcv1 "github.com/percona/percona-xtradb-cluster-operator/pkg/apis/pxc/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -98,6 +101,10 @@ func init() {

utilruntime.Must(everestv1alpha1.AddToScheme(scheme))
utilruntime.Must(vmv1beta1.AddToScheme(scheme))

utilruntime.Must(pgv2.SchemeBuilder.AddToScheme(scheme))
utilruntime.Must(psmdbv1.SchemeBuilder.AddToScheme(scheme))
utilruntime.Must(pxcv1.SchemeBuilder.AddToScheme(scheme))
mayankshah1607 marked this conversation as resolved.
Show resolved Hide resolved
// +kubebuilder:scaffold:scheme
}

Expand Down Expand Up @@ -225,36 +232,42 @@ func main() {
os.Exit(1)
}
}
podRef := corev1.ObjectReference{Name: cfg.PodName, Namespace: cfg.SystemNamespace}
// Initialise the controllers.
if err = (&controllers.DatabaseClusterReconciler{
clusterReconciler := &controllers.DatabaseClusterReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
Cache: mgr.GetCache(),
}
if err := clusterReconciler.SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "DatabaseCluster")
os.Exit(1)
}
if err = (&controllers.DatabaseEngineReconciler{
restoreReconciler := &controllers.DatabaseClusterRestoreReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr, podRef, dbNamespaces); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "DatabaseEngine")
os.Exit(1)
Cache: mgr.GetCache(),
}
if err = (&controllers.DatabaseClusterRestoreReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
if err := restoreReconciler.SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "DatabaseClusterRestore")
os.Exit(1)
}
if err = (&controllers.DatabaseClusterBackupReconciler{
backupReconciler := &controllers.DatabaseClusterBackupReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
Cache: mgr.GetCache(),
}
if err := backupReconciler.SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "DatabaseClusterBackup")
os.Exit(1)
}
if err = (&controllers.DatabaseEngineReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Controllers: []controllers.DatabaseController{clusterReconciler, restoreReconciler, backupReconciler},
}).SetupWithManager(mgr, dbNamespaces); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "DatabaseEngine")
os.Exit(1)
}
if err = (&controllers.BackupStorageReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Expand Down
1 change: 1 addition & 0 deletions internal/controller/backupstorage_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func (r *BackupStorageReconciler) Reconcile(ctx context.Context, req ctrl.Reques
// SetupWithManager sets up the controller with the Manager.
func (r *BackupStorageReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
Named("BackupStorage").
For(&everestv1alpha1.BackupStorage{}).
Owns(&corev1.Secret{}).
Watches(
Expand Down
59 changes: 59 additions & 0 deletions internal/controller/controller_watcher_registry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// everest-operator
// Copyright (C) 2022 Percona LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package controllers contains a set of controllers for everest
package controllers

import (
"sync"

"github.com/go-logr/logr"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/source"
)

// controllerWatcherRegistry is a wrapper arond controller.Controller that provides a way to
// store and keep track of the sources that have been added to the controller.
type controllerWatcherRegistry struct {
controller.Controller
store sync.Map
log logr.Logger
}

func newControllerWatcherRegistry(log logr.Logger, c controller.Controller) *controllerWatcherRegistry {
return &controllerWatcherRegistry{
Controller: c,
log: log,
}
}

// addWatchers adds the provided sources to the controller's watch, and stores the name of the sources in a map to avoid adding them again.
func (c *controllerWatcherRegistry) addWatchers(
name string,
sources ...source.Source,
) error {
_, ok := c.store.Load(name)
if ok {
return nil // watcher group already exists with this name, so skip.
}
for _, src := range sources {
if err := c.Controller.Watch(src); err != nil {
return err
}
}
c.log.Info("Added watchers", "name", name)
c.store.Store(name, struct{}{})
return nil
}
Loading
Loading