Skip to content

Commit 5709810

Browse files
Pass port into autoscaler url from webhook policy (agones-dev#1765)
* Pass port into autoscaler url from webhook policy The port in the URL created by createURL was hardcoded to 8000 rather than the port specified in the WebhookPolicy. I have made the port default to 8000 when a port is not given and added a test to check the URL produced is correct when a port is given. * Adding port description to FleetAutoscaler documentation * Adding a feature flag around port description in the FleetAutoscaler documentation Co-authored-by: Mark Mandel <markmandel@google.com>
1 parent 247aaaf commit 5709810

3 files changed

Lines changed: 23 additions & 4 deletions

File tree

‎pkg/fleetautoscalers/fleetautoscalers.go‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,19 @@ func buildURLFromWebhookPolicy(w *autoscalingv1.WebhookPolicy) (u *url.URL, err
9191
w.Service.Namespace = "default"
9292
}
9393

94-
return createURL(scheme, w.Service.Name, w.Service.Namespace, *w.Service.Path), nil
94+
return createURL(scheme, w.Service.Name, w.Service.Namespace, *w.Service.Path, w.Service.Port), nil
9595
}
9696

9797
// moved to a separate method to cover it with unit tests and check that URL corresponds to a proper pattern
98-
func createURL(scheme, name, namespace, path string) *url.URL {
98+
func createURL(scheme, name, namespace, path string, port *int32) *url.URL {
99+
var hostPort int32 = 8000
100+
if port != nil {
101+
hostPort = *port
102+
}
103+
99104
return &url.URL{
100105
Scheme: scheme,
101-
Host: fmt.Sprintf("%s.%s.svc:8000", name, namespace),
106+
Host: fmt.Sprintf("%s.%s.svc:%d", name, namespace, hostPort),
102107
Path: path,
103108
}
104109
}

‎pkg/fleetautoscalers/fleetautoscalers_test.go‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,13 +578,15 @@ func TestApplyWebhookPolicyNilFleet(t *testing.T) {
578578

579579
func TestCreateURL(t *testing.T) {
580580
t.Parallel()
581+
var nonStandardPort int32 = 8888
581582

582583
var testCases = []struct {
583584
description string
584585
scheme string
585586
name string
586587
namespace string
587588
path string
589+
port *int32
588590
expected string
589591
}{
590592
{
@@ -611,11 +613,20 @@ func TestCreateURL(t *testing.T) {
611613
path: "",
612614
expected: "http://service1.default.svc:8000",
613615
},
616+
{
617+
description: "OK, port specified",
618+
scheme: "http",
619+
name: "service1",
620+
namespace: "default",
621+
path: "scale",
622+
port: &nonStandardPort,
623+
expected: "http://service1.default.svc:8888/scale",
624+
},
614625
}
615626

616627
for _, tc := range testCases {
617628
t.Run(tc.description, func(t *testing.T) {
618-
res := createURL(tc.scheme, tc.name, tc.namespace, tc.path)
629+
res := createURL(tc.scheme, tc.name, tc.namespace, tc.path, tc.port)
619630

620631
if assert.NotNil(t, res) {
621632
assert.Equal(t, tc.expected, res.String())

‎site/content/en/docs/Reference/fleetautoscaler.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ The `spec` field is the actual `FleetAutoscaler` specification and it is compose
9292
- `namespace` is the kubernetes namespace where webhook is deployed. Optional
9393
If not specified, the "default" would be used
9494
- `path` is an optional URL path which will be sent in any request to this service. (i. e. /scale)
95+
{{% feature publishVersion="1.9.0" %}}
96+
- `port` is optional, it is the port for the service which is hosting the webhook. The default is 8000 for backward compatibility. If given, it should be a valid port number (1-65535, inclusive).
97+
{{% /feature %}}
9598
- `url` gives the location of the webhook, in standard URL form (`[scheme://]host:port/path`). Exactly one of `url` or `service` must be specified. The `host` should not refer to a service running in the cluster; use the `service` field instead. (optional, instead of service)
9699
- `caBundle` is a PEM encoded certificate authority bundle which is used to issue and then validate the webhook's server certificate. Base64 encoded PEM string. Required only for HTTPS. If not present HTTP client would be used.
97100

0 commit comments

Comments
 (0)