@@ -27,10 +27,8 @@ import (
2727 securejoin "github.com/cyphar/filepath-securejoin"
2828 "github.com/fluxcd/pkg/runtime/logger"
2929 corev1 "k8s.io/api/core/v1"
30- apierrors "k8s.io/apimachinery/pkg/api/errors"
3130 "k8s.io/apimachinery/pkg/runtime"
3231 "k8s.io/apimachinery/pkg/types"
33- kerrors "k8s.io/apimachinery/pkg/util/errors"
3432 kuberecorder "k8s.io/client-go/tools/record"
3533 ctrl "sigs.k8s.io/controller-runtime"
3634 "sigs.k8s.io/controller-runtime/pkg/builder"
@@ -49,41 +47,41 @@ import (
4947 sourcev1 "github.com/fluxcd/source-controller/api/v1beta2"
5048 serror "github.com/fluxcd/source-controller/internal/error"
5149 sreconcile "github.com/fluxcd/source-controller/internal/reconcile"
50+ "github.com/fluxcd/source-controller/internal/reconcile/summarize"
5251 "github.com/fluxcd/source-controller/internal/util"
5352 "github.com/fluxcd/source-controller/pkg/git"
5453 "github.com/fluxcd/source-controller/pkg/git/strategy"
5554 "github.com/fluxcd/source-controller/pkg/sourceignore"
5655)
5756
58- // Status conditions owned by the GitRepository reconciler.
59- var gitRepoOwnedConditions = []string {
60- sourcev1 .SourceVerifiedCondition ,
61- sourcev1 .FetchFailedCondition ,
62- sourcev1 .IncludeUnavailableCondition ,
63- sourcev1 .ArtifactOutdatedCondition ,
64- meta .ReadyCondition ,
65- meta .ReconcilingCondition ,
66- meta .StalledCondition ,
67- }
68-
69- // Conditions that Ready condition is influenced by in descending order of their
70- // priority.
71- var gitRepoReadyDeps = []string {
72- sourcev1 .IncludeUnavailableCondition ,
73- sourcev1 .SourceVerifiedCondition ,
74- sourcev1 .FetchFailedCondition ,
75- sourcev1 .ArtifactOutdatedCondition ,
76- meta .StalledCondition ,
77- meta .ReconcilingCondition ,
78- }
79-
80- // Negative conditions that Ready condition is influenced by.
81- var gitRepoReadyDepsNegative = []string {
82- sourcev1 .FetchFailedCondition ,
83- sourcev1 .IncludeUnavailableCondition ,
84- sourcev1 .ArtifactOutdatedCondition ,
85- meta .StalledCondition ,
86- meta .ReconcilingCondition ,
57+ // gitRepoReadyConditions contains all the conditions information needed
58+ // for GitRepository Ready status conditions summary calculation.
59+ var gitRepoReadyConditions = summarize.Conditions {
60+ Target : meta .ReadyCondition ,
61+ Owned : []string {
62+ sourcev1 .SourceVerifiedCondition ,
63+ sourcev1 .FetchFailedCondition ,
64+ sourcev1 .IncludeUnavailableCondition ,
65+ sourcev1 .ArtifactOutdatedCondition ,
66+ meta .ReadyCondition ,
67+ meta .ReconcilingCondition ,
68+ meta .StalledCondition ,
69+ },
70+ Summarize : []string {
71+ sourcev1 .IncludeUnavailableCondition ,
72+ sourcev1 .SourceVerifiedCondition ,
73+ sourcev1 .FetchFailedCondition ,
74+ sourcev1 .ArtifactOutdatedCondition ,
75+ meta .StalledCondition ,
76+ meta .ReconcilingCondition ,
77+ },
78+ NegativePolarity : []string {
79+ sourcev1 .FetchFailedCondition ,
80+ sourcev1 .IncludeUnavailableCondition ,
81+ sourcev1 .ArtifactOutdatedCondition ,
82+ meta .StalledCondition ,
83+ meta .ReconcilingCondition ,
84+ },
8785}
8886
8987// +kubebuilder:rbac:groups=source.toolkit.fluxcd.io,resources=gitrepositories,verbs=get;list;watch;create;update;patch;delete
@@ -157,7 +155,19 @@ func (r *GitRepositoryReconciler) Reconcile(ctx context.Context, req ctrl.Reques
157155 // Always attempt to patch the object and status after each reconciliation
158156 // NOTE: The final runtime result and error are set in this block.
159157 defer func () {
160- result , retErr = r .summarizeAndPatch (ctx , obj , patchHelper , recResult , retErr )
158+ summarizeHelper := summarize .NewHelper (r .EventRecorder , patchHelper )
159+ summarizeOpts := []summarize.Option {
160+ summarize .WithConditions (gitRepoReadyConditions ),
161+ summarize .WithReconcileResult (recResult ),
162+ summarize .WithReconcileError (retErr ),
163+ summarize .WithIgnoreNotFound (),
164+ summarize .WithProcessors (
165+ summarize .RecordContextualError ,
166+ summarize .RecordReconcileReq ,
167+ ),
168+ summarize .WithResultBuilder (sreconcile.AlwaysRequeueResultBuilder {RequeueAfter : obj .GetInterval ().Duration }),
169+ }
170+ result , retErr = summarizeHelper .SummarizeAndPatch (ctx , obj , summarizeOpts ... )
161171
162172 // Always record readiness and duration metrics
163173 r .Metrics .RecordReadiness (ctx , obj )
@@ -189,50 +199,6 @@ func (r *GitRepositoryReconciler) Reconcile(ctx context.Context, req ctrl.Reques
189199 return
190200}
191201
192- // summarizeAndPatch analyzes the object conditions to create a summary of the
193- // status conditions, computes runtime results and patches the object in the K8s
194- // API server.
195- func (r * GitRepositoryReconciler ) summarizeAndPatch (
196- ctx context.Context ,
197- obj * sourcev1.GitRepository ,
198- patchHelper * patch.Helper ,
199- res sreconcile.Result ,
200- recErr error ) (ctrl.Result , error ) {
201- sreconcile .RecordContextualError (ctx , r .EventRecorder , obj , recErr )
202-
203- // Record the value of the reconciliation request if any.
204- if v , ok := meta .ReconcileAnnotationValue (obj .GetAnnotations ()); ok {
205- obj .Status .SetLastHandledReconcileRequest (v )
206- }
207-
208- // Compute the reconcile results, obtain patch options and reconcile error.
209- var patchOpts []patch.Option
210- var result ctrl.Result
211- patchOpts , result , recErr = sreconcile .ComputeReconcileResult (obj , obj .GetRequeueAfter (), res , recErr , gitRepoOwnedConditions )
212-
213- // Summarize the Ready condition based on abnormalities that may have been observed.
214- conditions .SetSummary (obj ,
215- meta .ReadyCondition ,
216- conditions .WithConditions (
217- gitRepoReadyDeps ... ,
218- ),
219- conditions .WithNegativePolarityConditions (
220- gitRepoReadyDepsNegative ... ,
221- ),
222- )
223-
224- // Finally, patch the resource.
225- if err := patchHelper .Patch (ctx , obj , patchOpts ... ); err != nil {
226- // Ignore patch error "not found" when the object is being deleted.
227- if ! obj .ObjectMeta .DeletionTimestamp .IsZero () {
228- err = kerrors .FilterOut (err , func (e error ) bool { return apierrors .IsNotFound (e ) })
229- }
230- recErr = kerrors .NewAggregate ([]error {recErr , err })
231- }
232-
233- return result , recErr
234- }
235-
236202// reconcile steps iterates through the actual reconciliation tasks for objec,
237203// it returns early on the first step that returns ResultRequeue or produces an
238204// error.
0 commit comments