Skip to content

Commit 11e8246

Browse files
authored
Merge pull request #2549 from shivi28/aws_2275
Enabled GPU optimised AMIs for EKS
2 parents 8c0d921 + 1956af1 commit 11e8246

18 files changed

+259
-105
lines changed

api/v1alpha3/conversion.go

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package v1alpha3
1818

1919
import (
20+
"unsafe"
21+
2022
apiconversion "k8s.io/apimachinery/pkg/conversion"
2123
"sigs.k8s.io/cluster-api-provider-aws/api/v1alpha4"
2224
apiv1alpha3 "sigs.k8s.io/cluster-api/api/v1alpha3"
@@ -76,14 +78,34 @@ func (r *AWSClusterList) ConvertFrom(srcRaw conversion.Hub) error {
7678
func (r *AWSMachine) ConvertTo(dstRaw conversion.Hub) error {
7779
dst := dstRaw.(*v1alpha4.AWSMachine)
7880

79-
return Convert_v1alpha3_AWSMachine_To_v1alpha4_AWSMachine(r, dst, nil)
81+
if err := Convert_v1alpha3_AWSMachine_To_v1alpha4_AWSMachine(r, dst, nil); err != nil {
82+
return err
83+
}
84+
85+
// Manually restore data.
86+
restored := &v1alpha4.AWSMachine{}
87+
if ok, err := utilconversion.UnmarshalData(r, restored); err != nil || !ok {
88+
return err
89+
}
90+
91+
RestoreAMIReference(&restored.Spec.AMI, &dst.Spec.AMI)
92+
return nil
8093
}
8194

8295
// ConvertFrom converts the v1alpha4 AWSMachine receiver to a v1alpha3 AWSMachine.
8396
func (r *AWSMachine) ConvertFrom(srcRaw conversion.Hub) error {
8497
src := srcRaw.(*v1alpha4.AWSMachine)
8598

86-
return Convert_v1alpha4_AWSMachine_To_v1alpha3_AWSMachine(src, r, nil)
99+
if err := Convert_v1alpha4_AWSMachine_To_v1alpha3_AWSMachine(src, r, nil); err != nil {
100+
return err
101+
}
102+
103+
// Preserve Hub data on down-conversion.
104+
if err := utilconversion.MarshalData(src, r); err != nil {
105+
return err
106+
}
107+
108+
return nil
87109
}
88110

89111
// ConvertTo converts the v1alpha3 AWSMachineList receiver to a v1alpha4 AWSMachineList.
@@ -104,14 +126,32 @@ func (r *AWSMachineList) ConvertFrom(srcRaw conversion.Hub) error {
104126
func (r *AWSMachineTemplate) ConvertTo(dstRaw conversion.Hub) error {
105127
dst := dstRaw.(*v1alpha4.AWSMachineTemplate)
106128

107-
return Convert_v1alpha3_AWSMachineTemplate_To_v1alpha4_AWSMachineTemplate(r, dst, nil)
129+
if err := Convert_v1alpha3_AWSMachineTemplate_To_v1alpha4_AWSMachineTemplate(r, dst, nil); err != nil {
130+
return err
131+
}
132+
// Manually restore data.
133+
restored := &v1alpha4.AWSMachineTemplate{}
134+
if ok, err := utilconversion.UnmarshalData(r, restored); err != nil || !ok {
135+
return err
136+
}
137+
138+
RestoreAMIReference(&restored.Spec.Template.Spec.AMI, &dst.Spec.Template.Spec.AMI)
139+
return nil
108140
}
109141

110142
// ConvertFrom converts the v1alpha4 AWSMachineTemplate receiver to a v1alpha3 AWSMachineTemplate.
111143
func (r *AWSMachineTemplate) ConvertFrom(srcRaw conversion.Hub) error {
112144
src := srcRaw.(*v1alpha4.AWSMachineTemplate)
113145

114-
return Convert_v1alpha4_AWSMachineTemplate_To_v1alpha3_AWSMachineTemplate(src, r, nil)
146+
if err := Convert_v1alpha4_AWSMachineTemplate_To_v1alpha3_AWSMachineTemplate(src, r, nil); err != nil {
147+
return err
148+
}
149+
// Preserve Hub data on down-conversion.
150+
if err := utilconversion.MarshalData(src, r); err != nil {
151+
return err
152+
}
153+
154+
return nil
115155
}
116156

117157
// ConvertTo converts the v1alpha3 AWSMachineTemplateList receiver to a v1alpha4 AWSMachineTemplateList.
@@ -258,3 +298,23 @@ func restoreInstance(restored, dst *v1alpha4.Instance) {
258298
}
259299
dst.VolumeIDs = restored.VolumeIDs
260300
}
301+
302+
// Convert_v1alpha3_AWSResourceReference_To_v1alpha4_AMIReference is a conversion function.
303+
func Convert_v1alpha3_AWSResourceReference_To_v1alpha4_AMIReference(in *AWSResourceReference, out *v1alpha4.AMIReference, s apiconversion.Scope) error {
304+
out.ID = (*string)(unsafe.Pointer(in.ID))
305+
return nil
306+
}
307+
308+
// Convert_v1alpha4_AMIReference_To_v1alpha3_AWSResourceReference is a conversion function.
309+
func Convert_v1alpha4_AMIReference_To_v1alpha3_AWSResourceReference(in *v1alpha4.AMIReference, out *AWSResourceReference, s apiconversion.Scope) error {
310+
out.ID = (*string)(unsafe.Pointer(in.ID))
311+
return nil
312+
}
313+
314+
// RestoreAMIReference manually restore the EKSOptimizedLookupType for AWSMachine and AWSMachineTemplate
315+
func RestoreAMIReference(restored, dst *v1alpha4.AMIReference) {
316+
if restored == nil || restored.EKSOptimizedLookupType == nil {
317+
return
318+
}
319+
dst.EKSOptimizedLookupType = restored.EKSOptimizedLookupType
320+
}

api/v1alpha3/conversion_test.go

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ import (
3232
func fuzzFuncs(_ runtimeserializer.CodecFactory) []interface{} {
3333
return []interface{}{
3434
AWSClusterStaticIdentityFuzzer,
35+
AWSMachineFuzzer,
36+
AWSMachineTemplateFuzzer,
3537
}
3638
}
3739

@@ -42,6 +44,22 @@ func AWSClusterStaticIdentityFuzzer(obj *AWSClusterStaticIdentity, c fuzz.Contin
4244
obj.Spec.SecretRef.Namespace = ""
4345
}
4446

47+
func AWSMachineFuzzer(obj *AWSMachine, c fuzz.Continue) {
48+
c.FuzzNoCustom(obj)
49+
50+
// AWSMachine.Spec.AMI.ARN and AWSMachine.Spec.AMI.Filters has been removed in v1alpha4, so setting it to nil in order to avoid v1alpha3 --> v1alpha4 --> v1alpha3 round trip errors.
51+
obj.Spec.AMI.ARN = nil
52+
obj.Spec.AMI.Filters = nil
53+
}
54+
55+
func AWSMachineTemplateFuzzer(obj *AWSMachineTemplate, c fuzz.Continue) {
56+
c.FuzzNoCustom(obj)
57+
58+
// AWSMachineTemplate.Spec.Template.Spec.AMI.ARN and AWSMachineTemplate.Spec.Template.Spec.AMI.Filters has been removed in v1alpha4, so setting it to nil in order to avoid v1alpha3 --> v1alpha4 --> v1alpha3 round trip errors.
59+
obj.Spec.Template.Spec.AMI.ARN = nil
60+
obj.Spec.Template.Spec.AMI.Filters = nil
61+
}
62+
4563
func TestFuzzyConversion(t *testing.T) {
4664
g := NewWithT(t)
4765
scheme := runtime.NewScheme()
@@ -55,15 +73,17 @@ func TestFuzzyConversion(t *testing.T) {
5573
}))
5674

5775
t.Run("for AWSMachine", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
58-
Scheme: scheme,
59-
Hub: &v1alpha4.AWSMachine{},
60-
Spoke: &AWSMachine{},
76+
Scheme: scheme,
77+
Hub: &v1alpha4.AWSMachine{},
78+
Spoke: &AWSMachine{},
79+
FuzzerFuncs: []fuzzer.FuzzerFuncs{fuzzFuncs},
6180
}))
6281

6382
t.Run("for AWSMachineTemplate", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
64-
Scheme: scheme,
65-
Hub: &v1alpha4.AWSMachineTemplate{},
66-
Spoke: &AWSMachineTemplate{},
83+
Scheme: scheme,
84+
Hub: &v1alpha4.AWSMachineTemplate{},
85+
Spoke: &AWSMachineTemplate{},
86+
FuzzerFuncs: []fuzzer.FuzzerFuncs{fuzzFuncs},
6787
}))
6888

6989
t.Run("for AWSClusterStaticIdentity", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{

api/v1alpha3/zz_generated.conversion.go

Lines changed: 12 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/v1alpha4/awsmachine_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ type AWSMachineSpec struct {
4848
InstanceID *string `json:"instanceID,omitempty"`
4949

5050
// AMI is the reference to the AMI from which to create the machine instance.
51-
AMI AWSResourceReference `json:"ami,omitempty"`
51+
AMI AMIReference `json:"ami,omitempty"`
5252

5353
// ImageLookupFormat is the AMI naming format to look up the image for this
5454
// machine It will be ignored if an explicit AMI is set. Supports

api/v1alpha4/types.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,20 @@ type AWSResourceReference struct {
4949
Filters []Filter `json:"filters,omitempty"`
5050
}
5151

52+
// AMIReference is a reference to a specific AWS resource by ID, ARN, or filters.
53+
// Only one of ID, ARN or Filters may be specified. Specifying more than one will result in
54+
// a validation error.
55+
type AMIReference struct {
56+
// ID of resource
57+
// +optional
58+
ID *string `json:"id,omitempty"`
59+
60+
// EKSOptimizedLookupType If specified, will look up an EKS Optimized image in SSM Parameter store
61+
// +kubebuilder:validation:Enum:=AmazonLinux;AmazonLinuxGPU
62+
// +optional
63+
EKSOptimizedLookupType *EKSAMILookupType `json:"eksLookupType,omitempty"`
64+
}
65+
5266
// AWSMachineTemplateResource describes the data needed to create am AWSMachine from a template
5367
type AWSMachineTemplateResource struct {
5468
// Spec is the specification of the desired behavior of the machine.
@@ -724,3 +738,13 @@ type SpotMarketOptions struct {
724738
// +kubebuilder:validation:pattern="^[0-9]+(\.[0-9]+)?$"
725739
MaxPrice *string `json:"maxPrice,omitempty"`
726740
}
741+
742+
// EKSAMILookupType specifies which AWS AMI to use for a AWSMachine and AWSMachinePool.
743+
type EKSAMILookupType string
744+
745+
const (
746+
// AmazonLinux is the default AMI type.
747+
AmazonLinux EKSAMILookupType = "AmazonLinux"
748+
// AmazonLinuxGPU is the AmazonLinux GPU AMI type.
749+
AmazonLinuxGPU EKSAMILookupType = "AmazonLinuxGPU"
750+
)

api/v1alpha4/zz_generated.deepcopy.go

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/infrastructure.cluster.x-k8s.io_awsmachinepools.yaml

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -597,31 +597,13 @@ spec:
597597
description: AMI is the reference to the AMI from which to create
598598
the machine instance.
599599
properties:
600-
arn:
601-
description: ARN of resource
600+
eksLookupType:
601+
description: EKSOptimizedLookupType If specified, will look
602+
up an EKS Optimized image in SSM Parameter store
603+
enum:
604+
- AmazonLinux
605+
- AmazonLinuxGPU
602606
type: string
603-
filters:
604-
description: 'Filters is a set of key/value pairs used to
605-
identify a resource They are applied according to the rules
606-
defined by the AWS API: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Using_Filtering.html'
607-
items:
608-
description: Filter is a filter used to identify an AWS
609-
resource
610-
properties:
611-
name:
612-
description: Name of the filter. Filter names are case-sensitive.
613-
type: string
614-
values:
615-
description: Values includes one or more filter values.
616-
Filter values are case-sensitive.
617-
items:
618-
type: string
619-
type: array
620-
required:
621-
- name
622-
- values
623-
type: object
624-
type: array
625607
id:
626608
description: ID of resource
627609
type: string

config/crd/bases/infrastructure.cluster.x-k8s.io_awsmachines.yaml

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -576,30 +576,13 @@ spec:
576576
description: AMI is the reference to the AMI from which to create
577577
the machine instance.
578578
properties:
579-
arn:
580-
description: ARN of resource
579+
eksLookupType:
580+
description: EKSOptimizedLookupType If specified, will look up
581+
an EKS Optimized image in SSM Parameter store
582+
enum:
583+
- AmazonLinux
584+
- AmazonLinuxGPU
581585
type: string
582-
filters:
583-
description: 'Filters is a set of key/value pairs used to identify
584-
a resource They are applied according to the rules defined by
585-
the AWS API: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Using_Filtering.html'
586-
items:
587-
description: Filter is a filter used to identify an AWS resource
588-
properties:
589-
name:
590-
description: Name of the filter. Filter names are case-sensitive.
591-
type: string
592-
values:
593-
description: Values includes one or more filter values.
594-
Filter values are case-sensitive.
595-
items:
596-
type: string
597-
type: array
598-
required:
599-
- name
600-
- values
601-
type: object
602-
type: array
603586
id:
604587
description: ID of resource
605588
type: string

config/crd/bases/infrastructure.cluster.x-k8s.io_awsmachinetemplates.yaml

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -465,32 +465,13 @@ spec:
465465
description: AMI is the reference to the AMI from which to
466466
create the machine instance.
467467
properties:
468-
arn:
469-
description: ARN of resource
468+
eksLookupType:
469+
description: EKSOptimizedLookupType If specified, will
470+
look up an EKS Optimized image in SSM Parameter store
471+
enum:
472+
- AmazonLinux
473+
- AmazonLinuxGPU
470474
type: string
471-
filters:
472-
description: 'Filters is a set of key/value pairs used
473-
to identify a resource They are applied according to
474-
the rules defined by the AWS API: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Using_Filtering.html'
475-
items:
476-
description: Filter is a filter used to identify an
477-
AWS resource
478-
properties:
479-
name:
480-
description: Name of the filter. Filter names are
481-
case-sensitive.
482-
type: string
483-
values:
484-
description: Values includes one or more filter
485-
values. Filter values are case-sensitive.
486-
items:
487-
type: string
488-
type: array
489-
required:
490-
- name
491-
- values
492-
type: object
493-
type: array
494475
id:
495476
description: ID of resource
496477
type: string

0 commit comments

Comments
 (0)