Skip to content

Commit 9b6fe34

Browse files
authored
Merge pull request #4848 from muraee/rosa-additional-sgs
✨ ROSA: Add additionalSecurityGroups field to ROSAMachinePool
2 parents 2d004cc + 4df5b91 commit 9b6fe34

File tree

5 files changed

+67
-15
lines changed

5 files changed

+67
-15
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ spec:
4747
spec:
4848
description: RosaMachinePoolSpec defines the desired state of RosaMachinePool.
4949
properties:
50+
additionalSecurityGroups:
51+
description: AdditionalSecurityGroups is an optional set of security
52+
groups to associate with all node instances of the machine pool.
53+
items:
54+
type: string
55+
type: array
5056
autoRepair:
5157
default: false
5258
description: AutoRepair specifies whether health checks should be

exp/api/v1beta2/rosamachinepool_types.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ type RosaMachinePoolSpec struct {
7979
// +optional
8080
TuningConfigs []string `json:"tuningConfigs,omitempty"`
8181

82+
// AdditionalSecurityGroups is an optional set of security groups to associate
83+
// with all node instances of the machine pool.
84+
//
85+
// +immutable
86+
// +optional
87+
AdditionalSecurityGroups []string `json:"additionalSecurityGroups,omitempty"`
88+
8289
// ProviderIDList contain a ProviderID for each machine instance that's currently managed by this machine pool.
8390
// +optional
8491
ProviderIDList []string `json:"providerIDList,omitempty"`

exp/api/v1beta2/rosamachinepool_webhook.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package v1beta2
22

33
import (
44
"github.com/blang/semver"
5+
"github.com/google/go-cmp/cmp"
6+
"github.com/pkg/errors"
57
apierrors "k8s.io/apimachinery/pkg/api/errors"
68
runtime "k8s.io/apimachinery/pkg/runtime"
79
"k8s.io/apimachinery/pkg/util/validation/field"
@@ -44,12 +46,20 @@ func (r *ROSAMachinePool) ValidateCreate() (warnings admission.Warnings, err err
4446

4547
// ValidateUpdate implements admission.Validator.
4648
func (r *ROSAMachinePool) ValidateUpdate(old runtime.Object) (warnings admission.Warnings, err error) {
47-
var allErrs field.ErrorList
49+
oldPool, ok := old.(*ROSAMachinePool)
50+
if !ok {
51+
return nil, apierrors.NewInvalid(GroupVersion.WithKind("ROSAMachinePool").GroupKind(), r.Name, field.ErrorList{
52+
field.InternalError(nil, errors.New("failed to convert old ROSAMachinePool to object")),
53+
})
54+
}
4855

56+
var allErrs field.ErrorList
4957
if err := r.validateVersion(); err != nil {
5058
allErrs = append(allErrs, err)
5159
}
5260

61+
allErrs = append(allErrs, validateImmutable(oldPool.Spec.AdditionalSecurityGroups, r.Spec.AdditionalSecurityGroups, "additionalSecurityGroups")...)
62+
5363
if len(allErrs) == 0 {
5464
return nil, nil
5565
}
@@ -78,6 +88,19 @@ func (r *ROSAMachinePool) validateVersion() *field.Error {
7888
return nil
7989
}
8090

91+
func validateImmutable(old, updated interface{}, name string) field.ErrorList {
92+
var allErrs field.ErrorList
93+
94+
if !cmp.Equal(old, updated) {
95+
allErrs = append(
96+
allErrs,
97+
field.Invalid(field.NewPath("spec", name), updated, "field is immutable"),
98+
)
99+
}
100+
101+
return allErrs
102+
}
103+
81104
// Default implements admission.Defaulter.
82105
func (r *ROSAMachinePool) Default() {
83106
}

exp/api/v1beta2/zz_generated.deepcopy.go

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

exp/controllers/rosamachinepool_controller.go

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -352,16 +352,18 @@ func (r *ROSAMachinePoolReconciler) updateNodePool(machinePoolScope *scope.RosaM
352352

353353
currentSpec := nodePoolToRosaMachinePoolSpec(nodePool)
354354
currentSpec.ProviderIDList = desiredSpec.ProviderIDList // providerIDList is set by the controller and shouldn't be compared here.
355-
currentSpec.Version = desiredSpec.Version // Version changed are reconciled separately and shouldn't be compared here.
355+
currentSpec.Version = desiredSpec.Version // Version changes are reconciled separately and shouldn't be compared here.
356356

357357
if cmp.Equal(desiredSpec, currentSpec) {
358358
// no changes detected.
359359
return nodePool, nil
360360
}
361361

362-
npBuilder := nodePoolBuilder(*desiredSpec, machinePoolScope.MachinePool.Spec)
363-
npBuilder.Version(nil) // eunsure version is unset.
362+
// zero-out fields that shouldn't be part of the update call.
363+
desiredSpec.Version = ""
364+
desiredSpec.AdditionalSecurityGroups = nil
364365

366+
npBuilder := nodePoolBuilder(*desiredSpec, machinePoolScope.MachinePool.Spec)
365367
nodePoolSpec, err := npBuilder.Build()
366368
if err != nil {
367369
return nil, fmt.Errorf("failed to build nodePool spec: %w", err)
@@ -401,8 +403,11 @@ func validateMachinePoolSpec(machinePoolScope *scope.RosaMachinePoolScope) (*str
401403
func nodePoolBuilder(rosaMachinePoolSpec expinfrav1.RosaMachinePoolSpec, machinePoolSpec expclusterv1.MachinePoolSpec) *cmv1.NodePoolBuilder {
402404
npBuilder := cmv1.NewNodePool().ID(rosaMachinePoolSpec.NodePoolName).
403405
Labels(rosaMachinePoolSpec.Labels).
404-
AutoRepair(rosaMachinePoolSpec.AutoRepair).
405-
TuningConfigs(rosaMachinePoolSpec.TuningConfigs...)
406+
AutoRepair(rosaMachinePoolSpec.AutoRepair)
407+
408+
if rosaMachinePoolSpec.TuningConfigs != nil {
409+
npBuilder = npBuilder.TuningConfigs(rosaMachinePoolSpec.TuningConfigs...)
410+
}
406411

407412
if len(rosaMachinePoolSpec.Taints) > 0 {
408413
taintBuilders := []*cmv1.TaintBuilder{}
@@ -430,7 +435,12 @@ func nodePoolBuilder(rosaMachinePoolSpec expinfrav1.RosaMachinePoolSpec, machine
430435
npBuilder.Subnet(rosaMachinePoolSpec.Subnet)
431436
}
432437

433-
npBuilder.AWSNodePool(cmv1.NewAWSNodePool().InstanceType(rosaMachinePoolSpec.InstanceType))
438+
awsNodePool := cmv1.NewAWSNodePool().InstanceType(rosaMachinePoolSpec.InstanceType)
439+
if rosaMachinePoolSpec.AdditionalSecurityGroups != nil {
440+
awsNodePool = awsNodePool.AdditionalSecurityGroupIds(rosaMachinePoolSpec.AdditionalSecurityGroups...)
441+
}
442+
npBuilder.AWSNodePool(awsNodePool)
443+
434444
if rosaMachinePoolSpec.Version != "" {
435445
npBuilder.Version(cmv1.NewVersion().ID(ocm.CreateVersionID(rosaMachinePoolSpec.Version, ocm.DefaultChannelGroup)))
436446
}
@@ -440,14 +450,15 @@ func nodePoolBuilder(rosaMachinePoolSpec expinfrav1.RosaMachinePoolSpec, machine
440450

441451
func nodePoolToRosaMachinePoolSpec(nodePool *cmv1.NodePool) expinfrav1.RosaMachinePoolSpec {
442452
spec := expinfrav1.RosaMachinePoolSpec{
443-
NodePoolName: nodePool.ID(),
444-
Version: rosa.RawVersionID(nodePool.Version()),
445-
AvailabilityZone: nodePool.AvailabilityZone(),
446-
Subnet: nodePool.Subnet(),
447-
Labels: nodePool.Labels(),
448-
AutoRepair: nodePool.AutoRepair(),
449-
InstanceType: nodePool.AWSNodePool().InstanceType(),
450-
TuningConfigs: nodePool.TuningConfigs(),
453+
NodePoolName: nodePool.ID(),
454+
Version: rosa.RawVersionID(nodePool.Version()),
455+
AvailabilityZone: nodePool.AvailabilityZone(),
456+
Subnet: nodePool.Subnet(),
457+
Labels: nodePool.Labels(),
458+
AutoRepair: nodePool.AutoRepair(),
459+
InstanceType: nodePool.AWSNodePool().InstanceType(),
460+
TuningConfigs: nodePool.TuningConfigs(),
461+
AdditionalSecurityGroups: nodePool.AWSNodePool().AdditionalSecurityGroupIds(),
451462
}
452463

453464
if nodePool.Autoscaling() != nil {

0 commit comments

Comments
 (0)