Skip to content

Commit fb092fe

Browse files
authored
Introduce annotation to tweak scalingConfig.desiredSize control (#85)
Introduce Annotation for Dynamic Nodegroup `spec.ScalingConfig.desiredSize` Control This patch introduces a new annotation that allows flexible management of the desired size for a nodegroup scaling configuration. The annotation, `eks.services.k8s.aws/desired-size-managed-by`, can be set on a nodegroup custom resource and specifies whether the desired size is managed externally by an autoscaler or by the ACK controller. The possible values for the annotation include 'external-autoscaler' and 'ack-eks-controller'. If the annotation is not set or has an invalid value, the controller defaults to managing the desired size. Adjustments are made to delta comparison logic, custom pre and post-compare hooks, and the update configuration logic to ensure proper handling of the desired size based on the annotation. This enhancement provides flexibility in managing the desired size of a nodegroup, allowing external entities or the ACK controller to control this aspect. Signed-off-by: Amine Hilaly <[email protected]> By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 9c8df0d commit fb092fe

File tree

9 files changed

+443
-16
lines changed

9 files changed

+443
-16
lines changed

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,38 @@ Kubernetes Github project.
88

99
[ack-issues]: https://github.com/aws-controllers-k8s/community/issues
1010

11+
## Getting started
12+
13+
To install the `eks-controller` on your cluster, follow the the
14+
[installation][ack-install] instructions.
15+
16+
Currently, the `eks-controller` is GA and supports the following resources:
17+
- `Cluster`
18+
- `Nodegroup`
19+
- `FargateProfile`
20+
- `Addon`
21+
- `PodIdentityAssociation`
22+
23+
A detailed list of the resources supported specifications can be found in the
24+
[references][ack-references] section.
25+
26+
## Annotations
27+
28+
For some resources, the `eks-controller` supports annotations to customize the
29+
behavior of the controller. The following annotations are supported:
30+
31+
- **Nodegroup**
32+
- `eks.service.k8s.aws/desired-size-managed-by`: used to control whether the
33+
controller should manage the desiredSize of the Nodegroup `spec.ScalingConfig.DesiredSize`.
34+
It supports the following values:
35+
- `ack-eks-controller`: If set the controller will be responsible for
36+
managing the desired size of the nodegroup.
37+
- `external-autoscaler`: If set will ignore any changes to the
38+
`spec.ScalingConfig.DesiredSize` and will not manage the desired size
39+
of the nodegroup.
40+
41+
If not set, the controller will default to `ack-eks-controller`.
42+
1143
## Contributing
1244

1345
We welcome community contributions and pull requests.
@@ -24,3 +56,6 @@ You can also learn more about our [Governance](/GOVERNANCE.md) structure.
2456
## License
2557

2658
This project is [licensed](/LICENSE) under the Apache-2.0 License.
59+
60+
[ack-references]:https://aws-controllers-k8s.github.io/community/reference
61+
[ack-install]:https://aws-controllers-k8s.github.io/community/docs/user-docs/install/
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
ack_generate_info:
2-
build_date: "2023-12-14T21:31:34Z"
3-
build_hash: 1f16813c807af6889060b4ce7ded2a69dc027d8c
2+
build_date: "2024-01-02T04:11:25Z"
3+
build_hash: 00e081fb541587f33970ad80c99f2ac02e9c2525
44
go_version: go1.21.5
5-
version: v0.28.0
6-
api_directory_checksum: c243e9ab23c6b9038f6f6aebba5f0b34efafdf6a
5+
version: v0.28.0-8-g00e081f
6+
api_directory_checksum: e3ab17e79de95c932895a4372c0b01e976aec4cb
77
api_version: v1alpha1
88
aws_sdk_go_version: v1.49.0
99
generator_config_info:
10-
file_checksum: 9b2af7d38552d1e11a320576c802f375dcba3a06
10+
file_checksum: 95f5a8da41f547e5d9c7f207d5405b64ed866196
1111
original_file_name: generator.yaml
1212
last_modification:
1313
reason: API generation

apis/v1alpha1/annotation.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"). You may
4+
// not use this file except in compliance with the License. A copy of the
5+
// License is located at
6+
//
7+
// http://aws.amazon.com/apache2.0/
8+
//
9+
// or in the "license" file accompanying this file. This file is distributed
10+
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
// express or implied. See the License for the specific language governing
12+
// permissions and limitations under the License.
13+
14+
package v1alpha1
15+
16+
import "fmt"
17+
18+
var (
19+
// DesiredSizeManagedByAnnotation is the annotation key used to set the management style for the
20+
// desired size of a nodegroup scaling configuration. This annotation can only be set on a
21+
// nodegroup custom resource.
22+
//
23+
// The value of this annotation must be one of the following:
24+
//
25+
// - 'external-autoscaler': The desired size is managed by an external entity. Causing the
26+
// controller to completly ignore the `scalingConfig.desiredSize` field
27+
// and not reconcile the desired size of a nodegroup.
28+
//
29+
// - 'ack-eks-controller': The desired size is managed by the ACK controller. Causing the
30+
// controller to reconcile the desired size of the nodegroup with the
31+
// value of the `spec.scalingConfig.desiredSize` field.
32+
//
33+
// By default the desired size is managed by the controller. If the annotation is not set, or
34+
// the value is not one of the above, the controller will default to managing the desired size
35+
// as if the annotation was set to "controller".
36+
DesiredSizeManagedByAnnotation = fmt.Sprintf("%s/desired-size-managed-by", GroupVersion.Group)
37+
)
38+
39+
const (
40+
// DesiredSizeManagedByExternalAutoscaler is the value of the DesiredSizeManagedByAnnotation
41+
// annotation that indicates that the desired size of a nodegroup is managed by an external
42+
// autoscaler.
43+
DesiredSizeManagedByExternalAutoscaler = "external-autoscaler"
44+
// DesiredSizeManagedByACKController is the value of the DesiredSizeManagedByAnnotation
45+
// annotation that indicates that the desired size of a nodegroup is managed by the ACK
46+
// controller.
47+
DesiredSizeManagedByACKController = "ack-eks-controller"
48+
)

apis/v1alpha1/generator.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ resources:
202202
hooks:
203203
delta_pre_compare:
204204
code: customPreCompare(delta, a, b)
205+
delta_post_compare:
206+
code: customPostCompare(delta, a, b)
205207
sdk_create_post_set_output:
206208
template_path: hooks/nodegroup/sdk_create_post_set_output.go.tpl
207209
sdk_read_one_post_set_output:

generator.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ resources:
202202
hooks:
203203
delta_pre_compare:
204204
code: customPreCompare(delta, a, b)
205+
delta_post_compare:
206+
code: customPostCompare(delta, a, b)
205207
sdk_create_post_set_output:
206208
template_path: hooks/nodegroup/sdk_create_post_set_output.go.tpl
207209
sdk_read_one_post_set_output:

helm/crds/services.k8s.aws_adoptedresources.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,10 @@ spec:
161161
description: 'Kind of the referent. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
162162
type: string
163163
name:
164-
description: 'Name of the referent. More info: http://kubernetes.io/docs/user-guide/identifiers#names'
164+
description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names#names'
165165
type: string
166166
uid:
167-
description: 'UID of the referent. More info: http://kubernetes.io/docs/user-guide/identifiers#uids'
167+
description: 'UID of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names#uids'
168168
type: string
169169
required:
170170
- apiVersion

pkg/resource/nodegroup/delta.go

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

pkg/resource/nodegroup/hook.go

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,48 @@ func customPreCompare(
102102
}
103103
}
104104

105+
func getDesiredSizeManagedByAnnotation(nodegroup *svcapitypes.Nodegroup) (string, bool) {
106+
if len(nodegroup.Annotations) == 0 {
107+
return "", false
108+
}
109+
managedBy, ok := nodegroup.Annotations[svcapitypes.DesiredSizeManagedByAnnotation]
110+
return managedBy, ok
111+
}
112+
113+
func isManagedByExternalAutoscaler(nodegroup *svcapitypes.Nodegroup) bool {
114+
managedBy, ok := getDesiredSizeManagedByAnnotation(nodegroup)
115+
if !ok {
116+
return false
117+
}
118+
return managedBy == svcapitypes.DesiredSizeManagedByExternalAutoscaler
119+
}
120+
121+
func customPostCompare(
122+
delta *ackcompare.Delta,
123+
a *resource,
124+
b *resource,
125+
) {
126+
// We only want to compare the desiredSize field if and only if the
127+
// desiredSize is managed by the controller, meaning that in the case
128+
// where the desiredSize is managed by an external entity, we do not
129+
// want to compare the desiredSize field.
130+
// When managed by an external entity, an annotation is set on the
131+
// nodegroup resource to indicate that the desiredSize is managed
132+
// externally.
133+
if isManagedByExternalAutoscaler(a.ko) && delta.DifferentAt("Spec.ScalingConfig.DesiredSize") {
134+
// We need to unset the desiredSize field in the delta so that the
135+
// controller does not attempt to reconcile the desiredSize of the
136+
// nodegroup.
137+
newDiffs := make([]*ackcompare.Difference, 0)
138+
for _, d := range delta.Differences {
139+
if !d.Path.Contains("Spec.ScalingConfig.DesiredSize") {
140+
newDiffs = append(newDiffs, d)
141+
}
142+
}
143+
delta.Differences = newDiffs
144+
}
145+
}
146+
105147
// requeueWaitUntilCanModify returns a `ackrequeue.RequeueNeededAfter` struct
106148
// explaining the nodegroup cannot be modified until it reaches an active
107149
// status.
@@ -218,7 +260,7 @@ func (rm *resourceManager) customUpdate(
218260

219261
if delta.DifferentAt("Spec.Labels") || delta.DifferentAt("Spec.Taints") ||
220262
delta.DifferentAt("Spec.ScalingConfig") || delta.DifferentAt("Spec.UpdateConfig") {
221-
if err := rm.updateConfig(ctx, desired, latest); err != nil {
263+
if err := rm.updateConfig(ctx, delta, desired, latest); err != nil {
222264
return nil, err
223265
}
224266
return returnNodegroupUpdating(updatedRes)
@@ -342,8 +384,21 @@ func (rm *resourceManager) updateVersion(
342384
return nil
343385
}
344386

387+
func (rm *resourceManager) newUpdateScalingConfigPayload(
388+
desired, latest *resource,
389+
) *svcsdk.NodegroupScalingConfig {
390+
sc := rm.newNodegroupScalingConfig(desired)
391+
// We need to default the desiredSize to the current observed
392+
// value in the case where the desiredSize is managed externally.
393+
if isManagedByExternalAutoscaler(desired.ko) && latest.ko.Spec.ScalingConfig != nil {
394+
sc.DesiredSize = latest.ko.Spec.ScalingConfig.DesiredSize
395+
}
396+
return sc
397+
}
398+
345399
func (rm *resourceManager) updateConfig(
346400
ctx context.Context,
401+
delta *ackcompare.Delta,
347402
desired *resource,
348403
latest *resource,
349404
) (err error) {
@@ -359,7 +414,7 @@ func (rm *resourceManager) updateConfig(
359414
}
360415

361416
if desired.ko.Spec.ScalingConfig != nil {
362-
input.SetScalingConfig(rm.newNodegroupScalingConfig(desired))
417+
input.SetScalingConfig(rm.newUpdateScalingConfigPayload(desired, latest))
363418
}
364419

365420
if desired.ko.Spec.UpdateConfig != nil {

0 commit comments

Comments
 (0)