-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Provide scaler for Amazon managed service for Prometheus (#5373)
Signed-off-by: Siva Guruvareddiar <sivagurunath@gmail.com> Signed-off-by: Jorge Turrado <jorge_turrado@hotmail.es> Signed-off-by: Jorge Turrado Ferrero <Jorge_turrado@hotmail.es> Signed-off-by: Jorge Turrado <jorge.turrado@scrm.lidl> Signed-off-by: Zbynek Roubalik <zroubalik@gmail.com> Signed-off-by: Siva Guruvareddiar <gurusiva@gmail.com> Signed-off-by: Siva Guruvareddiar <1725781+sguruvar@users.noreply.github.com> Co-authored-by: Jorge Turrado Ferrero <Jorge_turrado@hotmail.es> Co-authored-by: Jorge Turrado <jorge.turrado@scrm.lidl> Co-authored-by: Zbynek Roubalik <zroubalik@gmail.com> Co-authored-by: Siva Guruvareddiar <gurusiva@amazon.com> Co-authored-by: Tom Kerkhove <kerkhove.tom@gmail.com>
- Loading branch information
1 parent
b58ca96
commit 49b0fd0
Showing
51 changed files
with
18,636 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
Copyright 2024 The KEDA Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
/* | ||
This file contains all the logic for caching aws.Config across all the (AWS) | ||
triggers. The first time when an aws.Config is requested, it's cached based on | ||
the authentication info (roleArn, Key&Secret, keda itself) and it's returned | ||
every time when an aws.Config is requested for the same authentication info. | ||
This is required because if we don't cache and share them, each scaler | ||
generates and refresh it's own token although all the tokens grants the same | ||
permissions | ||
*/ | ||
package aws | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go-v2/service/amp" | ||
|
||
"github.com/kedacore/keda/v2/pkg/scalers/scalersconfig" | ||
httputils "github.com/kedacore/keda/v2/pkg/util" | ||
) | ||
|
||
// roundTripper adds custom round tripper to sign requests | ||
type roundTripper struct { | ||
client *amp.Client | ||
region string | ||
} | ||
|
||
var ( | ||
// ErrAwsAMPNoAwsRegion is returned when "awsRegion" is missing from the config. | ||
ErrAwsAMPNoAwsRegion = errors.New("no awsRegion given") | ||
) | ||
|
||
// RoundTrip adds the roundTrip logic so that the request is SigV4 signed | ||
func (rt *roundTripper) RoundTrip(req *http.Request) (*http.Response, error) { | ||
cred, err := rt.client.Options().Credentials.Retrieve(req.Context()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// We need to sign the request because giving an empty string (not a hashed empty string) | ||
// fails in the backend as not signed request hence the following value is used | ||
// "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" is the sha256 of "" | ||
const reqHash = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" | ||
|
||
err = rt.client.Options().HTTPSignerV4.SignHTTP(req.Context(), cred, req, reqHash, "aps", rt.region, time.Now()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// Create default transport | ||
transport := httputils.CreateHTTPTransport(false) | ||
|
||
// Send signed request | ||
return transport.RoundTrip(req) | ||
} | ||
|
||
// parseAwsAMPMetadata parses the data to get the AWS sepcific auth info and metadata | ||
func parseAwsAMPMetadata(config *scalersconfig.ScalerConfig) (*awsConfigMetadata, error) { | ||
meta := awsConfigMetadata{} | ||
|
||
auth, err := GetAwsAuthorization(config.TriggerUniqueKey, config.PodIdentity, config.TriggerMetadata, config.AuthParams, config.ResolvedEnv) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
meta.awsAuthorization = auth | ||
return &meta, nil | ||
} | ||
|
||
// NewSigV4RoundTripper returns a new http.RoundTripper that will sign requests | ||
// using Amazon's Signature Verification V4 signing procedure. The request will | ||
// then be handed off to the next RoundTripper provided by next. If next is nil, | ||
// http.DefaultTransport will be used. | ||
// | ||
// Credentials for signing are retrieving used the default AWS credential chain. | ||
// If credentials could not be found, an error will be returned. | ||
func NewSigV4RoundTripper(config *scalersconfig.ScalerConfig) (http.RoundTripper, error) { | ||
// parseAwsAMPMetadata can return an error if AWS info is missing | ||
// but this can happen if we check for them on not AWS scalers | ||
// which is probably the reason to create a SigV4RoundTripper. | ||
// To prevent failures we check if the metadata is nil | ||
// (missing AWS info) and we hide the error | ||
metadata, _ := parseAwsAMPMetadata(config) | ||
if metadata == nil { | ||
return nil, nil | ||
} | ||
awsCfg, err := GetAwsConfig(context.Background(), metadata.awsRegion, metadata.awsAuthorization) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
client := amp.NewFromConfig(*awsCfg, func(o *amp.Options) {}) | ||
rt := &roundTripper{ | ||
client: client, | ||
region: metadata.awsRegion, | ||
} | ||
|
||
return rt, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
Copyright 2024 The KEDA Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
/* | ||
This file contains all the logic for caching aws.Config across all the (AWS) | ||
triggers. The first time when an aws.Config is requested, it's cached based on | ||
the authentication info (roleArn, Key&Secret, keda itself) and it's returned | ||
every time when an aws.Config is requested for the same authentication info. | ||
This is required because if we don't cache and share them, each scaler | ||
generates and refresh it's own token although all the tokens grants the same | ||
permissions | ||
*/ | ||
package aws | ||
|
||
import ( | ||
"net/http" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/kedacore/keda/v2/pkg/util" | ||
) | ||
|
||
func TestSigV4RoundTripper(t *testing.T) { | ||
transport := util.CreateHTTPTransport(false) | ||
|
||
cli := &http.Client{Transport: transport} | ||
|
||
req, err := http.NewRequest(http.MethodGet, "https://aps-workspaces.us-west-2.amazonaws.com/workspaces/ws-38377ca8-8db3-4b58-812d-b65a81837bb8/api/v1/query?query=vector(10)", strings.NewReader("Hello, world!")) | ||
require.NoError(t, err) | ||
r, err := cli.Do(req) | ||
require.NotEmpty(t, r) | ||
require.NoError(t, err) | ||
defer r.Body.Close() | ||
|
||
require.NotNil(t, req) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.