Add method and subpath flags to timetrigger object for triggering a function (#3017)

* Update timetrigger crd and add method and subpath fields in spec.
Update fission-cli to accept user input for method and subpath fields.
Update publisher package to utilize these fields for triggering a function.
Update timer controller to use method and subpath fields for publishing a request.
Add a new test TestPublisherSubpath in pulisher package.

Signed-off-by: Md Soharab Ansari <soharab.ansari@infracloud.io>

* Use kubebuilder default annotation.
Update test for fission-cli timetrigger create, update command to support method and subpath flags.

Signed-off-by: Md Soharab Ansari <soharab.ansari@infracloud.io>

---------

Signed-off-by: Md Soharab Ansari <soharab.ansari@infracloud.io>
This commit is contained in:
soharab-ic
2024-09-16 17:15:54 +05:30
committed by GitHub
parent 5d580e01aa
commit 93869d3bc8
16 changed files with 107 additions and 14 deletions
+1 -1
View File
@@ -25,6 +25,6 @@ type (
// Publish a request to a "target". Target's meaning depends on the
// publisher: it's a URL in the case of a webhook publisher, or a queue
// name in a queue-based publisher such as NATS.
Publish(ctx context.Context, body string, headers map[string]string, target string)
Publish(ctx context.Context, body string, headers map[string]string, method, target string)
}
)
+23 -1
View File
@@ -29,6 +29,28 @@ func TestPublisher(t *testing.T) {
}
wp := MakeWebhookPublisher(logger, s.URL)
wp.Publish(ctx, "", map[string]string{"X-Fission-Test": "aaa"}, fnName)
wp.Publish(ctx, "", map[string]string{"X-Fission-Test": "aaa"}, http.MethodPost, fnName)
time.Sleep(time.Second * 1)
}
func TestPublisherSubpath(t *testing.T) {
subpath := "/api/v1/read"
fnName := "test-fn-subpath"
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "/"+fnName+subpath, r.URL.Path)
assert.Equal(t, "aaa", r.Header.Get("X-Fission-Test"))
assert.Contains(t, r.Header, "Traceparent")
}))
ctx := context.Background()
logger := loggerfactory.GetLogger()
shutdown, err := otelUtils.InitProvider(ctx, logger, fnName)
assert.NoError(t, err)
if shutdown != nil {
defer shutdown(ctx)
}
wp := MakeWebhookPublisher(logger, s.URL)
wp.Publish(ctx, "", map[string]string{"X-Fission-Test": "aaa"}, http.MethodGet, fnName+subpath)
time.Sleep(time.Second * 1)
}
+5 -3
View File
@@ -49,6 +49,7 @@ type (
ctx context.Context
body string
headers map[string]string
method string
target string
retries int
retryDelay time.Duration
@@ -72,7 +73,7 @@ func MakeWebhookPublisher(logger *zap.Logger, baseURL string) *WebhookPublisher
}
// Publish sends a request to the target with payload having given body and headers
func (p *WebhookPublisher) Publish(ctx context.Context, body string, headers map[string]string, target string) {
func (p *WebhookPublisher) Publish(ctx context.Context, body string, headers map[string]string, method, target string) {
tracer := otel.Tracer("WebhookPublisher")
ctx, span := tracer.Start(ctx, "WebhookPublisher/Publish")
defer span.End()
@@ -82,6 +83,7 @@ func (p *WebhookPublisher) Publish(ctx context.Context, body string, headers map
ctx: ctx,
body: body,
headers: headers,
method: method,
target: target,
retries: p.maxRetries,
retryDelay: p.retryDelay,
@@ -98,7 +100,7 @@ func (p *WebhookPublisher) svc() {
func (p *WebhookPublisher) makeHTTPRequest(r *publishRequest) {
url := p.baseURL + "/" + strings.TrimPrefix(r.target, "/")
msg := "making HTTP request"
msg := fmt.Sprintf("making HTTP %s request", r.method)
level := zap.ErrorLevel
fields := []zap.Field{zap.String("url", url), zap.String("type", "publish_request")}
@@ -113,7 +115,7 @@ func (p *WebhookPublisher) makeHTTPRequest(r *publishRequest) {
buf.WriteString(r.body)
// Create request
req, err := http.NewRequest(http.MethodPost, url, &buf)
req, err := http.NewRequest(r.method, url, &buf)
if err != nil {
fields = append(fields, zap.Error(err))
return