Skip to content

Commit 25bfed7

Browse files
committed
fix: Set state only after status report
Set the state annotation and labels in pipelineascode only after the state was reported to the SCM. This is achieved by patching the PLR with the state label/annotation after it was created. This change is needed in order to avoid a case where the watcher will report a status before PAC. With this change the issue can't happen, since the watcher only reconciles PLRs that has the state annotation, so the first reconcile of the PLRs will happen only after PAC already sent the initial status to the SCM. In addition, simplify the condition which checks if the "queued" state annotation/label should be set. We can only check if the PLR is pending, it doesn't matter if the reason is because of PAC's concurrency control or an external controller. Signed-off-by: Gal Ben Haim <[email protected]>
1 parent efd3ed3 commit 25bfed7

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

pkg/kubeinteraction/labels.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ func AddLabelsAndAnnotations(event *info.Event, pipelineRun *tektonv1.PipelineRu
5555
keys.SourceBranch: event.HeadBranch,
5656
keys.Repository: repo.GetName(),
5757
keys.GitProvider: providerConfig.Name,
58-
keys.State: StateStarted,
5958
keys.ControllerInfo: fmt.Sprintf(`{"name":"%s","configmap":"%s","secret":"%s", "gRepo": "%s"}`,
6059
paramsinfo.Controller.Name, paramsinfo.Controller.Configmap, paramsinfo.Controller.Secret, paramsinfo.Controller.GlobalRepository),
6160
}

pkg/pipelineascode/pipelineascode.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -176,14 +176,10 @@ func (p *PacRun) startPR(ctx context.Context, match matcher.Match) (*tektonv1.Pi
176176
p.logger.Errorf("Error adding labels/annotations to PipelineRun '%s' in namespace '%s': %v", match.PipelineRun.GetName(), match.Repo.GetNamespace(), err)
177177
}
178178

179-
// if concurrency is defined then start the pipelineRun in pending state and
180-
// state as queued
179+
// if concurrency is defined then start the pipelineRun in pending state
181180
if match.Repo.Spec.ConcurrencyLimit != nil && *match.Repo.Spec.ConcurrencyLimit != 0 {
182181
// pending status
183182
match.PipelineRun.Spec.Status = tektonv1.PipelineRunSpecStatusPending
184-
// pac state as queued
185-
match.PipelineRun.Labels[keys.State] = kubeinteraction.StateQueued
186-
match.PipelineRun.Annotations[keys.State] = kubeinteraction.StateQueued
187183
}
188184

189185
// Create the actual pipelineRun
@@ -240,26 +236,31 @@ func (p *PacRun) startPR(ctx context.Context, match matcher.Match) (*tektonv1.Pi
240236
OriginalPipelineRunName: pr.GetAnnotations()[keys.OriginalPRName],
241237
}
242238

239+
// Patch the pipelineRun with the appropriate annotations and labels.
240+
// Set the state so the watcher will continue with reconciling the pipelineRun
241+
// The watcher reconciles only pipelineRuns that has the state annotation.
243242
patchAnnotations := map[string]string{}
244243
patchLabels := map[string]string{}
245244
whatPatching := ""
246245
// if pipelineRun is in pending state then report status as queued
246+
// The pipelineRun can be pending because of PAC's concurrency limit or because of an external mutatingwebhook
247247
if pr.Spec.Status == tektonv1.PipelineRunSpecStatusPending {
248248
status.Status = queuedStatus
249249
if status.Text, err = mt.MakeTemplate(p.vcx.GetTemplate(provider.QueueingPipelineType)); err != nil {
250250
return nil, fmt.Errorf("cannot create message template: %w", err)
251251
}
252-
// If the PipelineRun is in the "queued" state, add the appropriate label and annotation.
253-
// These are later used by the watcher to determine whether the PipelineRun status
254-
// should be reported back to the Git provider. We do add the `state` annotations and label when
255-
// concurrency is enabled but this would happen when PipelineRun's status has been changed by
256-
// the other controller and PaC is not aware of that change.
257252
whatPatching = "annotations.state and labels.state"
258253
patchAnnotations[keys.State] = kubeinteraction.StateQueued
259254
patchLabels[keys.State] = kubeinteraction.StateQueued
260255
} else {
256+
// Mark that the start will be reported to the Git provider
261257
patchAnnotations[keys.SCMReportingPLRStarted] = "true"
262-
whatPatching = fmt.Sprintf("annotation.%s", keys.SCMReportingPLRStarted)
258+
patchAnnotations[keys.State] = kubeinteraction.StateStarted
259+
patchLabels[keys.State] = kubeinteraction.StateStarted
260+
whatPatching = fmt.Sprintf(
261+
"annotation.%s and annotations.state and labels.state",
262+
keys.SCMReportingPLRStarted,
263+
)
263264
}
264265

265266
if err := p.vcx.CreateStatus(ctx, p.event, status); err != nil {

pkg/pipelineascode/pipelineascode_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ func TestRun(t *testing.T) {
718718
secretName, ok := pr.GetAnnotations()[keys.GitAuthSecret]
719719
assert.Assert(t, ok, "Cannot find secret %s on annotations", secretName)
720720
}
721-
if tt.concurrencyLimit > 0 || pr.Spec.Status == pipelinev1.PipelineRunSpecStatusPending {
721+
if pr.Spec.Status == pipelinev1.PipelineRunSpecStatusPending {
722722
state, ok := pr.GetAnnotations()[keys.State]
723723
assert.Assert(t, ok, "State hasn't been set on PR", state)
724724
assert.Equal(t, state, kubeinteraction.StateQueued)

0 commit comments

Comments
 (0)