diff --git a/pkg/cmd/server/config/config.go b/pkg/cmd/server/config/config.go index ba1b9f3bf..dda3498cc 100644 --- a/pkg/cmd/server/config/config.go +++ b/pkg/cmd/server/config/config.go @@ -53,6 +53,7 @@ const ( var ( // DisableableControllers is a list of controllers that can be disabled DisableableControllers = []string{ + "backup-queue", constant.ControllerBackup, constant.ControllerBackupOperations, constant.ControllerBackupDeletion, diff --git a/pkg/controller/backup_queue_controller.go b/pkg/controller/backup_queue_controller.go new file mode 100644 index 000000000..8ff5571f1 --- /dev/null +++ b/pkg/controller/backup_queue_controller.go @@ -0,0 +1,69 @@ +/* +Copyright the Velero contributors. + +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 controller + +import ( + "context" + + "github.com/sirupsen/logrus" + "k8s.io/apimachinery/pkg/runtime" + ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/client" + + velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1" +) + +// backupQueueReconciler reconciles a Backup object +type backupQueueReconciler struct { + client.Client + Scheme *runtime.Scheme + logger logrus.FieldLogger +} + +// NewBackupQueueReconciler returns a new backupQueueReconciler +func NewBackupQueueReconciler( + client client.Client, + scheme *runtime.Scheme, + logger logrus.FieldLogger, +) *backupQueueReconciler { + return &backupQueueReconciler{ + Client: client, + Scheme: scheme, + logger: logger, + } +} + +// SetupWithManager adds the reconciler to the manager +func (r *backupQueueReconciler) SetupWithManager(mgr ctrl.Manager) error { + return ctrl.NewControllerManagedBy(mgr). + For(&velerov1api.Backup{}). + Complete(r) +} + +// Reconcile reconciles a Backup object +func (r *backupQueueReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { + log := r.logger.WithField("backup", req.NamespacedName.String()) + + log.Debug("Getting backup") + backup := &velerov1api.Backup{} + if err := r.Get(ctx, req.NamespacedName, backup); err != nil { + log.WithError(err).Error("unable to get backup") + return ctrl.Result{}, client.IgnoreNotFound(err) + } + + return ctrl.Result{}, nil +} diff --git a/pkg/controller/backup_queue_controller_test.go b/pkg/controller/backup_queue_controller_test.go new file mode 100644 index 000000000..159163300 --- /dev/null +++ b/pkg/controller/backup_queue_controller_test.go @@ -0,0 +1,61 @@ +/* +Copyright the Velero contributors. + +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 controller + +import ( + "context" + "testing" + + "github.com/sirupsen/logrus" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/types" + ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/client/fake" + + velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1" +) + +func TestBackupQueueReconciler(t *testing.T) { + scheme := runtime.NewScheme() + velerov1api.AddToScheme(scheme) + + backup := &velerov1api.Backup{ + ObjectMeta: metav1.ObjectMeta{ + Namespace: "velero", + Name: "test-backup", + }, + } + + fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(backup).Build() + logger := logrus.New() + log := logger.WithField("controller", "backup-queue-test") + + r := NewBackupQueueReconciler(fakeClient, scheme, log) + req := ctrl.Request{ + NamespacedName: types.NamespacedName{ + Namespace: "velero", + Name: "test-backup", + }, + } + + res, err := r.Reconcile(context.Background(), req) + require.NoError(t, err) + assert.Equal(t, ctrl.Result{}, res) +}