Merge pull request #39818 from thaJeztah/fix_awslogs_linting

awslogs: fix linting issues, and small refactoring
This commit is contained in:
Vincent Demeester 2019-09-11 09:14:38 +02:00 committed by GitHub
commit cd14846d0c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 25 deletions

View file

@ -308,8 +308,12 @@ var strftimeToRegex = map[string]string{
// newRegionFinder is a variable such that the implementation // newRegionFinder is a variable such that the implementation
// can be swapped out for unit tests. // can be swapped out for unit tests.
var newRegionFinder = func() regionFinder { var newRegionFinder = func() (regionFinder, error) {
return ec2metadata.New(session.New()) s, err := session.NewSession()
if err != nil {
return nil, err
}
return ec2metadata.New(s), nil
} }
// newSDKEndpoint is a variable such that the implementation // newSDKEndpoint is a variable such that the implementation
@ -333,12 +337,15 @@ func newAWSLogsClient(info logger.Info) (api, error) {
} }
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, err := newRegionFinder()
if err != nil {
logrus.WithError(err).Error("could not create EC2 metadata client")
return nil, errors.Wrap(err, "could not create EC2 metadata client")
}
r, err := ec2MetadataClient.Region() r, err := ec2MetadataClient.Region()
if err != nil { if err != nil {
logrus.WithFields(logrus.Fields{ logrus.WithError(err).Error("Could not get region from EC2 metadata, environment, or log option")
"error": err,
}).Error("Could not get region from EC2 metadata, environment, or log option")
return nil, errors.New("Cannot determine region for awslogs driver") return nil, errors.New("Cannot determine region for awslogs driver")
} }
region = &r region = &r
@ -429,25 +436,20 @@ func (l *logStream) Close() error {
// create creates log group and log stream for the instance of the awslogs logging driver // create creates log group and log stream for the instance of the awslogs logging driver
func (l *logStream) create() error { func (l *logStream) create() error {
if err := l.createLogStream(); err != nil { err := l.createLogStream()
if l.logCreateGroup { if err == nil {
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == resourceNotFoundCode { return nil
if err := l.createLogGroup(); err != nil { }
return errors.Wrap(err, "failed to create Cloudwatch log group") if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == resourceNotFoundCode && l.logCreateGroup {
} if err := l.createLogGroup(); err != nil {
err := l.createLogStream() return errors.Wrap(err, "failed to create Cloudwatch log group")
if err != nil {
return errors.Wrap(err, "failed to create Cloudwatch log stream")
}
return nil
}
} }
if err != nil { err = l.createLogStream()
return errors.Wrap(err, "failed to create Cloudwatch log stream") if err == nil {
return nil
} }
} }
return errors.Wrap(err, "failed to create Cloudwatch log stream")
return nil
} }
// createLogGroup creates a log group for the instance of the awslogs logging driver // createLogGroup creates a log group for the instance of the awslogs logging driver
@ -551,7 +553,6 @@ func (l *logStream) collectBatch(created chan bool) {
if !more { if !more {
// Flush event buffer and release resources // Flush event buffer and release resources
l.processEvent(batch, eventBuffer, eventBufferTimestamp) l.processEvent(batch, eventBuffer, eventBufferTimestamp)
eventBuffer = eventBuffer[:0]
l.publishBatch(batch) l.publishBatch(batch)
batch.reset() batch.reset()
return return

View file

@ -173,8 +173,8 @@ func TestNewAWSLogsClientRegionDetect(t *testing.T) {
} }
mockMetadata := newMockMetadataClient() mockMetadata := newMockMetadataClient()
newRegionFinder = func() regionFinder { newRegionFinder = func() (regionFinder, error) {
return mockMetadata return mockMetadata, nil
} }
mockMetadata.regionResult <- &regionResult{ mockMetadata.regionResult <- &regionResult{
successResult: "us-east-1", successResult: "us-east-1",