From 01bbad4eff5a166e078245df13188e2aefc5eb8f Mon Sep 17 00:00:00 2001 From: Krzysztof Kwiatosz Date: Tue, 31 Oct 2023 08:38:33 +0100 Subject: [PATCH] Fix scale to zero example (#274) * Fix scale to zero example * content-type header should match the content * change functions git reference to kyma-project's main branch * improve prerequisites * improve prerequisites * Apply suggestions from code review Co-authored-by: Grzegorz Karaluch --------- Co-authored-by: Grzegorz Karaluch --- scale-to-zero-with-keda/README.md | 12 +++-- .../k8s-resources/http-proxy-fn.yml | 4 +- .../k8s-resources/scalable-worker-fn.yml | 4 +- .../src/http-proxy-fn/handler.js | 45 ++++++++----------- .../src/http-proxy-fn/package.json | 1 - 5 files changed, 30 insertions(+), 36 deletions(-) diff --git a/scale-to-zero-with-keda/README.md b/scale-to-zero-with-keda/README.md index a62ef0b5..f770eca8 100644 --- a/scale-to-zero-with-keda/README.md +++ b/scale-to-zero-with-keda/README.md @@ -19,12 +19,16 @@ Keda is used to scale the worker Function. [KEDA Prometheus scaler](https://keda ## Prerequisites -- Kyma as the target deployment environment. -- Keda module enabled +- Kyma as the target Kubernetes runtime. +- [Keda module installed](https://github.com/kyma-project/keda-manager#install-keda-manager-and-keda-from-the-latest-release) +- [Serverless module installed](https://github.com/kyma-project/serverless-manager#install) +- [Custom Prometheus stack installed](https://github.com/kyma-project/examples/blob/main/prometheus/README.md#installation) with [Istio scraping enabled](https://github.com/kyma-project/examples/blob/main/prometheus/README.md#installation) + +>> NOTE: Serverless and Prometheus are included in Kyma versions up to 2.19.x ## Installation -Make sure istio sidecar injection is enabled in the target Namesapce: +Make sure Istio sidecar injection is enabled in the target Namespace: ```bash kubectl label namespace default istio-injection=enabled @@ -57,7 +61,7 @@ Once you generate a load (even a single request), the non-zero request rate targ Call the HTTP proxy Function once: ```bash - curl -H "Content-Type: application/cloudevents+json" -X POST -d '{"foo":"bar"}' https://incoming.{your_cluster_domain} + curl -H "Content-Type: application/json" -X POST -d '{"foo":"bar"}' https://incoming.{your_cluster_domain} ``` The message is pushed to the Kyma Eventing. diff --git a/scale-to-zero-with-keda/k8s-resources/http-proxy-fn.yml b/scale-to-zero-with-keda/k8s-resources/http-proxy-fn.yml index a4deb081..050708a6 100644 --- a/scale-to-zero-with-keda/k8s-resources/http-proxy-fn.yml +++ b/scale-to-zero-with-keda/k8s-resources/http-proxy-fn.yml @@ -9,9 +9,9 @@ spec: replicas: 1 source: gitRepository: - url: https://github.com/kwiatekus/examples.git + url: https://github.com/kyma-project/examples.git baseDir: /scale-to-zero-with-keda/src/http-proxy-fn - reference: scale-with-keda + reference: main env: - name: eventspecversion value: "1.0" diff --git a/scale-to-zero-with-keda/k8s-resources/scalable-worker-fn.yml b/scale-to-zero-with-keda/k8s-resources/scalable-worker-fn.yml index a18506c3..85911ccc 100644 --- a/scale-to-zero-with-keda/k8s-resources/scalable-worker-fn.yml +++ b/scale-to-zero-with-keda/k8s-resources/scalable-worker-fn.yml @@ -8,9 +8,9 @@ spec: runtime: nodejs18 source: gitRepository: - url: https://github.com/kwiatekus/examples.git + url: https://github.com/kyma-project/examples.git baseDir: /scale-to-zero-with-keda/src/scalable-worker-fn - reference: scale-with-keda + reference: main --- apiVersion: eventing.kyma-project.io/v1alpha2 kind: Subscription diff --git a/scale-to-zero-with-keda/src/http-proxy-fn/handler.js b/scale-to-zero-with-keda/src/http-proxy-fn/handler.js index 58226cdb..999dfbdf 100644 --- a/scale-to-zero-with-keda/src/http-proxy-fn/handler.js +++ b/scale-to-zero-with-keda/src/http-proxy-fn/handler.js @@ -1,39 +1,30 @@ -const { v4: uuidv4 } = require('uuid'); const { SpanStatusCode } = require("@opentelemetry/api/build/src/trace/status"); module.exports = { main: async function (event, context) { - const msgId = uuidv4(); const eventType = process.env['eventtype'] const eventSource = process.env['eventsource'] - const eventSpecVersion = process.env['eventspecversion'] - - var eventOut=event.buildResponseCloudEvent(msgId,eventType,event.data); - eventOut.source=eventSource; - eventOut.specversion=eventSpecVersion const span = event.tracer.startSpan('call-to-kyma-eventing'); - return await event.publishCloudEvent(eventOut) - .then(resp => { - if(resp.status!==204){ - throw new Error("Unexpected response from eventing proxy"); - } - span.addEvent("Event sent"); - span.setAttribute("event-type", eventType); - span.setAttribute("event-source", eventSource); - span.setAttribute("event-id", msgId); - span.setStatus({code: SpanStatusCode.OK}); - return "Event sent : "+JSON.stringify(event.data); - }).catch(err=> { - console.error(err) - span.setStatus({ - code: SpanStatusCode.ERROR, - message: err.message, - }); - return err.message; - }).finally(()=>{ - span.end(); + + return await event.emitCloudEvent(eventType, eventSource, event.data) + .then(resp => { + console.log(resp.status); + span.addEvent("Event sent"); + span.setAttribute("event-type", eventType); + span.setAttribute("event-source", eventSource); + span.setStatus({code: SpanStatusCode.OK}); + return "Event sent: "+JSON.stringify(event.data); + }).catch(err=> { + console.error(err) + span.setStatus({ + code: SpanStatusCode.ERROR, + message: err.message, }); + return err.message; + }).finally(()=>{ + span.end(); + }); } } diff --git a/scale-to-zero-with-keda/src/http-proxy-fn/package.json b/scale-to-zero-with-keda/src/http-proxy-fn/package.json index 0baa8772..9106c44b 100644 --- a/scale-to-zero-with-keda/src/http-proxy-fn/package.json +++ b/scale-to-zero-with-keda/src/http-proxy-fn/package.json @@ -2,7 +2,6 @@ "name": "http-proxy-fn", "version": "0.0.1", "dependencies": { - "uuidv4": "6.2.12", "@opentelemetry/api": "^1.0.4" } } \ No newline at end of file