@@ -24,16 +24,14 @@ import (
2424
2525 "github.com/sirupsen/logrus"
2626 "k8s.io/apimachinery/pkg/api/errors"
27- "k8s.io/apimachinery/pkg/types"
28- "k8s.io/apimachinery/pkg/util/wait"
29- "k8s.io/client-go/util/retry"
3027 "sigs.k8s.io/controller-runtime/pkg/builder"
3128 ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
3229 "sigs.k8s.io/controller-runtime/pkg/controller"
3330 "sigs.k8s.io/controller-runtime/pkg/manager"
3431 "sigs.k8s.io/controller-runtime/pkg/reconcile"
3532
3633 prowv1 "k8s.io/test-infra/prow/apis/prowjobs/v1"
34+ "k8s.io/test-infra/prow/crier/reporters/criercommonlib"
3735)
3836
3937type ReportClient interface {
@@ -80,66 +78,6 @@ func New(
8078 return nil
8179}
8280
83- func (r * reconciler ) updateReportState (ctx context.Context , pj * prowv1.ProwJob , log * logrus.Entry , reportedState prowv1.ProwJobState ) error {
84- // update pj report status
85- newpj := pj .DeepCopy ()
86- // we set omitempty on PrevReportStates, so here we need to init it if is nil
87- if newpj .Status .PrevReportStates == nil {
88- newpj .Status .PrevReportStates = map [string ]prowv1.ProwJobState {}
89- }
90- newpj .Status .PrevReportStates [r .reporter .GetName ()] = reportedState
91-
92- if err := r .pjclientset .Patch (ctx , newpj , ctrlruntimeclient .MergeFrom (pj )); err != nil {
93- return fmt .Errorf ("failed to patch: %w" , err )
94- }
95-
96- // Block until the update is in the lister to make sure that events from another controller
97- // that also does reporting dont trigger another report because our lister doesn't yet contain
98- // the updated Status
99- name := types.NamespacedName {Namespace : pj .Namespace , Name : pj .Name }
100- if err := wait .Poll (100 * time .Millisecond , 10 * time .Second , func () (bool , error ) {
101- if err := r .pjclientset .Get (ctx , name , pj ); err != nil {
102- return false , err
103- }
104- if pj .Status .PrevReportStates != nil &&
105- pj .Status .PrevReportStates [r .reporter .GetName ()] == reportedState {
106- return true , nil
107- }
108- return false , nil
109- }); err != nil {
110- return fmt .Errorf ("failed to wait for updated report status to be in lister: %w" , err )
111- }
112- return nil
113- }
114-
115- func (r * reconciler ) updateReportStateWithRetries (ctx context.Context , pj * prowv1.ProwJob , log * logrus.Entry ) error {
116- reportState := pj .Status .State
117- log = log .WithFields (logrus.Fields {
118- "prowjob" : pj .Name ,
119- "jobName" : pj .Spec .Job ,
120- "jobStatus" : reportState ,
121- })
122- // We have to retry here, if we return we lose the information that we already reported this job.
123- if err := retry .RetryOnConflict (retry .DefaultBackoff , func () error {
124- // Get it first, this is very cheap
125- name := types.NamespacedName {Namespace : pj .Namespace , Name : pj .Name }
126- if err := r .pjclientset .Get (ctx , name , pj ); err != nil {
127- return err
128- }
129- // Must not wrap until we have kube 1.19, otherwise the RetryOnConflict won't recognize conflicts
130- // correctly
131- return r .updateReportState (ctx , pj , log , reportState )
132- }); err != nil {
133- // Very subpar, we will report again. But even if we didn't do that now, we would do so
134- // latest when crier gets restarted. In an ideal world, all reporters are idempotent and
135- // reporting has no cost.
136- return fmt .Errorf ("failed to update report state on prowjob: %w" , err )
137- }
138-
139- log .Info ("Successfully updated report state on prowjob" )
140- return nil
141- }
142-
14381// Reconcile retrieves each queued item and takes the necessary handler action based off of if
14482// the item was created or deleted.
14583func (r * reconciler ) Reconcile (ctx context.Context , req reconcile.Request ) (reconcile.Result , error ) {
@@ -207,7 +145,7 @@ func (r *reconciler) reconcile(ctx context.Context, log *logrus.Entry, req recon
207145 log .WithField ("job-count" , len (pjs )).Info ("Reported job(s), now will update pj(s)." )
208146 var lastErr error
209147 for _ , pjob := range pjs {
210- if err := r . updateReportStateWithRetries (ctx , pjob , log ); err != nil {
148+ if err := criercommonlib . UpdateReportStateWithRetries (ctx , pjob , log , r . pjclientset , r . reporter . GetName () ); err != nil {
211149 log .WithError (err ).Error ("Failed to update report state on prowjob" )
212150 // The error above is alreay logged, so it would be duplicated
213151 // effort to combine all errors to return, only capture the last
0 commit comments