Skip to content

Commit

Permalink
bug: fix mock carbon forecast
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldotyu committed Apr 12, 2023
1 parent 41407e6 commit bdc6d20
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 338 deletions.
6 changes: 0 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,6 @@ kind-create: kind ## Create a KIND cluster if it doesn't already exist
$(KIND) create cluster; \
fi

.PHONY: kind-delete
kind-delete: kind ## Delete KIND cluster.
if [ `$(KIND) get clusters | wc -l` -gt 0 ]; then \
$(KIND) delete cluster; \
fi

.PHONY: kind-deploy
kind-deploy: manifests kustomize docker-build kind-create ## Deploy controller to the K8s cluster specified in ~/.kube/config.
$(KIND) load docker-image ${IMG}
Expand Down
77 changes: 45 additions & 32 deletions controllers/carbon_forecast_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package controllers
import (
"context"
"encoding/json"
"fmt"
"math/rand"
"time"

Expand Down Expand Up @@ -54,7 +53,6 @@ func (c *CarbonForecastConfigMapFetcher) Fetch(ctx context.Context) ([]CarbonFor
var cf []CarbonForecast
err = json.Unmarshal([]byte(cm.BinaryData[c.ConfigMapKey]), &cf)
if err != nil {
fmt.Println("got carbon forecast err yo")
return nil, err
}

Expand All @@ -73,40 +71,55 @@ func (c *CarbonForecastMockConfigMapFetcher) Fetch(ctx context.Context) ([]Carbo
return c.CarbonForecast, nil
}

// create a new dynamically sized array of CarbonForecast
c.CarbonForecast = make([]CarbonForecast, 0)

// for 3 hours ago and 7 days in the future loop at each 5 min increment and add a carbon intensity value
for i := -3; i < 7*24*12; i++ {
// generate a random number between 529 and 580
rand.Seed(time.Now().UnixNano())
c.CarbonForecast = append(c.CarbonForecast, CarbonForecast{
Timestamp: time.Now().UTC().Add(time.Duration(i*5) * time.Minute),
Value: rand.Float64()*51 + 529,
Duration: 5,
})
}
// if the configmap does not exist, create it
configMapName := "mock-carbon-intensity"
configMapNamespace := "kube-system"
configMapKey := "data"

// marshal the carbon forecast into byte array
forecast, err := json.Marshal(c.CarbonForecast)
cm := &corev1.ConfigMap{}
err := c.Client.Get(ctx, types.NamespacedName{Name: configMapName, Namespace: configMapNamespace}, cm)
if err != nil {
return nil, err
}
// create a new dynamically sized array of CarbonForecast
c.CarbonForecast = make([]CarbonForecast, 0)

// for 24 hours in the past and 7 days in the future loop at each 5 min increment and add a carbon intensity value
for i := -24; i < 7*24*12; i++ {
// generate a random number between 529 and 580
rand.Seed(time.Now().UnixNano())
c.CarbonForecast = append(c.CarbonForecast, CarbonForecast{
Timestamp: time.Now().UTC().Add(time.Duration(i*5) * time.Minute),
Value: rand.Float64()*51 + 529,
Duration: 5,
})
}

// create a configmap and pass carbon forecast as binary data
cm := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "carbon-intensity",
Namespace: "kube-system",
},
BinaryData: map[string][]byte{
"data": forecast,
},
}
// marshal the carbon forecast into byte array
forecast, err := json.Marshal(c.CarbonForecast)
if err != nil {
return nil, err
}

// create or update the configmap
if err = c.Client.Create(ctx, cm); err != nil {
if err = c.Client.Update(ctx, cm); err != nil {
// create a configmap and pass carbon forecast as binary data
cm = &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: configMapName,
Namespace: configMapNamespace,
},
BinaryData: map[string][]byte{
configMapKey: forecast,
},
}

// create or update the configmap
if err = c.Client.Create(ctx, cm); err != nil {
if err = c.Client.Update(ctx, cm); err != nil {
return nil, err
}
}
} else {
// unmarshal the configmap data into a map
err = json.Unmarshal([]byte(cm.BinaryData[configMapKey]), &c.CarbonForecast)
if err != nil {
return nil, err
}
}
Expand Down
9 changes: 3 additions & 6 deletions controllers/carbonawarekedascaler_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var _ = Describe("scenarios for the carbon aware KEDA Scaler", func() {
Context("the controller should be able to mocked data for demo purposes", func() {
When("carbonawarekedascaler is set to use mocked data", func() {
const (
configMapName = "carbon-intensity"
configMapName = "mock-carbon-intensity"
configMapNamespace = "kube-system"
configMapKey = "data"
)
Expand All @@ -52,7 +52,7 @@ var _ = Describe("scenarios for the carbon aware KEDA Scaler", func() {
Expect(cf).ShouldNot(BeNil())

cm := &corev1.ConfigMap{}
By("Confirming the ConfigMap named carbon-intensity is found")
By("Confirming the ConfigMap named mock-carbon-intensity is found")
Expect(k8sClient.Get(ctx, client.ObjectKey{Name: configMapName, Namespace: configMapNamespace}, cm)).Should(Succeed())
})
})
Expand Down Expand Up @@ -132,9 +132,6 @@ var _ = Describe("scenarios for the carbon aware KEDA Scaler", func() {
carbonAwareKedaScalerName = "test-carbonawarekedascaler"
carbonAwareKedaScalerNamespace = "default"
carbonAwareKedaScalerKedaTarget = "scaledobjects.keda.sh"
configMapName = "carbon-intensity"
configMapNamespace = "kube-system"
configMapKey = "data"
timeout = time.Second * 5
interval = time.Millisecond * 250
)
Expand Down Expand Up @@ -257,7 +254,7 @@ var _ = Describe("scenarios for the carbon aware KEDA Scaler", func() {

When("the carbon intensity is within a configured range", func() {
const (
testConfigMapName = "mock-carbon-intensity"
testConfigMapName = "another-mock-carbon-intensity"
testConfigMapNamespace = "kube-system"
testConfigMapKey = "data"
)
Expand Down
Loading

0 comments on commit bdc6d20

Please sign in to comment.