Skip to content

Commit

Permalink
feat: map aws statuses (#369)
Browse files Browse the repository at this point in the history
* feat: map aws statuses

[skip ci]

* chore: fix health.MapAWSStatus. supply resource type as well

* chore: upgrade is-healthy
  • Loading branch information
adityathebe authored Jan 30, 2024
1 parent 21f5489 commit b0fee8d
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 29 deletions.
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ require (
github.com/fergusstrange/embedded-postgres v1.25.0
github.com/flanksource/commons v1.20.0
github.com/flanksource/duty v1.0.282
github.com/flanksource/is-healthy v0.0.0-20231003215854-76c51e3a3ff7
github.com/flanksource/is-healthy v1.0.1
github.com/flanksource/ketall v1.1.3
github.com/flanksource/mapstructure v1.6.0
github.com/flanksource/postq v0.1.3
Expand All @@ -63,6 +63,7 @@ require (
github.com/onsi/gomega v1.29.0
github.com/pkg/errors v0.9.1
github.com/robfig/cron/v3 v3.0.1
github.com/samber/lo v1.39.0
github.com/sethvargo/go-retry v0.2.4
github.com/spf13/cobra v1.7.0
github.com/spf13/pflag v1.0.5
Expand Down Expand Up @@ -125,7 +126,6 @@ require (
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/robertkrimen/otto v0.2.1 // indirect
github.com/samber/lo v1.39.0 // indirect
github.com/sergi/go-diff v1.3.1 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/stoewer/go-strcase v1.3.0 // indirect
Expand Down Expand Up @@ -277,3 +277,5 @@ require (
// replace github.com/flanksource/duty => ../duty

// replace github.com/flanksource/postq => ../postq

// replace github.com/flanksource/is-healthy => ../is-healthy
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -843,8 +843,8 @@ github.com/flanksource/gomplate/v3 v3.20.4/go.mod h1:27BNWhzzSjDed1z8YShO6W+z6G9
github.com/flanksource/gomplate/v3 v3.20.30 h1:zRB1DVFIp64QiL6vkJJzVJK4Hj/PazwTNQ6+mDlHaxc=
github.com/flanksource/gomplate/v3 v3.20.30/go.mod h1:m2WVc04GMVBOcZhtDaz/LTtrVWKejeJhFM1Jy/h9VZQ=
github.com/flanksource/is-healthy v0.0.0-20230705092916-3b4cf510c5fc/go.mod h1:4pQhmF+TnVqJroQKY8wSnSp+T18oLson6YQ2M0qPHfQ=
github.com/flanksource/is-healthy v0.0.0-20231003215854-76c51e3a3ff7 h1:s6jf6P1pRfdvksVFjIXFRfnimvEYUR0/Mmla1EIjiRM=
github.com/flanksource/is-healthy v0.0.0-20231003215854-76c51e3a3ff7/go.mod h1:BH5gh9JyEAuuWVP6Q5y9h43VozS0RfKyjNpM9L4v4hw=
github.com/flanksource/is-healthy v1.0.1 h1:cr36GHndzge4rb6GDw+kSh5M6bUpglcZNbqqaTDAZeE=
github.com/flanksource/is-healthy v1.0.1/go.mod h1:cFejm0MapnJzgeoG3iizMv+tCIPthe0XqO+3nrhM79c=
github.com/flanksource/ketall v1.1.3 h1:M880vet2ypm7njAL2Da32qR1piFdoHMdRFHfLvxh6wE=
github.com/flanksource/ketall v1.1.3/go.mod h1:o6LPq4GxCn9wd5IE555Q8uVq7iiQsUYUS9q0LqpLIh8=
github.com/flanksource/kommons v0.31.4 h1:zksAgYjZuwPgS8XTejDIWEYB0nPSU1i3Jxcavm/vovI=
Expand Down
2 changes: 1 addition & 1 deletion hack/generate-schemas/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ require (
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/flanksource/duty v1.0.282 // indirect
github.com/flanksource/gomplate/v3 v3.20.30 // indirect
github.com/flanksource/is-healthy v0.0.0-20231003215854-76c51e3a3ff7 // indirect
github.com/flanksource/is-healthy v1.0.1 // indirect
github.com/flanksource/mapstructure v1.6.0 // indirect
github.com/flanksource/postq v0.1.3 // indirect
github.com/go-logr/logr v1.4.1 // indirect
Expand Down
4 changes: 2 additions & 2 deletions hack/generate-schemas/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,8 @@ github.com/flanksource/duty v1.0.282 h1:iddsHVcOs6IQ4Wm8U7pxeazrkRn8bO6T387As4zd
github.com/flanksource/duty v1.0.282/go.mod h1:zDVxlWxpSZjbpuBv0i9ke4vlG2elgyKjYY+656XhfPs=
github.com/flanksource/gomplate/v3 v3.20.30 h1:zRB1DVFIp64QiL6vkJJzVJK4Hj/PazwTNQ6+mDlHaxc=
github.com/flanksource/gomplate/v3 v3.20.30/go.mod h1:m2WVc04GMVBOcZhtDaz/LTtrVWKejeJhFM1Jy/h9VZQ=
github.com/flanksource/is-healthy v0.0.0-20231003215854-76c51e3a3ff7 h1:s6jf6P1pRfdvksVFjIXFRfnimvEYUR0/Mmla1EIjiRM=
github.com/flanksource/is-healthy v0.0.0-20231003215854-76c51e3a3ff7/go.mod h1:BH5gh9JyEAuuWVP6Q5y9h43VozS0RfKyjNpM9L4v4hw=
github.com/flanksource/is-healthy v1.0.1 h1:cr36GHndzge4rb6GDw+kSh5M6bUpglcZNbqqaTDAZeE=
github.com/flanksource/is-healthy v1.0.1/go.mod h1:cFejm0MapnJzgeoG3iizMv+tCIPthe0XqO+3nrhM79c=
github.com/flanksource/mapstructure v1.6.0 h1:+1kJ+QsO1SxjAgktfLlpZXetsVSJ0uCLhGKrA4BtwTE=
github.com/flanksource/mapstructure v1.6.0/go.mod h1:dttg5+FFE2sp4D/CrcPCVqufNDrBggDaM+08nk5S8Ps=
github.com/flanksource/postq v0.1.3 h1:eTslG04hwxAvntZv8gIUsXKQPLGeLiRPNkZC+kQdL7c=
Expand Down
38 changes: 16 additions & 22 deletions scrapers/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/configservice"
ec2 "github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
ec2Types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/aws/aws-sdk-go-v2/service/ecr"
"github.com/aws/aws-sdk-go-v2/service/efs"
"github.com/aws/aws-sdk-go-v2/service/eks"
Expand All @@ -28,7 +28,7 @@ import (
"github.com/flanksource/commons/logger"
"github.com/flanksource/config-db/api"
v1 "github.com/flanksource/config-db/api/v1"
"github.com/flanksource/duty/models"
"github.com/flanksource/is-healthy/pkg/health"
)

// Scraper ...
Expand All @@ -48,7 +48,7 @@ type AWSContext struct {
Subnets map[string]Zone
}

func getTags(tags []types.Tag) v1.JSONStringMap {
func getTags(tags []ec2Types.Tag) v1.JSONStringMap {
result := make(v1.JSONStringMap)
for _, tag := range tags {
result[*tag.Key] = *tag.Value
Expand Down Expand Up @@ -186,6 +186,7 @@ func (aws Scraper) eksClusters(ctx *AWSContext, config v1.AWS, results *v1.Scrap

cluster.Cluster.Tags["account"] = *ctx.Caller.Account
cluster.Cluster.Tags["region"] = getRegionFromArn(*cluster.Cluster.Arn, "eks")

*results = append(*results, v1.ScrapeResult{
Type: v1.AWSEKSCluster,
CreatedAt: cluster.Cluster.CreatedAt,
Expand All @@ -200,6 +201,7 @@ func (aws Scraper) eksClusters(ctx *AWSContext, config v1.AWS, results *v1.Scrap
ParentExternalID: *cluster.Cluster.ResourcesVpcConfig.VpcId,
ParentType: v1.AWSEC2VPC,
RelationshipResults: relationships,
Status: health.MapAWSStatus(string(cluster.Cluster.Status), health.AWSResourceTypeEKS),
})
}
}
Expand Down Expand Up @@ -387,6 +389,7 @@ func (aws Scraper) ebs(ctx *AWSContext, config v1.AWS, results *v1.ScrapeResults
ID: *volume.VolumeId,
ParentExternalID: lo.FromPtr(ctx.Caller.Account),
ParentType: v1.AWSAccount,
Status: health.MapAWSStatus(string(volume.State), health.AWSResourceTypeEBS),
})
}
}
Expand All @@ -395,13 +398,15 @@ func (aws Scraper) rds(ctx *AWSContext, config v1.AWS, results *v1.ScrapeResults
if !config.Includes("RDS") {
return
}

describeInput := &rds.DescribeDBInstancesInput{}
RDS := rds.NewFromConfig(*ctx.Session)
describeOutput, err := RDS.DescribeDBInstances(ctx, describeInput)
if err != nil {
results.Errorf(err, "failed to get rds")
return
}

for _, instance := range describeOutput.DBInstances {
tags := make(v1.JSONStringMap)
for _, tag := range instance.TagList {
Expand Down Expand Up @@ -438,6 +443,7 @@ func (aws Scraper) rds(ctx *AWSContext, config v1.AWS, results *v1.ScrapeResults
ParentExternalID: *instance.DBSubnetGroup.VpcId,
ParentType: v1.AWSEC2VPC,
RelationshipResults: relationships,
Status: health.MapAWSStatus(lo.FromPtr(instance.DBInstanceStatus), health.AWSResourceTypeRDS),
})
}
}
Expand All @@ -446,12 +452,14 @@ func (aws Scraper) vpcs(ctx *AWSContext, config v1.AWS, results *v1.ScrapeResult
if !config.Includes("VPC") {
return
}

describeInput := &ec2.DescribeVpcsInput{}
describeOutput, err := ctx.EC2.DescribeVpcs(ctx, describeInput)
if err != nil {
results.Errorf(err, "failed to get vpcs")
return
}

for _, vpc := range describeOutput.Vpcs {
var relationships v1.RelationshipResults
// DHCPOptions relationship
Expand Down Expand Up @@ -489,6 +497,7 @@ func (aws Scraper) vpcs(ctx *AWSContext, config v1.AWS, results *v1.ScrapeResult
ParentExternalID: lo.FromPtr(ctx.Caller.Account),
ParentType: v1.AWSAccount,
RelationshipResults: relationships,
Status: health.MapAWSStatus(string(vpc.State), health.AWSResourceTypeVPC),
})
}
}
Expand Down Expand Up @@ -611,27 +620,9 @@ func (aws Scraper) instances(ctx *AWSContext, config v1.AWS, results *v1.ScrapeR
tags["network"] = instance.VpcID
tags["subnet"] = instance.SubnetID

var configStatus string
if i.State != nil {
switch string(i.State.Name) {
case "running":
configStatus = models.ConfigStatusRunning
case "pending":
configStatus = models.ConfigStatusPending
case "stopping":
configStatus = models.ConfigStatusStopping
case "stopped":
configStatus = models.ConfigStatusStopped
case "shutting-down":
configStatus = models.ConfigStatusDeleting
case "terminated":
configStatus = models.ConfigStatusDeleted
}
}

*results = append(*results, v1.ScrapeResult{
Type: v1.AWSEC2Instance,
Status: configStatus,
Status: health.MapAWSStatus(string(i.State.Name), health.AWSResourceTypeEC2),
Tags: tags,
BaseScraper: config.BaseScraper,
Config: instance,
Expand Down Expand Up @@ -923,6 +914,7 @@ func (aws Scraper) loadBalancers(ctx *AWSContext, config v1.AWS, results *v1.Scr
ParentExternalID: *lb.VpcId,
ParentType: v1.AWSEC2VPC,
RelationshipResults: relationships,
Status: health.MapAWSStatus(string(lb.State.Code), health.AWSResourceTypeELB),
})
}

Expand Down Expand Up @@ -982,6 +974,7 @@ func (aws Scraper) subnets(ctx *AWSContext, config v1.AWS, results *v1.ScrapeRes
Config: subnet,
ParentExternalID: lo.FromPtr(subnet.VpcId),
ParentType: v1.AWSEC2VPC,
Status: health.MapAWSStatus(string(subnet.State), health.AWSResourceTypeSubnet),
RelationshipResults: relationships,
}

Expand All @@ -993,6 +986,7 @@ func (aws Scraper) iamRoles(ctx *AWSContext, config v1.AWS, results *v1.ScrapeRe
if !config.Includes("Roles") {
return
}

roles, err := ctx.IAM.ListRoles(ctx, nil)
if err != nil {
results.Errorf(err, "failed to get roles")
Expand Down

0 comments on commit b0fee8d

Please sign in to comment.