/
search.go
145 lines (125 loc) · 3.42 KB
/
search.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package cmd
import (
"context"
"errors"
"fmt"
"log"
"os"
"strconv"
"github.com/aws/aws-sdk-go-v2/aws"
awshttp "github.com/aws/aws-sdk-go-v2/aws/transport/http"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/guessi/eks-ami-finder/pkg/constants"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/jedib0t/go-pretty/v6/text"
)
func findAmiMatches(ctx context.Context, svc ec2.DescribeImagesAPIClient, input *ec2.DescribeImagesInput, maxResults int) ([]types.Image, error) {
var images []types.Image
var returnSize int
paginator := ec2.NewDescribeImagesPaginator(svc, input)
for paginator.HasMorePages() {
out, err := paginator.NextPage(ctx)
if err != nil {
return nil, err
}
images = append(images, out.Images...)
if len(images) > maxResults {
break
}
}
returnSize = maxResults
if len(images) < maxResults {
returnSize = len(images)
}
return images[:returnSize], nil
}
func amiSearch(input amiSearchInputSpec) {
// do nothing if maxResults is invalid input
if input.MAX_RESULTS <= 0 {
log.Fatalf("Can not pass --max-results with a value lower or equal to 0.\n")
}
if len(input.RELEASE_DATE) != 0 {
// releaseDate is expected to have at least Year included.
if len(input.RELEASE_DATE) < 4 || len(input.RELEASE_DATE) > 8 {
log.Fatalf("Invalid --release-date passed.\n")
}
// Amazon EKS was first released back at Jun 05, 2018
// - https://aws.amazon.com/blogs/aws/amazon-eks-now-generally-available/
if r, err := strconv.Atoi(input.RELEASE_DATE); err == nil {
if r < 2018 {
log.Fatalf("Invalid --release-date passed.\n")
}
} else {
log.Fatalf("Invalid --release-date passed.\n")
}
}
cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithRegion(input.AWS_REGION),
)
if err != nil {
log.Fatalf("unable to load SDK config, %v", err)
}
svc := ec2.NewFromConfig(cfg)
pattern := fmt.Sprintf("%s-%s-v%s*", constants.AmiPrefixMappings[input.AMI_TYPE], input.KUBERNETES_VERSION, input.RELEASE_DATE)
filters := []types.Filter{
{
Name: aws.String("owner-id"),
Values: []string{
input.AMI_OWNER_ID,
},
},
{
Name: aws.String("name"),
Values: []string{
pattern,
},
},
}
describeImagesInput := ec2.DescribeImagesInput{
Filters: filters,
NextToken: nil,
IncludeDeprecated: aws.Bool(input.INCLUDE_DEPRECATED),
}
images, err := findAmiMatches(context.TODO(), svc, &describeImagesInput, input.MAX_RESULTS)
if err != nil {
var re *awshttp.ResponseError
if errors.As(err, &re) {
log.Fatalf("requestID: %s, error: %v", re.ServiceRequestID(), re.Unwrap())
}
}
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{
"Region",
"AMI ID",
"Name",
"Description",
"Creation Date",
"DeprecationTime",
"Architecture",
})
// tricky trick to sort AMI by creation date
t.SortBy([]table.SortBy{{Name: "Creation Date", Mode: table.Dsc}})
t.SetColumnConfigs([]table.ColumnConfig{{Name: "Creation Date", Hidden: true}})
for _, i := range images {
t.AppendRow(table.Row{
input.AWS_REGION,
*i.ImageId,
*i.Name,
*i.Description,
*i.CreationDate,
*i.DeprecationTime,
i.Architecture,
})
}
t.Style().Format.Header = text.FormatDefault
t.Render()
if input.DEBUG_MODE {
println()
print(fmt.Sprintf("OwerId: %s\n", input.AMI_OWNER_ID))
print(fmt.Sprintf("Filter: %s\n", pattern))
}
}