From a5f4fae14596e2135edd986281825df177f061c5 Mon Sep 17 00:00:00 2001 From: Francis Lavoie Date: Sun, 12 Sep 2021 23:50:04 -0400 Subject: [PATCH] reverseproxy: Add ID field for upstreams --- modules/caddyhttp/reverseproxy/admin.go | 8 +++++--- modules/caddyhttp/reverseproxy/hosts.go | 9 +++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/modules/caddyhttp/reverseproxy/admin.go b/modules/caddyhttp/reverseproxy/admin.go index 25685a3a3..1126b9bfd 100644 --- a/modules/caddyhttp/reverseproxy/admin.go +++ b/modules/caddyhttp/reverseproxy/admin.go @@ -34,7 +34,8 @@ type adminUpstreams struct{} // upstreamResults holds the status of a particular upstream type upstreamStatus struct { - Address string `json:"address"` + ID string `json:"id"` + Address string `json:"address"` // Address is deprecated, should be removed in a future release. Healthy bool `json:"healthy"` NumRequests int `json:"num_requests"` Fails int `json:"fails"` @@ -78,7 +79,7 @@ func (adminUpstreams) handleUpstreams(w http.ResponseWriter, r *http.Request) er // Iterate over the upstream pool (needs to be fast) var rangeErr error hosts.Range(func(key, val interface{}) bool { - address, ok := key.(string) + id, ok := key.(string) if !ok { rangeErr = caddy.APIError{ HTTPStatus: http.StatusInternalServerError, @@ -97,7 +98,8 @@ func (adminUpstreams) handleUpstreams(w http.ResponseWriter, r *http.Request) er } results = append(results, upstreamStatus{ - Address: address, + ID: id, + Address: id, Healthy: !upstream.Unhealthy(), NumRequests: upstream.NumRequests(), Fails: upstream.Fails(), diff --git a/modules/caddyhttp/reverseproxy/hosts.go b/modules/caddyhttp/reverseproxy/hosts.go index b9817d237..147d9a7b2 100644 --- a/modules/caddyhttp/reverseproxy/hosts.go +++ b/modules/caddyhttp/reverseproxy/hosts.go @@ -65,6 +65,12 @@ type UpstreamPool []*Upstream type Upstream struct { Host `json:"-"` + // The unique ID for this upstream, to disambiguate multiple + // upstreams with the same Dial address. This is optional, + // and only necessary if the upstream states need to be + // separate, such as having different health checking policies. + ID string `json:"id,omitempty"` + // The [network address](/docs/conventions#network-addresses) // to dial to connect to the upstream. Must represent precisely // one socket (i.e. no port ranges). A valid network address @@ -98,6 +104,9 @@ type Upstream struct { } func (u Upstream) String() string { + if u.ID != "" { + return u.ID + } if u.LookupSRV != "" { return u.LookupSRV }