Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support of aws codepipeline getpipeline method #1186

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified dashboard/public/favicon.ico
Binary file not shown.
1 change: 1 addition & 0 deletions providers/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func listOfSupportedServices() []providers.FetchDataFunction {
ec2.VpcPeeringConnections,
kinesis.Streams,
redshift.EventSubscriptions,
codepipeline.GetPipeline,
}
}

Expand Down
101 changes: 101 additions & 0 deletions providers/aws/codepipeline/GetPipeline.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package codepipeline

import (
"context"
"fmt"
"time"

log "github.com/sirupsen/logrus"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/pipeline"
"github.com/aws/aws-sdk-go-v2/service/sts"
. "github.com/tailwarden/komiser/models"
. "github.com/tailwarden/komiser/providers"
"github.com/tailwarden/komiser/utils"
)

func GetPipeline(ctx context.Context, client ProviderClient) ([]Resource, error) {
resources := make([]Resource, 0)
var config eks.ListClustersInput
pipelineClient := eks.NewFromConfig(*client.AWSClient)

stsClient := sts.NewFromConfig(*client.AWSClient)
stsOutput, err := stsClient.GetCallerIdentity(ctx, &sts.GetCallerIdentityInput{})
if err != nil {
return resources, err
}

accountId := stsOutput.Account

for {
output, err := eksClient.ListClusters(ctx, &config)
if err != nil {
return resources, err
}

for _, cluster := range output.Clusters {
resourceArn := fmt.Sprintf("arn:aws:eks:%s:%s:cluster/%s", client.AWSClient.Region, *accountId, cluster)
outputTags, err := eksClient.ListTagsForResource(ctx, &eks.ListTagsForResourceInput{
ResourceArn: &resourceArn,
})

tags := make([]Tag, 0)

if err == nil {
for key, value := range outputTags.Tags {
tags = append(tags, Tag{
Key: key,
Value: value,
})
}
}

outputDescribe, err := eksClient.DescribeCluster(ctx, &eks.DescribeClusterInput{
Name: &cluster,
})

monthlyCost := 0.0
createdAt := time.Now()
if err == nil {
startOfMonth := utils.BeginningOfMonth(time.Now())
hourlyUsage := 0
if (*outputDescribe.Cluster.CreatedAt).Before(startOfMonth) {
hourlyUsage = int(time.Since(startOfMonth).Hours())
} else {
hourlyUsage = int(time.Since(*outputDescribe.Cluster.CreatedAt).Hours())
}
monthlyCost = float64(hourlyUsage) * 0.10
createdAt = *outputDescribe.Cluster.CreatedAt
}

resources = append(resources, Resource{
Provider: "AWS",
Account: client.Name,
Service: "pipeline",
ResourceId: resourceArn,
Region: client.AWSClient.Region,
Name: cluster,
Cost: monthlyCost,
Tags: tags,
CreatedAt: createdAt,
FetchedAt: time.Now(),
Link: fmt.Sprintf("https://%s.console.aws.amazon.com/eks/home?region=%s#/clusters/%s", client.AWSClient.Region, client.AWSClient.Region, cluster),
})
}

if aws.ToString(output.NextToken) == "" {
break
}

config.NextToken = output.NextToken
}
log.WithFields(log.Fields{
"provider": "AWS",
"account": client.Name,
"region": client.AWSClient.Region,
"service": "pipeline",
"resources": len(resources),
}).Info("Fetched resources")
return resources, nil
}