From 535f52d4c5877f5e0ba2ad4d44358b93edb984e9 Mon Sep 17 00:00:00 2001 From: Matthew Gardner Date: Tue, 15 Oct 2024 20:39:54 +0100 Subject: [PATCH 1/2] Add optional builds --- README.md | 12 +++++- gitops/base/account-service/deployment.yaml | 2 +- gitops/base/database/deployment.yaml | 2 +- gitops/base/people-service/deployment.yaml | 2 +- gitops/base/position-service/deployment.yaml | 24 ++++++++++- gitops/base/reference-data/deployment.yaml | 4 +- gitops/base/trade-feed/deployment.yaml | 2 +- gitops/base/trade-processor/deployment.yaml | 2 +- gitops/base/trade-service/deployment.yaml | 2 +- gitops/base/web-front-end/deployment.yaml | 2 +- gitops/local/Tiltfile | 19 ++++---- position-service/Dockerfile | 14 ++++-- .../controller/HealthController.java | 43 +++++++++++++++++++ 13 files changed, 105 insertions(+), 25 deletions(-) create mode 100644 position-service/src/main/java/finos/traderx/positionservice/controller/HealthController.java diff --git a/README.md b/README.md index 39b882bf..a9d4d3a2 100644 --- a/README.md +++ b/README.md @@ -164,7 +164,7 @@ cd .corp ## Usage (K8s) -The following are instructions to build and deploy all TraderX apps to your local enviroment using [tilt](https://tilt.dev) and kustomize files. +The following are instructions to deploy all TraderX apps to your local enviroment using [tilt](https://tilt.dev) and kustomize files. ## Prerequistes - Running [Docker](https://www.docker.com/products/docker-desktop/) or similar @@ -183,13 +183,21 @@ Check your cluster is running using the epic tool `k9s` - https://k9scli.io/ ### Start Tilt -This command will build and start all locally built applications and deploy them to your local K8s environment. +This command will deploy all the services to your local K8s environment. ``` cd ./gitops/local/ tilt up ``` +### Local Developement + +With all the services running you can then chose which ones you actively build locally. + +If you go to your local [Tiltfile](./gitops/local/Tiltfile) you simply need to uncomment all the lines, or just for the respective applicaiton that you want to work on. Those applications will be built locally and deployed to your cluster instead of the host images. + + + # Getting Involved diff --git a/gitops/base/account-service/deployment.yaml b/gitops/base/account-service/deployment.yaml index 8c22673e..a4ed862c 100644 --- a/gitops/base/account-service/deployment.yaml +++ b/gitops/base/account-service/deployment.yaml @@ -23,7 +23,7 @@ spec: value: "18088" - name: DATABASE_TCP_HOST value: database - image: traderx/account-service + image: ghcr.io/finos/traderx/account-service imagePullPolicy: IfNotPresent ports: - containerPort: 18088 diff --git a/gitops/base/database/deployment.yaml b/gitops/base/database/deployment.yaml index d6eb58eb..32833857 100644 --- a/gitops/base/database/deployment.yaml +++ b/gitops/base/database/deployment.yaml @@ -16,7 +16,7 @@ spec: spec: containers: - name: database-app - image: traderx/database + image: ghcr.io/finos/traderx/database imagePullPolicy: IfNotPresent ports: - containerPort: 18082 diff --git a/gitops/base/people-service/deployment.yaml b/gitops/base/people-service/deployment.yaml index 6fcd46ab..b1ca8b5b 100644 --- a/gitops/base/people-service/deployment.yaml +++ b/gitops/base/people-service/deployment.yaml @@ -18,7 +18,7 @@ spec: spec: containers: - name: people-service - image: traderx/people-service + image: ghcr.io/finos/traderx/people-service imagePullPolicy: IfNotPresent ports: - containerPort: 18089 diff --git a/gitops/base/position-service/deployment.yaml b/gitops/base/position-service/deployment.yaml index f4eb6302..42123b58 100644 --- a/gitops/base/position-service/deployment.yaml +++ b/gitops/base/position-service/deployment.yaml @@ -23,11 +23,33 @@ spec: value: database - name: POSITION_SERVICE_PORT value: "18090" - image: traderx/position-service + image: ghcr.io/finos/traderx/position-service imagePullPolicy: IfNotPresent ports: - containerPort: 18090 protocol: TCP workingDir: /position-service + + # Liveness Probe: Checks if the container is alive + livenessProbe: + httpGet: + path: /health/alive # The endpoint for the liveness probe + port: 18090 # Port to hit on the container + initialDelaySeconds: 10 # Delay before starting the probe after the container starts + periodSeconds: 10 # How often to run the probe (every 10 seconds) + timeoutSeconds: 5 # Timeout for the probe to respond + failureThreshold: 3 # If it fails 3 times, restart the container + + # Readiness Probe: Checks if the container is ready to serve traffic + readinessProbe: + httpGet: + path: /health/ready # The endpoint for the readiness probe + port: 18090 # Port to hit on the container + initialDelaySeconds: 5 # Delay before starting the probe after the container starts + periodSeconds: 5 # How often to run the probe (every 5 seconds) + timeoutSeconds: 5 # Timeout for the probe to respond + successThreshold: 1 # Minimum number of successful probes to mark it as ready + failureThreshold: 3 # If it fails 3 times, the pod is marked as unready + restartPolicy: Always diff --git a/gitops/base/reference-data/deployment.yaml b/gitops/base/reference-data/deployment.yaml index 9f3945b1..be71eaa8 100644 --- a/gitops/base/reference-data/deployment.yaml +++ b/gitops/base/reference-data/deployment.yaml @@ -20,8 +20,8 @@ spec: - name: reference-data env: - name: REFERENCE_DATA_SERVICE_PORT - value: "18095" - image: traderx/reference-data + value: "18085" + image: ghcr.io/finos/traderx/reference-data imagePullPolicy: IfNotPresent ports: - containerPort: 18085 diff --git a/gitops/base/trade-feed/deployment.yaml b/gitops/base/trade-feed/deployment.yaml index 3759bc29..1af7cef7 100644 --- a/gitops/base/trade-feed/deployment.yaml +++ b/gitops/base/trade-feed/deployment.yaml @@ -17,7 +17,7 @@ spec: app: trade-feed spec: containers: - - image: traderx/trade-feed + - image: ghcr.io/finos/traderx/trade-feed imagePullPolicy: IfNotPresent name: trade-feed env: diff --git a/gitops/base/trade-processor/deployment.yaml b/gitops/base/trade-processor/deployment.yaml index d540635e..5a7478b8 100644 --- a/gitops/base/trade-processor/deployment.yaml +++ b/gitops/base/trade-processor/deployment.yaml @@ -16,7 +16,7 @@ spec: spec: containers: - name: trade-processor-app - image: traderx/trade-processor + image: ghcr.io/finos/traderx/trade-processor imagePullPolicy: IfNotPresent ports: - containerPort: 18091 diff --git a/gitops/base/trade-service/deployment.yaml b/gitops/base/trade-service/deployment.yaml index 9a69a7e5..25ae1358 100644 --- a/gitops/base/trade-service/deployment.yaml +++ b/gitops/base/trade-service/deployment.yaml @@ -28,7 +28,7 @@ spec: value: reference-data - name: TRADE_FEED_HOST value: trade-feed - image: traderx/trade-service + image: ghcr.io/finos/traderx/trade-service:latest imagePullPolicy: IfNotPresent name: trade-service ports: diff --git a/gitops/base/web-front-end/deployment.yaml b/gitops/base/web-front-end/deployment.yaml index 30a7805d..766fdf77 100644 --- a/gitops/base/web-front-end/deployment.yaml +++ b/gitops/base/web-front-end/deployment.yaml @@ -17,7 +17,7 @@ spec: app: web-front-end-angular spec: containers: - - image: traderx/web-front-end-angular + - image: ghcr.io/finos/traderx/web-front-end-angular:latest imagePullPolicy: IfNotPresent name: web-front-end-angular env: diff --git a/gitops/local/Tiltfile b/gitops/local/Tiltfile index 7d81e97c..b4bbfe7f 100644 --- a/gitops/local/Tiltfile +++ b/gitops/local/Tiltfile @@ -1,12 +1,13 @@ -docker_build('traderx/database', './../../database/.') -docker_build('traderx/account-service', './../../account-service/.') -docker_build('traderx/people-service', './../../people-service/.') -docker_build('traderx/position-service', './../../position-service/.') -docker_build('traderx/reference-data', './../../reference-data/.') -docker_build('traderx/trade-feed', './../../trade-feed/.') -docker_build('traderx/trade-processor', './../../trade-processor/.') -docker_build('traderx/trade-service', './../../trade-service/.') -docker_build('traderx/web-front-end-angular', './../../web-front-end/angular/.') +# Uncomment lines to use locally built version +# docker_build('ghcr.io/finos/traderx/database', './../../database/.') +# docker_build('ghcr.io/finos/traderx/account-service', './../../account-service/.') +# docker_build('ghcr.io/finos/traderx/people-service', './../../people-service/.') +# docker_build('ghcr.io/finos/traderx/position-service', './../../position-service/.') +# docker_build('ghcr.io/finos/traderx/reference-data', './../../reference-data/.') +# docker_build('ghcr.io/finos/traderx/trade-feed', './../../trade-feed/.') +# docker_build('ghcr.io/finos/traderx/trade-processor', './../../trade-processor/.') +# docker_build('ghcr.io/finos/traderx/trade-service', './../../trade-service/.') +# docker_build('ghcr.io/finos/traderx/web-front-end-angular', './../../web-front-end/angular/.') yaml = kustomize(('./traderx')) print(yaml) k8s_yaml(yaml) \ No newline at end of file diff --git a/position-service/Dockerfile b/position-service/Dockerfile index 69d6b262..88167759 100644 --- a/position-service/Dockerfile +++ b/position-service/Dockerfile @@ -2,12 +2,18 @@ # [Choice] Java version (use -bullseye variants on local arm64/Apple Silicon): 11, 17, 11-bullseye, 17-bullseye, 11-buster, 17-buster ARG VARIANT="21" -FROM mcr.microsoft.com/vscode/devcontainers/java:1-${VARIANT} +FROM mcr.microsoft.com/vscode/devcontainers/java:1-${VARIANT} AS build-stage +# Copy code WORKDIR /position-service COPY . . -EXPOSE 18090 - +# Build code RUN ./gradlew build -ENTRYPOINT ./gradlew bootRun \ No newline at end of file + +FROM mcr.microsoft.com/openjdk/jdk:${VARIANT}-ubuntu +RUN mkdir /app +COPY --from=build-stage /position-service/build/libs/*.jar /app/position-service.jar +EXPOSE 18090 +# ENTRYPOINT ./gradlew bootRun +ENTRYPOINT ["java","-jar","/app/position-service.jar"] \ No newline at end of file diff --git a/position-service/src/main/java/finos/traderx/positionservice/controller/HealthController.java b/position-service/src/main/java/finos/traderx/positionservice/controller/HealthController.java new file mode 100644 index 00000000..09935d04 --- /dev/null +++ b/position-service/src/main/java/finos/traderx/positionservice/controller/HealthController.java @@ -0,0 +1,43 @@ +package finos.traderx.positionservice.controller; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import finos.traderx.positionservice.model.Position; +import finos.traderx.positionservice.service.PositionService; + +@CrossOrigin("*") +@RestController +@RequestMapping(value="/health", produces="application/json") +public class HealthController { + + @Autowired + PositionService positionService; + + + @GetMapping("/ready") + public ResponseEntity isReady() { + List retVal = this.positionService.getAllPositions(); + return ResponseEntity.ok(retVal.size()>0); + } + + @GetMapping("/alive") + public ResponseEntity isAlive() { + return ResponseEntity.ok(true); + } + + + @ExceptionHandler(Exception.class) + public ResponseEntity generalError(Exception e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage()); + } +} From dd3e642209b962627ce2a6e458c47a254cbb0c09 Mon Sep 17 00:00:00 2001 From: Matthew Gardner Date: Wed, 30 Oct 2024 14:57:56 +0000 Subject: [PATCH 2/2] Update readme as per feedback --- README.md | 147 ++++++++++++++++------ gitops/base/trade-service/deployment.yaml | 2 +- gitops/base/web-front-end/deployment.yaml | 2 +- 3 files changed, 112 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index a9d4d3a2..7478666b 100644 --- a/README.md +++ b/README.md @@ -50,11 +50,23 @@ The project consists of multiple moving parts, and you can see how things hang t | [trade-processor](trade-processor) | java/spring | Trade Feed consumer which processes trade/orders | | [web-front-end](web-front-end) | html/angular or react | Interactive UI for executing trades and viewing blotter. Note: the AngularJS GUI was an initial contribution and contains account management capabilities. The React GUI was contributed during a hack day and may not work for managing accounts, but it does work for executing trades and viewing the blotter | -## Installation +## Check out Code This is installed locally through normal git clone operations. -## Usage example (Manual) +``` +git clone https://github.com/finos/traderX.git +``` + +## Run locally + +There are a number of ways to run TraderX locally. You should choose the method you are most comfortable with. +- [Run locally manually](#run-locally-manually) +- [Run locally within a Corporate Environments](#run-locally-within-a-corporate-environments) +- [Run locally using Docker & Docker Compose](#run-locally-using-docker--docker-compose) +- [Run locally using Kubernetes](#run-locally-using-kubernetes) + +## Run locally manually In order to get things working together, it is recommended to select a range of ports to provide all running processes with, so that the pieces can interconnect as needed. To run this all up 'by hand' here are default ports which are used, and you can easily export these variables to your favorite shell. @@ -87,31 +99,7 @@ trade-service web-front-end ``` -## Usage (Docker + Docker Compose) - -The easiest way to run up the entire system is using Docker Compose. This should work on your local computer using Docker Desktop / Docker Compose (tested on Mac Silicon) and also in Github Codespaces. - -### Codespaces -If using Github Codespaces it is recommended you select an 8-core type machine with 32GB RAM to ensure all the components have the required resources to start. - -To do this -* Select the Green Code menu at the top of this page -* Select the Codespace tab then click the three dots '...' and select 'New with options...'. -* Change the machine type to '8-core' and click 'Create codespace' - -As of writing, personal Github accounts receive 120 free core hours per month for using Codespaces, see the most recent details [here](https://docs.github.com/en/billing/managing-billing-for-github-codespaces/about-billing-for-github-codespaces#monthly-included-storage-and-core-hours-for-personal-accounts) - -Once you have cloned the repository locally or once your Codespace has started, from the root traderX directory run -``` -docker compose up -``` -On first run this will build all of the containers from the project specific Dockerfile's and then start them in the correct sequence. - -The Docker containers are configured via Docker Compose to connect to a shred virtual network enabling them to communciate whether running on your local computer or via a Codespace. - -Once everything has started the WebUI will be accessible at http://localhost:8080 (even if using a codespace, the localhost URL will be mapped through from your local browser to the Codespace). - -## Local Building (Corporate Environments) +## Run locally within a Corporate Environments When building locally in your company, if you are using a corporate artifact repository, you might need to override certain settings such as mavenCentral() in gradle, for the Java projects. @@ -162,14 +150,44 @@ cd .corp ./gradlew account-service:bootRun ``` -## Usage (K8s) +## Run locally using Docker & Docker Compose + +The easiest way to run up the entire system is using Docker Compose. This should work on your local computer using Docker Desktop / Docker Compose (tested on Mac Silicon) and also in Github Codespaces. + +### Codespaces +If using Github Codespaces it is recommended you select an 8-core type machine with 32GB RAM to ensure all the components have the required resources to start. + +To do this +* Select the Green Code menu at the top of this page +* Select the Codespace tab then click the three dots '...' and select 'New with options...'. +* Change the machine type to '8-core' and click 'Create codespace' + +As of writing, personal Github accounts receive 120 free core hours per month for using Codespaces, see the most recent details [here](https://docs.github.com/en/billing/managing-billing-for-github-codespaces/about-billing-for-github-codespaces#monthly-included-storage-and-core-hours-for-personal-accounts) + +Once you have cloned the repository locally or once your Codespace has started, from the root traderX directory run +``` +docker compose up +``` +On first run this will build all of the containers from the project specific Dockerfile's and then start them in the correct sequence. + +The Docker containers are configured via Docker Compose to connect to a shred virtual network enabling them to communciate whether running on your local computer or via a Codespace. + +Once everything has started the WebUI will be accessible at http://localhost:8080 (even if using a codespace, the localhost URL will be mapped through from your local browser to the Codespace). -The following are instructions to deploy all TraderX apps to your local enviroment using [tilt](https://tilt.dev) and kustomize files. -## Prerequistes -- Running [Docker](https://www.docker.com/products/docker-desktop/) or similar -- Running K8s - [Kind](https://kind.sigs.k8s.io/) /[Minikube](https://minikube.sigs.k8s.io/docs/start/)/[k3s](https://k3s.io/) or similar -- Install an [Ingress Controller](https://kubernetes.github.io/ingress-nginx/deploy/) +## Run locally using Kubernetes + +Another easy way to run up the entire system is using Kubernetes (K8s), which using [tilt.dev](https://tilt.dev) also allows you to easily swap in locally built images. The following are instructions to deploy all TraderX apps to your local enviroment using [tilt.dev](https://tilt.dev) and kustomize files. + +### Prerequistes +- Install and run [Docker](https://www.docker.com/products/docker-desktop/) or similar +- Install and run K8s - this could be one of the following well know distributions + - [K8s with Docker Desktop](https://docs.docker.com/desktop/kubernetes/) + - [Kind](https://kind.sigs.k8s.io/) + - [Minikube](https://minikube.sigs.k8s.io/docs/start/) + - [k3s](https://k3s.io/) or similar +- Install an [Ingress Controller](https://kubernetes.github.io/ingress-nginx/deploy/) - this will need to match your k8s distribution +- Install [tilt.dev](https://tilt.dev) ### Preflight checks @@ -177,27 +195,82 @@ The following are instructions to deploy all TraderX apps to your local envirome kubectl get pods ``` -or +You should see the kube pods as well as your ingress controller. Example below: -Check your cluster is running using the epic tool `k9s` - https://k9scli.io/ +``` +NAMESPACE NAME READY STATUS RESTARTS AGE +ingress-nginx ingress-nginx-admission-create-6rh79 0/1 Completed 0 38s +ingress-nginx ingress-nginx-admission-patch-f4kdg 0/1 Completed 1 38s +ingress-nginx ingress-nginx-controller-7d4db76476-7wfl2 1/1 Running 0 38s +kube-system coredns-7db6d8ff4d-ntdcr 1/1 Running 0 4h8m +kube-system coredns-7db6d8ff4d-ptfcs 1/1 Running 0 4h8m +kube-system etcd-docker-desktop 1/1 Running 2 4h8m +kube-system kube-apiserver-docker-desktop 1/1 Running 2 4h8m +kube-system kube-controller-manager-docker-desktop 1/1 Running 2 4h8m +kube-system kube-proxy-qt5z4 1/1 Running 0 4h8m +kube-system kube-scheduler-docker-desktop 1/1 Running 10 4h8m +kube-system storage-provisioner 1/1 Running 0 4h8m +``` -### Start Tilt +### Start Tilt.dev This command will deploy all the services to your local K8s environment. +Note: Ensure all the [pre-requistes](#prerequistes) are complete +After cloning the repo, you should `cd` into the folder (`cd traderx`) ``` +# cd into gitops repo cd ./gitops/local/ + +# Start Tilt tilt up ``` +Expected Console Output: +``` +Tilt started on http://localhost:10350/ +v0.30.2, built 2022-06-06 + +(space) to open the browser +(s) to stream logs (--stream=true) +(t) to open legacy terminal mode (--legacy=true) +(ctrl-c) to exit +``` + +This will download all the images and start them in your local k8s cluster. + +Launching the tilt.dev local webpage will allow you to monitor the progress by pressing the space bar. Or use one of the other options from the console. + +Note: if you run `tilt up` and you get an error stating `template engine not found for: up.`. Your OS might be trying to run a ruby library, please check the tilt.dev installation instructions, or you path settings. + + +### Start Tilt.dev + ### Local Developement With all the services running you can then chose which ones you actively build locally. -If you go to your local [Tiltfile](./gitops/local/Tiltfile) you simply need to uncomment all the lines, or just for the respective applicaiton that you want to work on. Those applications will be built locally and deployed to your cluster instead of the host images. +If you go to your local [Tiltfile](./gitops/local/Tiltfile) you simply need to uncomment all the lines, or just for the respective applicaiton that you want to work on. Those applications will be built locally and deployed to your cluster instead of the host images. For example. In your local [Tiltfile](./gitops/local/Tiltfile) uncommenting the line for position_service will ask that Tilt.dev builds the images and your locally built image is deployed in your local k8s cluster in place of the prebuilt image from the Github Container Registry. +``` +# Uncomment lines to use locally built version +# docker_build('ghcr.io/finos/traderx/database', './../../database/.') +# docker_build('ghcr.io/finos/traderx/account-service', './../../account-service/.') +# docker_build('ghcr.io/finos/traderx/people-service', './../../people-service/.') +docker_build('ghcr.io/finos/traderx/position-service', './../../position-service/.') +# docker_build('ghcr.io/finos/traderx/reference-data', './../../reference-data/.') +# docker_build('ghcr.io/finos/traderx/trade-feed', './../../trade-feed/.') +# docker_build('ghcr.io/finos/traderx/trade-processor', './../../trade-processor/.') +# docker_build('ghcr.io/finos/traderx/trade-service', './../../trade-service/.') +# docker_build('ghcr.io/finos/traderx/web-front-end-angular', './../../web-front-end/angular/.') +yaml = kustomize(('./traderx')) +print(yaml) +k8s_yaml(yaml) +``` +### Clean up +Simply stop tilt and then run `tilt down` # Getting Involved diff --git a/gitops/base/trade-service/deployment.yaml b/gitops/base/trade-service/deployment.yaml index 25ae1358..222d9ab7 100644 --- a/gitops/base/trade-service/deployment.yaml +++ b/gitops/base/trade-service/deployment.yaml @@ -28,7 +28,7 @@ spec: value: reference-data - name: TRADE_FEED_HOST value: trade-feed - image: ghcr.io/finos/traderx/trade-service:latest + image: ghcr.io/finos/traderx/trade-service imagePullPolicy: IfNotPresent name: trade-service ports: diff --git a/gitops/base/web-front-end/deployment.yaml b/gitops/base/web-front-end/deployment.yaml index 766fdf77..f33f112c 100644 --- a/gitops/base/web-front-end/deployment.yaml +++ b/gitops/base/web-front-end/deployment.yaml @@ -17,7 +17,7 @@ spec: app: web-front-end-angular spec: containers: - - image: ghcr.io/finos/traderx/web-front-end-angular:latest + - image: ghcr.io/finos/traderx/web-front-end-angular imagePullPolicy: IfNotPresent name: web-front-end-angular env: