Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Allow using AMIs from other accounts #91

Merged
merged 5 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func NewKopsMachinePool(name, namespace, clusterName string) *infrastructurev1al
Spec: infrastructurev1alpha1.KopsMachinePoolSpec{
ClusterName: clusterName,
KopsInstanceGroupSpec: kopsapi.InstanceGroupSpec{
Image: "xxxx/ubuntu-v1",
Image: "000000000000/ubuntu-v1",
Role: "ControlPlane",
Subnets: []string{
"dummy-subnet",
Expand Down
3 changes: 2 additions & 1 deletion utils/fixtures/karpenter/test_successful_ec2_node_class.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ spec:
amiFamily: Custom
amiSelectorTerms:
- name: ubuntu-v1
owner: "000000000000"
metadataOptions:
httpEndpoint: enabled
httpProtocolIPv6: disabled
Expand Down Expand Up @@ -39,4 +40,4 @@ spec:
kops.k8s.io/instancegroup: test-machine-pool
KubernetesCluster: test-cluster.test.k8s.cluster
userData: |
dummy content
dummy content
9 changes: 6 additions & 3 deletions utils/karpenter_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func GetKubeletConfiguration(kubeletSpec *kopsapi.KubeletConfigSpec) *karpenterv
}

func CreateEC2NodeClassFromKopsLaunchTemplateInfo(kopsCluster *kopsapi.Cluster, kmp *infrastructurev1alpha1.KopsMachinePool, nodePoolName, terraformOutputDir string) (string, error) {
amiName, err := GetAmiNameFromImageSource(kmp.Spec.KopsInstanceGroupSpec.Image)
amiName, amiAccount, err := GetAmiNameFromImageSource(kmp.Spec.KopsInstanceGroupSpec.Image)
if err != nil {
return "", err
}
Expand All @@ -185,6 +185,7 @@ func CreateEC2NodeClassFromKopsLaunchTemplateInfo(kopsCluster *kopsapi.Cluster,
data := struct {
Name string
AmiName string
AmiAccount string
ClusterName string
IGName string
Tags map[string]string
Expand All @@ -194,6 +195,7 @@ func CreateEC2NodeClassFromKopsLaunchTemplateInfo(kopsCluster *kopsapi.Cluster,
}{
Name: nodePoolName,
AmiName: amiName,
AmiAccount: amiAccount,
IGName: kmp.Name,
ClusterName: kopsCluster.Name,
Tags: kopsCluster.Spec.CloudLabels,
Expand Down Expand Up @@ -222,7 +224,7 @@ func CreateEC2NodeClassFromKopsLaunchTemplateInfo(kopsCluster *kopsapi.Cluster,
}

func CreateEC2NodeClassV1FromKopsLaunchTemplateInfo(kopsCluster *kopsapi.Cluster, kmp *infrastructurev1alpha1.KopsMachinePool, nodePoolName, terraformOutputDir string) (*karpenterv1.EC2NodeClass, error) {
amiName, err := GetAmiNameFromImageSource(kmp.Spec.KopsInstanceGroupSpec.Image)
amiName, amiAccount, err := GetAmiNameFromImageSource(kmp.Spec.KopsInstanceGroupSpec.Image)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -257,7 +259,8 @@ func CreateEC2NodeClassV1FromKopsLaunchTemplateInfo(kopsCluster *kopsapi.Cluster
AMIFamily: &karpenterv1.AMIFamilyCustom,
AMISelectorTerms: []karpenterv1.AMISelectorTerm{
{
Name: amiName,
Name: amiName,
Owner: amiAccount,
},
},
MetadataOptions: &karpenterv1.MetadataOptions{
Expand Down
5 changes: 3 additions & 2 deletions utils/karpenter_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func TestCreateEC2NodeClassFromKopsLaunchTemplateInfo(t *testing.T) {
g.Expect(err).ToNot(HaveOccurred())
expectedOutput, err := os.ReadFile(tc.expectedOutputFile)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(ec2NodeClassString).To(Equal(string(expectedOutput)))
g.Expect(ec2NodeClassString).To(BeEquivalentTo(string(expectedOutput)))

}
})
Expand Down Expand Up @@ -127,7 +127,8 @@ func TestCreateEC2NodeClassV1FromKopsLaunchTemplateInfo(t *testing.T) {
AMIFamily: &karpenterv1.AMIFamilyCustom,
AMISelectorTerms: []karpenterv1.AMISelectorTerm{
{
Name: "ubuntu-v1",
Name: "ubuntu-v1",
Owner: "000000000000",
},
},
MetadataOptions: &karpenterv1.MetadataOptions{
Expand Down
6 changes: 3 additions & 3 deletions utils/kops_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,12 +343,12 @@ func KopsDeleteResources(ctx context.Context, cloud fi.Cloud, kopsClientset simp

}

func GetAmiNameFromImageSource(image string) (string, error) {
func GetAmiNameFromImageSource(image string) (string, string, error) {
parts := strings.SplitN(image, "/", 2)
if len(parts) > 1 {
return parts[1], nil
return parts[1], parts[0], nil
} else {
return "", errors.New("invalid image format, should receive image source")
return "", "", errors.New("invalid image format, should receive image source")
}
}

Expand Down
5 changes: 4 additions & 1 deletion utils/kops_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,12 +454,14 @@ func TestGetAmiNameFromImageSource(t *testing.T) {
description string
input string
output string
output2 string
expectedError error
}{
{
description: "should return the ami name from the image source",
input: "000000000000/ubuntu-v1.0.0",
output: "ubuntu-v1.0.0",
output2: "000000000000",
},
{
description: "should fail when receiving ami id",
Expand All @@ -479,13 +481,14 @@ func TestGetAmiNameFromImageSource(t *testing.T) {

t.Run(tc.description, func(t *testing.T) {

amiName, err := GetAmiNameFromImageSource(tc.input)
amiName, amiAccount, err := GetAmiNameFromImageSource(tc.input)
if tc.expectedError != nil {
g.Expect(err).To(HaveOccurred())
g.Expect(err).To(Equal(tc.expectedError))
} else {
g.Expect(err).To(BeNil())
g.Expect(amiName).To(Equal(tc.output))
g.Expect(amiAccount).To(Equal(tc.output2))
}

})
Expand Down
3 changes: 2 additions & 1 deletion utils/templates/ec2nodeclass.yaml.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ spec:
amiFamily: Custom
amiSelectorTerms:
- name: {{ .AmiName }}
owner: "{{ .AmiAccount }}"
metadataOptions:
httpEndpoint: enabled
httpProtocolIPv6: disabled
Expand Down Expand Up @@ -42,4 +43,4 @@ spec:
{{ $key }}: {{ $value | quote }}
{{- end }}
userData: |
{{ .UserData | indent 4 }}
{{ .UserData | indent 4 }}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ spec:
amiFamily: Custom
amiSelectorTerms:
- name: ubuntu-v1
owner: "000000000000"
metadataOptions:
httpEndpoint: enabled
httpProtocolIPv6: disabled
Expand Down Expand Up @@ -107,3 +108,4 @@ spec:
set -o errexit
set -o nounset
set -o pipefail

Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ spec:
amiFamily: Custom
amiSelectorTerms:
- name: ubuntu-v1
owner: "000000000000"
associatePublicIPAddress: false
blockDeviceMappings:
- deviceName: /dev/sda1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ spec:
amiFamily: Custom
amiSelectorTerms:
- name: ubuntu-v1
owner: "000000000000"
associatePublicIPAddress: false
blockDeviceMappings:
- deviceName: /dev/sda1
Expand Down
Loading