Skip to content

Commit e8f1752

Browse files
committed
fix race conditions on pr concurrency
when storing a pointer in the context, the pointer get nil sometime on high load which causes some of the pipelinerun issue we fix this by passing the value around instead of passing the pointer in context. There is more going on in this bug that meet the eyes but that seems to fix it. remove all the StoreInfo and GetInfo functions and use the value directly. Signed-off-by: Chmouel Boudjnah <[email protected]>
1 parent 24313af commit e8f1752

File tree

27 files changed

+72
-194
lines changed

27 files changed

+72
-194
lines changed

cmd/pipelines-as-code-controller/main.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ func main() {
4242
ctx = evadapter.WithConfiguratorOptions(ctx, []evadapter.ConfiguratorOption{copt})
4343

4444
ctx = info.StoreNS(ctx, ns)
45-
ctx = info.StoreInfo(ctx, rinfo.Controller.Name, rinfo)
4645
ctx = info.StoreCurrentControllerName(ctx, rinfo.Controller.Name)
4746

4847
ctx = context.WithValue(ctx, client.Key{}, run.Clients.Kube)

pkg/adapter/adapter.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,12 @@ func (l *listener) Start(ctx context.Context) error {
101101
func (l listener) handleEvent(ctx context.Context) http.HandlerFunc {
102102
return func(response http.ResponseWriter, request *http.Request) {
103103
// we should fix this, this basically reads configmap on every request, is that supposed to be okay?
104-
currentControllerName := info.GetCurrentControllerName(ctx)
105104
if err := l.run.UpdatePACInfo(ctx); err != nil {
106105
log.Fatalf("error getting config and setting from configmaps: %v", err)
107106
}
108107

109108
ninfo := &info.Info{}
110109
l.run.Info.DeepCopy(ninfo)
111-
ctx = info.StoreInfo(ctx, currentControllerName, ninfo)
112110

113111
if request.Method != http.MethodPost {
114112
l.writeResponse(response, http.StatusOK, "ok")
@@ -223,6 +221,7 @@ func (l listener) detectProvider(req *http.Request, reqBody string) (provider.In
223221
}
224222

225223
gitHub := github.New()
224+
gitHub.Run = l.run
226225
isGH, processReq, logger, reason, err := gitHub.Detect(req, reqBody, &log)
227226
if isGH {
228227
return l.processRes(processReq, gitHub, logger, reason, err)

pkg/adapter/adapter_test.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,6 @@ func TestHandleEvent(t *testing.T) {
4141
logger, _ := logger.GetLogger()
4242

4343
ctx = info.StoreCurrentControllerName(ctx, "default")
44-
ctx = info.StoreInfo(ctx, "default", &info.Info{
45-
Controller: &info.ControllerInfo{
46-
Secret: info.DefaultPipelinesAscodeSecretName,
47-
Configmap: info.DefaultPipelinesAscodeConfigmapName,
48-
},
49-
})
5044
ctx = info.StoreNS(ctx, "default")
5145

5246
emptys := &unstructured.Unstructured{}

pkg/adapter/incoming.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ func (l *listener) detectIncoming(ctx context.Context, req *http.Request, payloa
104104

105105
if repo.Spec.GitProvider == nil || repo.Spec.GitProvider.Type == "" {
106106
gh := github.New()
107+
gh.Run = l.run
107108
ns := info.GetNS(ctx)
108109
enterpriseURL, token, installationID, err := app.GetAndUpdateInstallationID(ctx, req, l.run, repo, gh, ns)
109110
if err != nil {

pkg/adapter/incoming_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -655,19 +655,18 @@ func Test_listener_detectIncoming(t *testing.T) {
655655
t.Run(tt.name, func(t *testing.T) {
656656
ctx, _ := rtesting.SetupFakeContext(t)
657657
ctx = info.StoreCurrentControllerName(ctx, "default")
658-
ctx = info.StoreInfo(ctx, "default", &info.Info{
659-
Controller: &info.ControllerInfo{
660-
Secret: info.DefaultPipelinesAscodeSecretName,
661-
},
662-
})
663658
ctx = info.StoreNS(ctx, testNamespace.GetName())
664659
cs, _ := testclient.SeedTestData(t, ctx, tt.args.data)
665660
client := &params.Run{
666661
Clients: clients.Clients{
667662
PipelineAsCode: cs.PipelineAsCode,
668663
Kube: cs.Kube,
669664
},
670-
Info: info.Info{},
665+
Info: info.Info{
666+
Controller: &info.ControllerInfo{
667+
Secret: info.DefaultPipelinesAscodeSecretName,
668+
},
669+
},
671670
}
672671
observer, _ := zapobserver.New(zap.InfoLevel)
673672
logger := zap.New(observer).Sugar()
@@ -681,6 +680,7 @@ func Test_listener_detectIncoming(t *testing.T) {
681680
kint: kint,
682681
event: info.NewEvent(),
683682
}
683+
684684
// make a new request
685685
req := httptest.NewRequest(tt.args.method,
686686
fmt.Sprintf("http://localhost%s?repository=%s&secret=%s&pipelinerun=%s&branch=%s", tt.args.queryURL,

pkg/cmd/tknpac/info/install_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,18 +160,18 @@ func TestInfo(t *testing.T) {
160160
if tt.secret != nil {
161161
name = tt.secret.GetName()
162162
}
163-
ctx = info.StoreInfo(ctx, "default", &info.Info{
164-
Controller: &info.ControllerInfo{
165-
Secret: name,
166-
},
167-
})
168163
stdata, _ := testclient.SeedTestData(t, ctx, tdata)
169164
cs := &params.Run{
170165
Clients: clients.Clients{
171166
PipelineAsCode: stdata.PipelineAsCode,
172167
Kube: stdata.Kube,
173168
HTTP: *httpTestClient,
174169
},
170+
Info: info.Info{
171+
Controller: &info.ControllerInfo{
172+
Secret: name,
173+
},
174+
},
175175
}
176176

177177
io, out := tcli.NewIOStream()

pkg/kubeinteraction/labels.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package kubeinteraction
22

33
import (
4-
"context"
54
"fmt"
65
"strconv"
76

87
"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode"
98
"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/keys"
109
apipac "github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/v1alpha1"
1110
"github.com/openshift-pipelines/pipelines-as-code/pkg/formatting"
11+
"github.com/openshift-pipelines/pipelines-as-code/pkg/params"
1212
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/info"
1313
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/version"
1414
tektonv1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
@@ -21,13 +21,13 @@ const (
2121
StateFailed = "failed"
2222
)
2323

24-
func AddLabelsAndAnnotations(ctx context.Context, event *info.Event, pipelineRun *tektonv1.PipelineRun, repo *apipac.Repository, providerinfo *info.ProviderConfig) error {
24+
func AddLabelsAndAnnotations(event *info.Event, pipelineRun *tektonv1.PipelineRun, repo *apipac.Repository, providerConfig *info.ProviderConfig, paramsRun *params.Run) error {
2525
if event == nil {
2626
return fmt.Errorf("event should not be nil")
2727
}
28-
// Add labels on the soon to be created pipelinerun so UI/CLI can easily
28+
paramsinfo := paramsRun.Info
29+
// Add labels on the soon-to-be created pipelinerun so UI/CLI can easily
2930
// query them.
30-
paramsinfo := info.GetInfo(ctx, info.GetCurrentControllerName(ctx))
3131
labels := map[string]string{
3232
// These keys are used in LabelSelector query so we are keeping in Labels as it is.
3333
// But adding same keys to Annotations so UI/CLI can fetch the actual value instead of modified value
@@ -44,7 +44,7 @@ func AddLabelsAndAnnotations(ctx context.Context, event *info.Event, pipelineRun
4444
// In PAC v0.20.x releases we will remove these keys from Labels
4545
keys.Sender: formatting.CleanValueKubernetes(event.Sender),
4646
keys.Branch: formatting.CleanValueKubernetes(event.BaseBranch),
47-
keys.GitProvider: providerinfo.Name,
47+
keys.GitProvider: providerConfig.Name,
4848
}
4949

5050
annotations := map[string]string{
@@ -58,7 +58,7 @@ func AddLabelsAndAnnotations(ctx context.Context, event *info.Event, pipelineRun
5858
keys.EventType: event.EventType,
5959
keys.Branch: event.BaseBranch,
6060
keys.Repository: repo.GetName(),
61-
keys.GitProvider: providerinfo.Name,
61+
keys.GitProvider: providerConfig.Name,
6262
keys.State: StateStarted,
6363
keys.ControllerInfo: fmt.Sprintf(`{"name":"%s","configmap":"%s","secret":"%s"}`, paramsinfo.Controller.Name, paramsinfo.Controller.Configmap, paramsinfo.Controller.Secret),
6464
}
@@ -69,7 +69,7 @@ func AddLabelsAndAnnotations(ctx context.Context, event *info.Event, pipelineRun
6969
}
7070

7171
// TODO: move to provider specific function
72-
if providerinfo.Name == "github" || providerinfo.Name == "github-enterprise" {
72+
if providerConfig.Name == "github" || providerConfig.Name == "github-enterprise" {
7373
if event.InstallationID != -1 {
7474
annotations[keys.InstallationID] = strconv.FormatInt(event.InstallationID, 10)
7575
}

pkg/kubeinteraction/labels_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import (
66

77
"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/keys"
88
apipac "github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/v1alpha1"
9+
"github.com/openshift-pipelines/pipelines-as-code/pkg/params"
910
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/info"
1011
tektonv1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
1112
"gotest.tools/v3/assert"
1213
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
13-
rtesting "knative.dev/pkg/reconciler/testing"
1414
)
1515

1616
func TestAddLabelsAndAnnotations(t *testing.T) {
@@ -58,12 +58,12 @@ func TestAddLabelsAndAnnotations(t *testing.T) {
5858
}
5959
for _, tt := range tests {
6060
t.Run(tt.name, func(t *testing.T) {
61-
ctx, _ := rtesting.SetupFakeContext(t)
62-
ctx = info.StoreCurrentControllerName(ctx, "default")
63-
ctx = info.StoreInfo(ctx, "default", &info.Info{
64-
Controller: tt.args.controllerInfo,
65-
})
66-
err := AddLabelsAndAnnotations(ctx, tt.args.event, tt.args.pipelineRun, tt.args.repo, &info.ProviderConfig{})
61+
paramsRun := &params.Run{
62+
Info: info.Info{
63+
Controller: tt.args.controllerInfo,
64+
},
65+
}
66+
err := AddLabelsAndAnnotations(tt.args.event, tt.args.pipelineRun, tt.args.repo, &info.ProviderConfig{}, paramsRun)
6767
assert.NilError(t, err)
6868
assert.Equal(t, tt.args.pipelineRun.Labels[keys.URLOrg], tt.args.event.Organization, "'%s' != %s",
6969
tt.args.pipelineRun.Labels[keys.URLOrg], tt.args.event.Organization)

pkg/params/info/runinfo.go

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
package info
22

3-
import (
4-
"context"
5-
)
6-
73
type Info struct {
84
Pac *PacOpts
95
Kube *KubeOpts
@@ -17,53 +13,3 @@ func (i *Info) DeepCopy(out *Info) {
1713
type (
1814
contextKey string
1915
)
20-
21-
type CtxInfo struct {
22-
Pac *PacOpts
23-
Kube *KubeOpts
24-
Controller *ControllerInfo
25-
}
26-
27-
// GetInfo Pac Settings for that label.
28-
func GetInfo(ctx context.Context, label string) *Info {
29-
labelContextKey := contextKey(label)
30-
if val := ctx.Value(labelContextKey); val != nil {
31-
if ctxInfo, ok := val.(CtxInfo); ok {
32-
return &Info{
33-
Pac: ctxInfo.Pac,
34-
Kube: ctxInfo.Kube,
35-
Controller: ctxInfo.Controller,
36-
}
37-
}
38-
}
39-
return nil
40-
}
41-
42-
// StoreInfo Pac Settings for a label.
43-
func StoreInfo(ctx context.Context, label string, info *Info) context.Context {
44-
labelContextKey := contextKey(label)
45-
if val := ctx.Value(labelContextKey); val != nil {
46-
if ctxInfo, ok := val.(CtxInfo); ok {
47-
if ctxInfo.Pac == nil {
48-
ctxInfo.Pac = &PacOpts{}
49-
}
50-
if ctxInfo.Kube == nil {
51-
ctxInfo.Kube = &KubeOpts{
52-
Namespace: GetNS(ctx),
53-
}
54-
}
55-
if ctxInfo.Controller == nil {
56-
ctxInfo.Controller = &ControllerInfo{}
57-
}
58-
ctxInfo.Pac = info.Pac
59-
ctxInfo.Kube = info.Kube
60-
ctxInfo.Controller = info.Controller
61-
return context.WithValue(ctx, labelContextKey, ctxInfo)
62-
}
63-
}
64-
return context.WithValue(ctx, labelContextKey, CtxInfo{
65-
Pac: info.Pac,
66-
Kube: info.Kube,
67-
Controller: info.Controller,
68-
})
69-
}

pkg/params/info/runinfo_test.go

Lines changed: 0 additions & 53 deletions
This file was deleted.

0 commit comments

Comments
 (0)