Skip to content

Commit

Permalink
Merge pull request #38 from kyleaa/imds-at-startup
Browse files Browse the repository at this point in the history
Fetch instance metadata at startup only
  • Loading branch information
kyleaa authored May 30, 2024
2 parents c50d97b + 5ce3d5a commit a6ddfe4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
1 change: 0 additions & 1 deletion lib/libcluster_ec2.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
defmodule ClusterEC2 do

@moduledoc File.read!("#{__DIR__}/../README.md")

@doc """
Expand Down
21 changes: 17 additions & 4 deletions lib/strategy/tags.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,32 @@ defmodule ClusterEC2.Strategy.Tags do
# libcluster ~> 3.0
@impl GenServer
def init([%State{} = state]) do
state = state |> Map.put(:meta, MapSet.new())
instance_meta = [
instance_id: ClusterEC2.local_instance_id(),
region: ClusterEC2.instance_region()
]

state =
state
|> Map.put(:meta, MapSet.new())
|> Map.put(:config, state.config ++ instance_meta)

{:ok, load(state)}
end

# libcluster ~> 2.0
def init(opts) do
instance_meta = [
instance_id: ClusterEC2.local_instance_id(),
region: ClusterEC2.instance_region()
]

state = %State{
topology: Keyword.fetch!(opts, :topology),
connect: Keyword.fetch!(opts, :connect),
disconnect: Keyword.fetch!(opts, :disconnect),
list_nodes: Keyword.fetch!(opts, :list_nodes),
config: Keyword.fetch!(opts, :config),
config: Keyword.fetch!(opts, :config) ++ instance_meta,
meta: MapSet.new([])
}

Expand Down Expand Up @@ -121,8 +134,8 @@ defmodule ClusterEC2.Strategy.Tags do

@spec get_nodes(State.t()) :: {:ok, [atom()]} | {:error, []}
defp get_nodes(%State{topology: topology, config: config}) do
instance_id = ClusterEC2.local_instance_id()
region = ClusterEC2.instance_region()
instance_id = Keyword.get(config, :instance_id, "")
region = Keyword.get(config, :region, "")
tag_name = Keyword.fetch!(config, :ec2_tagname)
tag_value = Keyword.get(config, :ec2_tagvalue, &local_instance_tag_value(&1, instance_id, region))
app_prefix = Keyword.get(config, :app_prefix, "app")
Expand Down

0 comments on commit a6ddfe4

Please sign in to comment.