Skip to content

Commit

Permalink
feat: Add support for containers running on AWS ECS (#65)
Browse files Browse the repository at this point in the history
* Add configuration entrypoint for ECS

* Instantiate RuntimeContainer based on container metadata URI

* Remove unneeded deps

* Use metadata V3 URI
  • Loading branch information
karls authored Jul 18, 2023
1 parent 6d4e83d commit 3a6a356
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
9 changes: 9 additions & 0 deletions judoscale/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ def initialize(cls, env: Mapping = os.environ):
return cls.for_heroku(env)
elif env.get("RENDER_INSTANCE_ID"):
return cls.for_render(env)
elif env.get("ECS_CONTAINER_METADATA_URI"):
return cls.for_ecs(env)
else:
return cls(None, "", env)

Expand All @@ -55,6 +57,13 @@ def for_render(cls, env: Mapping):
api_base_url = f"https://adapter.judoscale.com/api/{service_id}"
return cls(runtime_container, api_base_url, env)

@classmethod
def for_ecs(cls, env: Mapping):
instance = env["ECS_CONTAINER_METADATA_URI"].split("/")[-1]
runtime_container = RuntimeContainer(instance)
api_base_url = env.get("JUDOSCALE_URL")
return cls(runtime_container, api_base_url, env)

@property
def is_enabled(self) -> bool:
return bool(self["API_BASE_URL"])
Expand Down
14 changes: 14 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ def test_on_render(self):
assert config["LOG_LEVEL"] == "WARN"
assert config["API_BASE_URL"] == "https://adapter.judoscale.com/api/srv-123"

def test_on_ecs(self):
fake_env = {
"ECS_CONTAINER_METADATA_URI": "http://169.254.170.2/v3/a8880ee042bc4db3ba878dce65b769b6-2750272591", # noqa
"JUDOSCALE_URL": "https://adapter.judoscale.com/api/srv-123",
"LOG_LEVEL": "WARN",
}
config = Config.for_ecs(fake_env)

assert (
config["RUNTIME_CONTAINER"] == "a8880ee042bc4db3ba878dce65b769b6-2750272591"
)
assert config["LOG_LEVEL"] == "WARN"
assert config["API_BASE_URL"] == "https://adapter.judoscale.com/api/srv-123"

def test_is_enabled(self):
config = Config(None, "", {})
assert not config.is_enabled
Expand Down

0 comments on commit 3a6a356

Please sign in to comment.