Skip to content

Commit

Permalink
fix: aws ecs health
Browse files Browse the repository at this point in the history
  • Loading branch information
moshloop committed Nov 20, 2024
1 parent 0a53795 commit 460ef65
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 15 deletions.
20 changes: 10 additions & 10 deletions pkg/health/health_aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@ func GetAWSResourceHealth(_, status string) (health HealthStatus) {
}

func getAWSHealthByConfigType(configType string, obj map[string]any, states ...string) HealthStatus {
switch configType {
case "AWS::ECS::Task":
switch strings.ToLower(configType) {
case "aws::ecs::task":
return GetECSTaskHealth(obj)
case "AWS::Cloudformation::Stack":
case "aws::cloudformation::stack":
return GetHealthFromStatusName(get(obj, "StackStatus"), get(obj, "StackStatusReason"))
case "AWS::EC2::Instance":
case "aws::ec2::instance":
return GetHealthFromStatusName(get(obj, "State"))
case "AWS::RDS::DBInstance":
case "aws::rds::dbinstance":
return GetHealthFromStatusName(get(obj, "DBInstanceStatus"))
case "AWS::ElasticLoadBalancing::LoadBalancer":
case "aws::elasticloadbalancing::loadbalancer":
return GetHealthFromStatusName(get(obj, "State", "Code"))
case "AWS::AutoScaling::AutoScalingGroup":
case "aws::autoscaling::autoscalinggroup":
return GetHealthFromStatusName(get(obj, "Status"))
case "AWS::Lambda::Function":
case "aws::lambda::function":
return GetHealthFromStatusName(get(obj, "State"), get(obj, "StateReasonCode"))
case "AWS::DynamoDB::Table":
case "aws::dynamodb::table":
return GetHealthFromStatusName(get(obj, "TableStatus"))
case "AWS::ElastiCache::CacheCluster":
case "aws::elasticache::cachecluster":
return GetHealthFromStatusName(get(obj, "CacheClusterStatus"))
}

Expand Down
6 changes: 2 additions & 4 deletions pkg/health/health_aws_ecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@ package health

import (
"strings"

"github.com/samber/lo"
)

func GetECSTaskHealth(obj map[string]any) (health HealthStatus) {
hr := HealthStatus{
Status: HealthStatusCode(lo.CamelCase(obj["LastStatus"].(string))),
Status: HealthStatusCode(HumanCase(obj["LastStatus"].(string))),
Health: HealthUnknown,
Ready: false,
}

if v, ok := obj["HealthStatus"].(string); ok {
if v, ok := obj["HealthStatus"].(string); ok && v != "UNKNOWN" {
hr.Status = HealthStatusCode(HumanCase(v))
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/health/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func testFixture(t *testing.T, yamlPath string) {
}

if v, ok := obj.GetAnnotations()["expected-health"]; ok {
assert.Equal(t, v, hr.Health)
assert.Equal(t, health.Health(v), hr.Health)
}

if v, ok := obj.GetAnnotations()["expected-message"]; ok {
Expand Down
75 changes: 75 additions & 0 deletions pkg/health/testdata/AWS/ECS/Task/running_healthy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
annotations:
expected-status: Running
expected-health: healthy
Attachments:
- Id: beb2fa58-3e15-4877-b097-b8e0a4c91f04
Type: ElasticNetworkInterface
Status: ATTACHED
Details:
- Name: subnetId
Value: subnet-099e59970db9eeb65
- Name: networkInterfaceId
Value: eni-099e55471b484321b
- Name: macAddress
Value: 0a:f5:69:ba:ad:53
- Name: privateDnsName
Value: ip-172-31-44-167.eu-west-1.compute.internal
- Name: privateIPv4Address
Value: 172.31.44.167
Attributes:
- Name: ecs.cpu-architecture
Value: x86_64
TargetType: ""
AvailabilityZone: eu-west-1b
ClusterArn: arn:aws:ecs:eu-west-1:123456789012:cluster/demo-dev-cluster
Connectivity: CONNECTED
ConnectivityAt: 2024-11-01T17:50:30.827Z
Containers:
- Cpu: "0"
Name: demo-another-container
Image: httpd:latest
GpuIds: []
TaskArn: arn:aws:ecs:eu-west-1:123456789012:task/demo-dev-cluster/d8f8db0d67e34774935b2fbbc82961a3
RuntimeId: d8f8db0d67e34774935b2fbbc82961a3-1560160490
LastStatus: RUNNING
ImageDigest: sha256:bbea29057f25d9543e6a96a8e3cc7c7c937206d20eab2323f478fdb2469d536d
ContainerArn: arn:aws:ecs:eu-west-1:123456789012:container/demo-dev-cluster/d8f8db0d67e34774935b2fbbc82961a3/edf9b73b-b387-480e-954f-2f8dcd531a70
HealthStatus: UNKNOWN
ManagedAgents: []
NetworkBindings: []
NetworkInterfaces:
- AttachmentId: beb2fa58-3e15-4877-b097-b8e0a4c91f04
PrivateIpv4Address: 172.31.44.167
Cpu: "512"
CreatedAt: 2024-11-01T17:50:27.075Z
DesiredStatus: RUNNING
EnableExecuteCommand: false
EphemeralStorage:
SizeInGiB: 20
FargateEphemeralStorage:
SizeInGiB: 20
Group: service:httpd-service
HealthStatus: UNKNOWN
InferenceAccelerators: []
LastStatus: RUNNING
LaunchType: FARGATE
Memory: "1024"
Overrides:
ContainerOverrides:
- Name: demo-another-container
Command: []
Environment: []
EnvironmentFiles: []
ResourceRequirements: []
InferenceAcceleratorOverrides: []
PlatformFamily: Linux
PlatformVersion: 1.4.0
PullStartedAt: 2024-11-01T17:50:37.967Z
PullStoppedAt: 2024-11-01T17:50:44.584Z
StartedAt: 2024-11-01T17:50:44.935Z
StartedBy: ecs-svc/4462357315744281090
StopCode: ""
Tags: []
TaskArn: arn:aws:ecs:eu-west-1:123456789012:task/demo-dev-cluster/d8f8db0d67e34774935b2fbbc82961a3
TaskDefinitionArn: arn:aws:ecs:eu-west-1:123456789012:task-definition/demo-httpd-task:1
Version: 4

0 comments on commit 460ef65

Please sign in to comment.