Skip to content

Commit

Permalink
Close gcp client on scaler closing
Browse files Browse the repository at this point in the history
Signed-off-by: Jorge Turrado <jorge_turrado@hotmail.es>
  • Loading branch information
JorTurFer committed Feb 1, 2024
1 parent c5cf467 commit 4084ee0
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Here is an overview of all new **experimental** features:

### Fixes

- **GCP Scalers**: Properly close the connection during the scaler cleaning process ([#5448](https://github.com/kedacore/keda/issues/5448))
- **GCP Scalers**: Restore previous time horizon to prevent querying issues ([#5429](https://github.com/kedacore/keda/issues/5429))
- **Prometheus Scaler**: Fix for missing AWS region from metadata ([#5419](https://github.com/kedacore/keda/issues/5419))

Expand Down
22 changes: 18 additions & 4 deletions pkg/scalers/gcp/gcp_stackdriver_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gcp
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
Expand Down Expand Up @@ -52,7 +53,7 @@ const (
// StackDriverClient is a generic client to fetch metrics from Stackdriver. Can be used
// for a stackdriver scaler in the future
type StackDriverClient struct {
MetricsClient *monitoring.MetricClient
metricsClient *monitoring.MetricClient
queryClient *monitoring.QueryClient
credentials GoogleApplicationCredentials
projectID string
Expand All @@ -79,7 +80,7 @@ func NewStackDriverClient(ctx context.Context, credentials string) (*StackDriver
}

return &StackDriverClient{
MetricsClient: metricsClient,
metricsClient: metricsClient,
queryClient: queryClient,
credentials: gcpCredentials,
}, nil
Expand Down Expand Up @@ -107,7 +108,7 @@ func NewStackDriverClientPodIdentity(ctx context.Context) (*StackDriverClient, e
}

return &StackDriverClient{
MetricsClient: metricsClient,
metricsClient: metricsClient,
queryClient: queryClient,
projectID: project,
}, nil
Expand Down Expand Up @@ -244,7 +245,7 @@ func (s StackDriverClient) GetMetrics(
req.Filter = filter

// Get an iterator with the list of time series
it := s.MetricsClient.ListTimeSeries(ctx, req)
it := s.metricsClient.ListTimeSeries(ctx, req)

var value float64 = -1

Expand Down Expand Up @@ -360,6 +361,19 @@ func (s StackDriverClient) BuildMQLQuery(projectID, resourceType, metric, resour
return q, nil
}

func (s *StackDriverClient) Close() error {
var queryClientError error
var metricsClientError error
if s.queryClient != nil {
queryClientError = s.queryClient.Close()
}
if s.metricsClient != nil {
metricsClientError = s.metricsClient.Close()
}
return errors.Join(queryClientError, metricsClientError)

}

// buildAggregation builds the aggregation part of a Monitoring Query Language (MQL) query
func buildAggregation(aggregation string) (string, error) {
// Match against "percentileX"
Expand Down
3 changes: 1 addition & 2 deletions pkg/scalers/gcp_cloud_tasks_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,12 @@ func parseGcpCloudTasksMetadata(config *scalersconfig.ScalerConfig) (*gcpCloudTa

func (s *gcpCloudTasksScaler) Close(context.Context) error {
if s.client != nil {
err := s.client.MetricsClient.Close()
err := s.client.Close()
s.client = nil
if err != nil {
s.logger.Error(err, "error closing StackDriver client")
}
}

return nil
}

Expand Down
3 changes: 1 addition & 2 deletions pkg/scalers/gcp_pubsub_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,12 @@ func parsePubSubMetadata(config *scalersconfig.ScalerConfig, logger logr.Logger)

func (s *pubsubScaler) Close(context.Context) error {
if s.client != nil {
err := s.client.MetricsClient.Close()
err := s.client.Close()
s.client = nil
if err != nil {
s.logger.Error(err, "error closing StackDriver client")
}
}

return nil
}

Expand Down
3 changes: 1 addition & 2 deletions pkg/scalers/gcp_stackdriver_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,12 @@ func initializeStackdriverClient(ctx context.Context, gcpAuthorization *gcp.Auth

func (s *stackdriverScaler) Close(context.Context) error {
if s.client != nil {
err := s.client.MetricsClient.Close()
err := s.client.Close()
s.client = nil
if err != nil {
s.logger.Error(err, "error closing StackDriver client")
}
}

return nil
}

Expand Down

0 comments on commit 4084ee0

Please sign in to comment.