From bdc6d20d62d6a2e5f773b06b4151c82cfd7087c7 Mon Sep 17 00:00:00 2001 From: Paul Yu Date: Wed, 12 Apr 2023 09:19:42 -0700 Subject: [PATCH] bug: fix mock carbon forecast --- Makefile | 6 - controllers/carbon_forecast_fetcher.go | 77 +++-- .../carbonawarekedascaler_controller_test.go | 9 +- hack/grafana/Carbon Aware KEDA-Dashboard.json | 311 +----------------- 4 files changed, 65 insertions(+), 338 deletions(-) diff --git a/Makefile b/Makefile index 24d3d87..2b09bfc 100644 --- a/Makefile +++ b/Makefile @@ -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} diff --git a/controllers/carbon_forecast_fetcher.go b/controllers/carbon_forecast_fetcher.go index 9c07ea3..ad1f135 100644 --- a/controllers/carbon_forecast_fetcher.go +++ b/controllers/carbon_forecast_fetcher.go @@ -3,7 +3,6 @@ package controllers import ( "context" "encoding/json" - "fmt" "math/rand" "time" @@ -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 } @@ -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 } } diff --git a/controllers/carbonawarekedascaler_controller_test.go b/controllers/carbonawarekedascaler_controller_test.go index 0f561ed..cfd783e 100644 --- a/controllers/carbonawarekedascaler_controller_test.go +++ b/controllers/carbonawarekedascaler_controller_test.go @@ -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" ) @@ -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()) }) }) @@ -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 ) @@ -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" ) diff --git a/hack/grafana/Carbon Aware KEDA-Dashboard.json b/hack/grafana/Carbon Aware KEDA-Dashboard.json index 96177f8..78207ed 100644 --- a/hack/grafana/Carbon Aware KEDA-Dashboard.json +++ b/hack/grafana/Carbon Aware KEDA-Dashboard.json @@ -11,12 +11,6 @@ ], "__elements": {}, "__requires": [ - { - "type": "panel", - "id": "gauge", - "name": "Gauge", - "version": "" - }, { "type": "grafana", "id": "grafana", @@ -82,7 +76,7 @@ "axisPlacement": "auto", "barAlignment": 0, "drawStyle": "line", - "fillOpacity": 0, + "fillOpacity": 25, "gradientMode": "none", "hideFrom": { "legend": false, @@ -90,6 +84,9 @@ "viz": false }, "lineInterpolation": "linear", + "lineStyle": { + "fill": "solid" + }, "lineWidth": 1, "pointSize": 5, "scaleDistribution": { @@ -123,88 +120,24 @@ "overrides": [] }, "gridPos": { - "h": 11, - "w": 12, + "h": 18, + "w": 24, "x": 0, "y": 0 }, - "id": 1, + "id": 2, "options": { "legend": { "calcs": [], "displayMode": "list", "placement": "bottom", - "showLegend": false + "showLegend": true }, "tooltip": { "mode": "single", "sort": "none" } }, - "targets": [ - { - "datasource": { - "type": "prometheus", - "uid": "${DS_PROMETHEUS}" - }, - "editorMode": "builder", - "expr": "carbon_aware_keda_scaler_carbon_intensity{namespace=\"carbon-aware-keda-operator-system\"}", - "instant": true, - "key": "Q-e92794d8-3767-4d1f-be65-82a22caf5572-0", - "legendFormat": "__auto", - "range": true, - "refId": "A" - } - ], - "title": "Carbon Intensity", - "type": "timeseries" - }, - { - "datasource": { - "type": "prometheus", - "uid": "${DS_PROMETHEUS}" - }, - "fieldConfig": { - "defaults": { - "color": { - "mode": "thresholds" - }, - "mappings": [], - "thresholds": { - "mode": "absolute", - "steps": [ - { - "color": "green", - "value": null - }, - { - "color": "red", - "value": 80 - } - ] - } - }, - "overrides": [] - }, - "gridPos": { - "h": 8, - "w": 12, - "x": 12, - "y": 0 - }, - "id": 7, - "options": { - "orientation": "auto", - "reduceOptions": { - "calcs": [ - "lastNotNull" - ], - "fields": "", - "values": false - }, - "showThresholdLabels": false, - "showThresholdMarkers": true - }, "pluginVersion": "9.4.3", "targets": [ { @@ -213,115 +146,11 @@ "uid": "${DS_PROMETHEUS}" }, "editorMode": "builder", - "expr": "carbon_aware_keda_scaler_reconciles_total{app=\"word-processor-carbon-aware-keda-scaler\"}", - "legendFormat": "Reconcile Count", - "range": true, - "refId": "A" - }, - { - "datasource": { - "type": "prometheus", - "uid": "${DS_PROMETHEUS}" - }, - "editorMode": "builder", - "expr": "carbon_aware_keda_scaler_reconcile_errors_total{app=\"word-processor-carbon-aware-keda-scaler\"}", + "expr": "carbon_aware_keda_scaler_carbon_intensity", "hide": false, - "legendFormat": "Error Count", - "range": true, - "refId": "B" - } - ], - "title": "Reconciles", - "type": "gauge" - }, - { - "datasource": { - "type": "prometheus", - "uid": "${DS_PROMETHEUS}" - }, - "description": "Counters for number of reconciles, errors, and eco mode off states", - "fieldConfig": { - "defaults": { - "color": { - "mode": "palette-classic" - }, - "custom": { - "axisCenteredZero": false, - "axisColorMode": "text", - "axisLabel": "", - "axisPlacement": "auto", - "barAlignment": 0, - "drawStyle": "line", - "fillOpacity": 0, - "gradientMode": "none", - "hideFrom": { - "legend": false, - "tooltip": false, - "viz": false - }, - "lineInterpolation": "linear", - "lineWidth": 1, - "pointSize": 5, - "scaleDistribution": { - "type": "linear" - }, - "showPoints": "auto", - "spanNulls": false, - "stacking": { - "group": "A", - "mode": "none" - }, - "thresholdsStyle": { - "mode": "off" - } - }, - "mappings": [], - "thresholds": { - "mode": "absolute", - "steps": [ - { - "color": "green", - "value": null - }, - { - "color": "red", - "value": 80 - } - ] - } - }, - "overrides": [] - }, - "gridPos": { - "h": 14, - "w": 12, - "x": 12, - "y": 8 - }, - "id": 5, - "options": { - "legend": { - "calcs": [], - "displayMode": "list", - "placement": "bottom", - "showLegend": true - }, - "tooltip": { - "mode": "single", - "sort": "none" - } - }, - "targets": [ - { - "datasource": { - "type": "prometheus", - "uid": "${DS_PROMETHEUS}" - }, - "editorMode": "builder", - "expr": "carbon_aware_keda_scaler_eco_mode_off{code=\"1\"}", - "legendFormat": "EcoModeOff Total", + "legendFormat": "Carbon Intensity", "range": true, - "refId": "A" + "refId": "C" }, { "datasource": { @@ -329,9 +158,9 @@ "uid": "${DS_PROMETHEUS}" }, "editorMode": "builder", - "expr": "carbon_aware_keda_scaler_reconciles_total", + "expr": "carbon_aware_keda_scaler_max_replicas", "hide": false, - "legendFormat": "Reconcile Total", + "legendFormat": "Max Replicas", "range": true, "refId": "B" }, @@ -341,119 +170,13 @@ "uid": "${DS_PROMETHEUS}" }, "editorMode": "builder", - "expr": "carbon_aware_keda_scaler_reconcile_errors_total", - "format": "time_series", - "hide": false, - "legendFormat": "Error Total", - "range": true, - "refId": "C" - } - ], - "title": "Reconciles, EcoModeOff, Error", - "type": "timeseries" - }, - { - "datasource": { - "type": "prometheus", - "uid": "${DS_PROMETHEUS}" - }, - "fieldConfig": { - "defaults": { - "color": { - "mode": "palette-classic" - }, - "custom": { - "axisCenteredZero": false, - "axisColorMode": "text", - "axisLabel": "", - "axisPlacement": "auto", - "barAlignment": 0, - "drawStyle": "line", - "fillOpacity": 0, - "gradientMode": "none", - "hideFrom": { - "legend": false, - "tooltip": false, - "viz": false - }, - "lineInterpolation": "linear", - "lineWidth": 1, - "pointSize": 5, - "scaleDistribution": { - "type": "linear" - }, - "showPoints": "auto", - "spanNulls": false, - "stacking": { - "group": "A", - "mode": "none" - }, - "thresholdsStyle": { - "mode": "off" - } - }, - "mappings": [], - "thresholds": { - "mode": "absolute", - "steps": [ - { - "color": "green", - "value": null - }, - { - "color": "red", - "value": 80 - } - ] - } - }, - "overrides": [] - }, - "gridPos": { - "h": 11, - "w": 12, - "x": 0, - "y": 11 - }, - "id": 3, - "options": { - "legend": { - "calcs": [], - "displayMode": "list", - "placement": "bottom", - "showLegend": true - }, - "tooltip": { - "mode": "single", - "sort": "none" - } - }, - "targets": [ - { - "datasource": { - "type": "prometheus", - "uid": "${DS_PROMETHEUS}" - }, - "editorMode": "builder", - "expr": "carbon_aware_keda_scaler_max_replicas{app=\"word-processor-carbon-aware-keda-scaler\"}", - "legendFormat": "Carbon Aware Max Replicas", - "range": true, - "refId": "A" - }, - { - "datasource": { - "type": "prometheus", - "uid": "${DS_PROMETHEUS}" - }, - "editorMode": "builder", - "expr": "carbon_aware_keda_scaler_default_max_replicas{app=\"word-processor-carbon-aware-keda-scaler\"}", - "hide": false, + "expr": "carbon_aware_keda_scaler_default_max_replicas", "legendFormat": "Default Max Replicas", "range": true, - "refId": "D" + "refId": "A" } ], - "title": "Replicas", + "title": "Carbon Aware", "type": "timeseries" } ], @@ -473,6 +196,6 @@ "timezone": "", "title": "Carbon Aware KEDA", "uid": "192F-9B4z", - "version": 6, + "version": 3, "weekStart": "" } \ No newline at end of file