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 codepipelines #1192

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
98 changes: 98 additions & 0 deletions providers/aws/CodePipelines/GetPipelines.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
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"
subhajit20 marked this conversation as resolved.
Show resolved Hide resolved
"github.com/tailwarden/komiser/utils"
)

func GetPipelines(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,
})

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())
}
subhajit20 marked this conversation as resolved.
Show resolved Hide resolved
createdAt = *outputDescribe.Cluster.CreatedAt
}

resources = append(resources, Resource{
Provider: "AWS",
Account: client.Name,
Service: "pipeline",
ResourceId: resourceArn,
Region: client.AWSClient.Region,
Name: cluster,
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
}
1 change: 1 addition & 0 deletions providers/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func listOfSupportedServices() []providers.FetchDataFunction {
ec2.VpcPeeringConnections,
kinesis.Streams,
redshift.EventSubscriptions,
codepipeline.GetPipelines,
}
}

Expand Down