-
Notifications
You must be signed in to change notification settings - Fork 2
/
hub_connect_agent.go
79 lines (71 loc) · 2.13 KB
/
hub_connect_agent.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
package hub
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/url"
"github.com/MayaraCloud/terraform-provider-anthos/k8s"
)
// ConnectAgent holds info needed to request and process a gke-connect-agent object
type ConnectAgent struct {
Proxy string
Namespace string
Version string
IsUpgrade bool
Registry string
ImagePullSecretContent string
Response k8s.ConnectManifestResponse
GCPSAKey string
}
// GenerateConnectManifest asks the gkehub API for a gke-connect-agent manifest
func (c *Client) GenerateConnectManifest(proxy string, namespace string, version string, isUpgrade bool, registry string, imagePullSecretContent string) (k8s.ConnectManifestResponse, error) {
var result k8s.ConnectManifestResponse
// Create a url object to append parameters to it
APIURL := prodAddr + "v1beta1/" + c.Resource.Name + ":generateConnectManifest"
// Create the url parameters
u, err := url.Parse(APIURL)
if err != nil {
return result, fmt.Errorf("Parsing %v url: %w", APIURL, err)
}
q := u.Query()
q.Set("alt", "json")
q.Set("name", c.Resource.Name)
if proxy != "" {
q.Set("connectAgent.proxy", proxy)
}
if namespace != "gke-connect" {
q.Set("connectAgent.namespace", namespace)
}
if version != "" {
q.Set("version", version)
}
if isUpgrade {
q.Set("isUpgrade", "true")
}
if registry != "" {
q.Set("registry", registry)
}
if imagePullSecretContent != "" {
q.Set("imagePullSecretContent", imagePullSecretContent)
}
u.RawQuery = q.Encode()
// Go ahead with the request
response, err := c.svc.client.Get(u.String())
if err != nil {
return result, fmt.Errorf("GET request: %w", err)
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return result, fmt.Errorf("reading get request body: %w", err)
}
statusOK := response.StatusCode >= 200 && response.StatusCode < 300
if !statusOK {
return result, fmt.Errorf("Bad status code: %v", string(body))
}
err = json.Unmarshal(body, &result)
if err != nil {
return result, fmt.Errorf("json Un-marshaling body: %w", err)
}
return result, nil
}