Skip to content

Commit

Permalink
Set RepositoryServerCR Conditions (#2169)
Browse files Browse the repository at this point in the history
* Revert "Revert "Refactor RepositoryServer controller (#2136)" (#2150)"

This reverts commit 64595c9.

* Remove logs from RepositoryServer Controller

Signed-off-by: Akanksha Kumari <akankshakumari393@gmail.com>

* Remove logs

Signed-off-by: Akanksha Kumari <akankshakumari393@gmail.com>

* Add logs and values that add name and namespace to the logger

Signed-off-by: Akanksha Kumari <akankshakumari393@gmail.com>

* Run gofmt

Signed-off-by: Akanksha Kumari <akankshakumari393@gmail.com>

* Run gofmt

Signed-off-by: Akanksha Kumari <akankshakumari393@gmail.com>

* Handle CR status conditions for Updating Service and pod

Signed-off-by: Akanksha Kumari <akankshakumari393@gmail.com>

* Update CR status after updating condition

Signed-off-by: Akanksha Kumari <akankshakumari393@gmail.com>

* update overall status intead of progress

Signed-off-by: Akanksha Kumari <akankshakumari393@gmail.com>

* Handle status condition for All stages in Reconcile function

Signed-off-by: Akanksha Kumari <akankshakumari393@gmail.com>

* Run gofmt

Signed-off-by: Akanksha Kumari <akankshakumari393@gmail.com>

* handle case for Pod in crashloopbackoff state

Signed-off-by: Akanksha Kumari <akankshakumari393@gmail.com>

* Remove handling for pod crashloopbackoff status

Signed-off-by: Akanksha Kumari <akankshakumari393@gmail.com>

* Handle failure scenario for starting Repo proxy server

Signed-off-by: Akanksha Kumari <akankshakumari393@gmail.com>

* Handle failure scenario for server refresh

Signed-off-by: Akanksha Kumari <akankshakumari393@gmail.com>

* Refactor reconcile function

Signed-off-by: Akanksha Kumari <akankshakumari393@gmail.com>

* typo in using err variable

Signed-off-by: Akanksha Kumari <akankshakumari393@gmail.com>

* Run gofmt

Signed-off-by: Akanksha Kumari <akankshakumari393@gmail.com>

* Fix lint

Signed-off-by: Akanksha Kumari <akankshakumari393@gmail.com>

* Refactor code that were not aligned due to rebase

Signed-off-by: Akanksha Kumari <akankshakumari393@gmail.com>

* Remove Event handling changes

Signed-off-by: Akanksha Kumari <akankshakumari393@gmail.com>

* Remmove Error handling for RefreshServer and StartRepoProxyServer

Signed-off-by: Akanksha Kumari <akankshakumari393@gmail.com>

* Remmove unnecessary log statement

Signed-off-by: Akanksha Kumari <akankshakumari393@gmail.com>

* Set condition to nil on every reconcilition run

Signed-off-by: Akanksha Kumari <akankshakumari393@gmail.com>

* Address review comments

Signed-off-by: Akanksha Kumari <akankshakumari393@gmail.com>

* Address review comments

Signed-off-by: Akanksha Kumari <akankshakumari393@gmail.com>

* Add ConsitionStatus struct

Signed-off-by: Akanksha Kumari <akankshakumari393@gmail.com>

* Address review comments Remove ConditionStatus Struct and its usage

Signed-off-by: Akanksha Kumari <akankshakumari393@gmail.com>

---------

Signed-off-by: Akanksha Kumari <akankshakumari393@gmail.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
akankshakumari393 and mergify[bot] committed Aug 9, 2023
1 parent 6f6ab7e commit 04fbe86
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 32 deletions.
69 changes: 48 additions & 21 deletions pkg/controllers/repositoryserver/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
apimeta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -93,12 +95,7 @@ func (h *RepoServerHandler) reconcileService(ctx context.Context) (*corev1.Servi
if err != nil {
return nil, err
}
h.Logger.Info("Update service name in RepositoryServer /status")
serverInfo := crv1alpha1.ServerInfo{
PodName: h.RepositoryServer.Status.ServerInfo.PodName,
ServiceName: svc.Name,
}
if err := h.updateServerInfoInCRStatus(ctx, serverInfo); err != nil {
if err := h.updateServerInfoInCRStatus(ctx, h.RepositoryServer.Status.ServerInfo.PodName, svc.Name); err != nil {
return nil, errors.Wrap(err, "Failed to update service name in RepositoryServer /status")
}
return svc, err
Expand Down Expand Up @@ -171,20 +168,25 @@ func (h *RepoServerHandler) reconcilePod(ctx context.Context, svc *corev1.Servic
return nil, nil, err
}
h.Logger.Info("Pod resource not found. Creating new pod")
return h.createPodAndUpdateStatus(ctx, repoServerNamespace, svc)
}

func (h *RepoServerHandler) setCondition(ctx context.Context, condition metav1.Condition, progress crkanisteriov1alpha1.RepositoryServerProgress) error {
if uerr := h.updateRepoServerProgress(ctx, progress, condition); uerr != nil {
return uerr
}
return nil
}

func (h *RepoServerHandler) createPodAndUpdateStatus(ctx context.Context, repoServerNamespace string, svc *corev1.Service) ([]corev1.EnvVar, *corev1.Pod, error) {
var envVars []corev1.EnvVar
pod, envVars, err = h.createPod(ctx, repoServerNamespace, svc)
pod, envVars, err := h.createPod(ctx, repoServerNamespace, svc)
if err != nil {
return nil, nil, err
}
h.Logger.Info("Update pod name in RepositoryServer /status")
serverInfo := crv1alpha1.ServerInfo{
PodName: pod.Name,
ServiceName: h.RepositoryServer.Status.ServerInfo.ServiceName,
}
if err := h.updateServerInfoInCRStatus(ctx, serverInfo); err != nil {
if err := h.updateServerInfoInCRStatus(ctx, pod.Name, h.RepositoryServer.Status.ServerInfo.ServiceName); err != nil {
return nil, nil, errors.Wrap(err, "Failed to update pod name in RepositoryServer /status")
}

return envVars, pod, nil
}

Expand Down Expand Up @@ -284,7 +286,7 @@ func (h *RepoServerHandler) preparePodOverride(ctx context.Context) (map[string]
return podOverride, nil
}

func (h *RepoServerHandler) updateServerInfoInCRStatus(ctx context.Context, info crv1alpha1.ServerInfo) error {
func (h *RepoServerHandler) updateServerInfoInCRStatus(ctx context.Context, podName string, serviceName string) error {
h.Logger.Info("Fetch latest version of RepositoryServer to update the ServerInfo in its status")
repoServerName := h.RepositoryServer.Name
repoServerNamespace := h.RepositoryServer.Namespace
Expand All @@ -293,7 +295,11 @@ func (h *RepoServerHandler) updateServerInfoInCRStatus(ctx context.Context, info
if err != nil {
return err
}
h.Logger.Info("Update the ServerInfo")

info := crv1alpha1.ServerInfo{
PodName: podName,
ServiceName: serviceName,
}
rs.Status.ServerInfo = info
err = h.Reconciler.Status().Update(ctx, &rs)
if err != nil {
Expand All @@ -311,15 +317,18 @@ func (h *RepoServerHandler) waitForPodReady(ctx context.Context, pod *corev1.Pod
return nil
}

func (h *RepoServerHandler) updateRepoServerProgress(ctx context.Context, progress crv1alpha1.RepositoryServerProgress) error {
func (h *RepoServerHandler) updateRepoServerProgress(ctx context.Context, progress crv1alpha1.RepositoryServerProgress, condition metav1.Condition) error {
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
}
rs.Status.Progress = progress
apimeta.SetStatusCondition(&rs.Status.Conditions, condition)
if progress != "" {
rs.Status.Progress = progress
}
err = h.Reconciler.Status().Update(ctx, &rs)
if err != nil {
return err
Expand All @@ -331,26 +340,44 @@ func (h *RepoServerHandler) updateRepoServerProgress(ctx context.Context, progre
func (h *RepoServerHandler) setupKopiaRepositoryServer(ctx context.Context, logger logr.Logger) (ctrl.Result, error) {
logger.Info("Start Kopia Repository Server")
if err := h.startRepoProxyServer(ctx); err != nil {
if uerr := h.updateRepoServerProgress(ctx, crkanisteriov1alpha1.Failed); uerr != nil {
condition := getCondition(metav1.ConditionFalse, conditionReasonServerInitializedErr, "", crkanisteriov1alpha1.ServerInitialized)
if uerr := h.setCondition(ctx, condition, crkanisteriov1alpha1.Failed); uerr != nil {
return ctrl.Result{}, uerr
}
return ctrl.Result{}, err
}

condition := getCondition(metav1.ConditionTrue, conditionReasonServerInitializedSuccess, "", crkanisteriov1alpha1.ServerInitialized)
if uerr := h.setCondition(ctx, condition, crkanisteriov1alpha1.Pending); uerr != nil {
return ctrl.Result{}, uerr
}

logger.Info("Add/Update users in Kopia Repository Server")
if err := h.createOrUpdateClientUsers(ctx); err != nil {
if uerr := h.updateRepoServerProgress(ctx, crkanisteriov1alpha1.Failed); uerr != nil {
condition := getCondition(metav1.ConditionFalse, conditionReasonClientInitializedErr, err.Error(), crkanisteriov1alpha1.ClientUserInitialized)
if uerr := h.setCondition(ctx, condition, crkanisteriov1alpha1.Failed); uerr != nil {
return ctrl.Result{}, uerr
}
return ctrl.Result{}, err
}

condition = getCondition(metav1.ConditionTrue, conditionReasonClientInitializedSuccess, "", crkanisteriov1alpha1.ClientUserInitialized)
if uerr := h.setCondition(ctx, condition, crkanisteriov1alpha1.Pending); uerr != nil {
return ctrl.Result{}, uerr
}

logger.Info("Refresh Kopia Repository Server")
if err := h.refreshServer(ctx); err != nil {
if uerr := h.updateRepoServerProgress(ctx, crkanisteriov1alpha1.Failed); uerr != nil {
condition := getCondition(metav1.ConditionFalse, conditionReasonServerRefreshedErr, err.Error(), crkanisteriov1alpha1.ServerRefreshed)
if uerr := h.setCondition(ctx, condition, crkanisteriov1alpha1.Failed); uerr != nil {
return ctrl.Result{}, uerr
}
return ctrl.Result{}, err
}

condition = getCondition(metav1.ConditionTrue, conditionReasonServerRefreshedSuccess, "", crkanisteriov1alpha1.ServerRefreshed)
if uerr := h.setCondition(ctx, condition, crkanisteriov1alpha1.Ready); uerr != nil {
return ctrl.Result{}, uerr
}
return ctrl.Result{}, nil
}
25 changes: 16 additions & 9 deletions pkg/controllers/repositoryserver/repositoryserver_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/record"
Expand Down Expand Up @@ -80,35 +81,41 @@ func (r *RepositoryServerReconciler) Reconcile(ctx context.Context, req ctrl.Req

repoServerHandler := newRepositoryServerHandler(ctx, req, logger, r, kubeCli, repositoryServer)
repoServerHandler.RepositoryServer = repositoryServer

repoServerHandler.RepositoryServer.Status.Progress = crkanisteriov1alpha1.Pending
repoServerHandler.RepositoryServer.Status.Conditions = nil
if err = r.Status().Update(ctx, repoServerHandler.RepositoryServer); err != nil {
return ctrl.Result{}, err
}

logger.Info("Create or update owned resources by Repository Server CR")
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.updateRepoServerProgress(ctx, crkanisteriov1alpha1.Failed); uerr != nil {
condition := getCondition(metav1.ConditionFalse, conditionReasonServerSetupErr, err.Error(), crkanisteriov1alpha1.ServerSetup)
if uerr := repoServerHandler.setCondition(ctx, condition, crkanisteriov1alpha1.Failed); uerr != nil {
return ctrl.Result{}, uerr
}
r.Recorder.Event(repoServerHandler.RepositoryServer, corev1.EventTypeWarning, "Failed", err.Error())
return ctrl.Result{}, err
}
condition := getCondition(metav1.ConditionTrue, conditionReasonServerSetupSuccess, "", crkanisteriov1alpha1.ServerSetup)
if uerr := repoServerHandler.setCondition(ctx, condition, crkanisteriov1alpha1.Pending); uerr != nil {
return ctrl.Result{}, uerr
}

logger.Info("Connect to Kopia Repository")
if err := repoServerHandler.connectToKopiaRepository(); err != nil {
if uerr := repoServerHandler.updateRepoServerProgress(ctx, crkanisteriov1alpha1.Failed); uerr != nil {
condition := getCondition(metav1.ConditionFalse, conditionReasonRepositoryConnectedErr, err.Error(), crkanisteriov1alpha1.RepositoryConnected)
if uerr := repoServerHandler.setCondition(ctx, condition, crkanisteriov1alpha1.Failed); uerr != nil {
return ctrl.Result{}, uerr
}
return ctrl.Result{}, err
}

if result, err := repoServerHandler.setupKopiaRepositoryServer(ctx, logger); err != nil {
return result, err
condition = getCondition(metav1.ConditionTrue, conditionReasonRepositoryConnectedSuccess, "", crkanisteriov1alpha1.RepositoryConnected)
if uerr := repoServerHandler.setCondition(ctx, condition, crkanisteriov1alpha1.Pending); uerr != nil {
return ctrl.Result{}, uerr
}

if uerr := repoServerHandler.updateRepoServerProgress(ctx, crkanisteriov1alpha1.Ready); uerr != nil {
return ctrl.Result{}, uerr
if result, err := repoServerHandler.setupKopiaRepositoryServer(ctx, logger); err != nil {
return result, err
}

return ctrl.Result{}, nil
Expand Down
3 changes: 1 addition & 2 deletions pkg/controllers/repositoryserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ func (h *RepoServerHandler) startRepoProxyServer(ctx context.Context) (err error
if err != nil {
return errors.Wrap(err, "Failed to check Kopia API server status")
}

return nil
}

Expand Down Expand Up @@ -230,7 +229,7 @@ func (h *RepoServerHandler) getRepositoryServerStartTimeout() time.Duration {
if serverStartTimeoutEnv != "" {
serverStartTimeout, err := time.ParseDuration(serverStartTimeoutEnv)
if err != nil {
h.Logger.Info("Error parsing env variable", err)
h.Logger.Info("Error parsing env variable", "error", err)
return DefaultServerStartTimeout
}
return serverStartTimeout * time.Second
Expand Down
24 changes: 24 additions & 0 deletions pkg/controllers/repositoryserver/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,21 @@ const (
tlsCertDefaultMountPath = "/mnt/secrets/tlscert"
tlsKeyPath = "/mnt/secrets/tlscert/tls.key"
tlsCertPath = "/mnt/secrets/tlscert/tls.crt"

conditionReasonServerSetupErr string = "KopiaRepositoryServerSetupFailed"
conditionReasonServerSetupSuccess string = "KopiaRepositoryServerSetupSucceeded"

conditionReasonRepositoryConnectedErr string = "KopiaRepositoryConnectionFailed"
conditionReasonRepositoryConnectedSuccess string = "KopiaRepositoryConnectionSucceeded"

conditionReasonServerInitializedErr string = "KopiaRepositoryServerInitializationFailed"
conditionReasonServerInitializedSuccess string = "KopiaRepositoryServerInitializationSucceeded"

conditionReasonClientInitializedErr string = "ClientInitializationFailed"
conditionReasonClientInitializedSuccess string = "ClientInitializationSucceeded"

conditionReasonServerRefreshedErr string = "ServerRefreshFailed"
conditionReasonServerRefreshedSuccess string = "ServerRefreshed"
)

func getRepoServerService(namespace string) corev1.Service {
Expand Down Expand Up @@ -230,6 +245,15 @@ func WaitTillCommandSucceed(ctx context.Context, cli kubernetes.Interface, cmd [
return err
}

func getCondition(status metav1.ConditionStatus, reason string, message string, conditionType string) metav1.Condition {
return metav1.Condition{
Status: status,
Reason: reason,
Message: message,
Type: conditionType,
}
}

func getVolumes(ctx context.Context, cli kubernetes.Interface, secret *corev1.Secret, namespace string) (map[string]string, error) {
vols := make(map[string]string, 0)
var claimName []byte
Expand Down

0 comments on commit 04fbe86

Please sign in to comment.