Skip to content

Commit

Permalink
*: clean up polling function with contexts
Browse files Browse the repository at this point in the history
Signed-off-by: Gyuho Lee <leegyuho@amazon.com>
  • Loading branch information
gyuho committed Jun 28, 2020
1 parent 1e2474f commit 96ee2b8
Show file tree
Hide file tree
Showing 17 changed files with 139 additions and 50 deletions.
5 changes: 4 additions & 1 deletion eks/cluster-loader/remote/cluster-loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,21 @@ func (ts *tester) Create() (err error) {
if err = ts.createJob(); err != nil {
return err
}
timeout := 5*time.Minute + 5*time.Minute*time.Duration(ts.cfg.EKSConfig.AddOnClusterLoaderRemote.Runs)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
var pods []v1.Pod
_, pods, err = k8s_client.WaitForJobCompletes(
ctx,
ts.cfg.Logger,
ts.cfg.Stopc,
ts.cfg.K8SClient,
2*time.Minute,
10*time.Second,
3*time.Minute+time.Duration(ts.cfg.EKSConfig.AddOnClusterLoaderRemote.Runs)*2*time.Minute,
ts.cfg.EKSConfig.AddOnClusterLoaderRemote.Namespace,
clusterLoaderJobName,
1,
)
cancel()
if err != nil {
return err
}
Expand Down
11 changes: 8 additions & 3 deletions eks/cluster/wait/poll.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/aws/aws-k8s-tester/eksconfig"
"github.com/aws/aws-k8s-tester/pkg/ctxutil"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/eks"
Expand Down Expand Up @@ -60,6 +61,7 @@ func Poll(
zap.String("desired-status", desiredClusterStatus),
zap.String("initial-wait", initialWait.String()),
zap.String("poll-interval", pollInterval.String()),
zap.String("ctx-time-left", ctxutil.TimeLeftTillDeadline(ctx)),
)

now := time.Now()
Expand Down Expand Up @@ -128,6 +130,7 @@ func Poll(
zap.String("cluster-name", clusterName),
zap.String("status", currentStatus),
zap.String("started", humanize.RelTime(now, time.Now(), "ago", "from now")),
zap.String("ctx-time-left", ctxutil.TimeLeftTillDeadline(ctx)),
)
switch currentStatus {
case desiredClusterStatus:
Expand Down Expand Up @@ -208,7 +211,7 @@ func PollUpdate(
requestID string,
desiredUpdateStatus string,
initialWait time.Duration,
interval time.Duration,
pollInterval time.Duration,
opts ...OpOption) <-chan UpdateStatus {

ret := Op{}
Expand All @@ -219,7 +222,8 @@ func PollUpdate(
zap.String("request-id", requestID),
zap.String("desired-update-status", desiredUpdateStatus),
zap.String("initial-wait", initialWait.String()),
zap.String("interval", interval.String()),
zap.String("poll-interval", pollInterval.String()),
zap.String("ctx-time-left", ctxutil.TimeLeftTillDeadline(ctx)),
)

now := time.Now()
Expand Down Expand Up @@ -251,7 +255,7 @@ func PollUpdate(
// in case stack has already reached desired status
// wait from second interation
if waitDur == time.Duration(0) {
waitDur = interval
waitDur = pollInterval
}
}

Expand Down Expand Up @@ -286,6 +290,7 @@ func PollUpdate(
zap.String("status", currentStatus),
zap.String("update-type", updateType),
zap.String("started", humanize.RelTime(now, time.Now(), "ago", "from now")),
zap.String("ctx-time-left", ctxutil.TimeLeftTillDeadline(ctx)),
)
switch currentStatus {
case desiredUpdateStatus:
Expand Down
5 changes: 4 additions & 1 deletion eks/configmaps/remote/configmaps.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,21 @@ func (ts *tester) Create() (err error) {
if err = ts.createJob(); err != nil {
return err
}
timeout := 5*time.Minute + 5*time.Minute*time.Duration(ts.cfg.EKSConfig.AddOnConfigmapsRemote.Completes) + time.Minute*time.Duration(ts.cfg.EKSConfig.AddOnConfigmapsRemote.Objects/100)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
var pods []v1.Pod
_, pods, err = k8s_client.WaitForJobCompletes(
ctx,
ts.cfg.Logger,
ts.cfg.Stopc,
ts.cfg.K8SClient,
3*time.Minute,
10*time.Second,
3*time.Minute+time.Duration(ts.cfg.EKSConfig.AddOnConfigmapsRemote.Completes)*30*time.Second,
ts.cfg.EKSConfig.AddOnConfigmapsRemote.Namespace,
configmapsJobName,
ts.cfg.EKSConfig.AddOnConfigmapsRemote.Completes,
)
cancel()
if err != nil {
return err
}
Expand Down
10 changes: 6 additions & 4 deletions eks/cron-jobs/cron-jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,21 @@ func (ts *tester) Create() (err error) {
if err = ts.createCronJob(); err != nil {
return err
}

// take about 4-min for 10 cron jobs to trigger
_, pods, err := k8s_client.WaitForCronJobCompletes(
timeout := 10*time.Minute + 5*time.Minute*time.Duration(ts.cfg.EKSConfig.AddOnCronJobs.Completes)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
var pods []v1.Pod
_, pods, err = k8s_client.WaitForCronJobCompletes(
ctx,
ts.cfg.Logger,
ts.cfg.Stopc,
ts.cfg.K8SClient,
3*time.Minute,
5*time.Second,
10*time.Minute+10*time.Duration(ts.cfg.EKSConfig.AddOnCronJobs.Completes)*time.Second,
ts.cfg.EKSConfig.AddOnCronJobs.Namespace,
cronJobName,
int(ts.cfg.EKSConfig.AddOnCronJobs.Completes),
)
cancel()
if err != nil {
return err
}
Expand Down
5 changes: 4 additions & 1 deletion eks/csrs/remote/csrs.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,21 @@ func (ts *tester) Create() (err error) {
if err = ts.createJob(); err != nil {
return err
}
timeout := 5*time.Minute + 5*time.Minute*time.Duration(ts.cfg.EKSConfig.AddOnCSRsRemote.Completes) + time.Minute*time.Duration(ts.cfg.EKSConfig.AddOnCSRsRemote.Objects/100)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
var pods []v1.Pod
_, pods, err = k8s_client.WaitForJobCompletes(
ctx,
ts.cfg.Logger,
ts.cfg.Stopc,
ts.cfg.K8SClient,
3*time.Minute,
10*time.Second,
3*time.Minute+time.Duration(ts.cfg.EKSConfig.AddOnCSRsRemote.Completes)*30*time.Second,
ts.cfg.EKSConfig.AddOnCSRsRemote.Namespace,
csrsJobName,
ts.cfg.EKSConfig.AddOnCSRsRemote.Completes,
)
cancel()
if err != nil {
return err
}
Expand Down
3 changes: 3 additions & 0 deletions eks/fargate/wait/poll.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"time"

"github.com/aws/aws-k8s-tester/pkg/ctxutil"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/eks"
Expand Down Expand Up @@ -53,6 +54,7 @@ func Poll(
zap.String("desired-status", desiredStatus),
zap.String("initial-wait", initialWait.String()),
zap.String("poll-interval", pollInterval.String()),
zap.String("ctx-time-left", ctxutil.TimeLeftTillDeadline(ctx)),
)

now := time.Now()
Expand Down Expand Up @@ -124,6 +126,7 @@ func Poll(
zap.String("fargate-name", profileName),
zap.String("status", currentStatus),
zap.String("started", humanize.RelTime(now, time.Now(), "ago", "from now")),
zap.String("ctx-time-left", ctxutil.TimeLeftTillDeadline(ctx)),
)
switch currentStatus {
case desiredStatus:
Expand Down
9 changes: 6 additions & 3 deletions eks/jobs-echo/jobs-echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,21 @@ func (ts *tester) Create() (err error) {
if err = ts.createJob(); err != nil {
return err
}

_, pods, err := k8s_client.WaitForJobCompletes(
timeout := 5*time.Minute + 5*time.Minute*time.Duration(ts.cfg.EKSConfig.AddOnJobsEcho.Completes)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
var pods []v1.Pod
_, pods, err = k8s_client.WaitForJobCompletes(
ctx,
ts.cfg.Logger,
ts.cfg.Stopc,
ts.cfg.K8SClient,
2*time.Minute,
5*time.Second,
3*time.Minute+10*time.Duration(ts.cfg.EKSConfig.AddOnJobsEcho.Completes)*time.Second,
ts.cfg.EKSConfig.AddOnJobsEcho.Namespace,
jobName,
int(ts.cfg.EKSConfig.AddOnJobsEcho.Completes),
)
cancel()
if err != nil {
return err
}
Expand Down
9 changes: 6 additions & 3 deletions eks/jobs-pi/jobs-pi.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,21 @@ func (ts *tester) Create() (err error) {
if err = ts.createJob(); err != nil {
return err
}

_, pods, err := k8s_client.WaitForJobCompletes(
timeout := 5*time.Minute + 5*time.Minute*time.Duration(ts.cfg.EKSConfig.AddOnJobsPi.Completes)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
var pods []v1.Pod
_, pods, err = k8s_client.WaitForJobCompletes(
ctx,
ts.cfg.Logger,
ts.cfg.Stopc,
ts.cfg.K8SClient,
2*time.Minute,
5*time.Second,
3*time.Minute+10*time.Duration(ts.cfg.EKSConfig.AddOnJobsPi.Completes)*time.Second,
ts.cfg.EKSConfig.AddOnJobsPi.Namespace,
jobName,
int(ts.cfg.EKSConfig.AddOnJobsPi.Completes),
)
cancel()
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion eks/mng/scale/scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (ts *tester) scaleMNG(mngName string, update eksconfig.MNGScaleUpdate) (err
return fmt.Errorf("MNGs[%q] Update.Id empty", mngName)
}
initialWait := 3 * time.Minute
totalWait := 5*time.Minute + 20*time.Second*time.Duration(cur.ASGDesiredCapacity)
totalWait := 10*time.Minute + 3*time.Minute*time.Duration(cur.ASGDesiredCapacity)
ts.cfg.Logger.Info("sent MNG scaler request; polling",
zap.String("cluster-name", ts.cfg.EKSConfig.Name),
zap.String("mng-name", mngName),
Expand Down
2 changes: 1 addition & 1 deletion eks/mng/version-upgrade/version-upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (ts *tester) Upgrade(mngName string) (err error) {

// takes TODO
initialWait := 3 * time.Minute
totalWait := 30*time.Minute + 3*time.Minute*time.Duration(cur.ASGDesiredCapacity)
totalWait := time.Hour + 3*time.Minute*time.Duration(cur.ASGDesiredCapacity)

ts.cfg.Logger.Info("sent MNG upgrade request; polling",
zap.String("cluster-name", ts.cfg.EKSConfig.Name),
Expand Down
11 changes: 8 additions & 3 deletions eks/mng/wait/poll.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"
"time"

"github.com/aws/aws-k8s-tester/pkg/ctxutil"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/eks"
Expand Down Expand Up @@ -58,6 +59,7 @@ func Poll(
zap.String("desired-status", desiredNodeGroupStatus),
zap.String("initial-wait", initialWait.String()),
zap.String("poll-interval", pollInterval.String()),
zap.String("ctx-time-left", ctxutil.TimeLeftTillDeadline(ctx)),
)

now := time.Now()
Expand Down Expand Up @@ -128,6 +130,7 @@ func Poll(
zap.String("mng-name", mngName),
zap.String("status", currentStatus),
zap.String("started", humanize.RelTime(now, time.Now(), "ago", "from now")),
zap.String("ctx-time-left", ctxutil.TimeLeftTillDeadline(ctx)),
)
switch currentStatus {
case desiredNodeGroupStatus:
Expand Down Expand Up @@ -229,7 +232,7 @@ func PollUpdate(
requestID string,
desiredUpdateStatus string,
initialWait time.Duration,
interval time.Duration,
pollInterval time.Duration,
opts ...OpOption) <-chan UpdateStatus {

ret := Op{}
Expand All @@ -241,7 +244,8 @@ func PollUpdate(
zap.String("request-id", requestID),
zap.String("desired-update-status", desiredUpdateStatus),
zap.String("initial-wait", initialWait.String()),
zap.String("interval", interval.String()),
zap.String("poll-interval", pollInterval.String()),
zap.String("ctx-time-left", ctxutil.TimeLeftTillDeadline(ctx)),
)

now := time.Now()
Expand Down Expand Up @@ -273,7 +277,7 @@ func PollUpdate(
// in case stack has already reached desired status
// wait from second interation
if waitDur == time.Duration(0) {
waitDur = interval
waitDur = pollInterval
}
}

Expand Down Expand Up @@ -310,6 +314,7 @@ func PollUpdate(
zap.String("status", currentStatus),
zap.String("update-type", updateType),
zap.String("started", humanize.RelTime(now, time.Now(), "ago", "from now")),
zap.String("ctx-time-left", ctxutil.TimeLeftTillDeadline(ctx)),
)
switch currentStatus {
case desiredUpdateStatus:
Expand Down
5 changes: 4 additions & 1 deletion eks/secrets/remote/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,21 @@ func (ts *tester) Create() (err error) {
if err = ts.createJob(); err != nil {
return err
}
timeout := 5*time.Minute + 5*time.Minute*time.Duration(ts.cfg.EKSConfig.AddOnSecretsRemote.Completes) + time.Minute*time.Duration(ts.cfg.EKSConfig.AddOnSecretsRemote.Objects/100)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
var pods []v1.Pod
_, pods, err = k8s_client.WaitForJobCompletes(
ctx,
ts.cfg.Logger,
ts.cfg.Stopc,
ts.cfg.K8SClient,
3*time.Minute,
10*time.Second,
3*time.Minute+time.Duration(ts.cfg.EKSConfig.AddOnSecretsRemote.Completes)*30*time.Second,
ts.cfg.EKSConfig.AddOnSecretsRemote.Namespace,
secretsJobName,
ts.cfg.EKSConfig.AddOnSecretsRemote.Completes,
)
cancel()
if err != nil {
return err
}
Expand Down
9 changes: 6 additions & 3 deletions eks/stresser/remote/stresser.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,16 @@ func (ts *tester) Create() (err error) {
if err = ts.createJob(); err != nil {
return err
}
timeout := 15*time.Minute + ts.cfg.EKSConfig.AddOnStresserRemote.Duration
ctx, cancel := context.WithTimeout(context.Background(), timeout)
var pods []v1.Pod
_, pods, err = k8s_client.WaitForJobCompletes(
ctx,
ts.cfg.Logger,
ts.cfg.Stopc,
ts.cfg.K8SClient,
3*time.Minute,
10*time.Second,
ts.cfg.EKSConfig.AddOnStresserRemote.Duration+10*time.Minute,
time.Minute+ts.cfg.EKSConfig.AddOnStresserRemote.Duration/2,
15*time.Second,
ts.cfg.EKSConfig.AddOnStresserRemote.Namespace,
stresserJobName,
ts.cfg.EKSConfig.AddOnStresserRemote.Completes,
Expand All @@ -161,6 +163,7 @@ func (ts *tester) Create() (err error) {
fmt.Fprintf(ts.cfg.LogWriter, "\n\"%s\":\n%s\n", cmdLogs, out)
}),
)
cancel()
if err != nil {
return err
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/aws/cfn/cfn.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"time"

"github.com/aws/aws-k8s-tester/pkg/ctxutil"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/aws/aws-sdk-go/service/cloudformation/cloudformationiface"
Expand Down Expand Up @@ -40,6 +41,7 @@ func Poll(
zap.String("want", desiredStackStatus),
zap.String("initial-wait", initialWait.String()),
zap.String("poll-interval", pollInterval.String()),
zap.String("ctx-time-left", ctxutil.TimeLeftTillDeadline(ctx)),
)
ch := make(chan StackStatus, 10)
go func() {
Expand Down Expand Up @@ -110,12 +112,13 @@ func Poll(
prevStatusReason = currentStatusReason
}

lg.Info("polling",
lg.Info("poll",
zap.String("name", aws.StringValue(stack.StackName)),
zap.String("desired", desiredStackStatus),
zap.String("current", currentStatus),
zap.String("current-reason", currentStatusReason),
zap.String("started", humanize.RelTime(now, time.Now(), "ago", "from now")),
zap.String("ctx-time-left", ctxutil.TimeLeftTillDeadline(ctx)),
)

if desiredStackStatus != cloudformation.ResourceStatusDeleteComplete &&
Expand Down
Loading

0 comments on commit 96ee2b8

Please sign in to comment.