ecs_composex
ecs_composex copied to clipboard
[BUG] x-elbv2 throws validation error using environment variable to set Service port
Using docker run and the latest image, the relevant snippet of my docker-compose file is:
x-elbv2:
testALB:
Properties:
Scheme: internal
Type: application
LoadBalancerAttributes:
deletion_protection.enabled: "false"
MacroParameters:
Ingress:
ExtSources:
- IPv4: 0.0.0.0/0
Name: ANY
Description: "ANY"
Listeners:
- Port: 80
Protocol: HTTP
DefaultActions:
- Redirect: HTTP_TO_HTTPS
- Port: 443
Protocol: HTTPS
Certificates:
- CertificateArn: REDACTED
Targets:
- name: family:service
access: service.com/
Services:
'family:service':
port: ${FAMILY_SERVICE_PORT}
protocol: HTTP
healthcheck: ${FAMILY_SERVICE_PORT}:HTTP:7:2:15:5:/health_check/:200
And .env containing:
FAMILY_SERVICE_PORT=8080
2024-09-03 17:53:39 [ ERROR] elbv2.testALB - Definition is not conform to schema.
Traceback (most recent call last):
File "/usr/local/bin/ecs-compose-x", line 8, in <module>
sys.exit(main())
File "/usr/local/lib/python3.10/site-packages/ecs_composex/cli.py", line 206, in main
root_stack = generate_full_template(settings)
File "/usr/local/lib/python3.10/site-packages/ecs_composex/ecs_composex.py", line 248, in generate_full_template
settings.mod_manager.init_mods_resources(settings)
File "/usr/local/lib/python3.10/site-packages/ecs_composex/mods_manager.py", line 307, in init_mods_resources
module.set_resources(settings)
File "/usr/local/lib/python3.10/site-packages/ecs_composex/mods_manager.py", line 266, in set_resources
new_definition = self.resource_class(
File "/usr/local/lib/python3.10/site-packages/ecs_composex/elbv2/elbv2_stack/elbv2.py", line 75, in __init__
super().__init__(name, definition, module, settings)
File "/usr/local/lib/python3.10/site-packages/ecs_composex/compose/x_resources/network_x_resources.py", line 37, in __init__
super().__init__(name, definition, module, settings)
File "/usr/local/lib/python3.10/site-packages/ecs_composex/compose/x_resources/services_resources.py", line 39, in __init__
super().__init__(name, definition, module, settings)
File "/usr/local/lib/python3.10/site-packages/ecs_composex/compose/x_resources/__init__.py", line 79, in __init__
self.validate_schema(name, definition, module.mod_key)
File "/usr/local/lib/python3.10/site-packages/ecs_composex/compose/x_resources/__init__.py", line 191, in validate_schema
_eval.validate(definition)
File "/usr/local/lib/python3.10/site-packages/jsonschema/validators.py", line 451, in validate
raise error
jsonschema.exceptions.ValidationError: '8080' is not of type 'integer'
This actually seems more broad than just the port. Attempts to use environment variables almost anywhere a number is expected, results in an error, like the following:
...
healthcheck: # https://docs.docker.com/reference/dockerfile/#healthcheck
test: ["CMD", "netcat", "-vz", "localhost", "8000"]
interval: "${WEB_HEALTHCHECK_INTERVAL}"
timeout: "${WEB_HEALTHCHECK_TIMEOUT}"
retries: "${WEB_HEALTHCHECK_RETRIES}"
start_period: "${WEB_HEALTHCHECK_START_PERIOD}"
Traceback (most recent call last):
File "/usr/local/bin/ecs-compose-x", line 8, in <module>
sys.exit(main())
File "/usr/local/lib/python3.10/site-packages/ecs_composex/cli.py", line 194, in main
settings = ComposeXSettings(**vars(args))
File "/usr/local/lib/python3.10/site-packages/ecs_composex/common/settings.py", line 177, in __init__
self.set_content(kwargs, content)
File "/usr/local/lib/python3.10/site-packages/ecs_composex/common/settings.py", line 526, in set_content
content_def = ComposeDefinition(files, content)
File "/usr/local/lib/python3.10/site-packages/compose_x_render/compose_x_render.py", line 324, in __init__
jsonschema.validate(
File "/usr/local/lib/python3.10/site-packages/jsonschema/validators.py", line 1332, in validate
raise error
jsonschema.exceptions.ValidationError: '5' is not of type 'number'
Failed validating 'type' in schema['properties']['services']['patternProperties']['^[a-zA-Z0-9._-]+$']['properties']['healthcheck']['properties']['retries']:
{'type': 'number'}
On instance['services']['web']['healthcheck']['retries']:
'5'
Hello @dfrank-a
Apologies for the late reply, I had forgotten to click "Comment" :/
The reason you are seeing this issue is that your env vars even though look and feel like an int are in fact str and therefore they are not the expected type. I might have a look at trying to soften the schema to allow either only a string of with digits or numbers.
@dfrank-a tl;dr if you change "${WEB_HEALTHCHECK_RETRIES}" and place a 5 there you will see it will pass. :)
WRT to your specific issue, I see two issues there. One is that, an environment variable is always interpreted as a string by the OS. So there would need to be a way to render it as int, or string in the config. ( I assume this might be "FR" for an overrides config? )
But also in your example config you have quotations, so even if it was an int, the quotes would render it as a string.
retries: "${WEB_HEALTHCHECK_RETRIES}"
I am a noob here, so I could be wrong :)
PS:
FWIW I just tried declare -i WEB_HEALTHCHECK_RETRIES=5 and got the same error, so even if the OS treats the variable as an int it is still imported as a string. Which I am pretty sure python does by default, a "feature" of "duck typing" :)