Skip to content

Commit

Permalink
feat: implement support for per-route options
Browse files Browse the repository at this point in the history
This commit adds the Options field to the RegistryMessage and Route
struct.
Required for route-emitter to forward the per-route-options.

Co-authored-by: Tamara Boehm <[email protected]>
See: cloudfoundry/community#909
  • Loading branch information
2 people authored and ameowlia committed Oct 23, 2024
1 parent dc53cac commit 5d27c66
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions routehandlers/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1076,6 +1076,7 @@ var _ = Describe("Handler", func() {
Hostnames: []string{hostname1},
Port: 8080,
RouteServiceUrl: "https://rs.example.com",
Options: json.RawMessage(`{"lb_algo":"least-connection"}`),
},
}.RoutingInfo()

Expand Down
2 changes: 2 additions & 0 deletions routingtable/endpoint.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package routingtable

import (
"encoding/json"
"fmt"

"code.cloudfoundry.org/bbs/models"
Expand Down Expand Up @@ -141,6 +142,7 @@ type Route struct {
LogGUID string
Protocol string
MetricTags map[string]*models.MetricTagValue
Options json.RawMessage
}

type routeHash struct {
Expand Down
3 changes: 3 additions & 0 deletions routingtable/registry_message.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package routingtable

import (
"encoding/json"
"fmt"
"strconv"

Expand All @@ -22,6 +23,7 @@ type RegistryMessage struct {
EndpointUpdatedAtNs int64 `json:"endpoint_updated_at_ns,omitempty" hash:"ignore"`
Tags map[string]string `json:"tags,omitempty" hash:"ignore"`
AvailabilityZone string `json:"availability_zone,omitempty" hash:"ignore"`
Options json.RawMessage `json:"options,omitempty" hash:"ignore"`
}

func RegistryMessageFor(endpoint Endpoint, route Route, emitEndpointUpdatedAt bool) RegistryMessage {
Expand Down Expand Up @@ -50,6 +52,7 @@ func RegistryMessageFor(endpoint Endpoint, route Route, emitEndpointUpdatedAt bo
PrivateInstanceIndex: index,
ServerCertDomainSAN: endpoint.InstanceGUID,
RouteServiceUrl: route.RouteServiceUrl,
Options: route.Options,
}
}

Expand Down
40 changes: 40 additions & 0 deletions routingtable/registry_message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,34 @@ var _ = Describe("RegistryMessage", func() {
Expect(message).To(Equal(expectedMessage))
})
})

Context("when a route option is provided", func() {
BeforeEach(func() {
expectedMessage.Options = json.RawMessage(`{"lb_algo":"least-connection"}`)
expectedJSON = `{
"host": "1.1.1.1",
"port": 61001,
"uris": ["host-1.example.com"],
"app" : "app-guid",
"private_instance_id": "instance-guid",
"server_cert_domain_san": "instance-guid",
"private_instance_index": "0",
"route_service_url": "https://hello.com",
"endpoint_updated_at_ns": 1000,
"tags": {"component":"route-emitter", "doo": "0", "foo": "bar", "goo": "instance-guid"},
"availability_zone": "some-zone",
"options": {"lb_algo":"least-connection"}
}`
})

It("correctly marshals the route option", func() {
message := routingtable.RegistryMessage{}

err := json.Unmarshal([]byte(expectedJSON), &message)
Expect(err).NotTo(HaveOccurred())
Expect(message).To(Equal(expectedMessage))
})
})
})

Describe("RegistryMessageFor", func() {
Expand Down Expand Up @@ -182,6 +210,18 @@ var _ = Describe("RegistryMessage", func() {
Expect(message).To(Equal(expectedMessage))
})
})

Context("when route options are provided", func() {
BeforeEach(func() {
route.Options = json.RawMessage(`{"lb_algo":"least-connection"}`)
expectedMessage.Options = json.RawMessage(`{"lb_algo":"least-connection"}`)
})

It("creates a valid message when route options are provided", func() {
message := routingtable.RegistryMessageFor(endpoint, route, true)
Expect(message).To(Equal(expectedMessage))
})
})
})

Describe("InternalAddressRegistryMessageFor", func() {
Expand Down
1 change: 1 addition & 0 deletions routingtable/routingtable.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ func httpRoutesFrom(lrp *models.DesiredLRP) map[RoutingKey][]routeMapping {
IsolationSegment: route.IsolationSegment,
MetricTags: lrp.MetricTags,
Protocol: route.Protocol,
Options: route.Options,
}
routes = append(routes, route)
}
Expand Down

0 comments on commit 5d27c66

Please sign in to comment.