Skip to content

Commit

Permalink
Merge pull request #273 from weaveworks/improve-ami-generator
Browse files Browse the repository at this point in the history
Improve AMI generator
  • Loading branch information
errordeveloper authored Oct 23, 2018
2 parents 6972196 + 57c8178 commit 3c3abe5
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 152 deletions.
2 changes: 1 addition & 1 deletion cmd/eksctl/version_generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func main() {
"// +build release",
"",
"package main",
"// this file was generated by version_generate.go",
"// This file was generated by version_generate.go; DO NOT EDIT.",
`var builtAt = "" // will be set by goreleaser tool`,
`var gitCommit = "" // will be set by goreleaser tool`,
fmt.Sprintf("var gitTag = %q", os.Getenv("RELEASE_GIT_TAG")),
Expand Down
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
33 changes: 16 additions & 17 deletions pkg/ami/static_resolver_ami.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package ami

// Code generated by go generate; DO NOT EDIT.
// This file was generated by static_resolver_ami_generate.go; DO NOT EDIT.

package ami
// StaticImages is a map that holds the list of AMIs to be used by for static resolution
var StaticImages = map[string]map[int]map[string]string{"AmazonLinux2": {

ImageClassGPU: {

"eu-west-1": "ami-0706dc8a5eed2eed9",
"us-east-1": "ami-058bfb8c236caae89",
"us-west-2": "ami-0731694d53ef9604b",
},
ImageClassGeneral: {

// StaticImages is a map that holds the list of amis to be used by
// for static ami resolution
var StaticImages = map[string]map[int]map[string]string{
ImageFamilyAmazonLinux2: {
ImageClassGeneral: {
"eu-west-1": "ami-0c7a4976cb6fafd3a",
"us-east-1": "ami-0440e4f6b9713faf6",
"us-west-2": "ami-0a54c984b9f908c81",
},
ImageClassGPU: {
"eu-west-1": "ami-0706dc8a5eed2eed9",
"us-east-1": "ami-058bfb8c236caae89",
"us-west-2": "ami-0731694d53ef9604b",
},
"eu-west-1": "ami-0c7a4976cb6fafd3a",
"us-east-1": "ami-0440e4f6b9713faf6",
"us-west-2": "ami-0a54c984b9f908c81",
},
}
}}
83 changes: 83 additions & 0 deletions pkg/ami/static_resolver_ami_generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// +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")

f.Comment("This file was generated by static_resolver_ami_generate.go; DO NOT EDIT.")
f.Line()

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")

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
4 changes: 3 additions & 1 deletion pkg/nodebootstrap/maxpods.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package nodebootstrap

// Generated from https://raw.github.com/awslabs/amazon-eks-ami/master/files/eni-max-pods.txt
// This file was generated by maxpods_generate.go; DO NOT EDIT.

// Source: https://raw.github.com/awslabs/amazon-eks-ami/master/files/eni-max-pods.txt
var maxPodsPerNodeType = map[string]int{
"c1.medium": 12,
"c1.xlarge": 58,
Expand Down
4 changes: 3 additions & 1 deletion pkg/nodebootstrap/maxpods_generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ const maxPodsPerNodeTypeSourceText = "https://raw.github.com/awslabs/amazon-eks-
func main() {
f := NewFile("nodebootstrap")

f.Comment("Generated from " + maxPodsPerNodeTypeSourceText)
f.Comment("This file was generated by maxpods_generate.go; DO NOT EDIT.")
f.Line()
f.Comment("Source: " + maxPodsPerNodeTypeSourceText)

d := Dict{}

Expand Down

0 comments on commit 3c3abe5

Please sign in to comment.