From 6fea75fd36e197cadb0a4a9da3407c74d46a3d4c Mon Sep 17 00:00:00 2001 From: Brandon Date: Thu, 16 Mar 2023 08:46:56 -0500 Subject: [PATCH] deprecate --service eks --- README.md | 4 +- cmd/main.go | 6 +- pkg/selector/eks.go | 134 --------------------------------------- pkg/selector/eks_test.go | 130 ------------------------------------- pkg/selector/services.go | 3 +- 5 files changed, 8 insertions(+), 269 deletions(-) delete mode 100644 pkg/selector/eks.go delete mode 100644 pkg/selector/eks_test.go diff --git a/README.md b/README.md index 766a14f..71ab0eb 100644 --- a/README.md +++ b/README.md @@ -294,7 +294,7 @@ $ ec2-instance-selector --help ``` ```bash#help -ec2-instance-selector is a CLI tool to filter EC2 instance types based on resource criteria. +ec2-instance-selector is a CLI tool to filter EC2 instance types based on resource criteria. Filtering allows you to select all the instance types that match your application requirements. Full docs can be found at github.com/aws/amazon-ec2-instance-selector @@ -378,7 +378,7 @@ Filter Flags: Suite Flags: --base-instance-type string Instance Type used to retrieve similarly spec'd instance types --flexible Retrieves a group of instance types spanning multiple generations based on opinionated defaults and user overridden resource filters - --service string Filter instance types based on service support (Example: eks, eks-20201211, or emr-5.20.0) + --service string Filter instance types based on service support (Example: emr-5.20.0) Global Flags: diff --git a/cmd/main.go b/cmd/main.go index c2dfede..5c1bdf7 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -210,7 +210,7 @@ Full docs can be found at github.com/aws/amazon-` + binName cli.SuiteStringFlag(instanceTypeBase, nil, nil, "Instance Type used to retrieve similarly spec'd instance types", nil) cli.SuiteBoolFlag(flexible, nil, nil, "Retrieves a group of instance types spanning multiple generations based on opinionated defaults and user overridden resource filters") - cli.SuiteStringFlag(service, nil, nil, "Filter instance types based on service support (Example: eks, eks-20201211, or emr-5.20.0)", nil) + cli.SuiteStringFlag(service, nil, nil, "Filter instance types based on service support (Example: emr-5.20.0)", nil) // Configuration Flags - These will be grouped at the bottom of the help flags @@ -242,6 +242,10 @@ Full docs can be found at github.com/aws/amazon-` + binName os.Exit(0) } + if flags[service] != nil { + log.Println("--service eks is deprecated. EKS generally supports all instance types") + } + sess, err := getRegionAndProfileAWSSession(cli.StringMe(flags[region]), cli.StringMe(flags[profile])) if err != nil { fmt.Println(err) diff --git a/pkg/selector/eks.go b/pkg/selector/eks.go deleted file mode 100644 index 184131f..0000000 --- a/pkg/selector/eks.go +++ /dev/null @@ -1,134 +0,0 @@ -// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"). You may -// not use this file except in compliance with the License. A copy of the -// License is located at -// -// http://aws.amazon.com/apache2.0/ -// -// or in the "license" file accompanying this file. This file is distributed -// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either -// express or implied. See the License for the specific language governing -// permissions and limitations under the License. - -package selector - -import ( - "archive/zip" - "bytes" - "fmt" - "io/ioutil" - "log" - "net/http" - "strings" - - "github.com/aws/aws-sdk-go/aws" -) - -const ( - eksAMIRepoURL = "https://github.com/awslabs/amazon-eks-ami" - eksFallbackLatestAMIVersion = "v20210125" - eksInstanceTypesFile = "eni-max-pods.txt" -) - -// EKS is a Service type for a custom service filter transform -type EKS struct { - AMIRepoURL string -} - -// Filters implements the Service interface contract for EKS -func (e *EKS) Filters(version string) (Filters, error) { - if e.AMIRepoURL == "" { - e.AMIRepoURL = eksAMIRepoURL - } - var filters Filters - - if version == "" { - var err error - version, err = e.getLatestAMIVersion() - if err != nil { - log.Printf("There was a problem fetching the latest EKS AMI version, using hardcoded fallback version %s\n", eksFallbackLatestAMIVersion) - version = eksFallbackLatestAMIVersion - } - } - supportedInstanceTypes, err := e.getSupportedInstanceTypes(version) - if err != nil { - log.Printf("Unable to retrieve EKS supported instance types for version %s: %v", version, err) - return filters, err - } - filters.InstanceTypes = &supportedInstanceTypes - filters.VirtualizationType = aws.String("hvm") - return filters, nil -} - -func (e *EKS) getSupportedInstanceTypes(version string) ([]string, error) { - supportedInstanceTypes := []string{} - resp, err := http.Get(fmt.Sprintf("%s/archive/%s.zip", e.AMIRepoURL, version)) - if err != nil { - return supportedInstanceTypes, err - } - - defer resp.Body.Close() - if resp.StatusCode != http.StatusOK { - return supportedInstanceTypes, fmt.Errorf("Unable to retrieve EKS supported instance types, got non-200 status code: %d", resp.StatusCode) - } - - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - return supportedInstanceTypes, err - } - - zipReader, err := zip.NewReader(bytes.NewReader(body), int64(len(body))) - if err != nil { - return supportedInstanceTypes, err - } - - // Read all the files from zip archive - for _, zipFile := range zipReader.File { - filePathParts := strings.Split(zipFile.Name, "/") - fileName := filePathParts[len(filePathParts)-1] - if fileName == eksInstanceTypesFile { - unzippedFileBytes, err := readZipFile(zipFile) - if err != nil { - log.Println(err) - continue - } - supportedInstanceTypesFileBody := string(unzippedFileBytes) - for _, line := range strings.Split(strings.Replace(supportedInstanceTypesFileBody, "\r\n", "\n", -1), "\n") { - if !strings.HasPrefix(line, "#") { - instanceType := strings.Split(line, " ")[0] - supportedInstanceTypes = append(supportedInstanceTypes, instanceType) - } - } - } - } - return supportedInstanceTypes, nil -} - -func (e EKS) getLatestAMIVersion() (string, error) { - client := &http.Client{ - CheckRedirect: func(req *http.Request, via []*http.Request) error { - return http.ErrUseLastResponse - }, - } - // Get latest version - resp, err := client.Get(fmt.Sprintf("%s/releases/latest", e.AMIRepoURL)) - if err != nil { - return "", err - } - if resp.StatusCode != http.StatusFound { - return "", fmt.Errorf("Can't retrieve latest release from github because redirect was not sent") - } - versionRedirect := resp.Header.Get("location") - pathParts := strings.Split(versionRedirect, "/") - return pathParts[len(pathParts)-1], nil -} - -func readZipFile(zf *zip.File) ([]byte, error) { - f, err := zf.Open() - if err != nil { - return nil, err - } - defer f.Close() - return ioutil.ReadAll(f) -} diff --git a/pkg/selector/eks_test.go b/pkg/selector/eks_test.go deleted file mode 100644 index 3da4380..0000000 --- a/pkg/selector/eks_test.go +++ /dev/null @@ -1,130 +0,0 @@ -// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"). You may -// not use this file except in compliance with the License. A copy of the -// License is located at -// -// http://aws.amazon.com/apache2.0/ -// -// or in the "license" file accompanying this file. This file is distributed -// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either -// express or implied. See the License for the specific language governing -// permissions and limitations under the License. - -package selector_test - -import ( - "fmt" - "io/ioutil" - "net/http" - "net/http/httptest" - "testing" - - "github.com/aws/amazon-ec2-instance-selector/v2/pkg/selector" - h "github.com/aws/amazon-ec2-instance-selector/v2/pkg/test" -) - -const ( - githubStaticReleasesDir = "GithubEKSAMIRelease" - githubReleaseVersion = "20210125" - githubZipFileName = "amazon-eks-ami-20210125.zip" -) - -// Tests - -func TestEKSDefaultService(t *testing.T) { - ghServer := eksGithubReleaseHTTPServer(false, false) - defer ghServer.Close() - - registry := selector.NewRegistry() - registry.Register("eks", &selector.EKS{ - AMIRepoURL: ghServer.URL, - }) - - eks := "eks" - filters := selector.Filters{ - Service: &eks, - } - - transformedFilters, err := registry.ExecuteTransforms(filters) - h.Ok(t, err) - h.Assert(t, transformedFilters != filters, " Filters should have been modified") - h.Assert(t, len(*transformedFilters.InstanceTypes) == 389, "389 instance types should be supported, but got %d", len(*transformedFilters.InstanceTypes)) - h.Assert(t, *transformedFilters.VirtualizationType == "hvm", "eks should only support hvm") - - eks = "eks-v" + githubReleaseVersion - filters.Service = &eks - transformedFilters, err = registry.ExecuteTransforms(filters) - h.Ok(t, err) - h.Assert(t, transformedFilters != filters, " Filters should have been modified") - h.Assert(t, len(*transformedFilters.InstanceTypes) == 389, "389 instance types should be supported, but got %d", len(*transformedFilters.InstanceTypes)) - h.Assert(t, *transformedFilters.VirtualizationType == "hvm", "eks should only support hvm") -} - -func TestEKSDefaultService_FailLatestReleaseUseFallbackStaticVersion(t *testing.T) { - ghServer := eksGithubReleaseHTTPServer(true, false) - defer ghServer.Close() - - registry := selector.NewRegistry() - registry.Register("eks", &selector.EKS{ - AMIRepoURL: ghServer.URL, - }) - - eks := "eks" - filters := selector.Filters{ - Service: &eks, - } - - transformedFilters, err := registry.ExecuteTransforms(filters) - h.Ok(t, err) - h.Assert(t, transformedFilters != filters, " Filters should have been modified") - h.Assert(t, len(*transformedFilters.InstanceTypes) == 389, "389 instance types should be supported, but got %d", len(*transformedFilters.InstanceTypes)) - h.Assert(t, *transformedFilters.VirtualizationType == "hvm", "eks should only support hvm") -} - -func TestEKSDefaultService_FailLatestReleaseAndFailExactVersionLookup(t *testing.T) { - ghServer := eksGithubReleaseHTTPServer(true, true) - defer ghServer.Close() - - registry := selector.NewRegistry() - registry.Register("eks", &selector.EKS{ - AMIRepoURL: ghServer.URL, - }) - - eks := "eks" - filters := selector.Filters{ - Service: &eks, - } - - _, err := registry.ExecuteTransforms(filters) - h.Nok(t, err) -} - -// Test Helpers Functions - -func eksGithubReleaseHTTPServer(failLatestRelease bool, failExactRelease bool) *httptest.Server { - return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.URL.Path == "/releases/latest" { - if failLatestRelease { - w.WriteHeader(404) - return - } - w.Header().Add("location", "/releases/tag/v"+githubReleaseVersion) - w.WriteHeader(302) - return - } - if r.URL.Path == "/archive/v"+githubReleaseVersion+".zip" { - if failExactRelease { - w.WriteHeader(404) - return - } - ghReleaseZipPath := fmt.Sprintf("%s/%s/%s", mockFilesPath, githubStaticReleasesDir, githubZipFileName) - eksAMIReleaseZipFile, err := ioutil.ReadFile(ghReleaseZipPath) - if err != nil { - panic("Could not read EKS AMI release zip file") - } - w.Write(eksAMIReleaseZipFile) - return - } - })) -} diff --git a/pkg/selector/services.go b/pkg/selector/services.go index b1ddf8a..b46495c 100644 --- a/pkg/selector/services.go +++ b/pkg/selector/services.go @@ -59,14 +59,13 @@ func (sr *ServiceRegistry) Register(name string, service Service) { // RegisterAWSServices registers the built-in AWS service filter transforms func (sr *ServiceRegistry) RegisterAWSServices() { - sr.Register("eks", &EKS{}) sr.Register("emr", &EMR{}) } // ExecuteTransforms will execute the ServiceRegistry's registered service filter transforms // Filters.Service will be parsed as - and passed to Service.Filters func (sr *ServiceRegistry) ExecuteTransforms(filters Filters) (Filters, error) { - if filters.Service == nil || *filters.Service == "" { + if filters.Service == nil || *filters.Service == "" || *filters.Service == "eks" { return filters, nil } serviceAndVersion := strings.ToLower(*filters.Service)