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

Dapr invoke hr api from the slack Api #13

Merged
merged 13 commits into from
Apr 19, 2021
Merged
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ repo := ghcr.io/redbdager

.PHONY: ci
ci: ## sets tag for deployment
cd ./manifests/$(service) \
&& kustomize edit set image $(repo)/$(service)=$(repo)/$(service)@$(digest)
cd ./manifests/overlays/production \
&& kustomize edit set image $(service)=$(repo)/$(service)@$(digest)

.PHONY: ci-finish
ci-finish: ## prepares for deployment
Expand Down
51 changes: 50 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,55 @@ There are three steps to the CI workflow:
doesn't update any services, only manifests, so the push from the `deploy`
job will never run another `deploy` job.

## WIP: Deploy Slack and Hr service locally

Use the 'docker-desktop' local k8s cluster.

Initialize Dapr on the cluster (https://docs.dapr.io/operations/hosting/kubernetes/kubernetes-deploy/)

```bash
dapr init -k
```

Create the things. The version of Kustomize inside kubectl is quite old, so we need to use kustomize directly. [Install](https://kubectl.docs.kubernetes.io/installation/kustomize/) with `brew install kustomize`. This works when using the "docker-desktop" k8s cluster, if you are using minikube you will probably need to [set the docker daemon](https://stackoverflow.com/questions/42564058/how-to-use-local-docker-images-with-minikube)

```bash
docker build --progress=plain -t slack:latest ./lib/slack
docker build --progress=plain -t hr:latest ./lib/hr
kustomize build "./manifests/overlays/development/" | kubectl apply -f -
```

Check things look ok

```bash
kubectl get pods --namespace slack
```

Check the ping endpoint works (http://localhost/ping)

Install the nginx ingress controller

```bash
helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
helm repo update
helm install nginx-ingress ingress-nginx/ingress-nginx -f ./manifests/ingress-controller.yaml -n default
```

Install the ingress rules. These forward to the dapr sidecar of the ingress controller above, which is called "nginx-ingress-dapr" ("-dapr" is added to the names of things to form the sidecar name)

```bash
kubectl apply -f ./manifests/ingress.yaml
```

Call the slack app via dapr. The external IP address of the ingress controller can be found from `kubectl get services`. **slack**.slack is from `dapr.io/app-id` in deployment.yaml and slack.**slack** is the namespace that the deployment is in.

```
curl http://ip-address/v1.0/invoke/slack.slack/method/ping
```

At this point the slack app is still available directly on the external ip address from the LoadBalancer service that it uses, but the yaml can be edited to change the service to ClusterIP to avoid that.


## WIP: Deploy slack service to kubernetes

Create a kubernetes cluster somewhere
Expand All @@ -67,7 +116,7 @@ Initialize Dapr on the cluster (https://docs.dapr.io/operations/hosting/kubernet
dapr init -k
```

Create the slack Node App.
Create the things

```bash
kubectl apply -k manifests/slack/
Expand Down
7 changes: 7 additions & 0 deletions lib/hr/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ async fn run() -> Result<()> {
Ok(resp)
});

app.at("/ping").get(|_| async move {
let mut resp = Response::new(StatusCode::Ok);
resp.set_body("pong");
resp.set_content_type(mime::PLAIN);
Ok(resp)
});

app.listen(host_and_port).await?;

Ok(())
Expand Down
4 changes: 3 additions & 1 deletion lib/slack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
"build": "tsc"
},
"dependencies": {
"express": "^4.17.1"
"express": "^4.17.1",
"node-fetch": "^2.6.1"
},
"devDependencies": {
"@types/express": "^4.17.11",
"@types/node": "^14.14.37",
"@types/node-fetch": "^2.5.10",
"ts-node": "^9.1.1",
"typescript": "^4.2.3"
}
Expand Down
11 changes: 9 additions & 2 deletions lib/slack/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import express from 'express'
import fetch from 'node-fetch';

const app = express()

// This environment variables are injected automatically into the container by dapr
const daprPort = process.env.DAPR_HTTP_PORT;

const port = process.env.PORT || 3000

app.get('/ping', (_req, res) => {
res.send('pong')
app.get('/ping', async (_req, res) => {
const response = await fetch(`http://localhost:${daprPort}/v1.0/invoke/hr.hr/method/ping`)
const body = await response.text()
res.send("Respose from hr api: " + body)

})

app.listen(port, () => console.log(`http://localhost:${port}`))
51 changes: 51 additions & 0 deletions lib/slack/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@
resolved "https://registry.yarnpkg.com/@types/mime/-/mime-1.3.2.tgz#93e25bf9ee75fe0fd80b594bc4feb0e862111b5a"
integrity sha512-YATxVxgRqNH6nHEIsvg6k2Boc1JHI9ZbH5iWFFv/MTkchz3b1ieGDa5T0a9RznNdI0KhVbdbWSN+KWWrQZRxTw==

"@types/node-fetch@^2.5.10":
version "2.5.10"
resolved "https://registry.yarnpkg.com/@types/node-fetch/-/node-fetch-2.5.10.tgz#9b4d4a0425562f9fcea70b12cb3fcdd946ca8132"
integrity sha512-IpkX0AasN44hgEad0gEF/V6EgR5n69VEqPEgnmoM8GsIGro3PowbWs4tR6IhxUTyPLpOn+fiGG6nrQhcmoCuIQ==
dependencies:
"@types/node" "*"
form-data "^3.0.0"

"@types/node@*", "@types/node@^14.14.37":
version "14.14.37"
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.14.37.tgz#a3dd8da4eb84a996c36e331df98d82abd76b516e"
Expand Down Expand Up @@ -82,6 +90,11 @@ array-flatten@1.1.1:
resolved "https://registry.yarnpkg.com/array-flatten/-/array-flatten-1.1.1.tgz#9a5f699051b1e7073328f2a008968b64ea2955d2"
integrity sha1-ml9pkFGx5wczKPKgCJaLZOopVdI=

asynckit@^0.4.0:
version "0.4.0"
resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79"
integrity sha1-x57Zf380y48robyXkLzDZkdLS3k=

body-parser@1.19.0:
version "1.19.0"
resolved "https://registry.yarnpkg.com/body-parser/-/body-parser-1.19.0.tgz#96b2709e57c9c4e09a6fd66a8fd979844f69f08a"
Expand All @@ -108,6 +121,13 @@ bytes@3.1.0:
resolved "https://registry.yarnpkg.com/bytes/-/bytes-3.1.0.tgz#f6cf7933a360e0588fa9fde85651cdc7f805d1f6"
integrity sha512-zauLjrfCG+xvoyaqLoV8bLVXXNGC4JqlxFCutSDWA6fJrTo2ZuvLYTqZ7aHBLZSMOopbzwv8f+wZcVzfVTI2Dg==

combined-stream@^1.0.8:
version "1.0.8"
resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f"
integrity sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==
dependencies:
delayed-stream "~1.0.0"

content-disposition@0.5.3:
version "0.5.3"
resolved "https://registry.yarnpkg.com/content-disposition/-/content-disposition-0.5.3.tgz#e130caf7e7279087c5616c2007d0485698984fbd"
Expand Down Expand Up @@ -142,6 +162,11 @@ debug@2.6.9:
dependencies:
ms "2.0.0"

delayed-stream@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619"
integrity sha1-3zrhmayt+31ECqrgsp4icrJOxhk=

depd@~1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/depd/-/depd-1.1.2.tgz#9bcd52e14c097763e749b274c4346ed2e560b5a9"
Expand Down Expand Up @@ -226,6 +251,15 @@ finalhandler@~1.1.2:
statuses "~1.5.0"
unpipe "~1.0.0"

form-data@^3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/form-data/-/form-data-3.0.1.tgz#ebd53791b78356a99af9a300d4282c4d5eb9755f"
integrity sha512-RHkBKtLWUVwd7SqRIvCZMEvAMoGUp0XU+seQiZejj0COz3RI3hWP4sCv3gZWWLjJTd7rGwcsF5eKZGii0r/hbg==
dependencies:
asynckit "^0.4.0"
combined-stream "^1.0.8"
mime-types "^2.1.12"

forwarded@~0.1.2:
version "0.1.2"
resolved "https://registry.yarnpkg.com/forwarded/-/forwarded-0.1.2.tgz#98c23dab1175657b8c0573e8ceccd91b0ff18c84"
Expand Down Expand Up @@ -305,6 +339,18 @@ mime-db@1.46.0:
resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.46.0.tgz#6267748a7f799594de3cbc8cde91def349661cee"
integrity sha512-svXaP8UQRZ5K7or+ZmfNhg2xX3yKDMUzqadsSqi4NCH/KomcH75MAMYAGVlvXn4+b/xOPhS3I2uHKRUzvjY7BQ==

mime-db@1.47.0:
version "1.47.0"
resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.47.0.tgz#8cb313e59965d3c05cfbf898915a267af46a335c"
integrity sha512-QBmA/G2y+IfeS4oktet3qRZ+P5kPhCKRXxXnQEudYqUaEioAU1/Lq2us3D/t1Jfo4hE9REQPrbB7K5sOczJVIw==

mime-types@^2.1.12:
version "2.1.30"
resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.30.tgz#6e7be8b4c479825f85ed6326695db73f9305d62d"
integrity sha512-crmjA4bLtR8m9qLpHvgxSChT+XoSlZi8J4n/aIdn3z92e/U47Z0V/yl+Wh9W046GgFVAmoNR/fmdbZYcSSIUeg==
dependencies:
mime-db "1.47.0"

mime-types@~2.1.24:
version "2.1.29"
resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.29.tgz#1d4ab77da64b91f5f72489df29236563754bb1b2"
Expand Down Expand Up @@ -332,6 +378,11 @@ negotiator@0.6.2:
resolved "https://registry.yarnpkg.com/negotiator/-/negotiator-0.6.2.tgz#feacf7ccf525a77ae9634436a64883ffeca346fb"
integrity sha512-hZXc7K2e+PgeI1eDBe/10Ard4ekbfrrqG8Ep+8Jmf4JID2bNg7NvCPOZN+kfF574pFQI7mum2AUqDidoKqcTOw==

node-fetch@^2.6.1:
version "2.6.1"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.1.tgz#045bd323631f76ed2e2b55573394416b639a0052"
integrity sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==

on-finished@~2.3.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/on-finished/-/on-finished-2.3.0.tgz#20f1336481b083cd75337992a16971aa2d906947"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ spec:
- name: github-container-registry
containers:
- name: hr
image: ghcr.io/redbadger/hr
imagePullPolicy: Never
image: hr
imagePullPolicy: IfNotPresent
ports:
- containerPort: 3000
resources:
Expand Down
10 changes: 10 additions & 0 deletions manifests/bases/hr/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

namespace: hr

resources:
- namespace.yaml
- service-account.yaml
- deployment.yaml
- service.yaml
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ spec:
- name: github-container-registry
containers:
- name: slack
image: ghcr.io/redbadger/slack
image: slack
imagePullPolicy: IfNotPresent
ports:
- containerPort: 3000
resources:
Expand Down
10 changes: 10 additions & 0 deletions manifests/bases/slack/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

namespace: slack

resources:
- namespace.yaml
- service-account.yaml
- deployment.yaml
- service.yaml
File renamed without changes.
File renamed without changes.
18 changes: 0 additions & 18 deletions manifests/hr/kustomization.yaml

This file was deleted.

19 changes: 19 additions & 0 deletions manifests/overlays/development/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
- ../../bases/hr
- ../../bases/slack

images:
- name: hr
newName: hr
newTag: latest
- name: slack
newName: slack
newTag: latest

patches:
- path: patch.yaml
target:
kind: Deployment
5 changes: 5 additions & 0 deletions manifests/overlays/development/patch.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# This assumes that there is only one container in the containers list
# We could add more indexes if required
- op: add
path: /spec/template/spec/containers/0/imagePullPolicy
value: Never
14 changes: 14 additions & 0 deletions manifests/overlays/production/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

images:
- digest: sha256:9584fbcb56f312991c0118fba11ec7dbd66c1fee307f7eca1dfccf37283e07d7
name: hr
newName: ghcr.io/redbdager/hr
- digest: sha256:63f6b4115727c2e921a9818bad0f1080754344d63f129323323aa457a34ce473
name: slack
newName: ghcr.io/redbdager/slack
resources:
- ../../bases/hr
- ../../bases/slack

18 changes: 0 additions & 18 deletions manifests/slack/kustomization.yaml

This file was deleted.