Skip to content

Commit 4f5dbd2

Browse files
authored
Add example for 'podman play kube' (#340)
* Add example for 'podman play kube' Signed-off-by: Yasumasa Suenaga <[email protected]> * Update Swagger JSON URL Signed-off-by: Yasumasa Suenaga <[email protected]> --------- Signed-off-by: Yasumasa Suenaga <[email protected]>
1 parent 1106968 commit 4f5dbd2

File tree

5 files changed

+188
-0
lines changed

5 files changed

+188
-0
lines changed

samples/casdk-demo/README.md

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Carbon Aware SDK demonstration on Podman
2+
3+
This folder contains an example for Carbon Aware SDK and Swagger UI for the SDK. The user can demonstrate Carbon Aware SDK via Swagger UI with just one command.
4+
5+
This demonstration uses `podman play kube` to deploy apps like a Kubernetes application. Deployment is defined in [demo.yaml](demo.yaml).
6+
7+
## Requirements
8+
9+
- Podman
10+
11+
## Ports to open
12+
13+
Podman creates virtual network, then all of containers in the deployment would be located flatten. So Each containers can access each other as a `localhost`. We need to consider TCP port:
14+
15+
* 8080: Carbon Aware SDK
16+
* 8081: Swagger UI
17+
* 8082: NGINX (for reverse proxy)
18+
19+
NGINX is a reverse proxy to both Carbon Aware SDK and Swagger UI. To avoid CORS error, you can access Swagger UI via NGINX ( http://localhost:8082/swagger-ui/ ).
20+
21+
## Reverse proxy rule
22+
23+
See [nginx-rp.conf](nginx-rp.conf)
24+
25+
/ -> Carbon Aware SDK
26+
/swagger.yaml -> OpenAPI document provided by Carbon Aware SDK
27+
/swagger-ui/ -> Swagger UI for Carbon Aware SDK
28+
29+
## How to run
30+
31+
1. Set environment variables prefixed with `CASDK_`: e.g. `CASDK_DataSources__EmissionsDataSource`
32+
33+
2. Start demonstration
34+
35+
```
36+
./demo.sh start
37+
```
38+
39+
:::warning
40+
41+
* [demo.sh](demo.sh) would create `/tmp/casdk-config.yaml` which may contain credentials (e.g. API token of backend service). This file would be removed by `./demo.sh stop`.
42+
* [demo.sh](demo.sh) would change security context of [nginx-rp.conf](nginx-rp.conf) to `container_file_t` if SELinux is enabled. It would not recover in `./demo.sh stop`, so you need to recover manually via `restorecon` if need.
43+
44+
:::
45+
46+
3. Access endpoints (e.g. http://localhost:8082/swagger-ui/ )
47+
48+
4. Stop demonstration
49+
50+
```
51+
./demo.sh stop
52+
```
53+
54+
## Example
55+
56+
Run demonstration with ElectricityMapsFree datasource
57+
58+
```
59+
export CASDK_DataSources__EmissionsDataSource=ElectricityMapsFree
60+
export CASDK_DataSources__Configurations__ElectricityMapsFree__Type=ElectricityMapsFree
61+
export CASDK_DataSources__Configurations__ElectricityMapsFree__token=YOUR_SECRET_TOKEN
62+
63+
./demo.sh start
64+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
apiVersion: v1
2+
kind: ConfigMap
3+
metadata:
4+
name: casdk-config
5+
data:
6+
# Environment variables would be added by demo.sh

samples/casdk-demo/demo.sh

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/bin/sh
2+
3+
THISFILE=$0
4+
BASEDIR=$(dirname $THISFILE)
5+
SUBCMD=$1
6+
CONFIGMAP=/tmp/casdk-config.yaml
7+
8+
help_and_exit () {
9+
echo "Usage:"
10+
echo " $THISFILE [start|stop]"
11+
12+
rm -f $CONFIGMAP
13+
exit 1
14+
}
15+
16+
start () {
17+
# Create ConfigMap for envvars in CASDK container
18+
cp -f $BASEDIR/casdk-config.yaml.template $CONFIGMAP
19+
for CASDK_ENV in `env | grep CASDK_`; do
20+
KEY=`echo $CASDK_ENV | cut -d '=' -f 1 | sed -e 's/^CASDK_//'`
21+
VALUE=`echo $CASDK_ENV | cut -d '=' -f '2'`
22+
23+
echo " $KEY: $VALUE" >> $CONFIGMAP
24+
done
25+
26+
27+
# Change security context of nginx-rp.conf because it would be mounted by
28+
# NGINX container in demo.yaml.
29+
SELINUX_MODE=`getenforce 2>/dev/null`
30+
if [ "$SELINUX_MODE" = 'Enforcing' ]; then
31+
chcon -t container_file_t $BASEDIR/nginx-rp.conf
32+
fi
33+
34+
# Start Podman
35+
# Move to BASEDIR because demo.yaml should refer nginx-rp.conf in that dir.
36+
pushd $BASEDIR > /dev/null 2>&1
37+
podman play kube --configmap=$CONFIGMAP demo.yaml
38+
popd > /dev/null 2>&1
39+
}
40+
41+
stop () {
42+
podman play kube --down $BASEDIR/demo.yaml
43+
rm -f $CONFIGMAP
44+
}
45+
46+
case "$SUBCMD" in
47+
start)
48+
start;;
49+
50+
stop)
51+
stop;;
52+
53+
*)
54+
help_and_exit;;
55+
esac

samples/casdk-demo/demo.yaml

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: casdk-demo
5+
labels:
6+
app: casdk-demo
7+
spec:
8+
selector:
9+
matchLabels:
10+
app: casdk-demo
11+
template:
12+
metadata:
13+
labels:
14+
app: casdk-demo
15+
spec:
16+
containers:
17+
- name: carbon-aware-sdk
18+
image: ghcr.io/green-software-foundation/carbon-aware-sdk:pre
19+
envFrom:
20+
- configMapRef:
21+
name: casdk-config
22+
ports:
23+
- containerPort: 8080
24+
hostPort: 8080
25+
- name: swagger-ui
26+
image: swaggerapi/swagger-ui
27+
env:
28+
- name: SWAGGER_JSON_URL
29+
value: /swagger.yaml
30+
- name: PORT
31+
value: "8081"
32+
ports:
33+
- containerPort: 8081
34+
hostPort: 8081
35+
- name: nginx
36+
image: nginx
37+
ports:
38+
- containerPort: 8082
39+
hostPort: 8082
40+
volumeMounts:
41+
- name: rp-config
42+
mountPath: /etc/nginx/conf.d/default.conf
43+
volumes:
44+
- name: rp-config
45+
hostPath:
46+
path: nginx-rp.conf
47+
type: File

samples/casdk-demo/nginx-rp.conf

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
server {
2+
listen 8082;
3+
4+
location / {
5+
proxy_pass http://localhost:8080;
6+
}
7+
8+
location /swagger.yaml {
9+
proxy_pass http://localhost:8080/api/v1/swagger.yaml;
10+
}
11+
12+
location /swagger-ui/ {
13+
proxy_pass http://localhost:8081/;
14+
}
15+
16+
}

0 commit comments

Comments
 (0)