Skip to content

Commit b9dbfe2

Browse files
oktalzmjuraga
authored andcommitted
BUG/MEDIUM: properly detect starting of dataplane
notify event for when dataplaneapi is starting was not properly checking all possible options. When using it only with https, dataplaneapi incorrectly read port where it was listening. With this commit, patch to server.go was created so we receive notification in right time regardless of setup
1 parent cc9c586 commit b9dbfe2

File tree

6 files changed

+25
-35
lines changed

6 files changed

+25
-35
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ generate:
3535
--build-arg GID=$(shell id -g) \
3636
-t dataplaneapi-swagger-gen .
3737
docker run --rm -it -v "$(PWD)":/data dataplaneapi-swagger-gen
38+
generate/post_swagger.sh
3839

3940
.PHONY: generate-native
4041
generate-native:
4142
generate/swagger/script.sh
43+
generate/post_swagger.sh

cmd/dataplaneapi/main.go

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,10 @@
1616
package main
1717

1818
import (
19-
"crypto/tls"
2019
"fmt"
21-
"net/http"
2220
"os"
2321
"path"
2422
"syscall"
25-
"time"
2623

2724
loads "github.com/go-openapi/loads"
2825
"github.com/go-openapi/runtime"
@@ -228,28 +225,7 @@ func startServer(cfg *configuration.Configuration) (reload configuration.AtomicB
228225
}()
229226

230227
server.ConfigureAPI()
231-
schema := "http"
232-
if len(server.EnabledListeners) > 0 {
233-
schema = server.EnabledListeners[0]
234-
}
235-
path := fmt.Sprintf("%s://%s:%d%s", schema, server.Host, server.Port, cfg.RuntimeData.APIBasePath)
236-
go func(path string) {
237-
tr := &http.Transport{
238-
// we just need to check if we have a response
239-
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, //nolint:gosec
240-
}
241-
client := &http.Client{Transport: tr}
242-
numTrys := 0
243-
for ; numTrys < 10; numTrys++ {
244-
_, err := client.Get(path)
245-
if err == nil {
246-
cfg.Notify.ServerStarted.Notify()
247-
return
248-
}
249-
time.Sleep(500 * time.Millisecond)
250-
}
251-
log.Fatalf("check if dataplane is running failed on %s", path)
252-
}(path)
228+
dataplaneapi.SetServerStartedCallback(cfg.Notify.ServerStarted.Notify)
253229
if err := server.Serve(); err != nil {
254230
log.Fatalf("Error running HAProxy Data Plane API: %s", err.Error())
255231
}

configure_data_plane.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,19 @@ import (
6969
// go:generate swagger generate server --target ../../../../../../github.com/haproxytech --name controller --spec ../../../../../../../../haproxy-api/haproxy-open-api-spec/build/haproxy_spec.yaml --server-package controller --tags Stats --tags Information --tags Configuration --tags Discovery --tags Frontend --tags Backend --tags Bind --tags Server --tags TCPRequestRule --tags HTTPRequestRule --tags HTTPResponseRule --tags Acl --tags BackendSwitchingRule --tags ServerSwitchingRule --tags TCPResponseRule --skip-models --exclude-main
7070

7171
var (
72-
Version string
73-
BuildTime string
74-
mWorker = false
75-
logFile *os.File
76-
AppLogger *log.Logger
77-
AccLogger *log.Logger
72+
Version string
73+
BuildTime string
74+
mWorker = false
75+
logFile *os.File
76+
AppLogger *log.Logger
77+
AccLogger *log.Logger
78+
serverStartedCallback func()
7879
)
7980

81+
func SetServerStartedCallback(callFunc func()) {
82+
serverStartedCallback = callFunc
83+
}
84+
8085
func configureFlags(api *operations.DataPlaneAPI) {
8186
cfg := dataplaneapi_config.Get()
8287

generate/post_swagger.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
3+
set -e
4+
echo -n " ---> adding serverStartedCallback() to "
5+
sed -i "s/wg.Wait()/serverStartedCallback()\nwg.Wait()/g" server.go
6+
go fmt server.go

generate/swagger/script.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,13 @@ swagger generate server -f $SPEC_DIR/haproxy_spec.yaml \
101101
-r $SPEC_DIR/copyright.txt
102102

103103
echo " ---> removing doc.go"
104-
rm doc.go
104+
rm doc.go || echo "doc.go does not exists"
105105
echo " ---> removing embedded_spec.go"
106-
rm embedded_spec.go
106+
rm embedded_spec.go || echo "embedded_spec.go does not exists"
107107
echo " ---> removing server.go"
108-
rm server.go
108+
rm server.go || echo "server.go does not exists"
109109
echo " ---> removing operations/*"
110-
rm -rf operations/*
110+
rm -rf operations/* || echo "operations/ does not exists"
111111

112112
echo " ---> copy generated files to destination"
113113
cp -a $DST_DIR/dataplaneapi/. .

server.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)