Skip to content

Commit 75e6468

Browse files
committed
proper management for metrics updates + working e2e
Signed-off-by: Elieser Pereira <[email protected]>
1 parent 8657c2a commit 75e6468

File tree

2 files changed

+65
-9
lines changed

2 files changed

+65
-9
lines changed

operator/controllers/http/httpscaledobject_controller.go

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package http
1919
import (
2020
"context"
2121
"fmt"
22+
"sync"
2223
"time"
2324

2425
kedav1alpha1 "github.com/kedacore/keda/v2/apis/keda/v1alpha1"
@@ -37,6 +38,15 @@ import (
3738
"github.com/kedacore/http-add-on/operator/metrics"
3839
)
3940

41+
type httpScaledObjectMetricsData struct {
42+
namespace string
43+
}
44+
45+
var (
46+
httpScaledObjectPromMetricsMap map[string]httpScaledObjectMetricsData
47+
httpScaledObjectPromMetricsLock *sync.Mutex
48+
)
49+
4050
// HTTPScaledObjectReconciler reconciles a HTTPScaledObject object
4151
//
4252
//revive:disable-next-line:exported
@@ -49,6 +59,11 @@ type HTTPScaledObjectReconciler struct {
4959
BaseConfig config.Base
5060
}
5161

62+
func init() {
63+
httpScaledObjectPromMetricsMap = make(map[string]httpScaledObjectMetricsData)
64+
httpScaledObjectPromMetricsLock = &sync.Mutex{}
65+
}
66+
5267
// +kubebuilder:rbac:groups=http.keda.sh,resources=httpscaledobjects,verbs=get;list;watch;create;update;patch;delete
5368
// +kubebuilder:rbac:groups=http.keda.sh,resources=httpscaledobjects/status,verbs=get;update;patch
5469
// +kubebuilder:rbac:groups=http.keda.sh,resources=httpscaledobjects/finalizers,verbs=update
@@ -79,6 +94,7 @@ func (r *HTTPScaledObjectReconciler) Reconcile(ctx context.Context, req ctrl.Req
7994
}
8095

8196
if httpso.GetDeletionTimestamp() != nil {
97+
r.updatePromMetricsOnDelete(ctx, httpso)
8298
return ctrl.Result{}, finalizeScaledObject(ctx, logger, r.Client, httpso)
8399
}
84100

@@ -99,7 +115,6 @@ func (r *HTTPScaledObjectReconciler) Reconcile(ctx context.Context, req ctrl.Req
99115
"DeploymentName",
100116
httpso.Name,
101117
)
102-
r.updatePromMetrics(ctx, httpso, req.Namespace)
103118

104119
// check if ScalingMetric is correct
105120
if httpso.Spec.ScalingMetric != nil &&
@@ -141,6 +156,7 @@ func (r *HTTPScaledObjectReconciler) Reconcile(ctx context.Context, req ctrl.Req
141156
)
142157

143158
// success reconciling
159+
r.updatePromMetrics(ctx, httpso)
144160
logger.Info("Reconcile success")
145161
return ctrl.Result{}, nil
146162
}
@@ -165,14 +181,34 @@ func (r *HTTPScaledObjectReconciler) SetupWithManager(mgr ctrl.Manager) error {
165181
Complete(r)
166182
}
167183

168-
func (r *HTTPScaledObjectReconciler) updatePromMetrics(ctx context.Context, scaledObject *httpv1alpha1.HTTPScaledObject, namespacedName string) {
184+
func (r *HTTPScaledObjectReconciler) updatePromMetrics(ctx context.Context, scaledObject *httpv1alpha1.HTTPScaledObject) {
185+
httpScaledObjectPromMetricsLock.Lock()
186+
defer httpScaledObjectPromMetricsLock.Unlock()
187+
188+
namespacedName := scaledObject.Name + scaledObject.Namespace
189+
metricsData, ok := httpScaledObjectPromMetricsMap[namespacedName]
190+
if ok {
191+
metrics.RecordDeleteHTTPScaledObjectCount(scaledObject.Namespace)
192+
}
193+
metricsData.namespace = scaledObject.Namespace
194+
169195
logger := log.FromContext(ctx, "updatePromMetrics", namespacedName)
170196
logger.Info("updatePromMetrics")
171-
metrics.RecordHTTPScaledObjectCount(namespacedName)
197+
metrics.RecordHTTPScaledObjectCount(scaledObject.Namespace)
198+
199+
httpScaledObjectPromMetricsMap[namespacedName] = metricsData
172200
}
173201

174-
func (r *HTTPScaledObjectReconciler) updatePromMetricsOnDelete(ctx context.Context, scaledObject *httpv1alpha1.HTTPScaledObject, namespacedName string) {
202+
func (r *HTTPScaledObjectReconciler) updatePromMetricsOnDelete(ctx context.Context, scaledObject *httpv1alpha1.HTTPScaledObject) {
203+
httpScaledObjectPromMetricsLock.Lock()
204+
defer httpScaledObjectPromMetricsLock.Unlock()
205+
206+
namespacedName := scaledObject.Name + scaledObject.Namespace
175207
logger := log.FromContext(ctx, "updatePromMetricsOnDelete", namespacedName)
176208
logger.Info("updatePromMetricsOnDelete")
177-
metrics.RecordDeleteHTTPScaledObjectCount(namespacedName)
209+
210+
if _, ok := httpScaledObjectPromMetricsMap[namespacedName]; ok {
211+
metrics.RecordDeleteHTTPScaledObjectCount(scaledObject.Namespace)
212+
}
213+
delete(httpScaledObjectPromMetricsMap, namespacedName)
178214
}

tests/checks/operator_prometheus_metrics/operator_prometheus_metrics_test.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,15 +165,20 @@ func TestMetricGeneration(t *testing.T) {
165165
// Send a test request to the interceptor
166166
sendLoad(t, kc, data)
167167

168+
httpScaledObjecData, httpScaledObjecDataTemplates := getTemplateHTTPScaledObjecData()
169+
KubectlApplyMultipleWithTemplate(t, httpScaledObjecData, httpScaledObjecDataTemplates)
170+
168171
// Fetch metrics and validate them
169172
family := fetchAndParsePrometheusMetrics(t, fmt.Sprintf("curl --insecure %s", kedaOperatorPrometheusURL))
170173
val, ok := family["keda_http_scaled_object_total"]
171174
assert.True(t, ok, "keda_http_scaled_object_total is available")
172175

173-
requestCount := getMetricsValue(val)
174-
assert.GreaterOrEqual(t, requestCount, float64(1))
176+
httpSacaledObjectCount := getMetricsValue(val)
177+
assert.GreaterOrEqual(t, httpSacaledObjectCount, float64(1))
178+
assert.Equal(t, httpSacaledObjectCount, float64(2))
175179

176180
// cleanup
181+
KubectlDeleteMultipleWithTemplate(t, httpScaledObjecData, httpScaledObjecDataTemplates)
177182
DeleteKubernetesResources(t, testNamespace, data, templates)
178183
}
179184

@@ -182,8 +187,8 @@ func sendLoad(t *testing.T, kc *kubernetes.Clientset, data templateData) {
182187

183188
KubectlApplyWithTemplate(t, data, "loadJobTemplate", loadJobTemplate)
184189

185-
assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, deploymentName, testNamespace, maxReplicaCount, 6, 10),
186-
"replica count should be %d after 1 minutes", maxReplicaCount)
190+
assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, deploymentName, testNamespace, maxReplicaCount, 12, 10),
191+
"replica count should be %d after 2 minutes", maxReplicaCount)
187192
}
188193

189194
func fetchAndParsePrometheusMetrics(t *testing.T, cmd string) map[string]*prommodel.MetricFamily {
@@ -233,3 +238,18 @@ func getTemplateData() (templateData, []Template) {
233238
{Name: "httpScaledObjectTemplate", Config: httpScaledObjectTemplate},
234239
}
235240
}
241+
242+
func getTemplateHTTPScaledObjecData() (templateData, []Template) {
243+
return templateData{
244+
TestNamespace: testNamespace,
245+
DeploymentName: "OtherDeployment",
246+
ServiceName: serviceName,
247+
ClientName: clientName,
248+
HTTPScaledObjectName: "OtherHttpScaledObjectName",
249+
Host: host,
250+
MinReplicas: minReplicaCount,
251+
MaxReplicas: maxReplicaCount,
252+
}, []Template{
253+
{Name: "OtherHttpScaledObjectName", Config: httpScaledObjectTemplate},
254+
}
255+
}

0 commit comments

Comments
 (0)