Skip to content

Commit 3ae8a18

Browse files
committed
fix lint errors
Signed-off-by: Pankaj Walke <[email protected]>
1 parent b6d7cf6 commit 3ae8a18

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

pkg/cloud/converters/eks.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package converters
2020
import (
2121
"errors"
2222
"fmt"
23+
"math"
2324

2425
"github.com/aws/aws-sdk-go-v2/aws"
2526
ekstypes "github.com/aws/aws-sdk-go-v2/service/eks/types"
@@ -182,20 +183,29 @@ func CapacityTypeToSDK(capacityType expinfrav1.ManagedMachinePoolCapacityType) (
182183
}
183184

184185
// NodegroupUpdateconfigToSDK is used to convert a CAPA UpdateConfig to AWS SDK NodegroupUpdateConfig.
185-
func NodegroupUpdateconfigToSDK(updateConfig *expinfrav1.UpdateConfig) *ekstypes.NodegroupUpdateConfig {
186+
func NodegroupUpdateconfigToSDK(updateConfig *expinfrav1.UpdateConfig) (*ekstypes.NodegroupUpdateConfig, error) {
186187
if updateConfig == nil {
187-
return nil
188+
return nil, nil
189+
}
190+
191+
maxUnavailable, err := toSafeInt32(*updateConfig.MaxUnavailable)
192+
if err != nil {
193+
return nil, err
194+
}
195+
maxUnavailablePercant, err := toSafeInt32(*updateConfig.MaxUnavailablePercentage)
196+
if err != nil {
197+
return nil, err
188198
}
189199

190200
converted := &ekstypes.NodegroupUpdateConfig{}
191201
if updateConfig.MaxUnavailable != nil {
192-
converted.MaxUnavailable = aws.Int32(int32(*updateConfig.MaxUnavailable))
202+
converted.MaxUnavailable = aws.Int32(maxUnavailable)
193203
}
194204
if updateConfig.MaxUnavailablePercentage != nil {
195-
converted.MaxUnavailablePercentage = aws.Int32(int32(*updateConfig.MaxUnavailablePercentage))
205+
converted.MaxUnavailablePercentage = aws.Int32(maxUnavailablePercant)
196206
}
197207

198-
return converted
208+
return converted, nil
199209
}
200210

201211
// NodegroupUpdateconfigFromSDK is used to convert a AWS SDK NodegroupUpdateConfig to a CAPA UpdateConfig.
@@ -246,3 +256,10 @@ func AddonConflictResolutionFromSDK(conflict ekstypes.ResolveConflicts) *string
246256
}
247257
return aws.String(string(ekscontrolplanev1.AddonResolutionOverwrite))
248258
}
259+
260+
func toSafeInt32(i int) (int32, error) {
261+
if i > math.MaxInt32 || i < math.MinInt32 {
262+
return 0, fmt.Errorf("value %d out of range for int32", i)
263+
}
264+
return int32(i), nil
265+
}

pkg/cloud/services/eks/nodegroup.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func (s *NodegroupService) scalingConfig() *ekstypes.NodegroupScalingConfig {
112112
return &cfg
113113
}
114114

115-
func (s *NodegroupService) updateConfig() *ekstypes.NodegroupUpdateConfig {
115+
func (s *NodegroupService) updateConfig() (*ekstypes.NodegroupUpdateConfig, error) {
116116
updateConfig := s.scope.ManagedMachinePool.Spec.UpdateConfig
117117

118118
return converters.NodegroupUpdateconfigToSDK(updateConfig)
@@ -203,7 +203,10 @@ func (s *NodegroupService) createNodegroup(ctx context.Context) (*ekstypes.Nodeg
203203
if err != nil {
204204
return nil, fmt.Errorf("failed getting nodegroup subnets: %w", err)
205205
}
206-
206+
updatedConfig, err := s.updateConfig()
207+
if err != nil {
208+
return nil, fmt.Errorf("failed creating nodegroup, invalid update config: %w", err)
209+
}
207210
input := &eks.CreateNodegroupInput{
208211
ScalingConfig: s.scalingConfig(),
209212
ClusterName: aws.String(eksClusterName),
@@ -213,7 +216,7 @@ func (s *NodegroupService) createNodegroup(ctx context.Context) (*ekstypes.Nodeg
213216
Labels: managedPool.Labels,
214217
Tags: tags,
215218
RemoteAccess: remoteAccess,
216-
UpdateConfig: s.updateConfig(),
219+
UpdateConfig: updatedConfig,
217220
}
218221
if managedPool.AMIType != nil && (managedPool.AWSLaunchTemplate == nil || managedPool.AWSLaunchTemplate.AMI.ID == nil) {
219222
input.AmiType = converters.AMITypeToSDK(*managedPool.AMIType)
@@ -466,9 +469,14 @@ func (s *NodegroupService) reconcileNodegroupConfig(ctx context.Context, ng *eks
466469
needsUpdate = true
467470
}
468471
currentUpdateConfig := converters.NodegroupUpdateconfigFromSDK(ng.UpdateConfig)
472+
updatedConfig, err := s.updateConfig()
473+
if err != nil {
474+
return errors.Wrap(err, "invalid update config")
475+
}
476+
469477
if !cmp.Equal(managedPool.UpdateConfig, currentUpdateConfig) {
470478
s.Debug("Nodegroup update configuration differs from spec, updating the nodegroup update config", "nodegroup", ng.NodegroupName)
471-
input.UpdateConfig = s.updateConfig()
479+
input.UpdateConfig = updatedConfig
472480
needsUpdate = true
473481
}
474482
if !needsUpdate {

0 commit comments

Comments
 (0)