-
Notifications
You must be signed in to change notification settings - Fork 14
/
kubernetes_engine.go
150 lines (130 loc) · 5.13 KB
/
kubernetes_engine.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// This file is part of gobizfly
package gobizfly
import (
"context"
"encoding/json"
"io/ioutil"
"log"
"net/http"
"strings"
)
const (
clusterPath = "/_"
kubeConfig = "kubeconfig"
k8sVersion = "/k8s_versions"
clusterInfo = "/engine/cluster_info"
clusterJoinEverywhere = "/engine/cluster_join_everywhere"
nodeEverywhere = "/_/node_everywhere"
k8sPackages = "/package/"
)
var _ KubernetesEngineService = (*kubernetesEngineService)(nil)
type kubernetesEngineService struct {
client *Client
}
type KubernetesEngineService interface {
List(ctx context.Context, opts *ListOptions) ([]*Cluster, error)
Create(ctx context.Context, req *ClusterCreateRequest) (*ExtendedCluster, error)
Get(ctx context.Context, id string) (*FullCluster, error)
Delete(ctx context.Context, id string) error
AddWorkerPools(ctx context.Context, id string, awp *AddWorkerPoolsRequest) ([]*ExtendedWorkerPool, error)
RecycleNode(ctx context.Context, clusterUID string, PoolID string, NodePhysicalID string) error
DeleteClusterWorkerPool(ctx context.Context, clusterUID string, PoolID string) error
GetClusterWorkerPool(ctx context.Context, clusterUID string, PoolID string) (*WorkerPoolWithNodes, error)
UpdateClusterWorkerPool(ctx context.Context, clusterUID string, PoolID string, uwp *UpdateWorkerPoolRequest) error
DeleteClusterWorkerPoolNode(ctx context.Context, clusterUID string, PoolID string, NodeID string) error
GetKubeConfig(ctx context.Context, clusterUID string, opts *GetKubeConfigOptions) (string, error)
GetKubernetesVersion(ctx context.Context) (*KubernetesVersionResponse, error)
GetClusterInfo(ctx context.Context, pool_id string) (*ClusterInfoResponse, error)
AddClusterEverywhere(ctx context.Context, id string, cjer *ClusterJoinEverywhereRequest) (*ClusterJoinEverywhereResponse, error)
GetEverywhere(ctx context.Context, id string) (*EverywhereNode, error)
UpdateCluster(ctx context.Context, id string, payload *UpdateClusterRequest) (*ExtendedCluster, error)
UpgradeClusterVersion(ctx context.Context, id string, payload *UpgradeClusterVersionRequest) error
GetUpgradeClusterVersion(ctx context.Context, id string) (*UpgradeClusterVersionResponse, error)
GetPackages(ctx context.Context, provisionType string) (*KubernetesPackagesResponse, error)
}
// KubernetesVersionResponse represents the get versions from the Kubernetes Engine API
type KubernetesVersionResponse struct {
ControllerVersions []ControllerVersion `json:"controller_versions"`
WorkerVersion []string `json:"worker_versions"`
}
type KubernetesPackagesResponse struct {
Packages []KubernetesPackage `json:"packages"`
}
type KubernetesPackage struct {
ID string `json:"id"`
Name string `json:"name"`
}
func (c *kubernetesEngineService) resourcePath() string {
return clusterPath + "/"
}
func (c *kubernetesEngineService) itemPath(id string) string {
return strings.Join([]string{clusterPath, id}, "/")
}
func (c *kubernetesEngineService) EverywherePath(id string) string {
return strings.Join([]string{nodeEverywhere, id}, "/")
}
type GetKubeConfigOptions struct {
ExpiteTime string `json:"expire_time,omitempty"`
}
// GetKubeConfig - Get Kubernetes config from the given cluster
func (c *kubernetesEngineService) GetKubeConfig(ctx context.Context, clusterUID string, opts *GetKubeConfigOptions) (string, error) {
req, err := c.client.NewRequest(ctx, http.MethodGet, kubernetesServiceName, strings.Join([]string{c.itemPath(clusterUID), kubeConfig}, "/"), nil)
if err != nil {
return "", err
}
params := req.URL.Query()
if opts != nil {
if opts.ExpiteTime != "" {
params.Add("expire_time", opts.ExpiteTime)
}
}
req.URL.RawQuery = params.Encode()
resp, err := c.client.Do(ctx, req)
if err != nil {
return "", nil
}
defer resp.Body.Close()
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
bodyString := string(bodyBytes)
return bodyString, nil
}
// GetKubernetesVersion - Get Kubernetes version from the Kubernetes Engine API
func (c *kubernetesEngineService) GetKubernetesVersion(ctx context.Context) (*KubernetesVersionResponse, error) {
req, err := c.client.NewRequest(ctx, http.MethodGet, kubernetesServiceName, k8sVersion, nil)
if err != nil {
return nil, err
}
resp, err := c.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var data *KubernetesVersionResponse
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return nil, err
}
return data, nil
}
// GetKubernetesVersion - Get Kubernetes Engine Package from the Kubernetes Engine API
func (c *kubernetesEngineService) GetPackages(ctx context.Context, provisionType string) (*KubernetesPackagesResponse, error) {
req, err := c.client.NewRequest(ctx, http.MethodGet, kubernetesServiceName, k8sPackages, nil)
if err != nil {
return nil, err
}
params := req.URL.Query()
params.Add("specify", provisionType)
req.URL.RawQuery = params.Encode()
resp, err := c.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var data *KubernetesPackagesResponse
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return nil, err
}
return data, nil
}