From 068905ec4d7aa3ab897b7d4d30aa4dac2ac6e50b Mon Sep 17 00:00:00 2001 From: Jason DeTiberus Date: Wed, 17 Apr 2019 10:57:11 -0400 Subject: [PATCH 1/3] Add ability to override Organization ID for image lookups --- .../v1alpha1/awsmachineproviderconfig_types.go | 3 +++ pkg/cloud/aws/services/ec2/ami.go | 9 ++++++--- pkg/cloud/aws/services/ec2/ami_test.go | 2 +- pkg/cloud/aws/services/ec2/instances.go | 2 +- 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/pkg/apis/awsprovider/v1alpha1/awsmachineproviderconfig_types.go b/pkg/apis/awsprovider/v1alpha1/awsmachineproviderconfig_types.go index e4d22ea985..5a7e04f558 100644 --- a/pkg/apis/awsprovider/v1alpha1/awsmachineproviderconfig_types.go +++ b/pkg/apis/awsprovider/v1alpha1/awsmachineproviderconfig_types.go @@ -37,6 +37,9 @@ type AWSMachineProviderSpec struct { // AMI is the reference to the AMI from which to create the machine instance. AMI AWSResourceReference `json:"ami,omitempty"` + // ImageLookupOrg is the AWS Organization ID to use for image lookup if AMI is not set. + ImageLookupOrg string `json:"imageLookupOrg,omitempty"` + // InstanceType is the type of instance to create. Example: m4.xlarge InstanceType string `json:"instanceType,omitempty"` diff --git a/pkg/cloud/aws/services/ec2/ami.go b/pkg/cloud/aws/services/ec2/ami.go index 56e356f58e..b5205e0011 100644 --- a/pkg/cloud/aws/services/ec2/ami.go +++ b/pkg/cloud/aws/services/ec2/ami.go @@ -30,7 +30,7 @@ import ( const ( // machineAMIOwnerID is a heptio/VMware owned account. Please see: // https://github.com/kubernetes-sigs/cluster-api-provider-aws/issues/487 - machineAMIOwnerID = "258751437250" + defaultMachineAMIOwnerID = "258751437250" // amiNameFormat is defined in the build/ directory of this project. // The pattern is: @@ -50,12 +50,15 @@ func amiName(baseOS, baseOSVersion, kubernetesVersion string) string { } // defaultAMILookup returns the default AMI based on region -func (s *Service) defaultAMILookup(baseOS, baseOSVersion, kubernetesVersion string) (string, error) { +func (s *Service) defaultAMILookup(ownerID, baseOS, baseOSVersion, kubernetesVersion string) (string, error) { + if ownerID == "" { + ownerID = defaultMachineAMIOwnerID + } describeImageInput := &ec2.DescribeImagesInput{ Filters: []*ec2.Filter{ { Name: aws.String("owner-id"), - Values: []*string{aws.String(machineAMIOwnerID)}, + Values: []*string{aws.String(ownerID)}, }, { Name: aws.String("name"), diff --git a/pkg/cloud/aws/services/ec2/ami_test.go b/pkg/cloud/aws/services/ec2/ami_test.go index 55b7a744ac..1ad5074754 100644 --- a/pkg/cloud/aws/services/ec2/ami_test.go +++ b/pkg/cloud/aws/services/ec2/ami_test.go @@ -76,7 +76,7 @@ func TestAMIs(t *testing.T) { tc.expect(ec2Mock.EXPECT()) s := NewService(scope) - id, err := s.defaultAMILookup("base os", "baseos version", "1.11.1") + id, err := s.defaultAMILookup("", "base os", "baseos version", "1.11.1") if err != nil { t.Fatalf("did not expect error calling a mock: %v", err) } diff --git a/pkg/cloud/aws/services/ec2/instances.go b/pkg/cloud/aws/services/ec2/instances.go index d6c2b2eeff..0925f78ac9 100644 --- a/pkg/cloud/aws/services/ec2/instances.go +++ b/pkg/cloud/aws/services/ec2/instances.go @@ -122,7 +122,7 @@ func (s *Service) createInstance(machine *actuators.MachineScope, bootstrapToken if machine.MachineConfig.AMI.ID != nil { input.ImageID = *machine.MachineConfig.AMI.ID } else { - input.ImageID, err = s.defaultAMILookup("ubuntu", "18.04", machine.Machine.Spec.Versions.Kubelet) + input.ImageID, err = s.defaultAMILookup(machine.MachineConfig.ImageLookupOrg, "ubuntu", "18.04", machine.Machine.Spec.Versions.Kubelet) if err != nil { return nil, err } From f9af1467d108c0c80c4c521071d3ad0c762162f7 Mon Sep 17 00:00:00 2001 From: Ashish Amarnath Date: Wed, 17 Apr 2019 11:43:15 -0400 Subject: [PATCH 2/3] Update pkg/cloud/aws/services/ec2/ami.go Co-Authored-By: detiber --- pkg/cloud/aws/services/ec2/ami.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/cloud/aws/services/ec2/ami.go b/pkg/cloud/aws/services/ec2/ami.go index b5205e0011..aa3ba7f395 100644 --- a/pkg/cloud/aws/services/ec2/ami.go +++ b/pkg/cloud/aws/services/ec2/ami.go @@ -28,7 +28,7 @@ import ( ) const ( - // machineAMIOwnerID is a heptio/VMware owned account. Please see: + // defaultMachineAMIOwnerID is a heptio/VMware owned account. Please see: // https://github.com/kubernetes-sigs/cluster-api-provider-aws/issues/487 defaultMachineAMIOwnerID = "258751437250" From d26d42a222badbe6be70231d5c99f55ac9fdfc71 Mon Sep 17 00:00:00 2001 From: Jason DeTiberus Date: Wed, 17 Apr 2019 12:10:51 -0400 Subject: [PATCH 3/3] Add updated generated crd --- config/crds/awsprovider_v1alpha1_awsmachineproviderspec.yaml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/config/crds/awsprovider_v1alpha1_awsmachineproviderspec.yaml b/config/crds/awsprovider_v1alpha1_awsmachineproviderspec.yaml index 9b8d2d5ae5..a41651d873 100644 --- a/config/crds/awsprovider_v1alpha1_awsmachineproviderspec.yaml +++ b/config/crds/awsprovider_v1alpha1_awsmachineproviderspec.yaml @@ -95,6 +95,10 @@ spec: description: IAMInstanceProfile is a name of an IAM instance profile to assign to the instance type: string + imageLookupOrg: + description: ImageLookupOrg is the AWS Organization ID to use for image + lookup if AMI is not set. + type: string instanceType: description: 'InstanceType is the type of instance to create. Example: m4.xlarge' type: string