|
1 | 1 | package perconaservermongodb
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "encoding/json" |
5 | 4 | "fmt"
|
6 | 5 | "net/http"
|
7 | 6 | "net/url"
|
8 | 7 | "strings"
|
9 | 8 | "time"
|
10 | 9 |
|
11 |
| - v1 "github.com/percona/percona-server-mongodb-operator/pkg/apis/psmdb/v1" |
12 |
| -) |
| 10 | + "github.com/percona/percona-server-mongodb-operator/versionserviceclient/models" |
13 | 11 |
|
14 |
| -func (vs VersionServiceClient) GetExactVersion(endpoint string, vm VersionMeta) (DepVersion, error) { |
15 |
| - client := http.Client{ |
16 |
| - Timeout: 5 * time.Second, |
17 |
| - } |
| 12 | + "github.com/percona/percona-server-mongodb-operator/versionserviceclient/version_service" |
18 | 13 |
|
19 |
| - requestURL, err := url.Parse( |
20 |
| - fmt.Sprintf("%s/v1/psmdb-operator/%s/%s", |
21 |
| - strings.TrimRight(endpoint, "/"), |
22 |
| - vs.OpVersion, |
23 |
| - vm.Apply, |
24 |
| - ), |
25 |
| - ) |
26 |
| - if err != nil { |
27 |
| - return DepVersion{}, err |
28 |
| - } |
| 14 | + "github.com/percona/percona-server-mongodb-operator/versionserviceclient" |
| 15 | +) |
29 | 16 |
|
30 |
| - q := requestURL.Query() |
31 |
| - q.Add("databaseVersion", vm.MongoVersion) |
32 |
| - q.Add("kubeVersion", vm.KubeVersion) |
33 |
| - q.Add("platform", vm.Platform) |
34 |
| - q.Add("customResourceUID", vm.CRUID) |
| 17 | +const productName = "psmdb-operator" |
35 | 18 |
|
36 |
| - if vm.PMMVersion != "" { |
37 |
| - q.Add("pmmVersion", vm.PMMVersion) |
38 |
| - } |
39 |
| - |
40 |
| - if vm.BackupVersion != "" { |
41 |
| - q.Add("backupVersion", vm.BackupVersion) |
| 19 | +func (vs VersionServiceClient) GetExactVersion(endpoint string, vm VersionMeta) (DepVersion, error) { |
| 20 | + if strings.Contains(endpoint, "https://check.percona.com/versions") { |
| 21 | + endpoint = "https://check.percona.com" |
42 | 22 | }
|
43 | 23 |
|
44 |
| - requestURL.RawQuery = q.Encode() |
45 |
| - req, err := http.NewRequest("GET", requestURL.String(), nil) |
| 24 | + requestURL, err := url.Parse(endpoint) |
46 | 25 | if err != nil {
|
47 | 26 | return DepVersion{}, err
|
48 | 27 | }
|
49 | 28 |
|
50 |
| - req.Header.Set("Accept", "application/json") |
51 |
| - |
52 |
| - resp, err := client.Do(req) |
53 |
| - if err != nil { |
54 |
| - return DepVersion{}, err |
| 29 | + vsClient := versionserviceclient.NewHTTPClientWithConfig(nil, &versionserviceclient.TransportConfig{ |
| 30 | + Host: requestURL.Host, |
| 31 | + BasePath: requestURL.Path, |
| 32 | + Schemes: []string{requestURL.Scheme}, |
| 33 | + }) |
| 34 | + |
| 35 | + applyParams := &version_service.VersionServiceApplyParams{ |
| 36 | + Apply: vm.Apply, |
| 37 | + BackupVersion: &vm.BackupVersion, |
| 38 | + CustomResourceUID: &vm.CRUID, |
| 39 | + DatabaseVersion: &vm.MongoVersion, |
| 40 | + KubeVersion: &vm.KubeVersion, |
| 41 | + OperatorVersion: "1.5.0", |
| 42 | + Platform: &vm.Platform, |
| 43 | + PmmVersion: &vm.PMMVersion, |
| 44 | + Product: productName, |
| 45 | + HTTPClient: &http.Client{Timeout: 10 * time.Second}, |
55 | 46 | }
|
| 47 | + applyParams = applyParams.WithTimeout(10 * time.Second) |
56 | 48 |
|
57 |
| - defer resp.Body.Close() |
58 |
| - if resp.StatusCode != http.StatusOK { |
59 |
| - return DepVersion{}, fmt.Errorf("received bad status code %s", resp.Status) |
60 |
| - } |
| 49 | + resp, err := vsClient.VersionService.VersionServiceApply(applyParams) |
61 | 50 |
|
62 |
| - r := VersionResponse{} |
63 |
| - err = json.NewDecoder(resp.Body).Decode(&r) |
64 | 51 | if err != nil {
|
65 |
| - return DepVersion{}, fmt.Errorf("failed to unmarshal response: %v", err) |
| 52 | + return DepVersion{}, err |
66 | 53 | }
|
67 | 54 |
|
68 |
| - if len(r.Versions) == 0 { |
| 55 | + if len(resp.Payload.Versions) == 0 { |
69 | 56 | return DepVersion{}, fmt.Errorf("empty versions response")
|
70 | 57 | }
|
71 | 58 |
|
72 |
| - mongoVersion, err := getVersion(r.Versions[0].Matrix.Mongo) |
| 59 | + mongoVersion, err := getVersion(resp.Payload.Versions[0].Matrix.Mongod) |
73 | 60 | if err != nil {
|
74 | 61 | return DepVersion{}, err
|
75 | 62 | }
|
76 | 63 |
|
77 |
| - backupVersion, err := getVersion(r.Versions[0].Matrix.Backup) |
| 64 | + backupVersion, err := getVersion(resp.Payload.Versions[0].Matrix.Backup) |
78 | 65 | if err != nil {
|
79 | 66 | return DepVersion{}, err
|
80 | 67 | }
|
81 | 68 |
|
82 |
| - pmmVersion, err := getVersion(r.Versions[0].Matrix.PMM) |
| 69 | + pmmVersion, err := getVersion(resp.Payload.Versions[0].Matrix.Pmm) |
83 | 70 | if err != nil {
|
84 | 71 | return DepVersion{}, err
|
85 | 72 | }
|
86 | 73 |
|
87 | 74 | return DepVersion{
|
88 |
| - MongoImage: r.Versions[0].Matrix.Mongo[mongoVersion].ImagePath, |
| 75 | + MongoImage: resp.Payload.Versions[0].Matrix.Mongod[mongoVersion].ImagePath, |
89 | 76 | MongoVersion: mongoVersion,
|
90 |
| - BackupImage: r.Versions[0].Matrix.Backup[backupVersion].ImagePath, |
| 77 | + BackupImage: resp.Payload.Versions[0].Matrix.Backup[backupVersion].ImagePath, |
91 | 78 | BackupVersion: backupVersion,
|
92 |
| - PMMImage: r.Versions[0].Matrix.PMM[pmmVersion].ImagePath, |
| 79 | + PMMImage: resp.Payload.Versions[0].Matrix.Mongod[pmmVersion].ImagePath, |
93 | 80 | PMMVersion: pmmVersion,
|
94 | 81 | }, nil
|
95 | 82 | }
|
96 | 83 |
|
97 |
| -func getVersion(versions map[string]Version) (string, error) { |
| 84 | +func getVersion(versions map[string]models.VersionVersion) (string, error) { |
98 | 85 | if len(versions) != 1 {
|
99 | 86 | return "", fmt.Errorf("response has multiple or zero versions")
|
100 | 87 | }
|
@@ -147,7 +134,7 @@ type VersionResponse struct {
|
147 | 134 | }
|
148 | 135 |
|
149 | 136 | type VersionMeta struct {
|
150 |
| - Apply v1.UpgradeStrategy |
| 137 | + Apply string |
151 | 138 | MongoVersion string
|
152 | 139 | KubeVersion string
|
153 | 140 | Platform string
|
|
0 commit comments