Skip to content

Commit

Permalink
Rename 'lb_algo' and 'loadbalancing-algorithm' to 'algorithm'
Browse files Browse the repository at this point in the history
Fix tests
  • Loading branch information
hoffmaen committed Dec 5, 2024
1 parent 436e963 commit c223429
Show file tree
Hide file tree
Showing 16 changed files with 77 additions and 77 deletions.
12 changes: 6 additions & 6 deletions app/messages/manifest_routes_update_message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def contains_non_route_hash_values?(routes)
validate :routes_are_uris, if: proc { |record| record.requested?(:routes) }
validate :route_protocols_are_valid, if: proc { |record| record.requested?(:routes) }
validate :route_options_are_valid, if: proc { |record| record.requested?(:routes) }
validate :lb_algos_are_valid, if: proc { |record| record.requested?(:routes) }
validate :loadbalancings_are_valid, if: proc { |record| record.requested?(:routes) }
validate :no_route_is_boolean
validate :default_route_is_boolean
validate :random_route_is_boolean
Expand Down Expand Up @@ -65,16 +65,16 @@ def route_options_are_valid
end
end

def lb_algos_are_valid
def loadbalancings_are_valid
return if errors[:routes].present?

routes.each do |r|
next unless r[:options] && r[:options][:'loadbalancing-algorithm']
next unless r[:options] && r[:options][:'loadbalancing']

lb_algo = r[:options][:'loadbalancing-algorithm']
RouteOptionsMessage::VALID_LOADBALANCING_ALGORITHMS.exclude?(lb_algo) &&
loadbalancing = r[:options][:'loadbalancing']
RouteOptionsMessage::VALID_LOADBALANCING_ALGORITHMS.exclude?(loadbalancing) &&
errors.add(:base,
message: "Route '#{r[:route]}' contains invalid load-balancing algorithm '#{lb_algo}'. \
message: "Route '#{r[:route]}' contains invalid load-balancing algorithm '#{loadbalancing}'. \
Valid algorithms: '#{RouteOptionsMessage::VALID_LOADBALANCING_ALGORITHMS.join(', ')}'")
end
end
Expand Down
8 changes: 4 additions & 4 deletions app/messages/route_options_message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

module VCAP::CloudController
class RouteOptionsMessage < BaseMessage
VALID_MANIFEST_ROUTE_OPTIONS = %i[loadbalancing-algorithm].freeze
VALID_ROUTE_OPTIONS = %i[lb_algo].freeze
VALID_MANIFEST_ROUTE_OPTIONS = %i[loadbalancing].freeze
VALID_ROUTE_OPTIONS = %i[loadbalancing].freeze
VALID_LOADBALANCING_ALGORITHMS = %w[round-robin least-connections].freeze

register_allowed_keys VALID_ROUTE_OPTIONS
validates_with NoAdditionalKeysValidator
validates :lb_algo,
inclusion: { in: VALID_LOADBALANCING_ALGORITHMS, message: "'%<value>s' is not a supported load-balancing algorithm" },
validates :loadbalancing,
inclusion: { in: VALID_LOADBALANCING_ALGORITHMS, message: "'%<value>s' is not supported" },
presence: true,
allow_nil: true
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def to_hash(route_mappings:, app:, **_)

if route_mapping.route.options
route_hash[:options] = {}
route_hash[:options][:'loadbalancing-algorithm'] = route_mapping.route.options[:lb_algo] if route_mapping.route.options[:lb_algo]
route_hash[:options][:'loadbalancing'] = route_mapping.route.options[:loadbalancing] if route_mapping.route.options[:loadbalancing]
end

route_hash
Expand Down
6 changes: 3 additions & 3 deletions docs/v3/source/includes/api_resources/_routes.erb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
}
],
"options": {
"lb_algo": "round-robin"
"loadbalancing": "round-robin"
},
"metadata": {
"labels": <%= metadata.fetch(:labels, {}).to_json(space: ' ', object_nl: ' ')%>,
Expand Down Expand Up @@ -128,7 +128,7 @@
}
],
"options": {
"lb_algo": "round-robin"
"loadbalancing": "round-robin"
},
"metadata": {
"labels": {},
Expand Down Expand Up @@ -221,6 +221,6 @@

<% content_for :route_options do %>
{
"lb_algo": "round-robin"
"loadbalancing": "round-robin"
}
<% end %>
2 changes: 1 addition & 1 deletion docs/v3/source/includes/resources/routes/_create.md.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ curl "https://api.example.org/v3/routes" \
}
},
"options": {
"lb_algo": "round-robin"
"loadbalancing": "round-robin"
}
"metadata": {
"labels": { "key": "value" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ Example Route-Options object

| Name | Type | Description |
|-------------|----------|----------------------------------------------------------------------------------------------------------|
| **lb_algo** | _string_ | The load balancer associated with this route. Valid algorithms are 'round-robin' and 'least-connections' |
| **loadbalancing** | _string_ | The load balancer associated with this route. Valid algorithms are 'round-robin' and 'least-connections' |
2 changes: 1 addition & 1 deletion docs/v3/source/includes/resources/routes/_update.md.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ curl "https://api.example.org/v3/routes/[guid]" \
-H "Content-type: application/json" \
-d '{
"options": {
"lb_algo": "round-robin"
"loadbalancing": "round-robin"
}
"metadata": {
"labels": {"key": "value"},
Expand Down
2 changes: 1 addition & 1 deletion lib/cloud_controller/app_manifest/manifest_route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def self.parse(route, options=nil)

attrs[:full_route] = route
attrs[:options] = {}
attrs[:options][:lb_algo] = options[:'loadbalancing-algorithm'] if options && options.key?(:'loadbalancing-algorithm')
attrs[:options][:loadbalancing] = options[:'loadbalancing'] if options && options.key?(:'loadbalancing')

ManifestRoute.new(attrs)
end
Expand Down
36 changes: 18 additions & 18 deletions spec/request/space_manifests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -647,20 +647,20 @@

expect(last_response).to have_status_code(422)
expect(last_response).to have_error_message("For application '#{app1_model.name}': \
Route 'https://#{route.host}.#{route.domain.name}' contains invalid route option 'doesnt-exist'. Valid keys: 'loadbalancing-algorithm'")
Route 'https://#{route.host}.#{route.domain.name}' contains invalid route option 'doesnt-exist'. Valid keys: 'loadbalancing'")
end
end

context 'updating existing route options' do
# using loadbalancing-algorithm as an example since it is the only route option currently supported
# using loadbalancing as an example since it is the only route option currently supported
before do
yml_manifest = {
'applications' => [
{ 'name' => app1_model.name,
'routes' => [
{ 'route' => "https://round-robin-app.#{shared_domain.name}",
'options' => {
'loadbalancing-algorithm' => 'round-robin'
'loadbalancing' => 'round-robin'
} }
] }
]
Expand All @@ -675,7 +675,7 @@
Delayed::Worker.new.work_off
expect(VCAP::CloudController::PollableJobModel.find(guid: job_guid)).to be_complete, VCAP::CloudController::PollableJobModel.find(guid: job_guid).cf_api_error
app1_model.reload
expect(app1_model.routes.first.options).to eq({ 'lb_algo' => 'round-robin' })
expect(app1_model.routes.first.options).to eq({ 'loadbalancing' => 'round-robin' })
end

it 'updates the route option when a new value is provided' do
Expand All @@ -685,7 +685,7 @@
'routes' => [
{ 'route' => "https://round-robin-app.#{shared_domain.name}",
'options' => {
'loadbalancing-algorithm' => 'least-connections'
'loadbalancing' => 'least-connections'
} }
] }
]
Expand All @@ -699,7 +699,7 @@
Delayed::Worker.new.work_off
expect(VCAP::CloudController::PollableJobModel.find(guid: job_guid)).to be_complete, VCAP::CloudController::PollableJobModel.find(guid: job_guid).cf_api_error
app1_model.reload
expect(app1_model.routes.first.options).to eq({ 'lb_algo' => 'least-connections' })
expect(app1_model.routes.first.options).to eq({ 'loadbalancing' => 'least-connections' })
end

it 'does not modify any route options when the options hash is not provided' do
Expand All @@ -721,7 +721,7 @@
Delayed::Worker.new.work_off
expect(VCAP::CloudController::PollableJobModel.find(guid: job_guid)).to be_complete, VCAP::CloudController::PollableJobModel.find(guid: job_guid).cf_api_error
app1_model.reload
expect(app1_model.routes.first.options).to eq({ 'lb_algo' => 'round-robin' })
expect(app1_model.routes.first.options).to eq({ 'loadbalancing' => 'round-robin' })
end

it 'does not modify any route options options: nil is provided' do
Expand All @@ -744,7 +744,7 @@
Delayed::Worker.new.work_off
expect(VCAP::CloudController::PollableJobModel.find(guid: job_guid)).to be_complete, VCAP::CloudController::PollableJobModel.find(guid: job_guid).cf_api_error
app1_model.reload
expect(app1_model.routes.first.options).to eq({ 'lb_algo' => 'round-robin' })
expect(app1_model.routes.first.options).to eq({ 'loadbalancing' => 'round-robin' })
end

it 'does not modify any route options if an empty options hash is provided' do
Expand All @@ -764,7 +764,7 @@
expect(last_response.status).to eq(202)

app1_model.reload
expect(app1_model.routes.first.options).to eq({ 'lb_algo' => 'round-robin' })
expect(app1_model.routes.first.options).to eq({ 'loadbalancing' => 'round-robin' })
end

it 'does not modify any option when options: { key: nil } is provided' do
Expand All @@ -774,7 +774,7 @@
'routes' => [
{ 'route' => "https://round-robin-app.#{shared_domain.name}",
'options' => {
'loadbalancing-algorithm' => nil
'loadbalancing' => nil
} }
] }
]
Expand All @@ -786,12 +786,12 @@
expect(last_response.status).to eq(202)

app1_model.reload
expect(app1_model.routes.first.options).to eq({ 'lb_algo' => 'round-robin' })
expect(app1_model.routes.first.options).to eq({ 'loadbalancing' => 'round-robin' })
end
end

context 'route-option: loadbalancing-algorithm' do
context 'when the loadbalancing-algorithm is not supported' do
context 'route-option: loadbalancing' do
context 'when the loadbalancing is not supported' do
let(:yml_manifest) do
{
'applications' => [
Expand All @@ -800,7 +800,7 @@
'routes' => [
{ 'route' => "https://#{route.host}.#{route.domain.name}",
'options' => {
'loadbalancing-algorithm' => 'unsupported-lb-algorithm'
'loadbalancing' => 'unsupported-lb-algorithm'
} }
]
}
Expand All @@ -817,22 +817,22 @@
end
end

context 'when the loadbalancing-algorithm is supported' do
context 'when the loadbalancing is supported' do
let(:yml_manifest) do
{
'applications' => [
{ 'name' => app1_model.name,
'routes' => [
{ 'route' => "https://round-robin-app.#{shared_domain.name}",
'options' => {
'loadbalancing-algorithm' => 'round-robin'
'loadbalancing' => 'round-robin'
} }
] }
]
}.to_yaml
end

it 'adds the loadbalancing-algorithm' do
it 'adds the loadbalancing' do
post "/v3/spaces/#{space.guid}/actions/apply_manifest", yml_manifest, yml_headers(user_header)

expect(last_response.status).to eq(202)
Expand All @@ -842,7 +842,7 @@
expect(VCAP::CloudController::PollableJobModel.find(guid: job_guid)).to be_complete, VCAP::CloudController::PollableJobModel.find(guid: job_guid).cf_api_error

app1_model.reload
expect(app1_model.routes.first.options).to eq({ 'lb_algo' => 'round-robin' })
expect(app1_model.routes.first.options).to eq({ 'loadbalancing' => 'round-robin' })
end
end
end
Expand Down
16 changes: 8 additions & 8 deletions spec/unit/actions/route_update_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module VCAP::CloudController
}
end
let(:old_options) do
'{"lb_algo": "round-robin"}'
'{"loadbalancing": "round-robin"}'
end
let(:new_labels) do
{
Expand Down Expand Up @@ -153,7 +153,7 @@ module VCAP::CloudController
let(:body) do
{
options: {
lb_algo: 'round-robin'
loadbalancing: 'round-robin'
}
}
end
Expand All @@ -163,14 +163,14 @@ module VCAP::CloudController
subject.update(route:, message:)

route.reload
expect(route[:options]).to eq('{"lb_algo":"round-robin"}')
expect(route[:options]).to eq('{"loadbalancing":"round-robin"}')
end
end
end

context 'when the route has existing options' do
before do
route[:options] = '{"lb_algo": "round-robin"}'
route[:options] = '{"loadbalancing": "round-robin"}'
end

context 'when no options are specified' do
Expand All @@ -182,15 +182,15 @@ module VCAP::CloudController
expect(message).to be_valid
subject.update(route:, message:)
route.reload
expect(route.options).to include({ 'lb_algo' => 'round-robin' })
expect(route.options).to include({ 'loadbalancing' => 'round-robin' })
end
end

context 'when an option is specified' do
let(:body) do
{
options: {
lb_algo: 'least-connections'
loadbalancing: 'least-connections'
}
}
end
Expand All @@ -200,15 +200,15 @@ module VCAP::CloudController
subject.update(route:, message:)
route.reload

expect(route.options).to include({ 'lb_algo' => 'least-connections' })
expect(route.options).to include({ 'loadbalancing' => 'least-connections' })
end
end

context 'when the option value is set to null' do
let(:body) do
{
options: {
lb_algo: nil
loadbalancing: nil
}
}
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ module VCAP::CloudController
end
end

context 'when there is a valid loadbalancing-algorithm' do
context 'when there is a valid loadbalancing' do
let(:route) { 'http://example.com' }
let(:options) { { 'loadbalancing-algorithm' => 'round-robin' } }
let(:options) { { 'loadbalancing' => 'round-robin' } }

it 'is valid' do
manifest_route = ManifestRoute.parse(route, options)
Expand Down Expand Up @@ -175,8 +175,8 @@ module VCAP::CloudController
})
end

it 'parses a route with a loadbalancing-algorithm into route components' do
route = ManifestRoute.parse('http://potato.sub.some-domain.com', { 'loadbalancing-algorithm': 'round-robin' })
it 'parses a route with a loadbalancing into route components' do
route = ManifestRoute.parse('http://potato.sub.some-domain.com', { 'loadbalancing': 'round-robin' })

expect(route.to_hash).to eq({
candidate_host_domain_pairs: [
Expand All @@ -185,7 +185,7 @@ module VCAP::CloudController
],
port: nil,
path: '',
options: { lb_algo: 'round-robin' }
options: { loadbalancing: 'round-robin' }
})
end
end
Expand Down
Loading

0 comments on commit c223429

Please sign in to comment.