-
Notifications
You must be signed in to change notification settings - Fork 90
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
chore(ec): send upgrade events instead of the operator #5017
Merged
emosbaugh
merged 7 commits into
main
from
emosbaugh/sc-116124/upgrade-events-should-be-sent-by-kots-instead
Nov 27, 2024
+232
−8
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7bef478
chore(ec): send upgrade events instead of the operator
emosbaugh d37d3ed
f
emosbaugh 58829b0
f
emosbaugh a36dba6
f
emosbaugh 65867a0
increase preflight timeout
emosbaugh ce04d56
feedback
emosbaugh 6547471
explain
emosbaugh 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package embeddedcluster | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
embeddedclusterv1beta1 "github.com/replicatedhq/embedded-cluster/kinds/apis/v1beta1" | ||
"github.com/replicatedhq/kots/pkg/logger" | ||
) | ||
|
||
// UpgradeStartedEvent is send back home when the upgrade starts. | ||
type UpgradeStartedEvent struct { | ||
ClusterID string `json:"clusterID"` | ||
TargetVersion string `json:"targetVersion"` | ||
InitialVersion string `json:"initialVersion"` | ||
AppVersion string `json:"appVersion"` | ||
} | ||
|
||
// UpgradeFailedEvent is send back home when the upgrade fails. | ||
type UpgradeFailedEvent struct { | ||
ClusterID string `json:"clusterID"` | ||
TargetVersion string `json:"targetVersion"` | ||
InitialVersion string `json:"initialVersion"` | ||
Reason string `json:"reason"` | ||
} | ||
|
||
// UpgradeSucceededEvent event is send back home when the upgrade succeeds. | ||
type UpgradeSucceededEvent struct { | ||
ClusterID string `json:"clusterID"` | ||
TargetVersion string `json:"targetVersion"` | ||
InitialVersion string `json:"initialVersion"` | ||
} | ||
|
||
// NotifyUpgradeStarted notifies the metrics server that an upgrade has started. | ||
func NotifyUpgradeStarted(ctx context.Context, baseURL string, ins, prev *embeddedclusterv1beta1.Installation, versionLabel string) error { | ||
if ins.Spec.AirGap { | ||
return nil | ||
} | ||
return sendEvent(ctx, "UpgradeStarted", baseURL, UpgradeStartedEvent{ | ||
ClusterID: ins.Spec.ClusterID, | ||
TargetVersion: ins.Spec.Config.Version, | ||
InitialVersion: prev.Spec.Config.Version, | ||
AppVersion: versionLabel, | ||
}) | ||
} | ||
|
||
// NotifyUpgradeFailed notifies the metrics server that an upgrade has failed. | ||
func NotifyUpgradeFailed(ctx context.Context, baseURL string, ins, prev *embeddedclusterv1beta1.Installation, reason string) error { | ||
if ins.Spec.AirGap { | ||
return nil | ||
} | ||
return sendEvent(ctx, "UpgradeFailed", baseURL, UpgradeFailedEvent{ | ||
ClusterID: ins.Spec.ClusterID, | ||
TargetVersion: ins.Spec.Config.Version, | ||
InitialVersion: prev.Spec.Config.Version, | ||
Reason: reason, | ||
}) | ||
} | ||
|
||
// NotifyUpgradeSucceeded notifies the metrics server that an upgrade has succeeded. | ||
func NotifyUpgradeSucceeded(ctx context.Context, baseURL string, ins, prev *embeddedclusterv1beta1.Installation) error { | ||
if ins.Spec.AirGap { | ||
return nil | ||
} | ||
return sendEvent(ctx, "UpgradeSucceeded", baseURL, UpgradeSucceededEvent{ | ||
ClusterID: ins.Spec.ClusterID, | ||
TargetVersion: ins.Spec.Config.Version, | ||
InitialVersion: prev.Spec.Config.Version, | ||
}) | ||
} | ||
|
||
// sendEvent sends the received event to the metrics server through a post request. | ||
func sendEvent(ctx context.Context, evname, baseURL string, ev interface{}) error { | ||
url := fmt.Sprintf("%s/embedded_cluster_metrics/%s", baseURL, evname) | ||
|
||
logger.Infof("Sending event %s to %s", evname, url) | ||
|
||
body := map[string]interface{}{"event": ev} | ||
buf := bytes.NewBuffer(nil) | ||
if err := json.NewEncoder(buf).Encode(body); err != nil { | ||
return err | ||
} | ||
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, buf) | ||
if err != nil { | ||
return err | ||
} | ||
req.Header.Set("Content-Type", "application/json") | ||
|
||
client := &http.Client{ | ||
Timeout: 5 * time.Second, | ||
} | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
return fmt.Errorf("failed to send event: %w", err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return fmt.Errorf("failed to send event: %s", resp.Status) | ||
} | ||
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
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
Oops, something went wrong.
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.
so if the upgrade fails prior to this point, we wouldn't report that an upgrade started, but we would report that it failed?
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.
i guess that's the caveat that you captured in the comment that the new installation object is required to report that the upgrade started. so nevermind.
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.
updated. im not sure if its better or worse this way