Skip to content

Commit

Permalink
Moving HTTP util function to pkg/util
Browse files Browse the repository at this point in the history
Signed-off-by: Aaron Schlesinger <aaron@ecomaz.net>
  • Loading branch information
arschles committed Nov 30, 2020
1 parent f94190f commit b4b7206
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 32 deletions.
3 changes: 2 additions & 1 deletion pkg/scalers/artemis_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"k8s.io/metrics/pkg/apis/external_metrics"
logf "sigs.k8s.io/controller-runtime/pkg/log"

"github.com/kedacore/keda/pkg/util"
kedautil "github.com/kedacore/keda/pkg/util"
)

Expand Down Expand Up @@ -58,7 +59,7 @@ func NewArtemisQueueScaler(config *ScalerConfig) (Scaler, error) {
// do we need to guarantee this timeout for a specific
// reason? if not, we can have buildScaler pass in
// the global client
httpClient := createHTTPClient(config.GlobalHTTPTimeout)
httpClient := util.CreateHTTPClient(config.GlobalHTTPTimeout)

artemisMetadata, err := parseArtemisMetadata(config)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/scalers/azure_blob_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strconv"

"github.com/kedacore/keda/pkg/scalers/azure"
"github.com/kedacore/keda/pkg/util"

v2beta2 "k8s.io/api/autoscaling/v2beta2"
"k8s.io/apimachinery/pkg/api/resource"
Expand Down Expand Up @@ -53,7 +54,7 @@ func NewAzureBlobScaler(config *ScalerConfig) (Scaler, error) {
return &azureBlobScaler{
metadata: meta,
podIdentity: podIdentity,
httpClient: createHTTPClient(config.GlobalHTTPTimeout),
httpClient: util.CreateHTTPClient(config.GlobalHTTPTimeout),
}, nil
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/scalers/azure_eventhub_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strconv"

"github.com/kedacore/keda/pkg/scalers/azure"
"github.com/kedacore/keda/pkg/util"

eventhub "github.com/Azure/azure-event-hubs-go"
"github.com/Azure/azure-storage-blob-go/azblob"
Expand Down Expand Up @@ -58,7 +59,7 @@ func NewAzureEventHubScaler(config *ScalerConfig) (Scaler, error) {
return &azureEventHubScaler{
metadata: parsedMetadata,
client: hub,
httpClient: createHTTPClient(config.GlobalHTTPTimeout),
httpClient: util.CreateHTTPClient(config.GlobalHTTPTimeout),
}, nil
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/scalers/azure_log_analytics_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
logf "sigs.k8s.io/controller-runtime/pkg/log"

kedav1alpha1 "github.com/kedacore/keda/api/v1alpha1"
"github.com/kedacore/keda/pkg/util"
kedautil "github.com/kedacore/keda/pkg/util"
)

Expand Down Expand Up @@ -100,7 +101,7 @@ func NewAzureLogAnalyticsScaler(config *ScalerConfig) (Scaler, error) {
cache: &sessionCache{metricValue: -1, metricThreshold: -1},
name: config.Name,
namespace: config.Namespace,
httpClient: createHTTPClient(config.GlobalHTTPTimeout),
httpClient: util.CreateHTTPClient(config.GlobalHTTPTimeout),
}, nil
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/scalers/azure_queue_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strconv"

"github.com/kedacore/keda/pkg/scalers/azure"
"github.com/kedacore/keda/pkg/util"

v2beta2 "k8s.io/api/autoscaling/v2beta2"
"k8s.io/apimachinery/pkg/api/resource"
Expand Down Expand Up @@ -50,7 +51,7 @@ func NewAzureQueueScaler(config *ScalerConfig) (Scaler, error) {
return &azureQueueScaler{
metadata: meta,
podIdentity: podIdentity,
httpClient: createHTTPClient(config.GlobalHTTPTimeout),
httpClient: util.CreateHTTPClient(config.GlobalHTTPTimeout),
}, nil
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/scalers/azure_servicebus_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/Azure/azure-amqp-common-go/v3/auth"
servicebus "github.com/Azure/azure-service-bus-go"
"github.com/kedacore/keda/pkg/scalers/azure"
"github.com/kedacore/keda/pkg/util"
v2beta2 "k8s.io/api/autoscaling/v2beta2"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -58,7 +59,7 @@ func NewAzureServiceBusScaler(config *ScalerConfig) (Scaler, error) {
return &azureServiceBusScaler{
metadata: meta,
podIdentity: config.PodIdentity,
httpClient: createHTTPClient(config.GlobalHTTPTimeout),
httpClient: util.CreateHTTPClient(config.GlobalHTTPTimeout),
}, nil
}

Expand Down
20 changes: 0 additions & 20 deletions pkg/scalers/http_clients.go

This file was deleted.

5 changes: 2 additions & 3 deletions pkg/scalers/metrics_api_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"k8s.io/metrics/pkg/apis/external_metrics"
logf "sigs.k8s.io/controller-runtime/pkg/log"

"github.com/kedacore/keda/pkg/util"
kedautil "github.com/kedacore/keda/pkg/util"
)

Expand Down Expand Up @@ -69,16 +70,14 @@ func NewMetricsAPIScaler(config *ScalerConfig) (Scaler, error) {
return nil, fmt.Errorf("error parsing metric API metadata: %s", err)
}

httpClient := createHTTPClient(config.GlobalHTTPTimeout)
httpClient := util.CreateHTTPClient(config.GlobalHTTPTimeout)

if meta.enableTLS {
config, err := kedautil.NewTLSConfig(meta.cert, meta.key, meta.ca)
if err != nil {
return nil, err
}

// TODO: this will add a transport for all of the HTTP clients.
// should it be global?
httpClient.Transport = &http.Transport{TLSClientConfig: config}
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/scalers/prometheus_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"k8s.io/metrics/pkg/apis/external_metrics"
logf "sigs.k8s.io/controller-runtime/pkg/log"

"github.com/kedacore/keda/pkg/util"
kedautil "github.com/kedacore/keda/pkg/util"
)

Expand Down Expand Up @@ -62,7 +63,7 @@ func NewPrometheusScaler(config *ScalerConfig) (Scaler, error) {

return &prometheusScaler{
metadata: meta,
httpClient: createHTTPClient(config.GlobalHTTPTimeout),
httpClient: util.CreateHTTPClient(config.GlobalHTTPTimeout),
}, nil
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/scalers/rabbitmq_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"k8s.io/metrics/pkg/apis/external_metrics"
logf "sigs.k8s.io/controller-runtime/pkg/log"

"github.com/kedacore/keda/pkg/util"
kedautil "github.com/kedacore/keda/pkg/util"
)

Expand Down Expand Up @@ -56,7 +57,7 @@ var rabbitmqLog = logf.Log.WithName("rabbitmq_scaler")

// NewRabbitMQScaler creates a new rabbitMQ scaler
func NewRabbitMQScaler(config *ScalerConfig) (Scaler, error) {
httpClient := createHTTPClient(config.GlobalHTTPTimeout)
httpClient := util.CreateHTTPClient(config.GlobalHTTPTimeout)
meta, err := parseRabbitMQMetadata(config)
if err != nil {
return nil, fmt.Errorf("error parsing rabbitmq metadata: %s", err)
Expand Down
3 changes: 2 additions & 1 deletion pkg/scalers/stan_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"k8s.io/metrics/pkg/apis/external_metrics"
logf "sigs.k8s.io/controller-runtime/pkg/log"

"github.com/kedacore/keda/pkg/util"
kedautil "github.com/kedacore/keda/pkg/util"
)

Expand Down Expand Up @@ -69,7 +70,7 @@ func NewStanScaler(config *ScalerConfig) (Scaler, error) {
return &stanScaler{
channelInfo: &monitorChannelInfo{},
metadata: stanMetadata,
httpClient: createHTTPClient(config.GlobalHTTPTimeout),
httpClient: util.CreateHTTPClient(config.GlobalHTTPTimeout),
}, nil
}

Expand Down
15 changes: 15 additions & 0 deletions pkg/util/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package util

import (
"net/http"
"time"
)

// HTTPDoer is an interface that matches the Do method on
Expand All @@ -10,3 +11,17 @@ import (
type HTTPDoer interface {
Do(*http.Request) (*http.Response, error)
}

// CreateHTTPClient returns a new HTTP client with the timeout set to
// timeoutMS milliseconds, or 300 milliseconds if timeoutMS <= 0
func CreateHTTPClient(timeout time.Duration) *http.Client {
// default the timeout to 300ms
if timeout <= 0 {
timeout = 300 * time.Millisecond
}
httpClient := &http.Client{
Timeout: timeout,
}

return httpClient
}

0 comments on commit b4b7206

Please sign in to comment.