-
Notifications
You must be signed in to change notification settings - Fork 88
/
preflight.go
97 lines (80 loc) · 2.96 KB
/
preflight.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package reporting
import (
"bytes"
"fmt"
"net/http"
"net/url"
"time"
"github.com/pkg/errors"
kotsv1beta1 "github.com/replicatedhq/kots/kotskinds/apis/kots/v1beta1"
downstream "github.com/replicatedhq/kots/pkg/kotsadmdownstream"
"github.com/replicatedhq/kots/pkg/logger"
"github.com/replicatedhq/kots/pkg/store"
)
func SendPreflightsReportToReplicatedApp(license *kotsv1beta1.License, appID string, clusterID string, sequence int, skipPreflights bool, installStatus string) error {
endpoint := license.Spec.Endpoint
if !canReport(endpoint) {
return nil
}
urlValues := url.Values{}
sequenceToStr := fmt.Sprintf("%d", sequence)
skipPreflightsToStr := fmt.Sprintf("%t", skipPreflights)
urlValues.Set("sequence", sequenceToStr)
urlValues.Set("skipPreflights", skipPreflightsToStr)
urlValues.Set("installStatus", installStatus)
url := fmt.Sprintf("%s/kots_metrics/preflights/%s/%s?%s", endpoint, appID, clusterID, urlValues.Encode())
var buf bytes.Buffer
postReq, err := http.NewRequest("POST", url, &buf)
if err != nil {
return errors.Wrap(err, "failed to call newrequest")
}
postReq.Header.Add("Authorization", license.Spec.LicenseID)
postReq.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(postReq)
if err != nil {
return errors.Wrap(err, "failed to send preflights reports")
}
defer resp.Body.Close()
if resp.StatusCode != 201 {
return errors.Errorf("Unexpected status code %d", resp.StatusCode)
}
return nil
}
func SendPreflightInfo(appID string, sequence int, isSkipPreflights bool, isUpdate bool) error {
license, err := store.GetStore().GetLatestLicenseForApp(appID)
if err != nil {
return errors.Wrap(err, "failed to find license for app")
}
downstreams, err := store.GetStore().ListDownstreamsForApp(appID)
if err != nil {
return errors.Wrap(err, "failed to list downstreams for app")
} else if len(downstreams) == 0 {
err = errors.New("no downstreams for app")
return err
}
clusterID := downstreams[0].ClusterID
if isSkipPreflights || isUpdate {
// at this point current version status does not exist so it's
// neccessary to create thread to get it after version is deployed
// isUpdate means that it's not on initial install
go func() {
<-time.After(20 * time.Second)
currentVersion, err := downstream.GetCurrentVersion(appID, clusterID)
if err != nil {
logger.Debugf("failed to get current downstream version: %v", err)
return
}
if currentVersion.Status != "" && currentVersion.Status != "deploying" {
if err := SendPreflightsReportToReplicatedApp(license, appID, clusterID, sequence, isSkipPreflights, currentVersion.Status); err != nil {
logger.Debugf("failed to send preflights data to replicated app: %v", err)
return
}
}
}()
} else {
if err := SendPreflightsReportToReplicatedApp(license, appID, clusterID, sequence, isSkipPreflights, ""); err != nil {
return errors.Wrap(err, "failed to send preflights data to replicated app")
}
}
return nil
}