Skip to content

Commit

Permalink
[chore]: enable gofumpt linter for internal (#36369)
Browse files Browse the repository at this point in the history
#### Description

[gofumpt](https://golangci-lint.run/usage/linters/#gofumpt) enforces a
stricter format than gofmt

Part of
#36363

Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
  • Loading branch information
mmorel-35 authored Nov 15, 2024
1 parent 40dbf0b commit c406f44
Show file tree
Hide file tree
Showing 37 changed files with 188 additions and 157 deletions.
10 changes: 6 additions & 4 deletions internal/aws/awsutil/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ const (

// newHTTPClient returns new HTTP client instance with provided configuration.
func newHTTPClient(logger *zap.Logger, maxIdle int, requestTimeout int, noVerify bool,
proxyAddress string) (*http.Client, error) {
proxyAddress string,
) (*http.Client, error) {
logger.Debug("Using proxy address: ",
zap.String("proxyAddr", proxyAddress),
)
Expand Down Expand Up @@ -206,7 +207,6 @@ func (c *Conn) newAWSSession(logger *zap.Logger, roleArn string, region string)
s, err = session.NewSession(&aws.Config{
Credentials: stsCreds,
})

if err != nil {
logger.Error("Error in creating session object : ", zap.Error(err))
return s, err
Expand Down Expand Up @@ -245,7 +245,8 @@ func getSTSCreds(logger *zap.Logger, region string, roleArn string) (*credential
// AWS STS recommends that you provide both the Region and endpoint when you make calls to a Regional endpoint.
// Reference: https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_temp_enable-regions.html#id_credentials_temp_enable-regions_writing_code
func getSTSCredsFromRegionEndpoint(logger *zap.Logger, sess *session.Session, region string,
roleArn string) *credentials.Credentials {
roleArn string,
) *credentials.Credentials {
regionalEndpoint := getSTSRegionalEndpoint(region)
// if regionalEndpoint is "", the STS endpoint is Global endpoint for classic regions except ap-east-1 - (HKG)
// for other opt-in regions, region value will create STS regional endpoint.
Expand All @@ -259,7 +260,8 @@ func getSTSCredsFromRegionEndpoint(logger *zap.Logger, sess *session.Session, re
// getSTSCredsFromPrimaryRegionEndpoint fetches STS credentials for provided roleARN from primary region endpoint in
// the respective partition.
func getSTSCredsFromPrimaryRegionEndpoint(logger *zap.Logger, t *session.Session, roleArn string,
region string) *credentials.Credentials {
region string,
) *credentials.Credentials {
logger.Info("Credentials for provided RoleARN being fetched from STS primary region endpoint.")
partitionID := getPartition(region)
switch partitionID {
Expand Down
2 changes: 1 addition & 1 deletion internal/aws/awsutil/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestRegionEnv(t *testing.T) {
region := "us-east-1"
t.Setenv("AWS_REGION", region)

var m = &mockConn{}
m := &mockConn{}
var expectedSession *session.Session
expectedSession, _ = session.NewSession()
m.sn = expectedSession
Expand Down
3 changes: 2 additions & 1 deletion internal/aws/containerinsight/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ func convertToFloat64(value any) float64 {
}

func checkMetricsAreExpected(t *testing.T, md pmetric.Metrics, fields map[string]any, tags map[string]string,
expectedUnits map[string]string) {
expectedUnits map[string]string,
) {
rms := md.ResourceMetrics()
assert.Equal(t, 1, rms.Len())

Expand Down
12 changes: 6 additions & 6 deletions internal/aws/cwlogs/cwlog_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ const (
errCodeThrottlingException = "ThrottlingException"
)

var (
containerInsightsRegexPattern = regexp.MustCompile(`^/aws/.*containerinsights/.*/(performance|prometheus)$`)
)
var containerInsightsRegexPattern = regexp.MustCompile(`^/aws/.*containerinsights/.*/(performance|prometheus)$`)

// Possible exceptions are combination of common errors (https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/CommonErrors.html)
// and API specific erros (e.g. https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_PutLogEvents.html#API_PutLogEvents_Errors)
Expand All @@ -53,10 +51,12 @@ func WithUserAgentExtras(userAgentExtras ...string) ClientOption {

// Create a log client based on the actual cloudwatch logs client.
func newCloudWatchLogClient(svc cloudwatchlogsiface.CloudWatchLogsAPI, logRetention int64, tags map[string]*string, logger *zap.Logger) *Client {
logClient := &Client{svc: svc,
logClient := &Client{
svc: svc,
logRetention: logRetention,
tags: tags,
logger: logger}
logger: logger,
}
return logClient
}

Expand Down Expand Up @@ -124,7 +124,7 @@ func (client *Client) PutLogEvents(input *cloudwatchlogs.PutLogEventsInput, retr
}
}

//TODO: Should have metrics to provide visibility of these failures
// TODO: Should have metrics to provide visibility of these failures
if response != nil {
if response.RejectedLogEventsInfo != nil {
rejectedLogEventsInfo := response.RejectedLogEventsInfo
Expand Down
60 changes: 39 additions & 21 deletions internal/aws/cwlogs/cwlog_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ func newAlwaysPassMockLogClient(putLogEventsFunc func(args mock.Arguments)) *Cli

svc.On("PutLogEvents", mock.Anything).Return(
&cloudwatchlogs.PutLogEventsOutput{
NextSequenceToken: &expectedNextSequenceToken},
NextSequenceToken: &expectedNextSequenceToken,
},
nil).Run(putLogEventsFunc)

svc.On("CreateLogGroup", mock.Anything).Return(new(cloudwatchlogs.CreateLogGroupOutput), nil)
Expand All @@ -36,7 +37,8 @@ func newAlwaysPassMockLogClient(putLogEventsFunc func(args mock.Arguments)) *Cli

svc.On("DescribeLogStreams", mock.Anything).Return(
&cloudwatchlogs.DescribeLogStreamsOutput{
LogStreams: []*cloudwatchlogs.LogStream{{UploadSequenceToken: &expectedNextSequenceToken}}},
LogStreams: []*cloudwatchlogs.LogStream{{UploadSequenceToken: &expectedNextSequenceToken}},
},
nil)
return newCloudWatchLogClient(svc, 0, nil, logger)
}
Expand Down Expand Up @@ -77,11 +79,13 @@ func (svc *mockCloudWatchLogsClient) TagResource(input *cloudwatchlogs.TagResour
}

// Tests
var previousSequenceToken = "0000"
var expectedNextSequenceToken = "1111"
var logGroup = "logGroup"
var logStreamName = "logStream"
var emptySequenceToken = ""
var (
previousSequenceToken = "0000"
expectedNextSequenceToken = "1111"
logGroup = "logGroup"
logStreamName = "logStream"
emptySequenceToken = ""
)

func TestPutLogEvents_HappyCase(t *testing.T) {
logger := zap.NewNop()
Expand All @@ -92,7 +96,8 @@ func TestPutLogEvents_HappyCase(t *testing.T) {
SequenceToken: &previousSequenceToken,
}
putLogEventsOutput := &cloudwatchlogs.PutLogEventsOutput{
NextSequenceToken: &expectedNextSequenceToken}
NextSequenceToken: &expectedNextSequenceToken,
}

svc.On("PutLogEvents", putLogEventsInput).Return(putLogEventsOutput, nil)

Expand All @@ -114,7 +119,8 @@ func TestPutLogEvents_HappyCase_SomeRejectedInfo(t *testing.T) {
rejectedLogEventsInfo := &cloudwatchlogs.RejectedLogEventsInfo{
ExpiredLogEventEndIndex: aws.Int64(1),
TooNewLogEventStartIndex: aws.Int64(2),
TooOldLogEventEndIndex: aws.Int64(3)}
TooOldLogEventEndIndex: aws.Int64(3),
}
putLogEventsOutput := &cloudwatchlogs.PutLogEventsOutput{
NextSequenceToken: &expectedNextSequenceToken,
RejectedLogEventsInfo: rejectedLogEventsInfo,
Expand All @@ -138,7 +144,8 @@ func TestPutLogEvents_NonAWSError(t *testing.T) {
SequenceToken: &previousSequenceToken,
}
putLogEventsOutput := &cloudwatchlogs.PutLogEventsOutput{
NextSequenceToken: &expectedNextSequenceToken}
NextSequenceToken: &expectedNextSequenceToken,
}

svc.On("PutLogEvents", putLogEventsInput).Return(putLogEventsOutput, errors.New("some random error")).Once()

Expand All @@ -158,7 +165,8 @@ func TestPutLogEvents_InvalidParameterException(t *testing.T) {
SequenceToken: &previousSequenceToken,
}
putLogEventsOutput := &cloudwatchlogs.PutLogEventsOutput{
NextSequenceToken: &expectedNextSequenceToken}
NextSequenceToken: &expectedNextSequenceToken,
}

invalidParameterException := &cloudwatchlogs.InvalidParameterException{}
svc.On("PutLogEvents", putLogEventsInput).Return(putLogEventsOutput, invalidParameterException).Once()
Expand All @@ -179,7 +187,8 @@ func TestPutLogEvents_OperationAbortedException(t *testing.T) {
SequenceToken: &previousSequenceToken,
}
putLogEventsOutput := &cloudwatchlogs.PutLogEventsOutput{
NextSequenceToken: &expectedNextSequenceToken}
NextSequenceToken: &expectedNextSequenceToken,
}

operationAbortedException := &cloudwatchlogs.OperationAbortedException{}
svc.On("PutLogEvents", putLogEventsInput).Return(putLogEventsOutput, operationAbortedException).Once()
Expand All @@ -200,7 +209,8 @@ func TestPutLogEvents_ServiceUnavailableException(t *testing.T) {
SequenceToken: &previousSequenceToken,
}
putLogEventsOutput := &cloudwatchlogs.PutLogEventsOutput{
NextSequenceToken: &expectedNextSequenceToken}
NextSequenceToken: &expectedNextSequenceToken,
}

serviceUnavailableException := &cloudwatchlogs.ServiceUnavailableException{}
svc.On("PutLogEvents", putLogEventsInput).Return(putLogEventsOutput, serviceUnavailableException).Once()
Expand All @@ -221,7 +231,8 @@ func TestPutLogEvents_UnknownException(t *testing.T) {
SequenceToken: &previousSequenceToken,
}
putLogEventsOutput := &cloudwatchlogs.PutLogEventsOutput{
NextSequenceToken: &expectedNextSequenceToken}
NextSequenceToken: &expectedNextSequenceToken,
}

unknownException := awserr.New("unknownException", "", nil)
svc.On("PutLogEvents", putLogEventsInput).Return(putLogEventsOutput, unknownException).Once()
Expand All @@ -242,7 +253,8 @@ func TestPutLogEvents_ThrottlingException(t *testing.T) {
SequenceToken: &previousSequenceToken,
}
putLogEventsOutput := &cloudwatchlogs.PutLogEventsOutput{
NextSequenceToken: &expectedNextSequenceToken}
NextSequenceToken: &expectedNextSequenceToken,
}

throttlingException := awserr.New(errCodeThrottlingException, "", nil)
svc.On("PutLogEvents", putLogEventsInput).Return(putLogEventsOutput, throttlingException).Once()
Expand All @@ -264,7 +276,8 @@ func TestPutLogEvents_ResourceNotFoundException(t *testing.T) {
}

putLogEventsOutput := &cloudwatchlogs.PutLogEventsOutput{
NextSequenceToken: &expectedNextSequenceToken}
NextSequenceToken: &expectedNextSequenceToken,
}
awsErr := &cloudwatchlogs.ResourceNotFoundException{}

svc.On("PutLogEvents", putLogEventsInput).Return(putLogEventsOutput, awsErr).Once()
Expand All @@ -291,7 +304,8 @@ func TestLogRetention_NeverExpire(t *testing.T) {
}

putLogEventsOutput := &cloudwatchlogs.PutLogEventsOutput{
NextSequenceToken: &expectedNextSequenceToken}
NextSequenceToken: &expectedNextSequenceToken,
}
awsErr := &cloudwatchlogs.ResourceNotFoundException{}

svc.On("PutLogEvents", putLogEventsInput).Return(putLogEventsOutput, awsErr).Once()
Expand Down Expand Up @@ -326,7 +340,8 @@ func TestLogRetention_RetentionDaysInputted(t *testing.T) {
}

putLogEventsOutput := &cloudwatchlogs.PutLogEventsOutput{
NextSequenceToken: &expectedNextSequenceToken}
NextSequenceToken: &expectedNextSequenceToken,
}
awsErr := &cloudwatchlogs.ResourceNotFoundException{}

svc.On("PutLogEvents", putLogEventsInput).Return(putLogEventsOutput, awsErr).Once()
Expand Down Expand Up @@ -362,7 +377,8 @@ func TestSetTags_NotCalled(t *testing.T) {
}

putLogEventsOutput := &cloudwatchlogs.PutLogEventsOutput{
NextSequenceToken: &expectedNextSequenceToken}
NextSequenceToken: &expectedNextSequenceToken,
}
awsErr := &cloudwatchlogs.ResourceNotFoundException{}

svc.On("PutLogEvents", putLogEventsInput).Return(putLogEventsOutput, awsErr).Once()
Expand Down Expand Up @@ -397,7 +413,8 @@ func TestSetTags_Called(t *testing.T) {
}

putLogEventsOutput := &cloudwatchlogs.PutLogEventsOutput{
NextSequenceToken: &expectedNextSequenceToken}
NextSequenceToken: &expectedNextSequenceToken,
}
awsErr := &cloudwatchlogs.ResourceNotFoundException{}

avalue := "avalue"
Expand Down Expand Up @@ -433,7 +450,8 @@ func TestPutLogEvents_AllRetriesFail(t *testing.T) {
}

putLogEventsOutput := &cloudwatchlogs.PutLogEventsOutput{
NextSequenceToken: nil}
NextSequenceToken: nil,
}
awsErr := &cloudwatchlogs.ResourceNotFoundException{}

svc.On("PutLogEvents", putLogEventsInput).Return(putLogEventsOutput, awsErr).Twice()
Expand Down
17 changes: 9 additions & 8 deletions internal/aws/cwlogs/pusher.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ const (
evenTimestampLimitInFuture = -2 * time.Hour // None of the log events in the batch can be more than 2 hours in the future.
)

var (
maxEventPayloadBytes = defaultMaxEventPayloadBytes
)
var maxEventPayloadBytes = defaultMaxEventPayloadBytes

// Event struct to present a log event.
type Event struct {
Expand All @@ -48,7 +46,8 @@ func NewEvent(timestampMs int64, message string) *Event {
event := &Event{
InputLogEvent: &cloudwatchlogs.InputLogEvent{
Timestamp: aws.Int64(timestampMs),
Message: aws.String(message)},
Message: aws.String(message),
},
}
return event
}
Expand Down Expand Up @@ -115,7 +114,8 @@ func newEventBatch(key StreamKey) *eventBatch {
putLogEventsInput: &cloudwatchlogs.PutLogEventsInput{
LogGroupName: aws.String(key.LogGroupName),
LogStreamName: aws.String(key.LogStreamName),
LogEvents: make([]*cloudwatchlogs.InputLogEvent, 0, maxRequestEventCount)},
LogEvents: make([]*cloudwatchlogs.InputLogEvent, 0, maxRequestEventCount),
},
}
}

Expand Down Expand Up @@ -194,7 +194,8 @@ type logPusher struct {

// NewPusher creates a logPusher instance
func NewPusher(streamKey StreamKey, retryCnt int,
svcStructuredLog Client, logger *zap.Logger) Pusher {
svcStructuredLog Client, logger *zap.Logger,
) Pusher {
pusher := newLogPusher(streamKey, svcStructuredLog, logger)

pusher.retryCnt = defaultRetryCount
Expand All @@ -207,7 +208,8 @@ func NewPusher(streamKey StreamKey, retryCnt int,

// Only create a logPusher, but not start the instance.
func newLogPusher(streamKey StreamKey,
svcStructuredLog Client, logger *zap.Logger) *logPusher {
svcStructuredLog Client, logger *zap.Logger,
) *logPusher {
pusher := &logPusher{
logGroupName: aws.String(streamKey.LogGroupName),
logStreamName: aws.String(streamKey.LogStreamName),
Expand Down Expand Up @@ -260,7 +262,6 @@ func (p *logPusher) pushEventBatch(req any) error {
startTime := time.Now()

err := p.svcStructuredLog.PutLogEvents(putLogEventsInput, p.retryCnt)

if err != nil {
return err
}
Expand Down
10 changes: 7 additions & 3 deletions internal/aws/cwlogs/pusher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ func TestLogEventBatch_sortLogEvents(t *testing.T) {
totalEvents := 10
logEventBatch := &eventBatch{
putLogEventsInput: &cloudwatchlogs.PutLogEventsInput{
LogEvents: make([]*cloudwatchlogs.InputLogEvent, 0, totalEvents)}}
LogEvents: make([]*cloudwatchlogs.InputLogEvent, 0, totalEvents),
},
}

for i := 0; i < totalEvents; i++ {
timestamp := rand.Int()
Expand Down Expand Up @@ -120,8 +122,10 @@ func newMockPusher() *logPusher {
// pusher Tests
//

var timestampMs = time.Now().UnixNano() / 1e6
var msg = "test log message"
var (
timestampMs = time.Now().UnixNano() / 1e6
msg = "test log message"
)

func TestPusher_newLogEventBatch(t *testing.T) {
p := newMockPusher()
Expand Down
2 changes: 0 additions & 2 deletions internal/aws/ecsutil/metadata_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ func (md *ecsMetadataProviderImpl) FetchTaskMetadata() (*TaskMetadata, error) {
taskMetadata := &TaskMetadata{}

err = json.NewDecoder(bytes.NewReader(resp)).Decode(taskMetadata)

if err != nil {
return nil, fmt.Errorf("encountered unexpected error reading response from ECS Task Metadata Endpoint: %w", err)
}
Expand All @@ -82,7 +81,6 @@ func (md *ecsMetadataProviderImpl) FetchContainerMetadata() (*ContainerMetadata,
containerMetadata := &ContainerMetadata{}

err = json.NewDecoder(bytes.NewReader(resp)).Decode(containerMetadata)

if err != nil {
return nil, fmt.Errorf("encountered unexpected error reading response from ECS Container Metadata Endpoint: %w", err)
}
Expand Down
6 changes: 2 additions & 4 deletions internal/aws/k8s/k8sclient/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ import (
"k8s.io/apimachinery/pkg/runtime"
)

type mockReflectorSyncChecker struct {
}
type mockReflectorSyncChecker struct{}

func (m *mockReflectorSyncChecker) Check(_ cacheReflector, _ string) {

}

var kubeConfigPath string
Expand Down Expand Up @@ -54,7 +52,7 @@ users:
if err != nil {
t.Error(err)
}
if err := os.WriteFile(tmpfile.Name(), []byte(content), 0600); err != nil {
if err := os.WriteFile(tmpfile.Name(), []byte(content), 0o600); err != nil {
t.Error(err)
}
// overwrite the default kube config path
Expand Down
Loading

0 comments on commit c406f44

Please sign in to comment.