From e9b7bc187d82068f7f1db91dfe3e552df08afaf4 Mon Sep 17 00:00:00 2001 From: Ilya Dmitrichenko Date: Tue, 23 Oct 2018 11:41:18 +0100 Subject: [PATCH] Rewrite AMI generator using jennifer --- pkg/ami/api.go | 6 ++ pkg/ami/generate/main.go | 131 ------------------------ pkg/ami/static_resolver.go | 2 +- pkg/ami/static_resolver_ami.go | 5 +- pkg/ami/static_resolver_ami_generate.go | 80 +++++++++++++++ pkg/eks/api/api.go | 7 ++ 6 files changed, 95 insertions(+), 136 deletions(-) delete mode 100644 pkg/ami/generate/main.go create mode 100644 pkg/ami/static_resolver_ami_generate.go diff --git a/pkg/ami/api.go b/pkg/ami/api.go index 4edd14201a..4c5a81d66f 100644 --- a/pkg/ami/api.go +++ b/pkg/ami/api.go @@ -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{ diff --git a/pkg/ami/generate/main.go b/pkg/ami/generate/main.go deleted file mode 100644 index d9980368bd..0000000000 --- a/pkg/ami/generate/main.go +++ /dev/null @@ -1,131 +0,0 @@ -// +build ignore - -package main - -import ( - "fmt" - "os" - "sort" - "text/template" - "time" - - "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" - amiapi "github.com/weaveworks/eksctl/pkg/ami" - "github.com/weaveworks/eksctl/pkg/eks/api" -) - -type imageDetail struct { - Region string - ImageId string -} - -type images struct { - General []imageDetail - Gpu []imageDetail - Timestamp time.Time -} - -var ( - supportedRegions = []string{api.EKS_REGION_US_WEST_2, api.EKS_REGION_EU_WEST_1, api.EKS_REGION_US_EAST_1} - amiTemplate = template.Must(template.New("").Parse(` -// Code generated by go generate; DO NOT EDIT. - -package ami - -// 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: { - {{- range .General }} - "{{ .Region}}": "{{ .ImageId}}", - {{- end }} - }, - ImageClassGPU: { - {{- range .Gpu }} - "{{ .Region}}": "{{ .ImageId}}", - {{- end }} - }, - }, -}`)) -) - -func main() { - fmt.Println("generating list of AMIs for the static resolvers") - - amiImages := images{} - for _, region := range supportedRegions { - general, gpu, err := getAmis(region) - if err != nil { - fmt.Println(err) - return - } - - fmt.Printf("region %s, general class = %s, gpu class = %s\n", region, general, gpu) - - amiImages.General = append(amiImages.General, imageDetail{ - ImageId: general, - Region: region, - }) - - amiImages.Gpu = append(amiImages.Gpu, imageDetail{ - ImageId: gpu, - Region: region, - }) - } - - // Sort on region - sort.Slice(amiImages.General, func(i, j int) bool { - return amiImages.General[i].Region < amiImages.General[j].Region - }) - sort.Slice(amiImages.Gpu, func(i, j int) bool { - return amiImages.Gpu[i].Region < amiImages.Gpu[j].Region - }) - - f, err := os.Create("./static_resolver_ami.go") - if err != nil { - fmt.Println(err) - return - } - defer f.Close() - - err = amiTemplate.Execute(f, amiImages) - if err != nil { - fmt.Println(err) - return - } -} - -func getAmis(region string) (normal string, gpu string, reterr error) { - s := newSession(region) - e := ec2.New(s) - - normal, err := amiapi.FindImage(e, "amazon-eks-node-*") - if err != nil { - return "", "", err - } - gpu, err = amiapi.FindImage(e, "amazon-eks-gpu-node-*") - if err != nil { - return "", "", err - } - - return -} - -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)) -} diff --git a/pkg/ami/static_resolver.go b/pkg/ami/static_resolver.go index fa8fb95059..72d525315d 100644 --- a/pkg/ami/static_resolver.go +++ b/pkg/ami/static_resolver.go @@ -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 { diff --git a/pkg/ami/static_resolver_ami.go b/pkg/ami/static_resolver_ami.go index ee20f78d0a..121867b0d6 100644 --- a/pkg/ami/static_resolver_ami.go +++ b/pkg/ami/static_resolver_ami.go @@ -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 @@ -18,4 +15,4 @@ var StaticImages = map[string]map[int]map[string]string{ "us-west-2": "ami-0731694d53ef9604b", }, }, -} \ No newline at end of file +} diff --git a/pkg/ami/static_resolver_ami_generate.go b/pkg/ami/static_resolver_ami_generate.go new file mode 100644 index 0000000000..4e0a65eb4b --- /dev/null +++ b/pkg/ami/static_resolver_ami_generate.go @@ -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 +} diff --git a/pkg/eks/api/api.go b/pkg/eks/api/api.go index 7c36ccff6b..1052aaeca5 100644 --- a/pkg/eks/api/api.go +++ b/pkg/eks/api/api.go @@ -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