Skip to content

Commit 8480d12

Browse files
authored
Merge pull request #177 from replicatedhq/divolgin/gzip-kots
gzip kots release data in http requests
2 parents ba075ca + 43e1b8e commit 8480d12

File tree

3 files changed

+58
-76
lines changed

3 files changed

+58
-76
lines changed

client/release.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func (c *Client) CreateRelease(appID string, appType string, yaml string) (*type
113113
} else if appType == "ship" {
114114
return c.ShipClient.CreateRelease(appID, yaml)
115115
} else if appType == "kots" {
116-
return c.KotsClient.CreateRelease(appID, yaml)
116+
return c.KotsHTTPClient.CreateRelease(appID, yaml)
117117
}
118118

119119
return nil, errors.New("unknown app type")
@@ -126,7 +126,7 @@ func (c *Client) UpdateRelease(appID string, appType string, sequence int64, yam
126126
} else if appType == "ship" {
127127
return c.ShipClient.UpdateRelease(appID, sequence, yaml)
128128
} else if appType == "kots" {
129-
return c.KotsClient.UpdateRelease(appID, sequence, yaml)
129+
return c.KotsHTTPClient.UpdateRelease(appID, sequence, yaml)
130130
}
131131
return errors.New("unknown app type")
132132
}

pkg/kotsclient/release.go

Lines changed: 42 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,18 @@
11
package kotsclient
22

33
import (
4+
"bytes"
5+
"compress/gzip"
46
"fmt"
7+
"io"
58
"net/http"
9+
"strings"
610

711
"github.com/pkg/errors"
812
"github.com/replicatedhq/replicated/pkg/graphql"
913
"github.com/replicatedhq/replicated/pkg/types"
1014
)
1115

12-
type GraphQLResponseKotsCreateRelease struct {
13-
Data KotsCreateReleaseData `json:"data,omitempty"`
14-
Errors []graphql.GQLError `json:"errors,omitempty"`
15-
}
16-
17-
type KotsCreateReleaseData struct {
18-
KotsReleaseData KotsReleaseSequence `json:"createKotsRelease"`
19-
}
20-
21-
type KotsReleaseSequence struct {
22-
Sequence int64 `json:"sequence"`
23-
}
24-
25-
type GraphQLResponseKotsUpdateRelease struct {
26-
Data KotsUpdateReleaseData `json:"data,omitempty"`
27-
Errors []graphql.GQLError `json:"errors,omitempty"`
28-
}
29-
30-
type KotsUpdateReleaseData struct {
31-
KotsReleaseData KotsReleaseSequence `json:"updateKotsRelease"`
32-
}
33-
3416
type GraphQLResponseListReleases struct {
3517
Data *KotsReleasesData `json:"data,omitempty"`
3618
Errors []graphql.GQLError `json:"errors,omitempty"`
@@ -48,72 +30,60 @@ type KotsRelease struct {
4830
Channels []*KotsChannel `json:"channels"`
4931
}
5032

51-
type GraphQLResponseUpdateKotsRelease struct {
52-
Data *KotsReleaseUpdateData `json:"data,omitempty"`
53-
}
54-
55-
type KotsReleaseUpdateData struct {
56-
UpdateKotsRelease *UpdateKotsRelease `json:"updateKotsRelease"`
57-
}
58-
59-
type UpdateKotsRelease struct {
60-
ID string `json:"id"`
61-
Config string `json:"spec,omitempty"`
62-
}
63-
64-
const createReleaseQuery = `
65-
mutation createKotsRelease($appId: ID!, $spec: String) {
66-
createKotsRelease(appId: $appId, spec: $spec) {
67-
sequence
33+
func (c *VendorV3Client) CreateRelease(appID string, multiyaml string) (*types.ReleaseInfo, error) {
34+
gzipData := bytes.NewBuffer(nil)
35+
gzipWriter := gzip.NewWriter(gzipData)
36+
_, err := io.Copy(gzipWriter, strings.NewReader(multiyaml))
37+
if err != nil {
38+
gzipWriter.Close()
39+
return nil, errors.Wrap(err, "failed to write gzip data")
6840
}
69-
}`
7041

71-
func (c *GraphQLClient) CreateRelease(appID string, multiyaml string) (*types.ReleaseInfo, error) {
72-
response := GraphQLResponseKotsCreateRelease{}
42+
if err := gzipWriter.Close(); err != nil {
43+
return nil, errors.Wrap(err, "failed to close gzip writer")
44+
}
7345

74-
request := graphql.Request{
75-
Query: createReleaseQuery,
76-
Variables: map[string]interface{}{
77-
"appId": appID,
78-
"spec": multiyaml,
79-
},
46+
request := types.KotsCreateReleaseRequest{
47+
SpecGzip: gzipData.Bytes(),
8048
}
8149

82-
if err := c.ExecuteRequest(request, &response); err != nil {
83-
return nil, err
50+
response := types.KotsGetReleaseResponse{}
51+
52+
url := fmt.Sprintf("/v3/app/%s/release", appID)
53+
err = c.DoJSON("POST", url, http.StatusCreated, request, &response)
54+
if err != nil {
55+
return nil, errors.Wrap(err, "failed to create release")
8456
}
8557

8658
releaseInfo := types.ReleaseInfo{
87-
AppID: appID,
88-
Sequence: response.Data.KotsReleaseData.Sequence,
59+
AppID: response.Release.AppID,
60+
Sequence: response.Release.Sequence,
8961
}
9062

9163
return &releaseInfo, nil
9264
}
9365

94-
const updateKotsRelease = `
95-
mutation updateKotsRelease($appId: ID!, $spec: String!, $sequence: Int) {
96-
updateKotsRelease(appId: $appId, spec: $spec, sequence: $sequence) {
97-
sequence
98-
}
99-
}
100-
`
101-
102-
func (c *GraphQLClient) UpdateRelease(appID string, sequence int64, multiyaml string) error {
103-
response := GraphQLResponseUpdateKotsRelease{}
66+
func (c *VendorV3Client) UpdateRelease(appID string, sequence int64, multiyaml string) error {
67+
gzipData := bytes.NewBuffer(nil)
68+
gzipWriter := gzip.NewWriter(gzipData)
69+
_, err := io.Copy(gzipWriter, strings.NewReader(multiyaml))
70+
if err != nil {
71+
gzipWriter.Close()
72+
return errors.Wrap(err, "failed to write gzip data")
73+
}
10474

105-
request := graphql.Request{
106-
Query: updateKotsRelease,
75+
if err := gzipWriter.Close(); err != nil {
76+
return errors.Wrap(err, "failed to close gzip writer")
77+
}
10778

108-
Variables: map[string]interface{}{
109-
"appId": appID,
110-
"spec": multiyaml,
111-
"sequence": sequence,
112-
},
79+
request := types.KotsUpdateReleaseRequest{
80+
SpecGzip: gzipData.Bytes(),
11381
}
11482

115-
if err := c.ExecuteRequest(request, &response); err != nil {
116-
return err
83+
url := fmt.Sprintf("/v3/app/%s/release/%d", appID, sequence)
84+
err = c.DoJSON("PUT", url, http.StatusOK, request, nil)
85+
if err != nil {
86+
return errors.Wrap(err, "failed to create release")
11787
}
11888

11989
return nil
@@ -124,7 +94,7 @@ func (c *VendorV3Client) ListReleases(appID string) ([]types.ReleaseInfo, error)
12494
done := false
12595
page := 0
12696
for !done {
127-
resp := types.ListReleasesResponse{}
97+
resp := types.KotsListReleasesResponse{}
12898
path := fmt.Sprintf("/v3/app/%s/releases?currentPage=%d&pageSize=20", appID, page)
12999
err := c.DoJSON("GET", path, http.StatusOK, nil, &resp)
130100
if err != nil {

pkg/types/release.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,20 @@ type LintLinePosition struct {
3232
Column int64 `json:"column"`
3333
}
3434

35-
// ListReleasesResponse contains the JSON releases list
36-
type ListReleasesResponse struct {
35+
type KotsCreateReleaseRequest struct {
36+
SpecGzip []byte `json:"spec_gzip"`
37+
}
38+
39+
type KotsGetReleaseResponse struct {
40+
Release KotsAppRelease `json:"release"`
41+
}
42+
43+
type KotsUpdateReleaseRequest struct {
44+
SpecGzip []byte `json:"spec_gzip"`
45+
}
46+
47+
// KotsListReleasesResponse contains the JSON releases list
48+
type KotsListReleasesResponse struct {
3749
Releases []*KotsAppRelease `json:"releases"`
3850
}
3951

0 commit comments

Comments
 (0)