From d95e654e59d3f3adf6c0a74980c73b4d05eb400c Mon Sep 17 00:00:00 2001 From: dankelleher Date: Wed, 15 Feb 2023 08:30:08 +0100 Subject: [PATCH] Add env vars to k8s driver deployments. Enable pushing images from CI --- .../workflows/universal-resolver-build.yml | 2 + ci/deploy-k8s-aws/scripts/k8s-template.yaml | 1 + .../scripts/prepare-deployment.py | 41 +++++++++++++++++++ ci/docker-build-push/entrypoint.sh | 2 +- docker-compose.yml | 14 +++---- 5 files changed, 52 insertions(+), 8 deletions(-) diff --git a/.github/workflows/universal-resolver-build.yml b/.github/workflows/universal-resolver-build.yml index c98a232d4..4587cb1e1 100644 --- a/.github/workflows/universal-resolver-build.yml +++ b/.github/workflows/universal-resolver-build.yml @@ -22,3 +22,5 @@ jobs: env: DOCKER_FILE: uni-resolver-web/docker/Dockerfile CONTAINER_TAG: identitydotcom/uni-resolver-web:latest + DOCKER_USERNAME : ${{ secrets.DOCKER_USERNAME }} + DOCKER_PASSWORD : ${{ secrets.DOCKER_PASSWORD }} \ No newline at end of file diff --git a/ci/deploy-k8s-aws/scripts/k8s-template.yaml b/ci/deploy-k8s-aws/scripts/k8s-template.yaml index 562660b5c..368947f57 100644 --- a/ci/deploy-k8s-aws/scripts/k8s-template.yaml +++ b/ci/deploy-k8s-aws/scripts/k8s-template.yaml @@ -39,6 +39,7 @@ spec: capabilities: drop: - NET_RAW + {{environmentVariables}} {{configMapVolume}} --- diff --git a/ci/deploy-k8s-aws/scripts/prepare-deployment.py b/ci/deploy-k8s-aws/scripts/prepare-deployment.py index 8885d5950..2cb8eec11 100755 --- a/ci/deploy-k8s-aws/scripts/prepare-deployment.py +++ b/ci/deploy-k8s-aws/scripts/prepare-deployment.py @@ -46,6 +46,7 @@ def generate_deployment_specs(containers, outputdir): for container in containers: container_tag = containers[container]['image'] container_port = get_container_port(containers[container]['ports']) + container_env = containers[container]['environment'] if 'environment' in containers[container] else None fin = open("k8s-template.yaml", "rt") deployment_file = "deployment-%s.yaml" % container fout = open(outputdir + '/' + deployment_file, "wt") @@ -55,10 +56,50 @@ def generate_deployment_specs(containers, outputdir): fin.close() fout.close() + + add_driver_environment_variables(outputdir, container_env, container) + # If there is a configmap-.yaml file, create a ConfigMap for it and add a volumeMounts mapping for it: add_driver_configmap_volume(outputdir, container) add_deployment(deployment_file, outputdir) +def add_driver_environment_variables(outputdir, container_env, container): + """ + If the container has environment variables defined in the docker-compose file, + add them here. + NOTE: This does not support variable substitution + """ + + deployment_file = "deployment-%s.yaml" % container + with open(outputdir + '/' + deployment_file, 'r') as infile: + input_deployment_contents = infile.read() + + configmap_filename = 'configmap-%s.yaml' % container + configmap_path = '/app-specs/%s' % configmap_filename + + if container_env is None: + print('No environment variables found for driver ' + container) + output_deployment_contents = input_deployment_contents.replace('{{environmentVariables}}', '') + else: + print('Environment variables found for driver ' + container + ' . Adding environment to the deployment yaml.') + + # Write the environment definition to the driver Deployment spec: + env_txt = 'env:\n' + + for env_var in container_env: + env_txt += ' - name: %s\n' % env_var + env_txt += ' value: %s\n' % container_env[env_var] + + print(env_txt) + + output_deployment_contents = input_deployment_contents.replace('{{environmentVariables}}', env_txt) + + # tmp + print(output_deployment_contents) + + with open(outputdir + '/' + deployment_file, 'w') as outfile: + outfile.write(output_deployment_contents) + def add_driver_configmap_volume(outputdir, container): """ If there is a file named /app-specs/configmap-.yaml for this driver, diff --git a/ci/docker-build-push/entrypoint.sh b/ci/docker-build-push/entrypoint.sh index 65bc08bc2..67bcd981a 100755 --- a/ci/docker-build-push/entrypoint.sh +++ b/ci/docker-build-push/entrypoint.sh @@ -31,7 +31,7 @@ fi if [ -z "${DOCKER_FILE}" ] then - echo "No Dockerfile spectified. Using default file: Dockerfile" + echo "No Dockerfile specified. Using default file: Dockerfile" DOCKER_FILE=Dockerfile fi diff --git a/docker-compose.yml b/docker-compose.yml index 85031bca4..eafda1126 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,6 +1,6 @@ version: "3.5" -### NOTE - this file is used to generatee the Kubernetes pods +### NOTE - this file is used to generate the Kubernetes pods # Enable any DID drivers we want to support. # If enabling drivers, check if special config needs to be uncommented in ci/scripts/driver-config.yaml # or entrypoint.sh (e.g. bitcr) @@ -85,10 +85,10 @@ services: # image: sphereon/driver-did-factom:latest # ports: # - "8097:8080" - driver-did-key: - image: universalresolver/driver-did-key:latest - ports: - - "8098:8080" +# driver-did-key: +# image: universalresolver/driver-did-key:latest +# ports: +# - "8098:8080" # dock-did-driver: # image: docknetwork/dock-did-driver:latest # ports: @@ -200,8 +200,8 @@ services: driver-didkit: image: ghcr.io/spruceid/didkit-http:latest environment: - PORT: 8080 - HOST: 0.0.0.0 + PORT: "8080" + HOST: "0.0.0.0" ports: - "8121:8080" # eosio-driver: