Skip to content

Commit 88b740d

Browse files
authored
Merge pull request #240 from hyperspike/repos
bug: clean up repoes reconciliation
2 parents f3fe527 + 2110a2c commit 88b740d

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

internal/controller/repo_controller.go

+12-11
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package controller
1818

1919
import (
2020
"context"
21+
"fmt"
2122
"time"
2223

2324
g "code.gitea.io/sdk/gitea"
@@ -70,29 +71,29 @@ func (r *RepoReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
7071
return ctrl.Result{}, err
7172
}
7273
if h == nil && gitea == nil {
73-
return ctrl.Result{Requeue: true, RequeueAfter: time.Second * 5}, nil
74+
return ctrl.Result{Requeue: true, RequeueAfter: time.Second * 5}, fmt.Errorf("failed to get gitea client")
7475
}
7576
r.h = h
7677

7778
isRepoMarkedToBeDeleted := repo.GetDeletionTimestamp() != nil
7879
if isRepoMarkedToBeDeleted {
7980
if controllerutil.ContainsFinalizer(repo, repoFinalizer) {
8081
if err := r.deleteRepo(repo); err != nil {
81-
return ctrl.Result{}, err
82+
return ctrl.Result{Requeue: true, RequeueAfter: time.Second * 5}, err
8283
}
8384
controllerutil.RemoveFinalizer(repo, repoFinalizer)
8485
if err := r.Update(ctx, repo); err != nil {
85-
return ctrl.Result{}, err
86+
return ctrl.Result{Requeue: true, RequeueAfter: time.Second * 5}, err
8687
}
8788
}
8889
return ctrl.Result{}, nil
8990
}
9091

9192
if repo.Spec.User == nil && repo.Spec.Org == nil {
92-
return ctrl.Result{}, nil
93+
return ctrl.Result{}, fmt.Errorf("repo must be created for either user or org")
9394
}
9495
if repo.Spec.User != nil && repo.Spec.Org != nil {
95-
return ctrl.Result{}, nil
96+
return ctrl.Result{}, fmt.Errorf("repo cannot be created for both user and org")
9697
}
9798
want := &g.Repository{
9899
Description: repo.Spec.Description,
@@ -101,13 +102,13 @@ func (r *RepoReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
101102
if repo.Spec.Org != nil {
102103
gRepo, resp, err := r.h.GetRepo(repo.Spec.Org.Name, repo.Name)
103104
if err != nil && resp.StatusCode != 404 {
104-
return ctrl.Result{}, err
105+
return ctrl.Result{Requeue: true, RequeueAfter: time.Second * 5}, err
105106
}
106107
if resp.StatusCode == 200 {
107108
if !compareRepo(want, gRepo) {
108109
logger.Info("updating repo")
109110
if _, _, err := r.h.EditRepo(repo.Spec.Org.Name, repo.Name, g.EditRepoOption{}); err != nil {
110-
return ctrl.Result{}, err
111+
return ctrl.Result{Requeue: true, RequeueAfter: time.Second * 5}, err
111112
}
112113
}
113114
}
@@ -125,13 +126,13 @@ func (r *RepoReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
125126
IssueLabels: repo.Spec.IssueLabels,
126127
})
127128
if err != nil {
128-
return ctrl.Result{}, nil
129+
return ctrl.Result{Requeue: true, RequeueAfter: time.Second * 5}, err
129130
}
130131
if !repo.Status.Provisioned {
131132
repo.Status.Provisioned = true
132133
if err := r.Client.Status().Update(ctx, repo); err != nil {
133134
logger.Error(err, "Failed to update Repo status")
134-
return ctrl.Result{}, nil
135+
return ctrl.Result{Requeue: true, RequeueAfter: time.Second * 5}, err
135136
}
136137
}
137138
if !controllerutil.ContainsFinalizer(repo, repoFinalizer) {
@@ -144,14 +145,14 @@ func (r *RepoReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
144145
if repo.Spec.User != nil {
145146
_, _, err := r.h.CreateRepo(g.CreateRepoOption{})
146147
if err != nil {
147-
return ctrl.Result{}, nil
148+
return ctrl.Result{}, err
148149
}
149150
}
150151
if !repo.Status.Provisioned {
151152
repo.Status.Provisioned = true
152153
if err := r.Client.Status().Update(ctx, repo); err != nil {
153154
logger.Error(err, "Failed to update Repo status")
154-
return ctrl.Result{}, nil
155+
return ctrl.Result{}, err
155156
}
156157
}
157158

0 commit comments

Comments
 (0)