Skip to content

Commit 18e4ebd

Browse files
authored
Make health server optional (#966)
* Make health server optional Related: #943 * fix test
1 parent 81e2472 commit 18e4ebd

File tree

7 files changed

+19
-12
lines changed

7 files changed

+19
-12
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Flags:
5252
--config string config file (default is $HOME/.flowlogs-pipeline)
5353
--dynamicParameters string json of configmap location for dynamic parameters
5454
--health.address string Health server address (default "0.0.0.0")
55-
--health.port string Health server port (default "8080")
55+
--health.port int Health server port (default: disable health server)
5656
-h, --help help for flowlogs-pipeline
5757
--log-level string Log level: debug, info, warning, error (default "error")
5858
--metricsSettings string json for global metrics settings

cmd/flowlogs-pipeline/main.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func initFlags() {
143143
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", fmt.Sprintf("config file (default is $HOME/%s)", defaultLogFileName))
144144
rootCmd.PersistentFlags().StringVar(&logLevel, "log-level", "error", "Log level: debug, info, warning, error")
145145
rootCmd.PersistentFlags().StringVar(&opts.Health.Address, "health.address", "0.0.0.0", "Health server address")
146-
rootCmd.PersistentFlags().StringVar(&opts.Health.Port, "health.port", "8080", "Health server port")
146+
rootCmd.PersistentFlags().IntVar(&opts.Health.Port, "health.port", 0, "Health server port (default: disable health server) ")
147147
rootCmd.PersistentFlags().IntVar(&opts.Profile.Port, "profile.port", 0, "Go pprof tool port (default: disabled)")
148148
rootCmd.PersistentFlags().StringVar(&opts.PipeLine, "pipeline", "", "json of config file pipeline field")
149149
rootCmd.PersistentFlags().StringVar(&opts.Parameters, "parameters", "", "json of config file parameters field")
@@ -199,15 +199,20 @@ func run() {
199199
}
200200

201201
// Start health report server
202-
healthServer := operational.NewHealthServer(&opts, mainPipeline.IsAlive, mainPipeline.IsReady)
202+
var healthServer *http.Server
203+
if opts.Health.Port > 0 {
204+
healthServer = operational.NewHealthServer(&opts, mainPipeline.IsAlive, mainPipeline.IsReady)
205+
}
203206

204207
// Starts the flows pipeline
205208
mainPipeline.Run()
206209

207210
if promServer != nil {
208211
_ = promServer.Shutdown(context.Background())
209212
}
210-
_ = healthServer.Shutdown(context.Background())
213+
if healthServer != nil {
214+
_ = healthServer.Shutdown(context.Background())
215+
}
211216

212217
// Give all threads a chance to exit and then exit the process
213218
time.Sleep(time.Second)

cmd/flowlogs-pipeline/main_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func TestPipelineConfigSetup(t *testing.T) {
5454
"PipeLine": "[{\"name\":\"grpc\"},{\"follows\":\"grpc\",\"name\":\"enrich\"},{\"follows\":\"enrich\",\"name\":\"loki\"},{\"follows\":\"enrich\",\"name\":\"prometheus\"}]",
5555
"Parameters": "[{\"ingest\":{\"grpc\":{\"port\":2055},\"type\":\"grpc\"},\"name\":\"grpc\"},{\"name\":\"enrich\",\"transform\":{\"network\":{\"rules\":[{\"kubernetes\":{\"ipField\":\"SrcAddr\",\"output\":\"SrcK8S\"},\"type\":\"add_kubernetes\"},{\"kubernetes\":{\"ipField\":\"DstAddr\",\"output\":\"DstK8S\"},\"type\":\"add_kubernetes\"},{\"add_service\":{\"input\":\"DstPort\",\"output\":\"Service\",\"protocol\":\"Proto\"},\"type\":\"add_service\"},{\"add_subnet\":{\"input\":\"SrcAddr\",\"output\":\"SrcSubnet\",\"subnet_mask\":\"/16\"},\"type\":\"add_subnet\"}]},\"type\":\"network\"}},{\"name\":\"loki\",\"write\":{\"loki\":{\"batchSize\":102400,\"batchWait\":\"1s\",\"clientConfig\":{\"follow_redirects\":false,\"proxy_url\":null,\"tls_config\":{\"insecure_skip_verify\":false}},\"labels\":[\"SrcK8S_Namespace\",\"SrcK8S_OwnerName\",\"DstK8S_Namespace\",\"DstK8S_OwnerName\",\"FlowDirection\"],\"maxBackoff\":\"5m0s\",\"maxRetries\":10,\"minBackoff\":\"1s\",\"staticLabels\":{\"app\":\"netobserv-flowcollector\"},\"tenantID\":\"netobserv\",\"timeout\":\"10s\",\"timestampLabel\":\"TimeFlowEndMs\",\"timestampScale\":\"1ms\",\"url\":\"http://loki.netobserv.svc:3100/\"},\"type\":\"loki\"}},{\"encode\":{\"prom\":{\"metrics\":[{\"buckets\":null,\"labels\":[\"Service\",\"SrcK8S_Namespace\"],\"name\":\"bandwidth_per_network_service_per_namespace\",\"type\":\"counter\",\"valueKey\":\"Bytes\"},{\"buckets\":null,\"labels\":[\"SrcSubnet\"],\"name\":\"bandwidth_per_source_subnet\",\"type\":\"counter\",\"valueKey\":\"Bytes\"},{\"buckets\":null,\"labels\":[\"Service\"],\"name\":\"network_service_total\",\"type\":\"counter\",\"valueKey\":\"\"}],\"prefix\":\"netobserv_\"},\"type\":\"prom\"},\"name\":\"prometheus\"}]",
5656
"Health": {
57-
"Port": "8080"
57+
"Port": 8080
5858
},
5959
"Profile": {
6060
"Port": 0

hack/examples/docker-ipfix-config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
log-level: info
2+
health:
3+
port: 8080
24
pipeline:
35
- name: ingest
46
- name: conntrack

pkg/config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ type HotReloadStruct struct {
6060

6161
type Health struct {
6262
Address string
63-
Port string
63+
Port int
6464
}
6565

6666
type Profile struct {

pkg/operational/health.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
package operational
1919

2020
import (
21-
"net"
21+
"fmt"
2222
"net/http"
2323
"time"
2424

@@ -30,7 +30,7 @@ import (
3030

3131
func NewHealthServer(opts *config.Options, isAlive healthcheck.Check, isReady healthcheck.Check) *http.Server {
3232
handler := healthcheck.NewHandler()
33-
address := net.JoinHostPort(opts.Health.Address, opts.Health.Port)
33+
address := fmt.Sprintf("%s:%d", opts.Health.Address, opts.Health.Port)
3434
handler.AddLivenessCheck("PipelineCheck", isAlive)
3535
handler.AddReadinessCheck("PipelineCheck", isReady)
3636

pkg/pipeline/health_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func TestNewHealthServer(t *testing.T) {
3535

3636
type args struct {
3737
pipeline Pipeline
38-
port string
38+
port int
3939
address string
4040
}
4141
type want struct {
@@ -47,15 +47,15 @@ func TestNewHealthServer(t *testing.T) {
4747
args args
4848
want want
4949
}{
50-
{name: "pipeline running", args: args{pipeline: Pipeline{IsRunning: true}, port: "7000", address: "0.0.0.0"}, want: want{statusCode: 200}},
51-
{name: "pipeline not running", args: args{pipeline: Pipeline{IsRunning: false}, port: "7001", address: "0.0.0.0"}, want: want{statusCode: 503}},
50+
{name: "pipeline running", args: args{pipeline: Pipeline{IsRunning: true}, port: 7000, address: "0.0.0.0"}, want: want{statusCode: 200}},
51+
{name: "pipeline not running", args: args{pipeline: Pipeline{IsRunning: false}, port: 7001, address: "0.0.0.0"}, want: want{statusCode: 503}},
5252
}
5353

5454
for _, tt := range tests {
5555
t.Run(tt.name, func(t *testing.T) {
5656

5757
opts := config.Options{Health: config.Health{Port: tt.args.port, Address: tt.args.address}}
58-
expectedAddr := fmt.Sprintf("%s:%s", opts.Health.Address, opts.Health.Port)
58+
expectedAddr := fmt.Sprintf("%s:%d", opts.Health.Address, opts.Health.Port)
5959
server := operational.NewHealthServer(&opts, tt.args.pipeline.IsAlive, tt.args.pipeline.IsReady)
6060
require.NotNil(t, server)
6161
require.Equal(t, expectedAddr, server.Addr)

0 commit comments

Comments
 (0)