feat: support configurate the http client timeout for webhook job (#18382)

Signed-off-by: chlins <chenyuzh@vmware.com>
This commit is contained in:
Chlins Zhang 2023-03-21 11:54:10 +08:00 committed by GitHub
parent f21b1481bb
commit 67d3f9add8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 43 additions and 3 deletions

View File

@ -118,6 +118,8 @@ jobservice:
notification:
# Maximum retry count for webhook job
webhook_job_max_retry: 10
# HTTP client timeout for webhook job
webhook_job_http_client_timeout: 3 #seconds
# Log configurations
log:

View File

@ -252,8 +252,16 @@ notification:
# Maximum retry count for webhook job
{% if notification is defined %}
webhook_job_max_retry: {{ notification.webhook_job_max_retry}}
# HTTP client timeout for webhook job
{% if notification.webhook_job_http_client_timeout is defined %}
webhook_job_http_client_timeout: {{ notification.webhook_job_http_client_timeout }}
{% else %}
webhook_job_http_client_timeout: 3 #seconds
{% endif %}
{% else %}
webhook_job_max_retry: 10
# HTTP client timeout for webhook job
webhook_job_http_client_timeout: 3 #seconds
{% endif %}
# Log configurations

View File

@ -4,6 +4,7 @@ JOBSERVICE_SECRET={{jobservice_secret}}
CORE_URL={{core_url}}
REGISTRY_CONTROLLER_URL={{registry_controller_url}}
JOBSERVICE_WEBHOOK_JOB_MAX_RETRY={{notification_webhook_job_max_retry}}
JOBSERVICE_WEBHOOK_JOB_HTTP_CLIENT_TIMEOUT={{notification_webhook_job_http_client_timeout}}
{%if internal_tls.enabled %}
INTERNAL_TLS_ENABLED=true

View File

@ -244,6 +244,7 @@ def parse_yaml_config(config_file_path, with_notary, with_trivy):
# notification config
notification_config = configs.get('notification') or {}
config_dict['notification_webhook_job_max_retry'] = notification_config["webhook_job_max_retry"]
config_dict['notification_webhook_job_http_client_timeout'] = notification_config["webhook_job_http_client_timeout"]
# Log configs
allowed_levels = ['debug', 'info', 'warning', 'error', 'fatal']

View File

@ -2,19 +2,45 @@ package notification
import (
"net/http"
"os"
"strconv"
"time"
commonhttp "github.com/goharbor/harbor/src/common/http"
"github.com/goharbor/harbor/src/jobservice/logger"
)
const (
secure = "secure"
insecure = "insecure"
// Max retry has the same meaning as max fails.
maxFails = "JOBSERVICE_WEBHOOK_JOB_MAX_RETRY"
// http client timeout for webhook job(seconds).
httpClientTimeout = "JOBSERVICE_WEBHOOK_JOB_HTTP_CLIENT_TIMEOUT"
)
var (
// timeout records the timeout for http client
timeout time.Duration
httpHelper *HTTPHelper
)
func init() {
// default timeout is 3 seconds
timeout = 3 * time.Second
if envTimeout, exist := os.LookupEnv(httpClientTimeout); exist {
t, err := strconv.ParseInt(envTimeout, 10, 64)
if err != nil {
logger.Warningf("Failed to parse timeout from environment, error: %v", err)
return
}
timeout = time.Duration(t) * time.Second
logger.Debugf("Set the http client timeout to %v for webhook job", timeout)
}
}
// HTTPHelper in charge of sending notification messages to remote endpoint
type HTTPHelper struct {
clients map[string]*http.Client
@ -26,8 +52,10 @@ func init() {
}
httpHelper.clients[secure] = &http.Client{
Transport: commonhttp.GetHTTPTransport(),
Timeout: timeout,
}
httpHelper.clients[insecure] = &http.Client{
Transport: commonhttp.GetHTTPTransport(commonhttp.WithInsecure(true)),
Timeout: timeout,
}
}

View File

@ -2,6 +2,7 @@ package notification
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
@ -9,9 +10,11 @@ import (
func TestHttpHelper(t *testing.T) {
c1 := httpHelper.clients[insecure]
assert.NotNil(t, c1)
assert.Equal(t, 3*time.Second, c1.Timeout)
c2 := httpHelper.clients[secure]
assert.NotNil(t, c2)
assert.Equal(t, 3*time.Second, c1.Timeout)
_, ok := httpHelper.clients["notExists"]
assert.False(t, ok)

View File

@ -13,9 +13,6 @@ import (
"github.com/goharbor/harbor/src/lib/errors"
)
// Max retry has the same meaning as max fails.
const maxFails = "JOBSERVICE_WEBHOOK_JOB_MAX_RETRY"
// WebhookJob implements the job interface, which send notification by http or https.
type WebhookJob struct {
client *http.Client