Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize docker image contents #134

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open

Conversation

alukach
Copy link
Collaborator

@alukach alukach commented Dec 24, 2024

What I'm changing

In this PR, we copy only the Python coded needed for the function of each service into their corresponding docker images.

How I did it

This was achieved by refactoring the code to place common files into a cerulean_cloud.common submodule. This made it simpler to copy only the common submodule and not the entirety of the cerulean_cloud module into each docker image.

Tip

Reviewing all of these changed files can be overwhelming. Care was given to have clean commits; reviewing each commit separately would probably be a simpler way of understanding what was changed.

Impact

Admittedly, the positive impact in regards to reducing deployment times was minimal. However, I believe it's worth considering keeping this change all the same.

Outside of scope

I believe that this project would benefit from a larger refactor of its code into separate submodules. It's ambiguous what each submodule requires in terms of dependencies. If each submodule had only its own dependencies, then services utilizing those submodules could list the modules themselves as dependencies and not concern its own requirements.txt file with these needs.

An example layout could look something like:

cerulean_cloud/
  auth/
    pyproject.toml  # `cerulean_cloud.auth`, describes minimum requirements
    src/
      __init__.py
      auth.py
  db/
    pyproject.toml  # `cerulean_cloud.db`, describes minimum requirements
    src/
      __init__.py
      database_client.py
      database_schema.py
  models/
    pyproject.toml  # `cerulean_cloud.models`, describes minimum requirements
    src/
      __init__.py
      models.py
  roda_sentinelhub_client/
    pyproject.toml  # `cerulean_cloud.roda_sentinelhub_client`, describes minimum requirements
    src/
      __init__.py   # Expose `RodaSentinelHubClient`
      client.py     # logic from `roda_sentinelhub_client.py`
  tiling/
    pyproject.toml  # `cerulean_cloud.tiling`, describes minimum requirements
    src/
      __init__.py   # expose `TitilerClient`, `pixel_to_location`, `adjacent_tile`, `offset_bounds_from_base_tiles`
      utils.py      # logic from `tiling.py`
      client.py     # logic from `titiler_client.py`

Services requirements files (e.g. cerulean_cloud/cloud_function_ais_analysis/requirements.txt) would then look like:

../db
# ...

Copy link

github-actions bot commented Dec 24, 2024

🍹 preview on cerulean-cloud-images/test

Pulumi report
   Previewing update (test):
@ previewing update.........

@ previewing update............
   pulumi:pulumi:Stack cerulean-cloud-images-test running 
@ previewing update....
   gcp:container:Registry cerulean-cloud-images-test-registry  
@ previewing update....
   docker:index:Image cerulean-cloud-images-test-cr-orchestrator-image  Building your image for linux/amd64 architecture.
~  docker:index:Image cerulean-cloud-images-test-cr-orchestrator-image update [diff: ~build]; Building your image for linux/amd64 architecture.
@ previewing update....
   docker:index:Image cerulean-cloud-images-test-cr-tipg-image  Building your image for linux/amd64 architecture.
~  docker:index:Image cerulean-cloud-images-test-cr-tipg-image update [diff: ~build]; Building your image for linux/amd64 architecture.
   docker:index:Image cerulean-cloud-images-test-cr-offset-tile-image  Building your image for linux/amd64 architecture.
~  docker:index:Image cerulean-cloud-images-test-cr-offset-tile-image update [diff: ~build]; Building your image for linux/amd64 architecture.
   pulumi:pulumi:Stack cerulean-cloud-images-test  
Diagnostics:
 docker:index:Image (cerulean-cloud-images-test-cr-orchestrator-image):
   Building your image for linux/amd64 architecture.
   To ensure you are building for the correct platform, consider explicitly setting the `platform` field on ImageBuildOptions.

 docker:index:Image (cerulean-cloud-images-test-cr-tipg-image):
   Building your image for linux/amd64 architecture.
   To ensure you are building for the correct platform, consider explicitly setting the `platform` field on ImageBuildOptions.

 docker:index:Image (cerulean-cloud-images-test-cr-offset-tile-image):
   Building your image for linux/amd64 architecture.
   To ensure you are building for the correct platform, consider explicitly setting the `platform` field on ImageBuildOptions.

Resources:
   ~ 3 to update
   2 unchanged

   

Copy link

github-actions bot commented Dec 24, 2024

🍹 preview on cerulean-cloud/test

Pulumi report
   Previewing update (test):

@ previewing update....
   pulumi:pulumi:Stack cerulean-cloud-test running 
@ previewing update....
   pulumi:providers:docker cerulean-cloud-images-test-gcr  
@ previewing update....
   gcp:storage:Bucket cerulean-cloud-test-bucket-cf-ais  
   gcp:sql:DatabaseInstance cerulean-cloud-test-database-instance  
   gcp:cloudtasks:Queue cerulean-cloud-test-queue-cloud-tasks-ais-analysis  
   gcp:serviceaccount:Account cerulean-cloud-test-cf-ais  
-- gcp:storage:BucketObject cerulean-cloud-test-source-cf-ais delete original 
+- gcp:storage:BucketObject cerulean-cloud-test-source-cf-ais replace [diff: ~detectMd5hash,source]
++ gcp:storage:BucketObject cerulean-cloud-test-source-cf-ais create replacement [diff: ~detectMd5hash,source]
   gcp:sql:User cerulean-cloud-test-database-users  
   gcp:projects:IAMMember cerulean-cloud-test-cf-ais-iam  
   gcp:sql:Database cerulean-cloud-test-database  
~  gcp:cloudfunctions:Function cerulean-cloud-test-cf-ais update [diff: ~environmentVariables,secretEnvironmentVariables]
   gcp:cloudfunctions:FunctionIamMember cerulean-cloud-test-cf-ais-invoker  
@ previewing update........
   gcp:serviceaccount:Account cerulean-cloud-test-cr-offset-tile  
   docker:index:RemoteImage cerulean-cloud-images-test-remote-offset  
   docker:index:RemoteImage cerulean-cloud-images-test-remote-orchestrator  
   docker:index:RemoteImage cerulean-cloud-images-test-remote-tipg  
   gcp:projects:IAMMember cerulean-cloud-test-cr-offset-tile-cloudSqlClient  
   gcp:projects:IAMMember cerulean-cloud-test-cr-offset-tile-secretmanagerSecretAccessor  
   gcp:secretmanager:SecretIamMember cerulean-cloud-test-cr-offset-tile-secret-accessor-binding  
   pulumi:pulumi:Stack cerulean-cloud-test running warning: serving_state is deprecated: `serving_state` is deprecated and will be removed in a future major release. This field is not supported by the Cloud Run API.
   pulumi:pulumi:Stack cerulean-cloud-test running warning: env_froms is deprecated: `env_from` is deprecated and will be removed in a future major release. This field is not supported by the Cloud Run API.
   pulumi:pulumi:Stack cerulean-cloud-test running warning: working_dir is deprecated: `working_dir` is deprecated and will be removed in a future major release. This field is not supported by the Cloud Run API.
~  gcp:cloudrun:Service cerulean-cloud-test-cr-offset-tiles update [diff: ~metadata,template]
   gcp:cloudrun:IamPolicy cerulean-cloud-test-cr-noauth-iam-policy-offset  
@ previewing update.....
   aws:iam:Role cerulean-cloud-test-lambda-titiler-role  
   aws:iam:Policy cerulean-cloud-test-lambda-titiler-policy  
   aws:s3:Bucket cerulean-cloud-test-titiler-lambda-archive  
   aws:sns:Topic cerulean-cloud-test-lambda-APIAbuseAlert  
   aws:apigatewayv2:Api cerulean-cloud-test-lambda-titiler-api  
   gcp:storage:Bucket cerulean-cloud-test-bucket-cf-sr  
   gcp:cloudtasks:Queue cerulean-cloud-test-queue-cr-orchestrator  
   gcp:serviceaccount:Account cerulean-cloud-test-cr-orchestrator  
   gcp:serviceaccount:Account cerulean-cloud-test-cr-tipg  
   gcp:serviceaccount:Account cerulean-cloud-test-cf-sr  
   aws:iam:Role cerulean-cloud-test-lambda-sentinel1-iam  
   aws:iam:RolePolicyAttachment cerulean-cloud-test-lambda-titiler-attachment2  
   aws:iam:RolePolicyAttachment cerulean-cloud-test-lambda-titiler-attachment  
   aws:sns:TopicSubscription cerulean-cloud-test-lambda-titiler-email-jona  
   aws:sns:TopicSubscription cerulean-cloud-test-lambda-titiler-email-support  
   aws:sns:TopicSubscription cerulean-cloud-test-lambda-titiler-email-aemon  
   aws:sns:TopicSubscription cerulean-cloud-test-lambda-titiler-email-jason  
-- gcp:storage:BucketObject cerulean-cloud-test-source-cf-sr delete original 
+- gcp:storage:BucketObject cerulean-cloud-test-source-cf-sr replace [diff: ~detectMd5hash,source]
++ gcp:storage:BucketObject cerulean-cloud-test-source-cf-sr create replacement [diff: ~detectMd5hash,source]
@ previewing update....
-- gcp:storage:BucketObject cerulean-cloud-test-source-cf-historical-run delete original 
+- gcp:storage:BucketObject cerulean-cloud-test-source-cf-historical-run replace [diff: ~detectMd5hash,source]
++ gcp:storage:BucketObject cerulean-cloud-test-source-cf-historical-run create replacement [diff: ~detectMd5hash,source]
   gcp:projects:IAMMember cerulean-cloud-test-cr-orchestrator-secretmanagerSecretAccessor  
   gcp:projects:IAMMember cerulean-cloud-test-cr-orchestrator-cloudTasksEnqueuer  
   gcp:projects:IAMMember cerulean-cloud-test-cr-orchestrator-cloudSqlClient  
   gcp:projects:IAMMember cerulean-cloud-test-cr-tipg-secretmanagerSecretAccessor  
   aws:iam:RolePolicyAttachment cerulean-cloud-test-lambda-sentinel1-basic-execution  
   gcp:projects:IAMMember cerulean-cloud-test-cr-tipg-cloudSqlClient  
   gcp:projects:IAMMember cerulean-cloud-test-cf-sr-iam  
   gcp:secretmanager:SecretIamMember cerulean-cloud-test-cr-orchestrator-secret-accessor-binding  
   gcp:secretmanager:SecretIamMember cerulean-cloud-test-cr-tipg-secret-accessor-binding  
   pulumi:pulumi:Stack cerulean-cloud-test running warning: serving_state is deprecated: `serving_state` is deprecated and will be removed in a future major release. This field is not supported by the Cloud Run API.
   pulumi:pulumi:Stack cerulean-cloud-test running warning: env_froms is deprecated: `env_from` is deprecated and will be removed in a future major release. This field is not supported by the Cloud Run API.
   pulumi:pulumi:Stack cerulean-cloud-test running warning: working_dir is deprecated: `working_dir` is deprecated and will be removed in a future major release. This field is not supported by the Cloud Run API.
   pulumi:pulumi:Stack cerulean-cloud-test running warning: serving_state is deprecated: `serving_state` is deprecated and will be removed in a future major release. This field is not supported by the Cloud Run API.
   pulumi:pulumi:Stack cerulean-cloud-test running warning: env_froms is deprecated: `env_from` is deprecated and will be removed in a future major release. This field is not supported by the Cloud Run API.
   pulumi:pulumi:Stack cerulean-cloud-test running warning: working_dir is deprecated: `working_dir` is deprecated and will be removed in a future major release. This field is not supported by the Cloud Run API.
~  gcp:cloudrun:Service cerulean-cloud-test-cr-tipg update [diff: ~metadata]
~  gcp:cloudrun:Service cerulean-cloud-test-cr-orchestrator update [diff: ~metadata,template]
   gcp:cloudrun:IamPolicy cerulean-cloud-test-cr-noauth-iam-policy-tipg  
   gcp:cloudrun:IamPolicy cerulean-cloud-test-cr-noauth-iam-policy-orchestrator  
~  gcp:cloudfunctions:Function cerulean-cloud-test-cf-sr update [diff: ~secretEnvironmentVariables]
~  gcp:cloudfunctions:Function cerulean-cloud-test-cf-historical-run update [diff: ~secretEnvironmentVariables]
   aws:lambda:Function cerulean-cloud-test-lambda-sentinel1-sub  
   gcp:cloudfunctions:FunctionIamMember cerulean-cloud-test-cf-sr-invoker  
   gcp:cloudfunctions:FunctionIamMember cerulean-cloud-test-cf-historical-run-invoker  
   aws:sns:TopicSubscription cerulean-cloud-test-sentinel1-subscription  
   aws:lambda:Permission cerulean-cloud-test-lambda-sentinel1-permission  
@ previewing update...............................................................................................................................
~  aws:s3:BucketObject cerulean-cloud-test-titiler-lambda-archive update [diff: ~source]
~  aws:lambda:Function cerulean-cloud-test-lambda-titiler-sentinel update [diff: ~sourceCodeHash]
   aws:apigatewayv2:Integration cerulean-cloud-test-lambda-titiler-integration  
   aws:cloudwatch:MetricAlarm cerulean-cloud-test-lambda-titiler-alarm  
   aws:lambda:Permission cerulean-cloud-test-lambda-titiler-permission  
   aws:apigatewayv2:Route cerulean-cloud-test-lambda-titiler-route  
   aws:apigatewayv2:Stage cerulean-cloud-test-lambda-titiler-stage  
   pulumi:pulumi:Stack cerulean-cloud-test running Creating lambda package in [/home/runner/work/cerulean-cloud/cerulean-cloud] [running in Docker]...
   pulumi:pulumi:Stack cerulean-cloud-test running Checking Docker is available...
   pulumi:pulumi:Stack cerulean-cloud-test running Building container image...
   pulumi:pulumi:Stack cerulean-cloud-test running Sucessfully built container image with id sha256:f6d4211352ee7a68bf26b34d2502e89815f3f41fa5d682a23872f9d30b433c8f
   pulumi:pulumi:Stack cerulean-cloud-test running Creating installation package.zip ...
   pulumi:pulumi:Stack cerulean-cloud-test running Sucessfully created package.zip at /home/runner/work/cerulean-cloud/cerulean-cloud/package.zip
   pulumi:pulumi:Stack cerulean-cloud-test  9 warnings; 6 messages
Diagnostics:
 pulumi:pulumi:Stack (cerulean-cloud-test):
   warning: serving_state is deprecated: `serving_state` is deprecated and will be removed in a future major release. This field is not supported by the Cloud Run API.
   warning: env_froms is deprecated: `env_from` is deprecated and will be removed in a future major release. This field is not supported by the Cloud Run API.
   warning: working_dir is deprecated: `working_dir` is deprecated and will be removed in a future major release. This field is not supported by the Cloud Run API.
   warning: serving_state is deprecated: `serving_state` is deprecated and will be removed in a future major release. This field is not supported by the Cloud Run API.
   warning: env_froms is deprecated: `env_from` is deprecated and will be removed in a future major release. This field is not supported by the Cloud Run API.
   warning: working_dir is deprecated: `working_dir` is deprecated and will be removed in a future major release. This field is not supported by the Cloud Run API.
   warning: serving_state is deprecated: `serving_state` is deprecated and will be removed in a future major release. This field is not supported by the Cloud Run API.
   warning: env_froms is deprecated: `env_from` is deprecated and will be removed in a future major release. This field is not supported by the Cloud Run API.
   warning: working_dir is deprecated: `working_dir` is deprecated and will be removed in a future major release. This field is not supported by the Cloud Run API.

   Creating lambda package in [/home/runner/work/cerulean-cloud/cerulean-cloud] [running in Docker]...
   Checking Docker is available...
   Building container image...
   Sucessfully built container image with id sha256:f6d4211352ee7a68bf26b34d2502e89815f3f41fa5d682a23872f9d30b433c8f
   Creating installation package.zip ...
   Sucessfully created package.zip at /home/runner/work/cerulean-cloud/cerulean-cloud/package.zip

Resources:
   ~ 8 to update
   +-3 to replace
   11 changes. 56 unchanged

   

@alukach alukach marked this pull request as ready for review December 24, 2024 05:46
@jonaraphael
Copy link
Collaborator

There seems to be an issue with this refactor... not sure what.

    scene_bounds = await titiler_client.get_bounds(payload.sceneid)
  File "/app/cerulean_cloud/common/titiler_client.py", line 47, in get_bounds
    resp.raise_for_status()  # Raises error for 4XX or 5XX status codes
  File "/app/site-packages/httpx/_models.py", line 829, in raise_for_status
    raise HTTPStatusError(message, request=request, response=self)
httpx.HTTPStatusError: Server error '500 Internal Server Error' for url 'https://3jkhpxsiq1.execute-api.eu-central-1.amazonaws.com/bounds?sceneid=S1A_IW_GRDH_1SDV_20210714T050536_20210714T050601_038769_049323_84FF'```

This is a scene that causes no problems in TEST on other branches, but fails here. It seems like the TiTiler failed to be deployed in a wholly functional way, but it's not immediately obvious what the failure was.

This is not ready to pull yet--need to figure out why the assets aren't being created correctly.

@jonaraphael jonaraphael marked this pull request as draft January 2, 2025 14:42
@jonaraphael jonaraphael marked this pull request as ready for review January 2, 2025 20:47
@jonaraphael jonaraphael mentioned this pull request Jan 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants