Skip to content

Commit 2afb7e8

Browse files
committed
Added a different style of defining processes and using option all
1 parent 6811f71 commit 2afb7e8

File tree

5 files changed

+179
-42
lines changed

5 files changed

+179
-42
lines changed

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

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -851,26 +851,31 @@ spec:
851851
for the given ASG. This is constantly reconciled. If a process is
852852
removed from this list it will automatically be resumed.
853853
properties:
854-
addToLoadBalancer:
855-
type: boolean
856-
alarmNotification:
857-
type: boolean
858854
all:
859855
type: boolean
860-
azRebalance:
861-
type: boolean
862-
healthCheck:
863-
type: boolean
864-
instanceRefresh:
865-
type: boolean
866-
launch:
867-
type: boolean
868-
replaceUnhealthy:
869-
type: boolean
870-
scheduledActions:
871-
type: boolean
872-
terminate:
873-
type: boolean
856+
processes:
857+
description: Processes defines the processes which can be enabled
858+
or disabled individually.
859+
properties:
860+
addToLoadBalancer:
861+
type: boolean
862+
alarmNotification:
863+
type: boolean
864+
azRebalance:
865+
type: boolean
866+
healthCheck:
867+
type: boolean
868+
instanceRefresh:
869+
type: boolean
870+
launch:
871+
type: boolean
872+
replaceUnhealthy:
873+
type: boolean
874+
scheduledActions:
875+
type: boolean
876+
terminate:
877+
type: boolean
878+
type: object
874879
type: object
875880
required:
876881
- awsLaunchTemplate

docs/book/src/topics/suspend-asg-processes.md

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ spec:
2626
instanceType: "${AWS_CONTROL_PLANE_MACHINE_TYPE}"
2727
sshKeyName: "${AWS_SSH_KEY_NAME}"
2828
suspendProcesses:
29-
launch: true
30-
alarmNotification: true
31-
azRebalance: true
29+
processes:
30+
launch: true
31+
alarmNotification: true
32+
azRebalance: true
3233
---
3334
```
3435

@@ -52,7 +53,8 @@ spec:
5253
instanceType: "${AWS_CONTROL_PLANE_MACHINE_TYPE}"
5354
sshKeyName: "${AWS_SSH_KEY_NAME}"
5455
suspendProcesses:
55-
launch: true
56+
processes:
57+
launch: true
5658
---
5759
```
5860

@@ -80,3 +82,24 @@ spec:
8082
suspendProcesses:
8183
all: true
8284
```
85+
86+
To exclude individual processes from `all` simply add them with value `false`:
87+
88+
```yaml
89+
apiVersion: infrastructure.cluster.x-k8s.io/v1beta2
90+
kind: AWSMachinePool
91+
metadata:
92+
name: capa-mp-0
93+
spec:
94+
minSize: 1
95+
maxSize: 10
96+
availabilityZones:
97+
- "${AWS_AVAILABILITY_ZONE}"
98+
awsLaunchTemplate:
99+
instanceType: "${AWS_CONTROL_PLANE_MACHINE_TYPE}"
100+
sshKeyName: "${AWS_SSH_KEY_NAME}"
101+
suspendProcesses:
102+
all: true
103+
processes:
104+
launch: false
105+
```

exp/api/v1beta2/awsmachinepool_types.go

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -93,33 +93,43 @@ type AWSMachinePoolSpec struct {
9393

9494
// SuspendProcessesTypes contains user friendly auto-completable values for suspended process names.
9595
type SuspendProcessesTypes struct {
96-
All bool `json:"all,omitempty"`
97-
Launch bool `json:"launch,omitempty"`
98-
Terminate bool `json:"terminate,omitempty"`
99-
AddToLoadBalancer bool `json:"addToLoadBalancer,omitempty"`
100-
AlarmNotification bool `json:"alarmNotification,omitempty"`
101-
AZRebalance bool `json:"azRebalance,omitempty"`
102-
HealthCheck bool `json:"healthCheck,omitempty"`
103-
InstanceRefresh bool `json:"instanceRefresh,omitempty"`
104-
ReplaceUnhealthy bool `json:"replaceUnhealthy,omitempty"`
105-
ScheduledActions bool `json:"scheduledActions,omitempty"`
96+
All bool `json:"all,omitempty"`
97+
Processes *Processes `json:"processes,omitempty"`
98+
}
99+
100+
// Processes defines the processes which can be enabled or disabled individually.
101+
type Processes struct {
102+
Launch *bool `json:"launch,omitempty"`
103+
Terminate *bool `json:"terminate,omitempty"`
104+
AddToLoadBalancer *bool `json:"addToLoadBalancer,omitempty"`
105+
AlarmNotification *bool `json:"alarmNotification,omitempty"`
106+
AZRebalance *bool `json:"azRebalance,omitempty"`
107+
HealthCheck *bool `json:"healthCheck,omitempty"`
108+
InstanceRefresh *bool `json:"instanceRefresh,omitempty"`
109+
ReplaceUnhealthy *bool `json:"replaceUnhealthy,omitempty"`
110+
ScheduledActions *bool `json:"scheduledActions,omitempty"`
106111
}
107112

108113
// ConvertSetValuesToStringSlice converts all the values that are set into a string slice for further processing.
109114
func (s *SuspendProcessesTypes) ConvertSetValuesToStringSlice() []string {
110115
if s == nil {
111116
return nil
112117
}
113-
e := reflect.ValueOf(s).Elem()
114118

119+
if s.Processes == nil {
120+
s.Processes = &Processes{}
121+
}
122+
123+
e := reflect.ValueOf(s.Processes).Elem()
115124
var result []string
116125
for i := 0; i < e.NumField(); i++ {
117126
if s.All {
118-
if e.Type().Field(i).Name == "All" {
127+
if !e.Field(i).IsNil() && !*e.Field(i).Interface().(*bool) {
128+
// don't enable if explicitly set to false.
119129
continue
120130
}
121131
result = append(result, e.Type().Field(i).Name)
122-
} else if e.Field(i).Bool() {
132+
} else if !e.Field(i).IsNil() && *e.Field(i).Interface().(*bool) {
123133
result = append(result, e.Type().Field(i).Name)
124134
}
125135
}

exp/api/v1beta2/zz_generated.deepcopy.go

Lines changed: 66 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

exp/controllers/awsmachinepool_controller_test.go

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,10 @@ func TestAWSMachinePoolReconciler(t *testing.T) {
253253
setSuspendedProcesses := func(t *testing.T, g *WithT) {
254254
t.Helper()
255255
ms.AWSMachinePool.Spec.SuspendProcesses = &expinfrav1.SuspendProcessesTypes{
256-
Launch: true,
257-
Terminate: true,
256+
Processes: &expinfrav1.Processes{
257+
Launch: pointer.Bool(true),
258+
Terminate: pointer.Bool(true),
259+
},
258260
}
259261
}
260262
t.Run("it should suspend these processes", func(t *testing.T) {
@@ -304,6 +306,34 @@ func TestAWSMachinePoolReconciler(t *testing.T) {
304306
"ScheduledActions",
305307
}).Return(nil).AnyTimes()
306308

309+
_, err := reconciler.reconcileNormal(context.Background(), ms, cs, cs)
310+
g.Expect(err).To(Succeed())
311+
})
312+
t.Run("and one or more processes are disabled, it should not list those", func(t *testing.T) {
313+
g := NewWithT(t)
314+
setup(t, g)
315+
defer teardown(t, g)
316+
setSuspendedProcesses(t, g)
317+
ms.AWSMachinePool.Spec.SuspendProcesses.Processes = &expinfrav1.Processes{
318+
Launch: pointer.Bool(false),
319+
AZRebalance: pointer.Bool(true), // this should still be included but not twice...
320+
}
321+
ec2Svc.EXPECT().ReconcileLaunchTemplate(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)
322+
asgSvc.EXPECT().GetASGByName(gomock.Any()).Return(nil, nil)
323+
asgSvc.EXPECT().CreateASG(gomock.Any()).Return(&expinfrav1.AutoScalingGroup{
324+
Name: "name",
325+
}, nil)
326+
asgSvc.EXPECT().SuspendProcesses("name", []string{
327+
"Terminate",
328+
"AddToLoadBalancer",
329+
"AlarmNotification",
330+
"AZRebalance",
331+
"HealthCheck",
332+
"InstanceRefresh",
333+
"ReplaceUnhealthy",
334+
"ScheduledActions",
335+
}).Return(nil).AnyTimes()
336+
307337
_, err := reconciler.reconcileNormal(context.Background(), ms, cs, cs)
308338
g.Expect(err).To(Succeed())
309339
})
@@ -313,8 +343,10 @@ func TestAWSMachinePoolReconciler(t *testing.T) {
313343
t.Helper()
314344

315345
ms.AWSMachinePool.Spec.SuspendProcesses = &expinfrav1.SuspendProcessesTypes{
316-
Launch: true,
317-
Terminate: true,
346+
Processes: &expinfrav1.Processes{
347+
Launch: pointer.Bool(true),
348+
Terminate: pointer.Bool(true),
349+
},
318350
}
319351
}
320352
t.Run("it should suspend and resume processes that are desired to be suspended and desired to be resumed", func(t *testing.T) {
@@ -624,8 +656,10 @@ func TestASGNeedsUpdates(t *testing.T) {
624656
Overrides: nil,
625657
},
626658
SuspendProcesses: &expinfrav1.SuspendProcessesTypes{
627-
Launch: true,
628-
Terminate: true,
659+
Processes: &expinfrav1.Processes{
660+
Launch: pointer.Bool(true),
661+
Terminate: pointer.Bool(true),
662+
},
629663
},
630664
},
631665
},

0 commit comments

Comments
 (0)