Skip to content

Commit 5fc7313

Browse files
mortenljtronghn
andcommittedSep 20, 2024
Use go-retry for UpdateTargetInstanceAfterPromotion
Co-authored-by: Trong Nguyen <trong.huu.nguyen@nav.no>
1 parent 70d3908 commit 5fc7313

File tree

2 files changed

+25
-24
lines changed

2 files changed

+25
-24
lines changed
 

‎internal/pkg/application/application.go

-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ import (
1818
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1919
)
2020

21-
const UpdateRetries = 3
22-
2321
func ScaleApplication(ctx context.Context, cfg *config.Config, mgr *common_main.Manager, replicas int32) error {
2422
mgr.Logger.Info("scaling application", "name", cfg.ApplicationName, "replicas", replicas)
2523
scaleApplyConfiguration := autoscaling_v1.Scale{

‎internal/pkg/instance/instance.go

+25-22
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828

2929
const (
3030
dummyAppImage = "europe-north1-docker.pkg.dev/nais-io/nais/images/kafka-debug:latest"
31-
updateRetries = 3
3231
migrationAuthNetworkPrefix = "migrator:"
3332
)
3433

@@ -291,8 +290,32 @@ func PrepareTargetInstance(ctx context.Context, target *resolved.Instance, mgr *
291290
}
292291

293292
func UpdateTargetInstanceAfterPromotion(ctx context.Context, target *resolved.Instance, mgr *common_main.Manager) error {
294-
err := updateTargetInstanceAfterPromotionWithRetries(ctx, target, mgr, updateRetries)
293+
mgr.Logger.Info("updating target instance after promotion")
294+
295+
b := retry.NewConstant(1 * time.Second)
296+
b = retry.WithMaxDuration(5*time.Minute, b)
297+
298+
err := retry.Do(ctx, b, func(ctx context.Context) error {
299+
targetSqlInstance, err := mgr.SqlInstanceClient.Get(ctx, target.Name)
300+
if err != nil {
301+
return fmt.Errorf("failed to get target instance: %w", err)
302+
}
303+
304+
targetSqlInstance.Spec.InstanceType = ptr.To("CLOUD_SQL_INSTANCE")
305+
targetSqlInstance.Spec.MasterInstanceRef = nil
306+
307+
_, err = mgr.SqlInstanceClient.Update(ctx, targetSqlInstance)
308+
if err != nil {
309+
if k8s_errors.IsConflict(err) {
310+
mgr.Logger.Warn("retrying update of target instance", "error", err)
311+
return retry.RetryableError(err)
312+
}
313+
return err
314+
}
315+
return nil
316+
})
295317
if err != nil {
318+
mgr.Logger.Error("failed to update target instance after promotion", "error", err)
296319
return err
297320
}
298321

@@ -315,26 +338,6 @@ func UpdateTargetInstanceAfterPromotion(ctx context.Context, target *resolved.In
315338
return nil
316339
}
317340

318-
func updateTargetInstanceAfterPromotionWithRetries(ctx context.Context, target *resolved.Instance, mgr *common_main.Manager, retries int) error {
319-
targetSqlInstance, err := mgr.SqlInstanceClient.Get(ctx, target.Name)
320-
if err != nil {
321-
return fmt.Errorf("failed to get target instance: %w", err)
322-
}
323-
324-
targetSqlInstance.Spec.InstanceType = ptr.To("CLOUD_SQL_INSTANCE")
325-
targetSqlInstance.Spec.MasterInstanceRef = nil
326-
327-
_, err = mgr.SqlInstanceClient.Update(ctx, targetSqlInstance)
328-
if err != nil {
329-
if k8s_errors.IsConflict(err) && retries > 0 {
330-
mgr.Logger.Info("retrying update of target instance", "remaining_retries", retries)
331-
return updateTargetInstanceAfterPromotionWithRetries(ctx, target, mgr, retries-1)
332-
}
333-
return err
334-
}
335-
return nil
336-
}
337-
338341
func DeleteInstance(ctx context.Context, instanceName string, gcpProject *resolved.GcpProject, mgr *common_main.Manager) error {
339342
instancesService := mgr.SqlAdminService.Instances
340343

0 commit comments

Comments
 (0)