From 1ea57ba93b847cc145cff3e9b720a1cdbdf0f116 Mon Sep 17 00:00:00 2001 From: Paul Yu Date: Tue, 11 Apr 2023 13:39:18 -0700 Subject: [PATCH] refactor: make kube-prometheus optional and renaming configmap --- Makefile | 12 ++++++++++++ config/default/kustomization.yaml | 2 +- .../carbonaware_v1alpha1_carbonawarekedascaler.yaml | 2 +- controllers/carbon_forecast_fetcher.go | 8 ++++---- controllers/carbonawarekedascaler_controller_test.go | 12 ++++++------ hack/workload/carbonawarekedascaler.yaml | 12 ++++++------ 6 files changed, 30 insertions(+), 18 deletions(-) diff --git a/Makefile b/Makefile index ed74bac..24d3d87 100644 --- a/Makefile +++ b/Makefile @@ -133,9 +133,21 @@ kind-delete: kind ## Delete KIND cluster. .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} + cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG} + $(KUSTOMIZE) build config/default | kubectl apply -f - + kubectl apply -f hack/keda/keda-2.10.0.yaml + kubectl wait --for=condition=Available --timeout=600s apiservice v1beta1.external.metrics.k8s.io + kubectl apply -f hack/workload/deployment.yaml + kubectl apply -f hack/workload/scaledobject.yaml + kubectl apply -f hack/workload/carbonawarekedascaler.yaml + +.PHONY: kind-deploy-prom +kind-deploy-prom: manifests kustomize docker-build kind-create ## Deploy controller to the K8s cluster specified in ~/.kube/config. $(KIND) load docker-image ${IMG} kubectl apply --server-side -f hack/prometheus/manifests/setup kubectl wait --for condition=Established --all CustomResourceDefinition --namespace=monitoring + sed -i 's^#- ../prometheus^- ../prometheus^g' config/default/kustomization.yaml cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG} $(KUSTOMIZE) build config/default | kubectl apply -f - kubectl apply -f hack/prometheus/manifests/ diff --git a/config/default/kustomization.yaml b/config/default/kustomization.yaml index fd92ef8..801025f 100644 --- a/config/default/kustomization.yaml +++ b/config/default/kustomization.yaml @@ -22,7 +22,7 @@ bases: # [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'. 'WEBHOOK' components are required. #- ../certmanager # [PROMETHEUS] To enable prometheus monitor, uncomment all sections with 'PROMETHEUS'. -- ../prometheus +#- ../prometheus patchesStrategicMerge: # Protect the /metrics endpoint by putting it behind auth. diff --git a/config/samples/carbonaware_v1alpha1_carbonawarekedascaler.yaml b/config/samples/carbonaware_v1alpha1_carbonawarekedascaler.yaml index 673a657..3fc9398 100644 --- a/config/samples/carbonaware_v1alpha1_carbonawarekedascaler.yaml +++ b/config/samples/carbonaware_v1alpha1_carbonawarekedascaler.yaml @@ -16,7 +16,7 @@ spec: carbonIntensityForecastDataSource: # carbon intensity forecast data source mockCarbonForecast: true # [OPTIONAL] use mock carbon forecast data localConfigMap: # [OPTIONAL] use configmap for carbon forecast data - name: carbonintensity + name: carbon-intensity namespace: kube-system key: data maxReplicasByCarbonIntensity: # array of carbon intensity values in ascending order; each threshold value represents the upper limit and previous entry represents lower limit diff --git a/controllers/carbon_forecast_fetcher.go b/controllers/carbon_forecast_fetcher.go index 873657c..9c07ea3 100644 --- a/controllers/carbon_forecast_fetcher.go +++ b/controllers/carbon_forecast_fetcher.go @@ -78,11 +78,11 @@ func (c *CarbonForecastMockConfigMapFetcher) Fetch(ctx context.Context) ([]Carbo // 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 500 and 700 + // 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()*200 + 500, + Value: rand.Float64()*51 + 529, Duration: 5, }) } @@ -96,8 +96,8 @@ func (c *CarbonForecastMockConfigMapFetcher) Fetch(ctx context.Context) ([]Carbo // create a configmap and pass carbon forecast as binary data cm := &corev1.ConfigMap{ ObjectMeta: metav1.ObjectMeta{ - Name: "mock-carbon-forecast", - Namespace: "default", + Name: "carbon-intensity", + Namespace: "kube-system", }, BinaryData: map[string][]byte{ "data": forecast, diff --git a/controllers/carbonawarekedascaler_controller_test.go b/controllers/carbonawarekedascaler_controller_test.go index 2257b7b..0f561ed 100644 --- a/controllers/carbonawarekedascaler_controller_test.go +++ b/controllers/carbonawarekedascaler_controller_test.go @@ -38,8 +38,8 @@ 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 = "mock-carbon-forecast" - configMapNamespace = "default" + configMapName = "carbon-intensity" + configMapNamespace = "kube-system" configMapKey = "data" ) It("will save forecast data in a ConfigMap", func() { @@ -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 mock-carbon-forecast is found") + By("Confirming the ConfigMap named carbon-intensity is found") Expect(k8sClient.Get(ctx, client.ObjectKey{Name: configMapName, Namespace: configMapNamespace}, cm)).Should(Succeed()) }) }) @@ -132,8 +132,8 @@ var _ = Describe("scenarios for the carbon aware KEDA Scaler", func() { carbonAwareKedaScalerName = "test-carbonawarekedascaler" carbonAwareKedaScalerNamespace = "default" carbonAwareKedaScalerKedaTarget = "scaledobjects.keda.sh" - configMapName = "mock-carbon-forecast" - configMapNamespace = "default" + configMapName = "carbon-intensity" + configMapNamespace = "kube-system" configMapKey = "data" timeout = time.Second * 5 interval = time.Millisecond * 250 @@ -257,7 +257,7 @@ var _ = Describe("scenarios for the carbon aware KEDA Scaler", func() { When("the carbon intensity is within a configured range", func() { const ( - testConfigMapName = "carbonintensity" + testConfigMapName = "mock-carbon-intensity" testConfigMapNamespace = "kube-system" testConfigMapKey = "data" ) diff --git a/hack/workload/carbonawarekedascaler.yaml b/hack/workload/carbonawarekedascaler.yaml index 84098f9..236c36b 100644 --- a/hack/workload/carbonawarekedascaler.yaml +++ b/hack/workload/carbonawarekedascaler.yaml @@ -16,20 +16,20 @@ spec: carbonIntensityForecastDataSource: # carbon intensity forecast data source mockCarbonForecast: true # [OPTIONAL] use mock carbon forecast data localConfigMap: # [OPTIONAL] use configmap for carbon forecast data - name: carbonintensity + name: carbon-intensity namespace: kube-system key: data maxReplicasByCarbonIntensity: # array of carbon intensity values in ascending order; each threshold value represents the upper limit and previous entry represents lower limit - - carbonIntensityThreshold: 562 # when carbon intensity is 562 or below + - carbonIntensityThreshold: 543 # when carbon intensity is 543 or below maxReplicas: 15 # do more - - carbonIntensityThreshold: 575 # when carbon intensity is >562 and <=575 + - carbonIntensityThreshold: 561 # when carbon intensity is >544 and <=561 maxReplicas: 8 - - carbonIntensityThreshold: 589 # when carbon intensity is >575 and <=589 (or above) + - carbonIntensityThreshold: 579 # when carbon intensity is >562 and <=579 (or above) maxReplicas: 1 # do less ecoModeOff: # [OPTIONAL] settings to override carbon awareness; can override based on high intensity duration or schedules - maxReplicas: 10 # when carbon awareness is disabled, use this value + maxReplicas: 100 # when carbon awareness is disabled, use this value carbonIntensityDuration: # [OPTIONAL] disable carbon awareness when carbon intensity is high for this length of time - carbonIntensityThreshold: 588 # when carbon intensity is equal to or above this value, consider it high + carbonIntensityThreshold: 555 # when carbon intensity is equal to or above this value, consider it high overrideEcoAfterDurationInMins: 60 # if carbon intensity is high for this many hours disable ecomode customSchedule: # [OPTIONAL] disable carbon awareness during specified time periods - startTime: "2023-03-31T16:45:00Z" # start time in UTC