Skip to content

Commit da96010

Browse files
committed
Use map for subnet and security group selectors
1 parent 5ae04e2 commit da96010

File tree

6 files changed

+57
-62
lines changed

6 files changed

+57
-62
lines changed

api/v1alpha1/karpentermachinepool_types.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,20 +120,14 @@ type EC2NodeClassSpec struct {
120120
// Owner is the owner for the ami.
121121
// You can specify a combination of AWS account IDs, "self", "amazon", and "aws-marketplace"
122122
AMIOwner string `json:"amiOwner,omitempty"`
123-
// - name: flatcar-stable-{{ $.Values.baseOS }}-kube-{{ $.Values.k8sVersion }}-tooling-{{ $.Values.toolingVersion }}-gs
124-
// // owner: {{ int64 $.Values.amiOwner | quote }}
125123

126124
// SecurityGroups specifies the security groups to use
127125
// +optional
128-
SecurityGroups []string `json:"securityGroups,omitempty"`
126+
SecurityGroups map[string]string `json:"securityGroups,omitempty"`
129127

130128
// Subnets specifies the subnets to use
131129
// +optional
132-
Subnets []string `json:"subnets,omitempty"`
133-
134-
// UserData specifies the user data to use
135-
// +optional
136-
UserData *string `json:"userData,omitempty"`
130+
Subnets map[string]string `json:"subnets,omitempty"`
137131

138132
// Tags specifies the tags to apply to EC2 instances
139133
// +optional

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 8 additions & 9 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_karpentermachinepools.yaml

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,23 +65,20 @@ spec:
6565
You can specify a combination of AWS account IDs, "self", "amazon", and "aws-marketplace"
6666
type: string
6767
securityGroups:
68-
description: SecurityGroups specifies the security groups to use
69-
items:
68+
additionalProperties:
7069
type: string
71-
type: array
70+
description: SecurityGroups specifies the security groups to use
71+
type: object
7272
subnets:
73-
description: Subnets specifies the subnets to use
74-
items:
73+
additionalProperties:
7574
type: string
76-
type: array
75+
description: Subnets specifies the subnets to use
76+
type: object
7777
tags:
7878
additionalProperties:
7979
type: string
8080
description: Tags specifies the tags to apply to EC2 instances
8181
type: object
82-
userData:
83-
description: UserData specifies the user data to use
84-
type: string
8582
type: object
8683
iamInstanceProfile:
8784
description: |-

controllers/karpentermachinepool_controller.go

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,28 @@ func (r *KarpenterMachinePoolReconciler) createOrUpdateEC2NodeClass(ctx context.
416416
// Generate user data for Ignition
417417
userData := r.generateUserData(awsCluster.Spec.S3Bucket.Name, cluster.Name, karpenterMachinePool.Name)
418418

419+
// Add security groups tag selector if specified
420+
securityGroupTagsSelector := map[string]string{
421+
fmt.Sprintf("sigs.k8s.io/cluster-api-provider-aws/cluster/%s", cluster.Name): "owned",
422+
"sigs.k8s.io/cluster-api-provider-aws/role": "node",
423+
}
424+
if karpenterMachinePool.Spec.EC2NodeClass != nil && len(karpenterMachinePool.Spec.EC2NodeClass.SecurityGroups) > 0 {
425+
for securityGroupTagKey, securityGroupTagValue := range karpenterMachinePool.Spec.EC2NodeClass.SecurityGroups {
426+
securityGroupTagsSelector[securityGroupTagKey] = securityGroupTagValue
427+
}
428+
}
429+
430+
// Add subnet tag selector if specified
431+
subnetTagsSelector := map[string]string{
432+
fmt.Sprintf("sigs.k8s.io/cluster-api-provider-aws/cluster/%s", cluster.Name): "owned",
433+
"giantswarm.io/role": "nodes",
434+
}
435+
if karpenterMachinePool.Spec.EC2NodeClass != nil && len(karpenterMachinePool.Spec.EC2NodeClass.Subnets) > 0 {
436+
for subnetTagKey, subnetTagValue := range karpenterMachinePool.Spec.EC2NodeClass.Subnets {
437+
subnetTagsSelector[subnetTagKey] = subnetTagValue
438+
}
439+
}
440+
419441
operation, err := controllerutil.CreateOrUpdate(ctx, workloadClusterClient, ec2NodeClass, func() error {
420442
// Build the EC2NodeClass spec
421443
spec := map[string]interface{}{
@@ -427,29 +449,17 @@ func (r *KarpenterMachinePoolReconciler) createOrUpdateEC2NodeClass(ctx context.
427449
},
428450
},
429451
"instanceProfile": karpenterMachinePool.Spec.IamInstanceProfile,
430-
"userData": userData,
431-
}
432-
433-
// Add security groups if specified
434-
if karpenterMachinePool.Spec.EC2NodeClass != nil && len(karpenterMachinePool.Spec.EC2NodeClass.SecurityGroups) > 0 {
435-
spec["securityGroupSelectorTerms"] = []map[string]interface{}{
452+
"securityGroupSelectorTerms": []map[string]interface{}{
436453
{
437-
"tags": map[string]string{
438-
"Name": karpenterMachinePool.Spec.EC2NodeClass.SecurityGroups[0], // Using first security group for now
439-
},
454+
"tags": securityGroupTagsSelector,
440455
},
441-
}
442-
}
443-
444-
// Add subnets if specified
445-
if karpenterMachinePool.Spec.EC2NodeClass != nil && len(karpenterMachinePool.Spec.EC2NodeClass.Subnets) > 0 {
446-
spec["subnetSelectorTerms"] = []map[string]interface{}{
456+
},
457+
"subnetSelectorTerms": []map[string]interface{}{
447458
{
448-
"tags": map[string]string{
449-
"Name": karpenterMachinePool.Spec.EC2NodeClass.Subnets[0], // Using first subnet for now
450-
},
459+
"tags": subnetTagsSelector,
451460
},
452-
}
461+
},
462+
"userData": userData,
453463
}
454464

455465
// Add tags if specified

controllers/karpentermachinepool_controller_test.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -500,9 +500,8 @@ var _ = Describe("KarpenterMachinePool reconciler", func() {
500500
EC2NodeClass: &karpenterinfra.EC2NodeClassSpec{
501501
AMIName: AMIName,
502502
AMIOwner: AMIOwner,
503-
SecurityGroups: []string{KarpenterNodesSecurityGroup},
504-
Subnets: []string{KarpenterNodesSubnets},
505-
// UserData: nil,
503+
SecurityGroups: map[string]string{"my-target-sg": "is-this"},
504+
Subnets: map[string]string{"my-target-subnet": "is-that"},
506505
// Tags: nil,
507506
},
508507
IamInstanceProfile: KarpenterNodesInstanceProfile,
@@ -802,7 +801,7 @@ var _ = Describe("KarpenterMachinePool reconciler", func() {
802801
// Assert the security group name field
803802
securityGroupTags, ok := securityGroupSelectorTerm0["tags"].(map[string]interface{})
804803
Expect(ok).To(BeTrue(), "expected tags to be a map[string]string")
805-
Expect(securityGroupTags["Name"]).To(Equal(KarpenterNodesSecurityGroup))
804+
Expect(securityGroupTags["my-target-sg"]).To(Equal("is-this"))
806805

807806
// Assert subnets are the expected ones
808807
subnetSelectorTerms, found, err := unstructured.NestedSlice(ec2nodeclassList.Items[0].Object, "spec", "subnetSelectorTerms")
@@ -815,7 +814,7 @@ var _ = Describe("KarpenterMachinePool reconciler", func() {
815814
// Assert the security group name field
816815
subnetTags, ok := subnetSelectorTerm0["tags"].(map[string]interface{})
817816
Expect(ok).To(BeTrue(), "expected tags to be a map[string]string")
818-
Expect(subnetTags["Name"]).To(Equal(KarpenterNodesSubnets))
817+
Expect(subnetTags["my-target-subnet"]).To(Equal("is-that"))
819818

820819
// Assert userdata is the expected one
821820
userData, found, err := unstructured.NestedString(ec2nodeclassList.Items[0].Object, "spec", "userData")
@@ -1017,9 +1016,8 @@ var _ = Describe("KarpenterMachinePool reconciler", func() {
10171016
},
10181017
Spec: karpenterinfra.KarpenterMachinePoolSpec{
10191018
EC2NodeClass: &karpenterinfra.EC2NodeClassSpec{
1020-
SecurityGroups: []string{KarpenterNodesSecurityGroup},
1021-
Subnets: []string{KarpenterNodesSubnets},
1022-
// UserData: nil,
1019+
SecurityGroups: map[string]string{"my-target-sg": "is-this"},
1020+
Subnets: map[string]string{"my-target-subnet": "is-that"},
10231021
// Tags: nil,
10241022
},
10251023
IamInstanceProfile: KarpenterNodesInstanceProfile,

helm/aws-resolver-rules-operator/templates/infrastructure.cluster.x-k8s.io_karpentermachinepools.yaml

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,23 +65,20 @@ spec:
6565
You can specify a combination of AWS account IDs, "self", "amazon", and "aws-marketplace"
6666
type: string
6767
securityGroups:
68-
description: SecurityGroups specifies the security groups to use
69-
items:
68+
additionalProperties:
7069
type: string
71-
type: array
70+
description: SecurityGroups specifies the security groups to use
71+
type: object
7272
subnets:
73-
description: Subnets specifies the subnets to use
74-
items:
73+
additionalProperties:
7574
type: string
76-
type: array
75+
description: Subnets specifies the subnets to use
76+
type: object
7777
tags:
7878
additionalProperties:
7979
type: string
8080
description: Tags specifies the tags to apply to EC2 instances
8181
type: object
82-
userData:
83-
description: UserData specifies the user data to use
84-
type: string
8582
type: object
8683
iamInstanceProfile:
8784
description: |-

0 commit comments

Comments
 (0)