Skip to content

Commit 3c2ab03

Browse files
savitaashturechmouel
authored andcommitted
Fix configmap watcher update issue
1 parent babf59c commit 3c2ab03

File tree

5 files changed

+18
-19
lines changed

5 files changed

+18
-19
lines changed

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,5 @@ func main() {
2828
log.Fatal("failed to init kinit client : ", err)
2929
}
3030

31-
c := make(chan struct{})
32-
go func() {
33-
log.Println("started goroutine to watch configmap changes for controller")
34-
c <- struct{}{}
35-
if err := run.WatchConfigMapChanges(ctx); err != nil {
36-
log.Fatal("error from WatchConfigMapChanges for controller : ", err)
37-
}
38-
}()
39-
// Force WatchConfigMapChanges go routines to actually start
40-
<-c
41-
4231
evadapter.MainWithContext(ctx, PACControllerLogKey, adapter.NewEnvConfig, adapter.New(run, kinteract))
4332
}

pkg/adapter/adapter.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"fmt"
77
"io"
8+
"log"
89
"net/http"
910
"os"
1011
"time"
@@ -59,7 +60,7 @@ func New(run *params.Run, k *kubeinteraction.Interaction) adapter.AdapterConstru
5960
}
6061
}
6162

62-
func (l *listener) Start(_ context.Context) error {
63+
func (l *listener) Start(ctx context.Context) error {
6364
adapterPort := globalAdapterPort
6465
envAdapterPort := os.Getenv("PAC_CONTROLLER_PORT")
6566
if envAdapterPort != "" {
@@ -75,7 +76,7 @@ func (l *listener) Start(_ context.Context) error {
7576
_, _ = fmt.Fprint(w, "ok")
7677
})
7778

78-
mux.HandleFunc("/", l.handleEvent())
79+
mux.HandleFunc("/", l.handleEvent(ctx))
7980

8081
//nolint: gosec
8182
srv := &http.Server{
@@ -97,9 +98,17 @@ func (l *listener) Start(_ context.Context) error {
9798
return nil
9899
}
99100

100-
func (l listener) handleEvent() http.HandlerFunc {
101+
func (l listener) handleEvent(ctx context.Context) http.HandlerFunc {
101102
return func(response http.ResponseWriter, request *http.Request) {
102-
ctx := context.Background()
103+
c := make(chan struct{})
104+
go func() {
105+
c <- struct{}{}
106+
if err := l.run.WatchConfigMapChanges(ctx); err != nil {
107+
log.Fatal("error from WatchConfigMapChanges for controller : ", err)
108+
}
109+
}()
110+
// Force WatchConfigMapChanges go routines to actually start
111+
<-c
103112

104113
if request.Method != http.MethodPost {
105114
l.writeResponse(response, http.StatusOK, "ok")

pkg/adapter/adapter_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@ func TestHandleEvent(t *testing.T) {
2424
cs, _ := testclient.SeedTestData(t, ctx, testclient.Data{})
2525
logger, _ := logger.GetLogger()
2626

27+
t.Setenv("SYSTEM_NAMESPACE", "test")
2728
l := listener{
2829
run: &params.Run{
2930
Clients: clients.Clients{
3031
PipelineAsCode: cs.PipelineAsCode,
32+
Log: logger,
33+
Kube: cs.Kube,
3134
},
3235
Info: info.Info{
3336
Pac: &info.PacOpts{
@@ -40,7 +43,7 @@ func TestHandleEvent(t *testing.T) {
4043
logger: logger,
4144
}
4245

43-
ts := httptest.NewServer(l.handleEvent())
46+
ts := httptest.NewServer(l.handleEvent(ctx))
4447
defer ts.Close()
4548

4649
// valid push event

pkg/params/run.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ func StringToBool(s string) bool {
3939

4040
// WatchConfigMapChanges watches for provide configmap
4141
func (r *Run) WatchConfigMapChanges(ctx context.Context) error {
42-
r.Clients.Log.Info("Inside WatchConfigMapChanges function")
4342
ns := os.Getenv("SYSTEM_NAMESPACE")
4443
if ns == "" {
4544
return fmt.Errorf("failed to find pipelines-as-code installation namespace")

pkg/reconciler/controller.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,9 @@ func NewController() func(context.Context, configmap.Watcher) *controller.Impl {
3636

3737
c := make(chan struct{})
3838
go func() {
39-
log.Println("started goroutine to watch configmap changes inside controller reconciler")
4039
c <- struct{}{}
4140
if err := run.WatchConfigMapChanges(ctx); err != nil {
42-
log.Fatal("error from WatchConfigMapChanges from controller reconciler : ", err)
41+
log.Fatal("error from WatchConfigMapChanges from watcher reconciler : ", err)
4342
}
4443
}()
4544
<-c

0 commit comments

Comments
 (0)