Skip to content

Commit

Permalink
Rewrite AMI generator using jennifer
Browse files Browse the repository at this point in the history
  • Loading branch information
errordeveloper committed Oct 23, 2018
1 parent 6972196 commit e9b7bc1
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 136 deletions.
6 changes: 6 additions & 0 deletions pkg/ami/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ const (
ImageClassGPU
)

// ImageClasses is a list of image class names
var ImageClasses = []string{
"ImageClassGeneral",
"ImageClassGPU",
}

// IsAvailable checks if a given AMI ID is available in AWS EC2
func IsAvailable(api ec2iface.EC2API, id string) (bool, error) {
input := &ec2.DescribeImagesInput{
Expand Down
131 changes: 0 additions & 131 deletions pkg/ami/generate/main.go

This file was deleted.

2 changes: 1 addition & 1 deletion pkg/ami/static_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/weaveworks/eksctl/pkg/utils"
)

//go:generate go run generate/main.go
//go:generate go run ./static_resolver_ami_generate.go

// StaticDefaultResolver resolves the AMI to the defaults for the region
type StaticDefaultResolver struct {
Expand Down
5 changes: 1 addition & 4 deletions pkg/ami/static_resolver_ami.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@

// Code generated by go generate; DO NOT EDIT.

package ami

// StaticImages is a map that holds the list of amis to be used by
Expand All @@ -18,4 +15,4 @@ var StaticImages = map[string]map[int]map[string]string{
"us-west-2": "ami-0731694d53ef9604b",
},
},
}
}
80 changes: 80 additions & 0 deletions pkg/ami/static_resolver_ami_generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// +build ignore

package main

import (
"fmt"
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"

"github.com/weaveworks/eksctl/pkg/ami"
"github.com/weaveworks/eksctl/pkg/eks/api"

. "github.com/dave/jennifer/jen"
)

func main() {
fmt.Println("generating list of AMIs for the static resolvers")

f := NewFile("ami")

d := Dict{}

client := newMultiRegionClient()

for family := range ami.ImageSearchPatterns {
familyImages := Dict{}
log.Printf("looking up %s images", family)
for class := range ami.ImageSearchPatterns[family] {
classImages := Dict{}
for _, region := range api.SupportedRegions {
p := ami.ImageSearchPatterns[family][class]
log.Printf("looking up images matching %q in %q", p, region)
image, err := ami.FindImage(client[region], p)
if err != nil {
log.Fatal(err)
}
classImages[Lit(region)] = Lit(image)
}
familyImages[Id(ami.ImageClasses[class])] = Block(classImages)
}
d[Lit(family)] = Block(familyImages)
}

f.Comment("StaticImages is a map that holds the list of AMIs to be used by for static resolution, it is generated by static_resolver_ami_generate.go")

f.Var().Id("StaticImages").Op("=").
Map(String()).Map(Int()).Map(String()).String().Values(d)

if err := f.Save("static_resolver_ami.go"); err != nil {
log.Fatal(err.Error())
}

}

func newSession(region string) *session.Session {
config := aws.NewConfig()
config = config.WithRegion(region)
config = config.WithCredentialsChainVerboseErrors(true)

// Create the options for the session
opts := session.Options{
Config: *config,
SharedConfigState: session.SharedConfigEnable,
AssumeRoleTokenProvider: stscreds.StdinTokenProvider,
}

return session.Must(session.NewSessionWithOptions(opts))
}

func newMultiRegionClient() map[string]*ec2.EC2 {
clients := make(map[string]*ec2.EC2)
for _, region := range api.SupportedRegions {
clients[region] = ec2.New(newSession(region))
}
return clients
}
7 changes: 7 additions & 0 deletions pkg/eks/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ const (
DefaultEKSRegion = EKSRegionUSWest2
)

// SupportedRegions are the regions where EKS is available
var SupportedRegions = []string{
EKSRegionUSWest2,
EKSRegionUSEast1,
EKSRegionEUWest1,
}

// DefaultWaitTimeout defines the default wait timeout
var DefaultWaitTimeout = 20 * time.Minute

Expand Down

0 comments on commit e9b7bc1

Please sign in to comment.