-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add dynamic wait for vm hibernate #98
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,17 +2,19 @@ package drivers | |
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"runtime/debug" | ||
"sort" | ||
"sync" | ||
"time" | ||
|
||
"github.com/cenkalti/backoff/v4" | ||
"github.com/drone-runners/drone-runner-aws/internal/certs" | ||
"github.com/drone-runners/drone-runner-aws/store" | ||
"github.com/drone-runners/drone-runner-aws/types" | ||
"github.com/drone/runner-go/logger" | ||
lehttp "github.com/harness/lite-engine/cli/client" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. import ordering There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess ordering is correct. No changes are shown on go fmt |
||
"github.com/pkg/errors" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
@@ -438,7 +440,7 @@ func (m *Manager) buildPool(ctx context.Context, pool *poolEntry) error { | |
Infoln("build pool: created new instance") | ||
|
||
go func() { | ||
herr := m.hibernate(ctx, pool.Name, inst.ID) | ||
herr := m.hibernate(context.Background(), pool.Name, inst.ID) | ||
if herr != nil { | ||
logr.WithError(herr).Errorln("failed to hibernate the vm") | ||
} | ||
|
@@ -540,8 +542,11 @@ func (m *Manager) hibernate(ctx context.Context, poolName, instanceID string) er | |
return nil | ||
} | ||
|
||
// TODO: Use health check to determine wait time before hibernating the instance | ||
time.Sleep(600 * time.Second) // nolint:gomnd | ||
if !pool.Driver.CanHibernate() { | ||
return nil | ||
} | ||
|
||
m.waitForInstanceConnectivity(ctx, instanceID) | ||
|
||
pool.Lock() | ||
defer pool.Unlock() | ||
|
@@ -577,3 +582,53 @@ func (m *Manager) forEach(ctx context.Context, f func(ctx context.Context, pool | |
|
||
return nil | ||
} | ||
|
||
func (m *Manager) waitForInstanceConnectivity(ctx context.Context, instanceID string) { | ||
bf := backoff.NewExponentialBackOff() | ||
for { | ||
duration := bf.NextBackOff() | ||
if duration == bf.Stop { | ||
return | ||
} | ||
|
||
select { | ||
case <-ctx.Done(): | ||
logrus.WithField("instanceID", instanceID).Warnln("hibernate: connectivity check deadline exceeded") | ||
return | ||
case <-time.After(duration): | ||
if err := m.checkInstanceConnectivity(ctx, instanceID); err == nil { | ||
return | ||
} else { | ||
logrus.WithError(err).WithField("instanceID", instanceID).Traceln("hibernate: instance connectivity check failed") | ||
} | ||
} | ||
} | ||
} | ||
|
||
func (m *Manager) checkInstanceConnectivity(ctx context.Context, instanceID string) error { | ||
instance, err := m.Find(ctx, instanceID) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to find the instance in db") | ||
} | ||
|
||
if instance.Address == "" { | ||
return errors.New("instance has not received IP address") | ||
} | ||
|
||
endpoint := fmt.Sprintf("https://%s:9079/", instance.Address) | ||
client, err := lehttp.NewHTTPClient(endpoint, m.runnerName, string(instance.CACert), string(instance.TLSCert), string(instance.TLSKey)) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to create client") | ||
} | ||
|
||
response, err := client.Health(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if !response.OK { | ||
return errors.New("health check call failed") | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be below drone imports in a separate section. See how other files do it in the repo
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
because it's 3rd party
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ignore you've already done it