1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Merge pull request #37374 from haikuoliu/branch_endpoint

Pass endpoint to the CloudWatch Logs logging driver
This commit is contained in:
Sebastiaan van Stijn 2018-06-30 11:22:55 +02:00 committed by GitHub
commit 8d1b280a25
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 29 deletions

View file

@ -29,6 +29,7 @@ import (
const ( const (
name = "awslogs" name = "awslogs"
regionKey = "awslogs-region" regionKey = "awslogs-region"
endpointKey = "awslogs-endpoint"
regionEnvKey = "AWS_REGION" regionEnvKey = "AWS_REGION"
logGroupKey = "awslogs-group" logGroupKey = "awslogs-group"
logStreamKey = "awslogs-stream" logStreamKey = "awslogs-stream"
@ -111,11 +112,11 @@ type eventBatch struct {
// New creates an awslogs logger using the configuration passed in on the // New creates an awslogs logger using the configuration passed in on the
// context. Supported context configuration variables are awslogs-region, // context. Supported context configuration variables are awslogs-region,
// awslogs-group, awslogs-stream, awslogs-create-group, awslogs-multiline-pattern // awslogs-endpoint, awslogs-group, awslogs-stream, awslogs-create-group,
// and awslogs-datetime-format. When available, configuration is // awslogs-multiline-pattern and awslogs-datetime-format.
// also taken from environment variables AWS_REGION, AWS_ACCESS_KEY_ID, // When available, configuration is also taken from environment variables
// AWS_SECRET_ACCESS_KEY, the shared credentials file (~/.aws/credentials), and // AWS_REGION, AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, the shared credentials
// the EC2 Instance Metadata Service. // file (~/.aws/credentials), and the EC2 Instance Metadata Service.
func New(info logger.Info) (logger.Logger, error) { func New(info logger.Info) (logger.Logger, error) {
logGroupName := info.Config[logGroupKey] logGroupName := info.Config[logGroupKey]
logStreamName, err := loggerutils.ParseLogTag(info, "{{.FullID}}") logStreamName, err := loggerutils.ParseLogTag(info, "{{.FullID}}")
@ -262,13 +263,16 @@ var newSDKEndpoint = credentialsEndpoint
// User-Agent string and automatic region detection using the EC2 Instance // User-Agent string and automatic region detection using the EC2 Instance
// Metadata Service when region is otherwise unspecified. // Metadata Service when region is otherwise unspecified.
func newAWSLogsClient(info logger.Info) (api, error) { func newAWSLogsClient(info logger.Info) (api, error) {
var region *string var region, endpoint *string
if os.Getenv(regionEnvKey) != "" { if os.Getenv(regionEnvKey) != "" {
region = aws.String(os.Getenv(regionEnvKey)) region = aws.String(os.Getenv(regionEnvKey))
} }
if info.Config[regionKey] != "" { if info.Config[regionKey] != "" {
region = aws.String(info.Config[regionKey]) region = aws.String(info.Config[regionKey])
} }
if info.Config[endpointKey] != "" {
endpoint = aws.String(info.Config[endpointKey])
}
if region == nil || *region == "" { if region == nil || *region == "" {
logrus.Info("Trying to get region from EC2 Metadata") logrus.Info("Trying to get region from EC2 Metadata")
ec2MetadataClient := newRegionFinder() ec2MetadataClient := newRegionFinder()
@ -290,6 +294,11 @@ func newAWSLogsClient(info logger.Info) (api, error) {
// attach region to cloudwatchlogs config // attach region to cloudwatchlogs config
sess.Config.Region = region sess.Config.Region = region
// attach endpoint to cloudwatchlogs config
if endpoint != nil {
sess.Config.Endpoint = endpoint
}
if uri, ok := info.Config[credentialsEndpointKey]; ok { if uri, ok := info.Config[credentialsEndpointKey]; ok {
logrus.Debugf("Trying to get credentials from awslogs-credentials-endpoint") logrus.Debugf("Trying to get credentials from awslogs-credentials-endpoint")
@ -606,7 +615,7 @@ func (l *logStream) putLogEvents(events []*cloudwatchlogs.InputLogEvent, sequenc
return resp.NextSequenceToken, nil return resp.NextSequenceToken, nil
} }
// ValidateLogOpt looks for awslogs-specific log options awslogs-region, // ValidateLogOpt looks for awslogs-specific log options awslogs-region, awslogs-endpoint
// awslogs-group, awslogs-stream, awslogs-create-group, awslogs-datetime-format, // awslogs-group, awslogs-stream, awslogs-create-group, awslogs-datetime-format,
// awslogs-multiline-pattern // awslogs-multiline-pattern
func ValidateLogOpt(cfg map[string]string) error { func ValidateLogOpt(cfg map[string]string) error {
@ -616,6 +625,7 @@ func ValidateLogOpt(cfg map[string]string) error {
case logStreamKey: case logStreamKey:
case logCreateGroupKey: case logCreateGroupKey:
case regionKey: case regionKey:
case endpointKey:
case tagKey: case tagKey:
case datetimeFormatKey: case datetimeFormatKey:
case multilinePatternKey: case multilinePatternKey:

View file

@ -67,13 +67,11 @@ func TestNewAWSLogsClientUserAgentHandler(t *testing.T) {
} }
client, err := newAWSLogsClient(info) client, err := newAWSLogsClient(info)
if err != nil { assert.NilError(t, err)
t.Fatal(err)
}
realClient, ok := client.(*cloudwatchlogs.CloudWatchLogs) realClient, ok := client.(*cloudwatchlogs.CloudWatchLogs)
if !ok { assert.Check(t, ok, "Could not cast client to cloudwatchlogs.CloudWatchLogs")
t.Fatal("Could not cast client to cloudwatchlogs.CloudWatchLogs")
}
buildHandlerList := realClient.Handlers.Build buildHandlerList := realClient.Handlers.Build
request := &request.Request{ request := &request.Request{
HTTPRequest: &http.Request{ HTTPRequest: &http.Request{
@ -90,6 +88,26 @@ func TestNewAWSLogsClientUserAgentHandler(t *testing.T) {
} }
} }
func TestNewAWSLogsClientAWSLogsEndpoint(t *testing.T) {
endpoint := "mock-endpoint"
info := logger.Info{
Config: map[string]string{
regionKey: "us-east-1",
endpointKey: endpoint,
},
}
client, err := newAWSLogsClient(info)
assert.NilError(t, err)
realClient, ok := client.(*cloudwatchlogs.CloudWatchLogs)
assert.Check(t, ok, "Could not cast client to cloudwatchlogs.CloudWatchLogs")
endpointWithScheme := realClient.Endpoint
expectedEndpointWithScheme := "https://" + endpoint
assert.Equal(t, endpointWithScheme, expectedEndpointWithScheme, "Wrong endpoint")
}
func TestNewAWSLogsClientRegionDetect(t *testing.T) { func TestNewAWSLogsClientRegionDetect(t *testing.T) {
info := logger.Info{ info := logger.Info{
Config: map[string]string{}, Config: map[string]string{},
@ -104,9 +122,7 @@ func TestNewAWSLogsClientRegionDetect(t *testing.T) {
} }
_, err := newAWSLogsClient(info) _, err := newAWSLogsClient(info)
if err != nil { assert.NilError(t, err)
t.Fatal(err)
}
} }
func TestCreateSuccess(t *testing.T) { func TestCreateSuccess(t *testing.T) {
@ -196,9 +212,7 @@ func TestCreateAlreadyExists(t *testing.T) {
err := stream.create() err := stream.create()
if err != nil { assert.NilError(t, err)
t.Fatal("Expected nil err")
}
} }
func TestLogClosed(t *testing.T) { func TestLogClosed(t *testing.T) {
@ -242,9 +256,8 @@ func TestLogBlocking(t *testing.T) {
} }
select { select {
case err := <-errorCh: case err := <-errorCh:
if err != nil { assert.NilError(t, err)
t.Fatal(err)
}
case <-time.After(30 * time.Second): case <-time.After(30 * time.Second):
t.Fatal("timed out waiting for read") t.Fatal("timed out waiting for read")
} }
@ -258,9 +271,7 @@ func TestLogNonBlockingBufferEmpty(t *testing.T) {
logNonBlocking: true, logNonBlocking: true,
} }
err := stream.Log(&logger.Message{}) err := stream.Log(&logger.Message{})
if err != nil { assert.NilError(t, err)
t.Fatal(err)
}
} }
func TestLogNonBlockingBufferFull(t *testing.T) { func TestLogNonBlockingBufferFull(t *testing.T) {
@ -1246,9 +1257,7 @@ func TestCreateTagSuccess(t *testing.T) {
err := stream.create() err := stream.create()
if err != nil { assert.NilError(t, err)
t.Errorf("Received unexpected err: %v\n", err)
}
argument := <-mockClient.createLogStreamArgument argument := <-mockClient.createLogStreamArgument
if *argument.LogStreamName != "test-container/container-abcdefghijklmnopqrstuvwxyz01234567890" { if *argument.LogStreamName != "test-container/container-abcdefghijklmnopqrstuvwxyz01234567890" {
@ -1340,7 +1349,6 @@ func TestNewAWSLogsClientCredentialEnvironmentVariable(t *testing.T) {
assert.Check(t, is.Equal(expectedAccessKeyID, creds.AccessKeyID)) assert.Check(t, is.Equal(expectedAccessKeyID, creds.AccessKeyID))
assert.Check(t, is.Equal(expectedSecretAccessKey, creds.SecretAccessKey)) assert.Check(t, is.Equal(expectedSecretAccessKey, creds.SecretAccessKey))
} }
func TestNewAWSLogsClientCredentialSharedFile(t *testing.T) { func TestNewAWSLogsClientCredentialSharedFile(t *testing.T) {