Skip to content

Instantly share code, notes, and snippets.

@embano1
Last active September 9, 2021 08:34
Show Gist options
  • Save embano1/414a7632595615be1f38b4ad1fece7cf to your computer and use it in GitHub Desktop.
Save embano1/414a7632595615be1f38b4ad1fece7cf to your computer and use it in GitHub Desktop.
HTTP CloudEvents function always returning 500 (for failure tests, retries, etc.)
export KIND_CLUSTER_NAME=knative-0.24
export KO_DOCKER_REPO=kind.local
ko apply -f .
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: always-fail
spec:
template:
metadata:
annotations:
autoscaling.knative.dev/maxScale: "1"
autoscaling.knative.dev/minScale: "1"
spec:
containers:
- image: ko://knative.dev/eventing-rabbitmq/example
package main
import (
"context"
"errors"
"net/http"
"time"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"knative.dev/pkg/logging"
"knative.dev/pkg/signals"
)
const (
addr = ":8080"
timeout = 5 * time.Second
)
func main() {
ctx := signals.NewContext()
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Post("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
})
srv := http.Server{
Addr: addr,
Handler: r,
ReadTimeout: timeout,
WriteTimeout: timeout,
}
go func() {
<-ctx.Done()
logging.FromContext(ctx).Info("shutting down")
if err := srv.Shutdown(context.Background()); err != nil {
logging.FromContext(ctx).Warnf("could not shutdown: %v", err)
}
}()
logging.FromContext(ctx).Infow("listening", "address", addr)
if err := srv.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
logging.FromContext(ctx).Fatal(err)
}
}
apiVersion: eventing.knative.dev/v1
kind: Trigger
metadata:
name: always-fail
spec:
broker: rabbit
filter:
attributes:
type: com.vmware.vsphere.VmPoweredOnEvent
subscriber:
ref:
apiVersion: serving.knative.dev/v1
kind: Service
name: always-fail
namespace: default
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment