|
| 1 | +package eksapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 8 | + "github.com/aws/aws-sdk-go-v2/service/ec2" |
| 9 | + ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types" |
| 10 | + "github.com/aws/aws-sdk-go-v2/service/ssm" |
| 11 | + "k8s.io/klog/v2" |
| 12 | +) |
| 13 | + |
| 14 | +func NewAMIResolver(awsClients *awsClients) *amiResolver { |
| 15 | + return &amiResolver{ |
| 16 | + clients: awsClients, |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +type amiResolver struct { |
| 21 | + clients *awsClients |
| 22 | +} |
| 23 | + |
| 24 | +func (r *amiResolver) Resolve(ctx context.Context, opts *deployerOptions) (string, error) { |
| 25 | + switch opts.UserDataFormat { |
| 26 | + case UserDataBootstrapSh: |
| 27 | + // TODO: AL2 is not a high priority, skipping for now. |
| 28 | + return "", fmt.Errorf("%s is not handled", opts.UserDataFormat) |
| 29 | + case UserDataNodeadm: |
| 30 | + return r.ResolveAL2023(ctx, opts) |
| 31 | + case UserDataBottlerocket: |
| 32 | + return r.ResolveBottlerocket(ctx, opts) |
| 33 | + default: |
| 34 | + return "", fmt.Errorf("unhandled userdata format: %s", opts.UserDataFormat) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +func (r *amiResolver) ResolveAL2023(ctx context.Context, opts *deployerOptions) (string, error) { |
| 39 | + describeInstanceTypesResponse, err := r.clients.EC2().DescribeInstanceTypes(ctx, &ec2.DescribeInstanceTypesInput{ |
| 40 | + InstanceTypes: []ec2types.InstanceType{ec2types.InstanceType(r.getInstance(opts))}, |
| 41 | + }) |
| 42 | + if err != nil { |
| 43 | + return "", err |
| 44 | + } |
| 45 | + instanceTypeInfo := describeInstanceTypesResponse.InstanceTypes[0] |
| 46 | + |
| 47 | + arch, err := r.resolveArch(instanceTypeInfo) |
| 48 | + if err != nil { |
| 49 | + return "", err |
| 50 | + } |
| 51 | + |
| 52 | + variant := "standard" |
| 53 | + if instanceTypeInfo.NeuronInfo != nil { |
| 54 | + if len(instanceTypeInfo.NeuronInfo.NeuronDevices) > 0 { |
| 55 | + variant = "neuron" |
| 56 | + } |
| 57 | + } else if instanceTypeInfo.GpuInfo != nil { |
| 58 | + for _, gpu := range instanceTypeInfo.GpuInfo.Gpus { |
| 59 | + if aws.ToString(gpu.Manufacturer) == "NVIDIA" { |
| 60 | + variant = "nvidia" |
| 61 | + break |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + getParameterReponse, err := r.clients.SSM().GetParameter(ctx, &ssm.GetParameterInput{ |
| 67 | + Name: aws.String(fmt.Sprintf("/aws/service/eks/optimized-ami/%s/amazon-linux-2023/%s/%s/recommended/image_id", opts.KubernetesVersion, arch, variant)), |
| 68 | + }) |
| 69 | + if err != nil { |
| 70 | + return "", err |
| 71 | + } |
| 72 | + |
| 73 | + return aws.ToString(getParameterReponse.Parameter.Value), nil |
| 74 | +} |
| 75 | + |
| 76 | +func (r *amiResolver) ResolveBottlerocket(ctx context.Context, opts *deployerOptions) (string, error) { |
| 77 | + describeInstanceTypesResponse, err := r.clients.EC2().DescribeInstanceTypes(ctx, &ec2.DescribeInstanceTypesInput{ |
| 78 | + InstanceTypes: []ec2types.InstanceType{ec2types.InstanceType(r.getInstance(opts))}, |
| 79 | + }) |
| 80 | + if err != nil { |
| 81 | + return "", err |
| 82 | + } |
| 83 | + instanceTypeInfo := describeInstanceTypesResponse.InstanceTypes[0] |
| 84 | + |
| 85 | + arch, err := r.resolveArch(instanceTypeInfo) |
| 86 | + if err != nil { |
| 87 | + return "", err |
| 88 | + } |
| 89 | + |
| 90 | + // TODO: enable fips |
| 91 | + flavorSuffix := "" |
| 92 | + if instanceTypeInfo.GpuInfo != nil { |
| 93 | + for _, gpu := range instanceTypeInfo.GpuInfo.Gpus { |
| 94 | + if aws.ToString(gpu.Manufacturer) == "NVIDIA" { |
| 95 | + flavorSuffix = "-nvidia" |
| 96 | + break |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + getParameterResponse, err := r.clients.SSM().GetParameter(ctx, &ssm.GetParameterInput{ |
| 102 | + Name: aws.String(fmt.Sprintf("/aws/service/bottlerocket/aws-k8s-%s%s/%s/latest/image_id", opts.KubernetesVersion, flavorSuffix, arch)), |
| 103 | + }) |
| 104 | + if err != nil { |
| 105 | + return "", err |
| 106 | + } |
| 107 | + |
| 108 | + return aws.ToString(getParameterResponse.Parameter.Value), nil |
| 109 | +} |
| 110 | + |
| 111 | +func (r *amiResolver) getInstance(opts *deployerOptions) string { |
| 112 | + instanceType := opts.InstanceTypes[0] |
| 113 | + if len(opts.InstanceTypes) > 1 { |
| 114 | + klog.Warningf("only resolving AMI based on first instance type: %s", instanceType) |
| 115 | + } |
| 116 | + return instanceType |
| 117 | +} |
| 118 | + |
| 119 | +func (r *amiResolver) resolveArch(instanceTypeInfo ec2types.InstanceTypeInfo) (string, error) { |
| 120 | + // TODO: the ordering might be weird because old instances might support |
| 121 | + // both i386 and x8664. |
| 122 | + switch arch := instanceTypeInfo.ProcessorInfo.SupportedArchitectures[0]; arch { |
| 123 | + case ec2types.ArchitectureTypeArm64, ec2types.ArchitectureTypeX8664: |
| 124 | + return string(arch), nil |
| 125 | + default: |
| 126 | + return "", fmt.Errorf("unhandled arch: %s", arch) |
| 127 | + } |
| 128 | +} |
0 commit comments