Skip to content

Commit

Permalink
feat: Add ElectricityMaps support (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
rossf7 authored Sep 14, 2023
1 parent fbcaf3b commit b8cea44
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 15 deletions.
45 changes: 38 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,54 @@ This repo provides a data exporter by which Kubernetes operators can leverage th
## Installation

We provide a helm chart to help install the exporter. Note that this data exporter ONLY retrieves the carbon intensity data from
[WattTime](https://www.watttime.org/). You need to get the **authentication ID/Password** from WattTime organization before using the exporter.
[WattTime](https://www.watttime.org/) OR [Electricity Maps](https://www.electricitymaps.com/).

### WattTime

You need to get the **authentication ID/Password** from WattTime organization before using the exporter.

```bash
export WT_USERNAME=XXXX # WattTime auth info.
export WT_PASSWORD=YYYY
export REGION=westus # The region where the AKS cluster locates.

helm del carbon-intensity-exporter
helm install carbon-intensity-exporter \
--set carbonDataExporter.region=$REGION \
--set wattTime.username=$WT_USERNAME \
--set wattTime.password=$WT_PASSWORD \
./charts/carbon-intensity-exporter
```

### Electricity Maps

You need to get an **API token** from Electricity Maps before using the exporter.
You can check the name of the API token HTTP header to use and the base URL in
the Electricity Maps API portal.

```bash
export WTUSERNAME=XXXX # WattTime auth info.
export WTPASSWORD=YYYY
export EM_API_TOKEN=XXXX # Electricity Maps API token.
export EM_API_TOKEN_HEADER=auth-token # Electricity Maps API token HTTP header.
export EM_BASE_URL=https://api.electricitymap.org/v3/
export PROVIDER=ElectricityMaps
export REGION=westus # The region where the AKS cluster locates.

helm del carbon-intensity-exporter
helm install carbon-intensity-exporter \
--set carbonDataExporter.region=$REGION \
--set apiServer.username=$WTUSERNAME \
--set apiServer.password=$WTPASSWORD \
--set providerName=$PROVIDER \
--set electricityMaps.apiToken=$EM_API_TOKEN \
--set electricityMaps.apiTokenHeader=$EM_API_TOKEN_HEADER \
--set electricityMaps.baseURL=$EM_BASE_URL \
./charts/carbon-intensity-exporter
```

## View carbon intensity data

You should be able to see one exporter Pod running in the `kube-system` namespace.
```bash
$ kubectl get pod -n kube-system | grep carbon-e2e-carbon-intensity-exporter
$ carbon-e2e-carbon-intensity-exporter-XXXXXXX-XXXXX 2/2 Running 0 3m25s
$ kubectl get pod -n kube-system | grep carbon-intensity-exporter
$ carbon-intensity-exporter-XXXXXXX-XXXXX 2/2 Running 0 3m25s
```

You should also see one configmap `carbon-intensity` is created in the `kube-system` namespace.
Expand Down
44 changes: 40 additions & 4 deletions charts/carbon-intensity-exporter/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,46 @@ spec:
- name: {{ .Values.apiServer.name }}
image: "{{ .Values.apiServer.image.repository }}:{{ .Values.apiServer.image.tag }}"
imagePullPolicy: {{ .Values.apiServer.image.pullPolicy }}
command:
- sh
- -c
- cd /app && sed "s/username/{{ .Values.apiServer.username }}/" -i appsettings.json && sed "s/password/{{ .Values.apiServer.password }}/" -i appsettings.json && dotnet CarbonAware.WebApi.dll
env:
- name: DataSources__EmissionsDataSource
value: {{ .Values.providerName }}
- name: DataSources__ForecastDataSource
value: {{ .Values.providerName }}
{{- if eq .Values.providerName "ElectricityMaps" }}
- name: DataSources__Configurations__ElectricityMaps__Type
value: {{ .Values.providerName }}
- name: DataSources__Configurations__ElectricityMaps__BaseURL
value: {{ .Values.electricityMaps.baseURL | trimSuffix "/" }}/
- name: DataSources__Configurations__ElectricityMaps__APITokenHeader
value: {{ .Values.electricityMaps.apiTokenHeader }}
- name: DataSources__Configurations__ElectricityMaps__APIToken
valueFrom:
secretKeyRef:
name: {{ include "carbon-intensity-exporter.fullname" . }}
key: apiToken
{{- end }}
{{- if eq .Values.providerName "WattTime" }}
- name: DataSources__Configurations__WattTime__Type
value: {{ .Values.providerName }}
- name: DataSources__Configurations__WattTime__BaseURL
value: {{ .Values.wattTime.baseURL | trimSuffix "/" }}/
- name: DataSources__Configurations__WattTime__Proxy__useProxy
value: "false"
- name: DataSources__Configurations__WattTime__Username
valueFrom:
secretKeyRef:
name: {{ include "carbon-intensity-exporter.fullname" . }}
key: username
- name: DataSources__Configurations__WattTime__Password
valueFrom:
secretKeyRef:
name: {{ include "carbon-intensity-exporter.fullname" . }}
key: password
{{- end }}
- name: LocationDataSourcesConfiguration__LocationSourceFiles__DataFileLocation
value: {{ .Values.apiServer.dataFileLocation }}
- name: Urls
value: {{ .Values.apiServer.urls }}
ports:
- name: api-server-port
containerPort: 7031
Expand Down
21 changes: 21 additions & 0 deletions charts/carbon-intensity-exporter/templates/secret.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
apiVersion: v1
kind: Secret
metadata:
name: {{ include "carbon-intensity-exporter.fullname" . }}
namespace: {{ .Values.namespace }}
labels:
{{- include "carbon-intensity-exporter.labels" . | nindent 4 }}
data:
{{- if eq .Values.providerName "ElectricityMaps" }}
{{- if .Values.electricityMaps.apiToken }}
apiToken: {{ .Values.electricityMaps.apiToken | b64enc | quote }}
{{- end }}
{{- end }}
{{- if eq .Values.providerName "WattTime" }}
{{- if .Values.wattTime.username }}
username: {{ .Values.wattTime.username | b64enc | quote }}
{{- end }}
{{- if .Values.wattTime.password }}
password: {{ .Values.wattTime.password | b64enc | quote }}
{{- end }}
{{- end }}
19 changes: 16 additions & 3 deletions charts/carbon-intensity-exporter/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,35 @@ namespace: kube-system

apiServer:
name: api-server
username: username
password: password
image:
repository: ghcr.io/azure/kubernetes-carbon-intensity-exporter/server
pullPolicy: IfNotPresent
tag: "0.1.0"
dataFileLocation: azure-regions.json
urls: http://0.0.0.0:7031;http://0.0.0.0:80;
carbonDataExporter:
name: carbon-data-exporter
image:
repository: ghcr.io/azure/kubernetes-carbon-intensity-exporter/exporter
pullPolicy: Always
pullPolicy: IfNotPresent
tag: "0.1.0"
configmapName: carbon-intensity
patrolInterval: 12h
region: westus

# One of either WattTime or ElectricityMaps
providerName: WattTime

electricityMaps:
apiToken: apiToken
apiTokenHeader: auth-token
baseURL: https://api.electricitymap.org/v3/

wattTime:
baseURL: https://api2.watttime.org/v2/
username: username
password: password

service:
type: ClusterIP
port: 80
Expand Down
2 changes: 1 addition & 1 deletion docker/server/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env
WORKDIR /app

# Copy everything from source
RUN git clone https://github.com/helayoty/carbon-aware-sdk.git && cd carbon-aware-sdk && git checkout 55d75c7
RUN git clone https://github.com/Green-Software-Foundation/carbon-aware-sdk.git && cd carbon-aware-sdk
# Use implicit restore to build and publish
RUN dotnet publish carbon-aware-sdk/src/CarbonAware.WebApi/src/CarbonAware.WebApi.csproj -c Release -o publish
# Generate OpenAPI spec
Expand Down

0 comments on commit b8cea44

Please sign in to comment.