From c13bbdea3f935659015047aa31a20182dc6ec2c4 Mon Sep 17 00:00:00 2001 From: Prasad Ghangal Date: Fri, 30 Jun 2023 19:02:08 +0530 Subject: [PATCH] Revert "Refactor RepositoryServer controller (#2136)" This reverts commit fb4bd07b02017b1852694e86d4149b39650c16f6. --- pkg/controllers/repositoryserver/handler.go | 32 ++++++-------- .../repositoryserver_controller.go | 42 +++---------------- pkg/controllers/repositoryserver/server.go | 13 ++++-- 3 files changed, 26 insertions(+), 61 deletions(-) diff --git a/pkg/controllers/repositoryserver/handler.go b/pkg/controllers/repositoryserver/handler.go index 67949083ee..b44cfe7721 100644 --- a/pkg/controllers/repositoryserver/handler.go +++ b/pkg/controllers/repositoryserver/handler.go @@ -70,6 +70,18 @@ func (h *RepoServerHandler) CreateOrUpdateOwnedResources(ctx context.Context) er return err } } + + if err := h.connectToKopiaRepository(); err != nil { + return errors.Wrap(err, "Failed to connect to Kopia repository") + } + + if err := h.startRepoProxyServer(ctx); err != nil { + return errors.Wrap(err, "Failed to start Kopia API server") + } + + if err := h.createOrUpdateClientUsers(ctx); err != nil { + return errors.Wrap(err, "Failed to create/update kopia API server access users") + } return nil } @@ -300,23 +312,3 @@ func (h *RepoServerHandler) waitForPodReady(ctx context.Context, pod *corev1.Pod } return nil } - -func (h *RepoServerHandler) updateProgressInCRStatus(ctx context.Context, progress crv1alpha1.RepositoryServerProgress) error { - h.Logger.Info("Fetch latest version of RepositoryServer to update the ServerInfo in its status") - repoServerName := h.RepositoryServer.Name - repoServerNamespace := h.RepositoryServer.Namespace - rs := crv1alpha1.RepositoryServer{} - err := h.Reconciler.Get(ctx, types.NamespacedName{Name: repoServerName, Namespace: repoServerNamespace}, &rs) - if err != nil { - return err - } - h.Logger.Info("Update the Progress") - rs.Status.Progress = progress - err = h.Reconciler.Status().Update(ctx, &rs) - if err != nil { - return err - } - h.Logger.Info("Use this updated RepositoryServer CR") - h.RepositoryServer = &rs - return nil -} diff --git a/pkg/controllers/repositoryserver/repositoryserver_controller.go b/pkg/controllers/repositoryserver/repositoryserver_controller.go index 4353516724..0c11d36103 100644 --- a/pkg/controllers/repositoryserver/repositoryserver_controller.go +++ b/pkg/controllers/repositoryserver/repositoryserver_controller.go @@ -87,51 +87,19 @@ func (r *RepositoryServerReconciler) Reconcile(ctx context.Context, req ctrl.Req if err = r.Status().Update(ctx, repoServerHandler.RepositoryServer); err != nil { return ctrl.Result{}, err } - logger.Info("Setup RepositoryServer, Create or update owned resources") if err := repoServerHandler.CreateOrUpdateOwnedResources(ctx); err != nil { logger.Info("Setting the CR status as 'Failed' since an error occurred in create/update event") - if uerr := repoServerHandler.updateProgressInCRStatus(ctx, crkanisteriov1alpha1.Failed); uerr != nil { + repoServerHandler.RepositoryServer.Status.Progress = crkanisteriov1alpha1.Failed + if uerr := r.Status().Update(ctx, repoServerHandler.RepositoryServer); uerr != nil { return ctrl.Result{}, uerr } r.Recorder.Event(repoServerHandler.RepositoryServer, corev1.EventTypeWarning, "Failed", err.Error()) return ctrl.Result{}, err } - - logger.Info("Connect to Kopia Repository") - if err := repoServerHandler.connectToKopiaRepository(); err != nil { - if uerr := repoServerHandler.updateProgressInCRStatus(ctx, crkanisteriov1alpha1.Failed); uerr != nil { - return ctrl.Result{}, uerr - } - return ctrl.Result{}, err - } - - logger.Info("Start Repository Server") - if err := repoServerHandler.startRepoProxyServer(ctx); err != nil { - if uerr := repoServerHandler.updateProgressInCRStatus(ctx, crkanisteriov1alpha1.Failed); uerr != nil { - return ctrl.Result{}, uerr - } - return ctrl.Result{}, err - } - - logger.Info("Add/Update users") - if err := repoServerHandler.createOrUpdateClientUsers(ctx); err != nil { - if uerr := repoServerHandler.updateProgressInCRStatus(ctx, crkanisteriov1alpha1.Failed); uerr != nil { - return ctrl.Result{}, uerr - } - return ctrl.Result{}, err - } - - logger.Info("Refresh Server") - if err := repoServerHandler.refreshServer(ctx); err != nil { - if uerr := repoServerHandler.updateProgressInCRStatus(ctx, crkanisteriov1alpha1.Failed); uerr != nil { - return ctrl.Result{}, uerr - } - return ctrl.Result{}, err - } - logger.Info("Setting the CR status as 'Ready' after completing the create/update event\n\n\n\n") - if uerr := repoServerHandler.updateProgressInCRStatus(ctx, crkanisteriov1alpha1.Ready); uerr != nil { - return ctrl.Result{}, uerr + repoServerHandler.RepositoryServer.Status.Progress = crkanisteriov1alpha1.Ready + if err = r.Status().Update(ctx, repoServerHandler.RepositoryServer); err != nil { + return ctrl.Result{}, err } return ctrl.Result{}, nil diff --git a/pkg/controllers/repositoryserver/server.go b/pkg/controllers/repositoryserver/server.go index d731d354d2..456cd4ba20 100644 --- a/pkg/controllers/repositoryserver/server.go +++ b/pkg/controllers/repositoryserver/server.go @@ -190,14 +190,19 @@ func (h *RepoServerHandler) createOrUpdateClientUsers(ctx context.Context) error return errors.Wrap(err, "Failed to add new user to the Kopia API server") } } - return nil -} -func (h *RepoServerHandler) refreshServer(ctx context.Context) error { - serverAddress, username, password, err := h.getServerDetails(ctx) + repoServerAddress, serverAdminUserName, serverAdminPassword, err := h.getServerDetails(ctx) if err != nil { return err } + err = h.refreshServer(ctx, repoServerAddress, serverAdminUserName, serverAdminPassword) + if err != nil { + return errors.Wrap(err, "Failed to refresh Kopia API server") + } + return nil +} + +func (h *RepoServerHandler) refreshServer(ctx context.Context, serverAddress, username, password string) error { repoPassword := string(h.RepositoryServerSecrets.repositoryPassword.Data[reposerver.RepoPasswordKey]) fingerprint, err := kopia.ExtractFingerprintFromCertSecret(ctx, h.KubeCli, h.RepositoryServerSecrets.serverTLS.Name, h.RepositoryServer.Namespace) if err != nil {