Skip to content

Commit 6755b42

Browse files
committed
Add separate port for health server
1 parent 5ee6c06 commit 6755b42

File tree

1 file changed

+47
-6
lines changed

1 file changed

+47
-6
lines changed

cmd/agent/main.go

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ type GrpcProxyAgentOptions struct {
6464
proxyServerHost string
6565
proxyServerPort int
6666

67+
// Ports for the health and admin server
68+
healthServerPort int
69+
adminServerPort int
70+
6771
agentID string
6872
syncInterval time.Duration
6973
probeInterval time.Duration
@@ -92,6 +96,8 @@ func (o *GrpcProxyAgentOptions) Flags() *pflag.FlagSet {
9296
flags.StringVar(&o.caCert, "ca-cert", o.caCert, "If non-empty the CAs we use to validate clients.")
9397
flags.StringVar(&o.proxyServerHost, "proxy-server-host", o.proxyServerHost, "The hostname to use to connect to the proxy-server.")
9498
flags.IntVar(&o.proxyServerPort, "proxy-server-port", o.proxyServerPort, "The port the proxy server is listening on.")
99+
flags.IntVar(&o.healthServerPort, "health-server-port", o.healthServerPort, "The port the health server is listening on.")
100+
flags.IntVar(&o.adminServerPort, "admin-server-port", o.adminServerPort, "The port the admin server is listening on.")
95101
flags.StringVar(&o.agentID, "agent-id", o.agentID, "The unique ID of this agent. Default to a generated uuid if not set.")
96102
flags.DurationVar(&o.syncInterval, "sync-interval", o.syncInterval, "The initial interval by which the agent periodically checks if it has connections to all instances of the proxy server.")
97103
flags.DurationVar(&o.probeInterval, "probe-interval", o.probeInterval, "The interval by which the agent periodically checks if its connections to the proxy server are ready.")
@@ -106,6 +112,8 @@ func (o *GrpcProxyAgentOptions) Print() {
106112
klog.Warningf("CACert set to \"%s\".\n", o.caCert)
107113
klog.Warningf("ProxyServerHost set to \"%s\".\n", o.proxyServerHost)
108114
klog.Warningf("ProxyServerPort set to %d.\n", o.proxyServerPort)
115+
klog.Warningf("HealthServerPort set to %d.\n", o.healthServerPort)
116+
klog.Warningf("AdminServerPort set to %d.\n", o.adminServerPort)
109117
klog.Warningf("AgentID set to %s.\n", o.agentID)
110118
klog.Warningf("SyncInterval set to %v.\n", o.syncInterval)
111119
klog.Warningf("ProbeInterval set to %v.\n", o.probeInterval)
@@ -138,6 +146,13 @@ func (o *GrpcProxyAgentOptions) Validate() error {
138146
if o.proxyServerPort <= 0 {
139147
return fmt.Errorf("proxy server port %d must be greater than 0", o.proxyServerPort)
140148
}
149+
if o.healthServerPort <= 0 {
150+
return fmt.Errorf("health server port %d must be greater than 0", o.healthServerPort)
151+
}
152+
if o.adminServerPort <= 0 {
153+
return fmt.Errorf("admin server port %d must be greater than 0", o.adminServerPort)
154+
}
155+
141156
if o.serviceAccountTokenPath != "" {
142157
if _, err := os.Stat(o.serviceAccountTokenPath); os.IsNotExist(err) {
143158
return fmt.Errorf("error checking service account token path %s, got %v", o.serviceAccountTokenPath, err)
@@ -153,6 +168,8 @@ func newGrpcProxyAgentOptions() *GrpcProxyAgentOptions {
153168
caCert: "",
154169
proxyServerHost: "127.0.0.1",
155170
proxyServerPort: 8091,
171+
healthServerPort: 8093,
172+
adminServerPort: 8094,
156173
agentID: uuid.New().String(),
157174
syncInterval: 5 * time.Second,
158175
probeInterval: 5 * time.Second,
@@ -187,6 +204,10 @@ func (a *Agent) run(o *GrpcProxyAgentOptions) error {
187204
return fmt.Errorf("failed to run proxy connection with %v", err)
188205
}
189206

207+
if err := a.runHealthServer(o); err != nil {
208+
return fmt.Errorf("failed to run health server with %v", err)
209+
}
210+
190211
if err := a.runAdminServer(o); err != nil {
191212
return fmt.Errorf("failed to run admin server with %v", err)
192213
}
@@ -211,33 +232,53 @@ func (a *Agent) runProxyConnection(o *GrpcProxyAgentOptions) error {
211232
return nil
212233
}
213234

214-
func (a *Agent) runAdminServer(o *GrpcProxyAgentOptions) error {
235+
func (a *Agent) runHealthServer(o *GrpcProxyAgentOptions) error {
215236
livenessHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
216237
fmt.Fprintf(w, "ok")
217238
})
218239
readinessHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
219240
fmt.Fprintf(w, "ok")
220241
})
242+
243+
muxHandler := http.NewServeMux()
244+
muxHandler.HandleFunc("/healthz", livenessHandler)
245+
muxHandler.HandleFunc("/ready", readinessHandler)
246+
healthServer := &http.Server{
247+
Addr: fmt.Sprintf(":%d", o.healthServerPort),
248+
Handler: muxHandler,
249+
MaxHeaderBytes: 1 << 20,
250+
}
251+
252+
go func() {
253+
err := healthServer.ListenAndServe()
254+
if err != nil {
255+
klog.Warningf("health server received %v.\n", err)
256+
}
257+
klog.Warningf("Health server stopped listening\n")
258+
}()
259+
260+
return nil
261+
}
262+
263+
func (a *Agent) runAdminServer(o *GrpcProxyAgentOptions) error {
221264
metricsHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
222265
prometheus.Handler().ServeHTTP(w, r)
223266
})
224267

225268
muxHandler := http.NewServeMux()
226-
muxHandler.HandleFunc("/healthz", livenessHandler)
227-
muxHandler.HandleFunc("/ready", readinessHandler)
228269
muxHandler.HandleFunc("/metrics", metricsHandler)
229270
adminServer := &http.Server{
230-
Addr: ":8093",
271+
Addr: fmt.Sprintf("127.0.0.1:%d", o.adminServerPort),
231272
Handler: muxHandler,
232273
MaxHeaderBytes: 1 << 20,
233274
}
234275

235276
go func() {
236277
err := adminServer.ListenAndServe()
237278
if err != nil {
238-
klog.Warningf("health server received %v.\n", err)
279+
klog.Warningf("admin server received %v.\n", err)
239280
}
240-
klog.Warningf("Health server stopped listening\n")
281+
klog.Warningf("Admin server stopped listening\n")
241282
}()
242283

243284
return nil

0 commit comments

Comments
 (0)