This repository has been archived by the owner on Jan 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
rollout: Publish rollout events to PubSub #109
Open
gvso
wants to merge
4
commits into
GoogleCloudPlatform:main
Choose a base branch
from
gvso:pubsub-implementation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 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
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 |
---|---|---|
|
@@ -9,6 +9,7 @@ import ( | |
"github.com/GoogleCloudPlatform/cloud-run-release-manager/internal/config" | ||
"github.com/GoogleCloudPlatform/cloud-run-release-manager/internal/health" | ||
"github.com/GoogleCloudPlatform/cloud-run-release-manager/internal/metrics" | ||
"github.com/GoogleCloudPlatform/cloud-run-release-manager/internal/notification/pubsub" | ||
runapi "github.com/GoogleCloudPlatform/cloud-run-release-manager/internal/run" | ||
"github.com/GoogleCloudPlatform/cloud-run-release-manager/internal/util" | ||
"github.com/jonboulle/clockwork" | ||
|
@@ -45,6 +46,7 @@ type Rollout struct { | |
runClient runapi.Client | ||
log *logrus.Entry | ||
time clockwork.Clock | ||
pubsubClient pubsub.Client | ||
|
||
// Used to determine if candidate should become stable during update. | ||
promoteToStable bool | ||
|
@@ -99,6 +101,12 @@ func (r *Rollout) WithClock(clock clockwork.Clock) *Rollout { | |
return r | ||
} | ||
|
||
// WithPubSub updates the Pub/Sub client in the rollout instance. | ||
func (r *Rollout) WithPubSub(psClient pubsub.Client) *Rollout { | ||
r.pubsubClient = psClient | ||
return r | ||
} | ||
|
||
// Rollout handles the gradual rollout. | ||
func (r *Rollout) Rollout() (bool, error) { | ||
r.log = r.log.WithFields(logrus.Fields{ | ||
|
@@ -145,7 +153,7 @@ func (r *Rollout) UpdateService(svc *run.Service) (*run.Service, bool, error) { | |
svc = r.updateAnnotations(svc, stable, candidate) | ||
r.setHealthReportAnnotation(svc, "new candidate, no health report available yet") | ||
|
||
err := r.replaceService(svc) | ||
err := r.replaceServiceAndPublish(svc, true, health.Healthy) | ||
return svc, true, errors.Wrap(err, "failed to replace service") | ||
} | ||
|
||
|
@@ -169,13 +177,21 @@ func (r *Rollout) UpdateService(svc *run.Service) (*run.Service, bool, error) { | |
report := health.StringReport(r.strategy.HealthCriteria, diagnosis, trafficChanged) | ||
r.setHealthReportAnnotation(svc, report) | ||
|
||
err = r.replaceService(svc) | ||
return svc, trafficChanged, errors.Wrap(err, "failed to replace service") | ||
err = r.replaceServiceAndPublish(svc, trafficChanged, diagnosis.OverallResult) | ||
return svc, true, errors.Wrap(err, "failed to replace service") | ||
} | ||
|
||
// replaceService updates the service object in Cloud Run. | ||
func (r *Rollout) replaceService(svc *run.Service) error { | ||
_, err := r.runClient.ReplaceService(r.project, r.serviceName, svc) | ||
// replaceServiceAndPublish updates the service object in Cloud Run. Then, it | ||
// publishes to Google Cloud Pub/Sub. | ||
func (r *Rollout) replaceServiceAndPublish(svc *run.Service, trafficChanged bool, diagnosis health.DiagnosisResult) error { | ||
svc, err := r.runClient.ReplaceService(r.project, r.serviceName, svc) | ||
|
||
if trafficChanged { | ||
pubErr := r.publish(svc, diagnosis) | ||
if pubErr != nil { | ||
r.log.Warnf("failed to publish rollout/rollback message: %v", pubErr) | ||
} | ||
} | ||
return errors.Wrapf(err, "could not update service %q", r.serviceName) | ||
} | ||
|
||
|
@@ -232,3 +248,23 @@ func (r *Rollout) diagnoseCandidate(candidate string, healthCriteria []config.He | |
d, err = health.Diagnose(ctx, healthCriteria, metricsValues) | ||
return d, errors.Wrap(err, "failed to diagnose candidate's health") | ||
} | ||
|
||
func (r *Rollout) publish(svc *run.Service, diagnosis health.DiagnosisResult) error { | ||
if r.pubsubClient == nil { | ||
return nil | ||
} | ||
|
||
event, err := pubsub.NewRolloutEvent(svc, diagnosis, r.promoteToStable) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to create message") | ||
} | ||
ctx := util.ContextWithLogger(r.ctx, r.log) | ||
err = r.pubsubClient.Publish(ctx, event) | ||
if err != nil { | ||
return errors.Wrap(err, "error when publishing message") | ||
} | ||
|
||
// Wait for all messages to be sent (or to fail). | ||
r.pubsubClient.Stop() | ||
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. Since we're publishing one event per go routine (in each rollout process), we might just wait for the event to be published using 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. yeah that's a decent idea. I think that could be done with
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. lets move Get into Publish |
||
return nil | ||
} |
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.
better name like publishEvent