Skip to content

Commit e0fcbb5

Browse files
committed
fix: restart watcher when concurrency backend changes
1 parent d7f278d commit e0fcbb5

File tree

2 files changed

+116
-1
lines changed

2 files changed

+116
-1
lines changed

pkg/params/config_sync.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ import (
1313
"knative.dev/pkg/system"
1414
)
1515

16+
var terminateProcessForConfigChange = func() {
17+
_ = syscall.Kill(os.Getpid(), syscall.SIGTERM)
18+
}
19+
1620
func StartConfigSync(ctx context.Context, run *Run) {
1721
// init pac config
1822
_ = run.UpdatePacConfig(ctx)
@@ -28,7 +32,19 @@ func StartConfigSync(ctx context.Context, run *Run) {
2832
// nothing to do
2933
},
3034
UpdateFunc: func(_, _ any) {
31-
_ = run.UpdatePacConfig(ctx)
35+
oldBackend, newBackend, changed, err := updatePacConfigAndDetectBackendChange(ctx, run)
36+
if err != nil {
37+
return
38+
}
39+
if changed {
40+
if run.Clients.Log != nil {
41+
run.Clients.Log.Infof(
42+
"concurrency-backend changed from %q to %q; restarting process so the queue backend is recreated",
43+
oldBackend, newBackend,
44+
)
45+
}
46+
terminateProcessForConfigChange()
47+
}
3248
},
3349
DeleteFunc: func(_ any) {
3450
// nothing to do
@@ -46,3 +62,13 @@ func StartConfigSync(ctx context.Context, run *Run) {
4662
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
4763
<-sig
4864
}
65+
66+
func updatePacConfigAndDetectBackendChange(ctx context.Context, run *Run) (string, string, bool, error) {
67+
oldBackend := run.Info.GetPacOpts().ConcurrencyBackend
68+
if err := run.UpdatePacConfig(ctx); err != nil {
69+
return oldBackend, oldBackend, false, err
70+
}
71+
72+
newBackend := run.Info.GetPacOpts().ConcurrencyBackend
73+
return oldBackend, newBackend, oldBackend != "" && newBackend != "" && oldBackend != newBackend, nil
74+
}

pkg/params/config_sync_test.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package params
2+
3+
import (
4+
"testing"
5+
6+
"github.com/openshift-pipelines/pipelines-as-code/pkg/consoleui"
7+
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/clients"
8+
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/info"
9+
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/settings"
10+
corev1 "k8s.io/api/core/v1"
11+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12+
kubefake "k8s.io/client-go/kubernetes/fake"
13+
rtesting "knative.dev/pkg/reconciler/testing"
14+
15+
"go.uber.org/zap"
16+
"gotest.tools/v3/assert"
17+
)
18+
19+
func TestUpdatePacConfigAndDetectBackendChange(t *testing.T) {
20+
tests := []struct {
21+
name string
22+
initialBackend string
23+
configData map[string]string
24+
wantOld string
25+
wantNew string
26+
wantChanged bool
27+
}{
28+
{
29+
name: "detects backend change",
30+
initialBackend: settings.ConcurrencyBackendMemory,
31+
configData: map[string]string{
32+
"concurrency-backend": settings.ConcurrencyBackendLease,
33+
"tekton-dashboard-url": "https://dashboard.example.test",
34+
},
35+
wantOld: settings.ConcurrencyBackendMemory,
36+
wantNew: settings.ConcurrencyBackendLease,
37+
wantChanged: true,
38+
},
39+
{
40+
name: "ignores unchanged backend",
41+
initialBackend: settings.ConcurrencyBackendLease,
42+
configData: map[string]string{
43+
"concurrency-backend": settings.ConcurrencyBackendLease,
44+
"tekton-dashboard-url": "https://dashboard.example.test",
45+
},
46+
wantOld: settings.ConcurrencyBackendLease,
47+
wantNew: settings.ConcurrencyBackendLease,
48+
wantChanged: false,
49+
},
50+
}
51+
52+
for _, tt := range tests {
53+
t.Run(tt.name, func(t *testing.T) {
54+
ctx, _ := rtesting.SetupFakeContext(t)
55+
ctx = info.StoreNS(ctx, "pac")
56+
57+
run := &Run{
58+
Clients: clients.Clients{
59+
Kube: kubefake.NewSimpleClientset(&corev1.ConfigMap{
60+
ObjectMeta: metav1.ObjectMeta{
61+
Name: "pac-config",
62+
Namespace: "pac",
63+
},
64+
Data: tt.configData,
65+
}),
66+
Log: zap.NewNop().Sugar(),
67+
},
68+
Info: info.Info{
69+
Pac: &info.PacOpts{
70+
Settings: settings.Settings{
71+
ConcurrencyBackend: tt.initialBackend,
72+
},
73+
},
74+
Controller: &info.ControllerInfo{
75+
Configmap: "pac-config",
76+
},
77+
},
78+
}
79+
run.Clients.InitClients()
80+
run.Clients.SetConsoleUI(consoleui.FallBackConsole{})
81+
82+
oldBackend, newBackend, changed, err := updatePacConfigAndDetectBackendChange(ctx, run)
83+
assert.NilError(t, err)
84+
assert.Equal(t, oldBackend, tt.wantOld)
85+
assert.Equal(t, newBackend, tt.wantNew)
86+
assert.Equal(t, changed, tt.wantChanged)
87+
})
88+
}
89+
}

0 commit comments

Comments
 (0)