Skip to content

Commit

Permalink
feat: add support to configure associatePublicIP
Browse files Browse the repository at this point in the history
  • Loading branch information
rafatio committed Sep 4, 2024
1 parent 1024137 commit 9eb8d1f
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 26 deletions.
24 changes: 12 additions & 12 deletions controllers/controlplane/kopscontrolplane_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package controlplane
import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"os"
Expand Down Expand Up @@ -1894,7 +1896,6 @@ func TestPrepareCustomCloudResources(t *testing.T) {
description string
kopsMachinePoolFunction func(*infrastructurev1alpha1.KopsMachinePool) *infrastructurev1alpha1.KopsMachinePool
karpenterResourcesOutput string
manifestHash string
spotInstEnabled bool
}{
{
Expand Down Expand Up @@ -1968,7 +1969,6 @@ func TestPrepareCustomCloudResources(t *testing.T) {
return kmp
},
karpenterResourcesOutput: "karpenter_resource_output_provisioner.yaml",
manifestHash: "d67c9504589dd859e46f1913780fb69bafb8df5328d90e6675affc79d3573f78",
},
{
description: "Should generate files based on template with one NodePool",
Expand Down Expand Up @@ -2054,7 +2054,6 @@ func TestPrepareCustomCloudResources(t *testing.T) {
return kmp
},
karpenterResourcesOutput: "karpenter_resource_output_node_pool.yaml",
manifestHash: "faae88e64643f1ab57172001063176e665a9519f6a939242557ea136ec4c4f21",
},
{
description: "Should generate files based on template with one NodePool and one Provisioner",
Expand Down Expand Up @@ -2202,7 +2201,6 @@ func TestPrepareCustomCloudResources(t *testing.T) {
return kmp
},
karpenterResourcesOutput: "karpenter_resource_output_node_pool_and_provisioner.yaml",
manifestHash: "b9b15e8457b2e877c708a44d2edbbd33dc5ed696063dd30134d825c1eb5d255a",
},
{
description: "Should generate files based on with spotinst enabled",
Expand Down Expand Up @@ -2275,7 +2273,6 @@ func TestPrepareCustomCloudResources(t *testing.T) {
return kmp
},
karpenterResourcesOutput: "karpenter_resource_output_provisioner.yaml",
manifestHash: "d67c9504589dd859e46f1913780fb69bafb8df5328d90e6675affc79d3573f78",
spotInstEnabled: true,
},
}
Expand Down Expand Up @@ -2332,11 +2329,20 @@ func TestPrepareCustomCloudResources(t *testing.T) {
templ, err := template.New(templateTestDir + "/karpenter_custom_addon_boostrap.tf").Parse(string(content))
g.Expect(err).NotTo(HaveOccurred())

generatedKarpenterResources, err := os.ReadFile(terraformOutputDir + "/data/aws_s3_object_karpenter_resources_content")
g.Expect(err).NotTo(HaveOccurred())
templatedKarpenterResources, err := os.ReadFile(templateTestDir + "/data/" + tc.karpenterResourcesOutput)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(string(generatedKarpenterResources)).To(BeEquivalentTo(string(templatedKarpenterResources)))

hash := sha256.New()
hash.Write(templatedKarpenterResources)

var templatedKarpenterBoostrapTF bytes.Buffer
data := struct {
ManifestHash string
}{
ManifestHash: tc.manifestHash,
ManifestHash: hex.EncodeToString(hash.Sum(nil)),
}

err = templ.Execute(&templatedKarpenterBoostrapTF, data)
Expand All @@ -2350,12 +2356,6 @@ func TestPrepareCustomCloudResources(t *testing.T) {
g.Expect(err).NotTo(HaveOccurred())
g.Expect(string(generatedLaunchTemplateTF)).To(BeEquivalentTo(string(templatedLaunchTemplateTF)))

generatedKarpenterResources, err := os.ReadFile(terraformOutputDir + "/data/aws_s3_object_karpenter_resources_content")
g.Expect(err).NotTo(HaveOccurred())
templatedKarpenterResources, err := os.ReadFile(templateTestDir + "/data/" + tc.karpenterResourcesOutput)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(string(generatedKarpenterResources)).To(BeEquivalentTo(string(templatedKarpenterResources)))

if tc.spotInstEnabled {
generatedSpotinstLaunchSpecTF, err := os.ReadFile(terraformOutputDir + "/spotinst_launch_spec_override.tf")
g.Expect(err).NotTo(HaveOccurred())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ spec:
httpProtocolIPv6: disabled
httpPutResponseHopLimit: 3
httpTokens: required
associatePublicIPAddress: false
blockDeviceMappings:
- deviceName: /dev/sda1
ebs:
Expand Down
37 changes: 23 additions & 14 deletions utils/karpenter_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,31 @@ func CreateEC2NodeClassFromKopsLaunchTemplateInfo(kopsCluster *kopsapi.Cluster,
return "", err
}

var associatePublicIP bool
if kmp.Spec.KopsInstanceGroupSpec.AssociatePublicIP != nil {
associatePublicIP = *kmp.Spec.KopsInstanceGroupSpec.AssociatePublicIP
} else {
associatePublicIP = false
}

data := struct {
Name string
AmiName string
ClusterName string
IGName string
Tags map[string]string
RootVolume *karpenterv1beta1.BlockDevice
UserData string
Name string
AmiName string
ClusterName string
IGName string
Tags map[string]string
RootVolume *karpenterv1beta1.BlockDevice
UserData string
AssociatePublicIP bool
}{
Name: nodePoolName,
AmiName: amiName,
IGName: kmp.Name,
ClusterName: kopsCluster.Name,
Tags: kopsCluster.Spec.CloudLabels,
RootVolume: BuildKarpenterVolumeConfigFromKops(kmp.Spec.KopsInstanceGroupSpec.RootVolume),
UserData: userData,
Name: nodePoolName,
AmiName: amiName,
IGName: kmp.Name,
ClusterName: kopsCluster.Name,
Tags: kopsCluster.Spec.CloudLabels,
RootVolume: BuildKarpenterVolumeConfigFromKops(kmp.Spec.KopsInstanceGroupSpec.RootVolume),
UserData: userData,
AssociatePublicIP: associatePublicIP,
}

content, err := templates.ReadFile("templates/ec2nodeclass.yaml.tpl")
Expand Down
1 change: 1 addition & 0 deletions utils/templates/ec2nodeclass.yaml.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ spec:
httpProtocolIPv6: disabled
httpPutResponseHopLimit: 3
httpTokens: required
associatePublicIPAddress: {{ .AssociatePublicIP }}
blockDeviceMappings:
- deviceName: /dev/sda1
ebs:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ spec:
httpProtocolIPv6: disabled
httpPutResponseHopLimit: 3
httpTokens: required
associatePublicIPAddress: false
blockDeviceMappings:
- deviceName: /dev/sda1
ebs:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ spec:
httpProtocolIPv6: disabled
httpPutResponseHopLimit: 3
httpTokens: required
associatePublicIPAddress: false
blockDeviceMappings:
- deviceName: /dev/sda1
ebs:
Expand Down

0 comments on commit 9eb8d1f

Please sign in to comment.