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

skeleton for proposal3 #150

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions .github/workflows/deploy-app-to-cluster.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
name: CI/CD Pipeline with Kubernetes Deployment

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
build-and-test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: 1.21

- name: Install dependencies
run: go mod tidy

- name: Run tests
run: go test app/./... -v

- name: Build application
run: go build -o sustainability-tool

- name: Build Docker image
run: |
docker build -t ghcr.io/${{ github.repository_owner }}/sustainability-tool:${{ github.sha }} .

- name: Push Docker image
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- run: |
docker push ghcr.io/${{ github.repository_owner }}/sustainability-tool:${{ github.sha }}

deploy:
needs: build-and-test
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up kubectl
uses: azure/setup-kubectl@v3
with:
version: 'v1.28.2' # Use the desired kubectl version

- name: Authenticate with Kubernetes cluster
run: |
echo "${{ secrets.KUBECONFIG }}" > kubeconfig
export KUBECONFIG=kubeconfig

- name: Update Kubernetes Deployment
run: |
kubectl set image deployment/sustainability-tool \
sustainability-tool=ghcr.io/${{ github.repository_owner }}/sustainability-tool:${{ github.sha }}
20 changes: 20 additions & 0 deletions app/collect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

type Metric struct {
Name string
Value float64
}

func CollectMetrics() ([]Metric, error) {
// Fetch metrics from Promehteus:
// container_cpu_usage_seconds_total
// container_memory_rss
// container_memory_working_set_bytes
// kepler_container_joules_total
metrics := []Metric{
// {"EnergyConsumption", energy},
// {"CarbonIntensity", ci},
// other metrics from prometheus
}
return metrics, nil
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AntonioDiTuri Thank you for creating this PR. It was v helpful when I was looking at dagger in #154

For now could we focus on collecting the metrics and pause looking at deployment?

If we use dagger we could run the pipeline as a pod in the cluster and access prometheus via its service https://docs.dagger.io/integrations/kubernetes

@ChrisChinchilla would be great to get your thoughts on that

cc @nikimanoledaki @locomundo

9 changes: 9 additions & 0 deletions app/compute.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

func ComputeSCI(metrics []Metric) (float64, error) {
var total float64
for _, metric := range metrics {
total += metric.Value
}
return total / float64(len(metrics)), nil
}
27 changes: 27 additions & 0 deletions app/example-deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: sustainability-tool
namespace: default
spec:
replicas: 2
selector:
matchLabels:
app: sustainability-tool
template:
metadata:
labels:
app: sustainability-tool
spec:
containers:
- name: sustainability-tool
image: ghcr.io/<YOUR_ORG>/sustainability-tool:latest
ports:
- containerPort: 8080
resources:
limits:
memory: "128Mi"
cpu: "500m"
requests:
memory: "64Mi"
cpu: "250m"
27 changes: 27 additions & 0 deletions app/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"log"
)

func main() {
// Step 1: Collect metrics
metricsData, err := CollectMetrics()
if err != nil {
log.Fatalf("Error collecting metrics: %v", err)
}

// Step 2: Compute SCI
sciValue, err := ComputeSCI(metricsData)
if err != nil {
log.Fatalf("Error computing SCI: %v", err)
}

// Step 3: Store results
err = StoreResults(sciValue)
if err != nil {
log.Fatalf("Error storing results: %v", err)
}

log.Println("Sustainability metrics reporting completed successfully.")
}
9 changes: 9 additions & 0 deletions app/storage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

import "fmt"

func StoreResults(sci float64) error {
// recieve the json in input
fmt.Printf("SCI stored: %f\n", sci)
return nil
}
Loading