Skip to content

Commit

Permalink
Merge pull request #484 from rbino/enhance-reconciliation
Browse files Browse the repository at this point in the history
reconciler: normalize triggers before comparing them
  • Loading branch information
szakhlypa authored Mar 29, 2024
2 parents bbab2b5 + 0f5dcbd commit d5a65f7
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [0.8.0] - Unreleased
### Changed
- Configure cookie attribute based on protocol.
- Normalize triggers when comparing them for reconciliation to avoid useless reconciliations.

## [0.8.0-rc.1] - 2024-03-22
### Fixed
Expand Down
23 changes: 20 additions & 3 deletions backend/lib/edgehog/tenants/reconciler/core.ex
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ defmodule Edgehog.Tenants.Reconciler.Core do

case Astarte.fetch_trigger(client, trigger_name) do
{:ok, existing_trigger} ->
if existing_trigger != required_trigger do
update_trigger!(client, trigger_name, required_trigger)
else
if trigger_matches?(existing_trigger, required_trigger) do
:ok
else
update_trigger!(client, trigger_name, required_trigger)
end

# This intentionally doesn't match on different errors since we want to crash
Expand Down Expand Up @@ -120,4 +120,21 @@ defmodule Edgehog.Tenants.Reconciler.Core do

:ok
end

defp trigger_matches?(required, existing) do
normalize_defaults(required) == normalize_defaults(existing)
end

defp normalize_defaults(trigger) do
trigger
|> drop_if_default(["action", "ignore_ssl_errors"], false)
|> drop_if_default(["action", "http_static_headers"], %{})
end

defp drop_if_default(trigger, path, default) do
case pop_in(trigger, path) do
{^default, no_default_trigger} -> no_default_trigger
{_not_default, _} -> trigger
end
end
end
20 changes: 20 additions & 0 deletions backend/test/edgehog/tenants/reconciler/core_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,26 @@ defmodule Edgehog.Tenants.Reconciler.CoreTest do
assert :ok = Core.reconcile_trigger!(client, trigger_map)
end

test "works even if Astarte returns the trigger without some defaults", ctx do
%{
client: client,
trigger_name: trigger_name,
trigger_map: trigger_map
} = ctx

Edgehog.Astarte.Realm.TriggersMock
|> expect(:get, fn ^client, ^trigger_name ->
{_, no_ignore_ssl_errors_map} = pop_in(trigger_map["action"]["ignore_ssl_errors"])
{_, no_defaults_map} = pop_in(no_ignore_ssl_errors_map["action"]["http_static_headers"])

{:ok, %{"data" => no_defaults_map}}
end)
|> expect(:create, 0, fn _client, _trigger_map -> :ok end)
|> expect(:delete, 0, fn _client, _trigger_name -> :ok end)

assert :ok = Core.reconcile_trigger!(client, trigger_map)
end

test "deletes and recreates the trigger if it differs from the required one", ctx do
%{
client: client,
Expand Down

0 comments on commit d5a65f7

Please sign in to comment.