-
Notifications
You must be signed in to change notification settings - Fork 428
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): allow svc status to show auto scaling alarms (#1384)
<!-- Provide summary of changes --> Since we now enable auto scaling in #1355. This feature enable `svc status` to show any alarms created by auto scaling. After this PR `svc status` should be able to show alarms created by auto scaling or alarms with copilot tags. <!-- Issue number, if available. E.g. "Fixes #31", "Addresses #42, 77" --> By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
- Loading branch information
1 parent
148d7f1
commit d1c8f2e
Showing
10 changed files
with
507 additions
and
100 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// Package aas provides a client to make API requests to Application Auto Scaling. | ||
package aas | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
aas "github.com/aws/aws-sdk-go/service/applicationautoscaling" | ||
|
||
"github.com/aws/aws-sdk-go/aws/session" | ||
) | ||
|
||
const ( | ||
// ECS service resource ID format: service/${clusterName}/${serviceName}. | ||
fmtECSResourceID = "service/%s/%s" | ||
ecsServiceNamespace = "ecs" | ||
) | ||
|
||
type api interface { | ||
DescribeScalingPolicies(input *aas.DescribeScalingPoliciesInput) (*aas.DescribeScalingPoliciesOutput, error) | ||
} | ||
|
||
// ApplicationAutoscaling wraps an Amazon Application Auto Scaling client. | ||
type ApplicationAutoscaling struct { | ||
client api | ||
} | ||
|
||
// New returns a ApplicationAutoscaling struct configured against the input session. | ||
func New(s *session.Session) *ApplicationAutoscaling { | ||
return &ApplicationAutoscaling{ | ||
client: aas.New(s), | ||
} | ||
} | ||
|
||
// ECSServiceAlarmNames returns names of the CloudWatch alarms associated with the | ||
// scaling policies attached to the ECS service. | ||
func (a *ApplicationAutoscaling) ECSServiceAlarmNames(cluster, service string) ([]string, error) { | ||
resourceID := fmt.Sprintf(fmtECSResourceID, cluster, service) | ||
var alarms []string | ||
var err error | ||
resp := &aas.DescribeScalingPoliciesOutput{} | ||
for { | ||
resp, err = a.client.DescribeScalingPolicies(&aas.DescribeScalingPoliciesInput{ | ||
ResourceId: aws.String(resourceID), | ||
ServiceNamespace: aws.String(ecsServiceNamespace), | ||
NextToken: resp.NextToken, | ||
}) | ||
if err != nil { | ||
return nil, fmt.Errorf("describe scaling policies for ECS service %s/%s: %w", cluster, service, err) | ||
} | ||
for _, policy := range resp.ScalingPolicies { | ||
for _, alarm := range policy.Alarms { | ||
alarms = append(alarms, aws.StringValue(alarm.AlarmName)) | ||
} | ||
} | ||
if resp.NextToken == nil { | ||
break | ||
} | ||
} | ||
return alarms, 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,141 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package aas | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
aas "github.com/aws/aws-sdk-go/service/applicationautoscaling" | ||
"github.com/aws/copilot-cli/internal/pkg/aws/aas/mocks" | ||
"github.com/golang/mock/gomock" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type aasMocks struct { | ||
client *mocks.Mockapi | ||
} | ||
|
||
func TestCloudWatch_ECSServiceAutoscalingAlarms(t *testing.T) { | ||
const ( | ||
mockCluster = "mockCluster" | ||
mockService = "mockService" | ||
mockResourceID = "service/mockCluster/mockService" | ||
mockNextToken = "mockNextToken" | ||
) | ||
mockError := errors.New("some error") | ||
|
||
testCases := map[string]struct { | ||
setupMocks func(m aasMocks) | ||
|
||
wantErr error | ||
wantAlarmNames []string | ||
}{ | ||
"errors if failed to retrieve auto scaling alarm names": { | ||
setupMocks: func(m aasMocks) { | ||
m.client.EXPECT().DescribeScalingPolicies(gomock.Any()).Return(nil, mockError) | ||
}, | ||
|
||
wantErr: fmt.Errorf("describe scaling policies for ECS service mockCluster/mockService: some error"), | ||
}, | ||
"success": { | ||
setupMocks: func(m aasMocks) { | ||
m.client.EXPECT().DescribeScalingPolicies(&aas.DescribeScalingPoliciesInput{ | ||
ResourceId: aws.String(mockResourceID), | ||
ServiceNamespace: aws.String(ecsServiceNamespace), | ||
}).Return(&aas.DescribeScalingPoliciesOutput{ | ||
ScalingPolicies: []*aas.ScalingPolicy{ | ||
{ | ||
Alarms: []*aas.Alarm{ | ||
{ | ||
AlarmName: aws.String("mockAlarm1"), | ||
}, | ||
{ | ||
AlarmName: aws.String("mockAlarm2"), | ||
}, | ||
}, | ||
}, | ||
{ | ||
Alarms: []*aas.Alarm{ | ||
{ | ||
AlarmName: aws.String("mockAlarm3"), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, nil) | ||
}, | ||
|
||
wantAlarmNames: []string{"mockAlarm1", "mockAlarm2", "mockAlarm3"}, | ||
}, | ||
"success with pagination": { | ||
setupMocks: func(m aasMocks) { | ||
gomock.InOrder( | ||
m.client.EXPECT().DescribeScalingPolicies(&aas.DescribeScalingPoliciesInput{ | ||
ResourceId: aws.String(mockResourceID), | ||
ServiceNamespace: aws.String(ecsServiceNamespace), | ||
}).Return(&aas.DescribeScalingPoliciesOutput{ | ||
ScalingPolicies: []*aas.ScalingPolicy{ | ||
{ | ||
Alarms: []*aas.Alarm{ | ||
{ | ||
AlarmName: aws.String("mockAlarm1"), | ||
}, | ||
}, | ||
}, | ||
}, | ||
NextToken: aws.String(mockNextToken), | ||
}, nil), | ||
m.client.EXPECT().DescribeScalingPolicies(&aas.DescribeScalingPoliciesInput{ | ||
ResourceId: aws.String(mockResourceID), | ||
ServiceNamespace: aws.String(ecsServiceNamespace), | ||
NextToken: aws.String(mockNextToken), | ||
}).Return(&aas.DescribeScalingPoliciesOutput{ | ||
ScalingPolicies: []*aas.ScalingPolicy{ | ||
{ | ||
Alarms: []*aas.Alarm{ | ||
{ | ||
AlarmName: aws.String("mockAlarm2"), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, nil), | ||
) | ||
}, | ||
|
||
wantAlarmNames: []string{"mockAlarm1", "mockAlarm2"}, | ||
}, | ||
} | ||
|
||
for name, tc := range testCases { | ||
t.Run(name, func(t *testing.T) { | ||
// GIVEN | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
mockClient := mocks.NewMockapi(ctrl) | ||
mocks := aasMocks{ | ||
client: mockClient, | ||
} | ||
|
||
tc.setupMocks(mocks) | ||
|
||
aasSvc := ApplicationAutoscaling{ | ||
client: mockClient, | ||
} | ||
|
||
gotAlarmNames, gotErr := aasSvc.ECSServiceAlarmNames(mockCluster, mockService) | ||
|
||
if gotErr != nil { | ||
require.EqualError(t, tc.wantErr, gotErr.Error()) | ||
} else { | ||
require.Equal(t, tc.wantAlarmNames, gotAlarmNames) | ||
} | ||
}) | ||
|
||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.