CloudMigrations: Send local events to gms during the migration process (#90637)
* add gms client function * add timeout config for endpoint * report events to gms * fix lint error * clean up report calls and make sure reports all have local ids * extra validation * improve error logging and fix url
This commit is contained in:
@@ -12,6 +12,7 @@ type Client interface {
|
||||
StartSnapshot(context.Context, cloudmigration.CloudMigrationSession) (*cloudmigration.StartSnapshotResponse, error)
|
||||
GetSnapshotStatus(context.Context, cloudmigration.CloudMigrationSession, cloudmigration.CloudMigrationSnapshot, int) (*cloudmigration.GetSnapshotStatusResponse, error)
|
||||
CreatePresignedUploadUrl(context.Context, cloudmigration.CloudMigrationSession, cloudmigration.CloudMigrationSnapshot) (string, error)
|
||||
ReportEvent(context.Context, cloudmigration.CloudMigrationSession, EventRequestDTO)
|
||||
}
|
||||
|
||||
const logPrefix = "cloudmigration.gmsclient"
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package gmsclient
|
||||
|
||||
import "time"
|
||||
|
||||
type MigrateDataType string
|
||||
|
||||
const (
|
||||
@@ -48,3 +50,21 @@ type MigrateDataResponseItemDTO struct {
|
||||
type CreateSnapshotUploadUrlResponseDTO struct {
|
||||
UploadUrl string `json:"uploadUrl"`
|
||||
}
|
||||
|
||||
type EventRequestDTO struct {
|
||||
LocalID string `json:"migrationClientId"`
|
||||
Event LocalEventType `json:"event"`
|
||||
Error string `json:"error"`
|
||||
DurationIfFinished time.Duration `json:"duration"`
|
||||
}
|
||||
|
||||
type LocalEventType string
|
||||
|
||||
const (
|
||||
EventConnect LocalEventType = "connect"
|
||||
EventDisconnect LocalEventType = "disconnect"
|
||||
EventStartBuildingSnapshot LocalEventType = "start_building_snapshot"
|
||||
EventDoneBuildingSnapshot LocalEventType = "done_building_snapshot"
|
||||
EventStartUploadingSnapshot LocalEventType = "start_uploading_snapshot"
|
||||
EventDoneUploadingSnapshot LocalEventType = "done_uploading_snapshot"
|
||||
)
|
||||
|
||||
@@ -249,6 +249,52 @@ func (c *gmsClientImpl) CreatePresignedUploadUrl(ctx context.Context, session cl
|
||||
return result.UploadUrl, nil
|
||||
}
|
||||
|
||||
func (c *gmsClientImpl) ReportEvent(ctx context.Context, session cloudmigration.CloudMigrationSession, event EventRequestDTO) {
|
||||
if event.LocalID == "" || event.Event == "" {
|
||||
return
|
||||
}
|
||||
|
||||
path := fmt.Sprintf("%s/api/v1/snapshots/events", c.buildBasePath(session.ClusterSlug))
|
||||
|
||||
var buf bytes.Buffer
|
||||
if err := json.NewEncoder(&buf).Encode(event); err != nil {
|
||||
c.log.Error("encoding event", "err", err.Error())
|
||||
return
|
||||
}
|
||||
// Send the request to gms with the associated auth token
|
||||
req, err := http.NewRequest(http.MethodPost, path, &buf)
|
||||
if err != nil {
|
||||
c.log.Error("error creating http request to report event", "err", err.Error())
|
||||
return
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %d:%s", session.StackID, session.AuthToken))
|
||||
|
||||
client := &http.Client{
|
||||
Timeout: c.cfg.CloudMigration.GMSReportEventTimeout,
|
||||
}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
c.log.Error("error sending http request for report event", "err", err.Error())
|
||||
return
|
||||
} else if resp.StatusCode >= 400 {
|
||||
c.log.Error("received error response for report event", "type", event.Event, "statusCode", resp.StatusCode)
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
c.log.Error("reading request body", "err", err.Error())
|
||||
return
|
||||
}
|
||||
c.log.Error("http request error", "body", string(body))
|
||||
return
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if err := resp.Body.Close(); err != nil {
|
||||
c.log.Error("closing request body", "err", err.Error())
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (c *gmsClientImpl) buildBasePath(clusterSlug string) string {
|
||||
domain := c.cfg.CloudMigration.GMSDomain
|
||||
if strings.HasPrefix(domain, "http://localhost") {
|
||||
|
||||
@@ -95,3 +95,6 @@ func (c *memoryClientImpl) GetSnapshotStatus(ctx context.Context, session cloudm
|
||||
func (c *memoryClientImpl) CreatePresignedUploadUrl(ctx context.Context, sess cloudmigration.CloudMigrationSession, snapshot cloudmigration.CloudMigrationSnapshot) (string, error) {
|
||||
return "http://localhost:3000", nil
|
||||
}
|
||||
|
||||
func (c *memoryClientImpl) ReportEvent(context.Context, cloudmigration.CloudMigrationSession, EventRequestDTO) {
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user