forked from 22Acacia/terraform-provider-googlecli
-
Notifications
You must be signed in to change notification settings - Fork 2
/
container_engine_replicacontroler.go
201 lines (177 loc) · 5.46 KB
/
container_engine_replicacontroler.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package main
import (
"fmt"
"time"
"bytes"
"os/exec"
)
// only modeling the parts of the responses as I need them
type kubectlItem struct {
Metadata struct {
Name string `json:"name"`
Uid string `json:"uid"`
} `json:"metadata"`
Spec interface{} `json:"spec"`
Status struct {
Replicas int `json:"replicas"`
} `json:"status"`
}
type kubectlService struct {
Status struct {
LoadBalancer struct {
Ingress []struct {
IP string `json:"ip"`
} `json:"ingress"`
} `json:"loadBalancer"`
} `json:"status"`
}
func addExtraArgs(run_args []string, optional_args, env_args map[string]string) ([]string) {
for k, v := range optional_args {
run_args = append(run_args, "--" + k + "=" +v)
}
for k, v := range env_args {
run_args = append(run_args, "--env=" + k + "=" + v)
}
return run_args
}
func CreateKubeRC(name, dockerImage, external_port string, optional_args, env_args map[string]string) (string, error) {
kubectl_cmd := "kubectl"
kubectl_run_args :=[]string{"run", name, "--image=" + dockerImage, "--output=json"}
kubectl_run_args = addExtraArgs(kubectl_run_args, optional_args, env_args)
run_replicacontroler := exec.Command(kubectl_cmd, kubectl_run_args...)
var stdout, stderr bytes.Buffer
run_replicacontroler.Stdout = &stdout
run_replicacontroler.Stderr = &stderr
err := run_replicacontroler.Run()
if err != nil {
for i := 0; i <= 10; i++ {
time.Sleep(5 * time.Second)
err := run_replicacontroler.Run()
if err == nil {
break
} else if i == 10 && err != nil {
return "", fmt.Errorf("Error creating our replicacontroler named %q with error %q and stdout: %q", name, stderr.String(), stdout.String())
}
}
}
var runReturn kubectlItem
err = parseJSON(&runReturn, stdout.String())
if err != nil {
return "", err
}
uid := runReturn.Metadata.Uid
var ex_err error
if external_port != "" {
ex_err = expose_rc_externally(name, external_port)
}
return uid,ex_err
}
func expose_rc_externally(name, external_port string) (error) {
expose_rc := exec.Command("kubectl", "expose", "deployments", name, "--port=" + external_port, "--type=LoadBalancer", "--output=json")
var stdout, stderr bytes.Buffer
expose_rc.Stdout = &stdout
expose_rc.Stderr = &stderr
err := expose_rc.Run()
if err != nil {
return fmt.Errorf("Error creating external load balancer on a specific port: %q, with stdout: %q", stderr.String(), stdout.String())
}
// wait at most 10 minutes for external ip to be set
exip_set := false
for i := 0; i < (10 * 6) && !exip_set; i++ {
time.Sleep(10 * time.Second)
exip, err := fetchExternalIp(name)
if err != nil {
return err
}
if exip != "" {
exip_set = true
}
}
return nil
}
// calling function needs to handle if the read is successful but the rc is dead or has no replicas
func ReadKubeRC(name, external_port string) (int, string, error) {
get_replicacontrolers := exec.Command("kubectl", "get", "deployments", name, "--output=json")
var stdout, stderr bytes.Buffer
get_replicacontrolers.Stdout = &stdout
get_replicacontrolers.Stderr = &stderr
err := get_replicacontrolers.Run()
if err != nil {
for i := 0; i <= 10; i++ {
time.Sleep(5 * time.Second)
err := get_replicacontrolers.Run()
if err == nil {
break
} else if i == 10 && err != nil {
return -1, "", fmt.Errorf("Error listing our replicacontrolers: %q with stdout: %q", stderr.String(), stdout.String())
}
}
}
var getReturn kubectlItem
err = parseJSON(&getReturn, stdout.String())
if err != nil {
return -1, "", err
}
var ex_err error
var external_ip string
if external_port != "" {
external_ip, ex_err = fetchExternalIp(name)
}
return getReturn.Status.Replicas, external_ip, ex_err
}
func fetchExternalIp(name string) (string, error) {
get_services := exec.Command("kubectl", "get", "services", name, "--output=json")
var stdout, stderr bytes.Buffer
get_services.Stdout = &stdout
get_services.Stderr = &stderr
err := get_services.Run()
if err != nil {
return "", fmt.Errorf("Error fetching services information for this cluster: %q and stdout: %q", stderr.String(), stdout.String())
}
var getReturn kubectlService
err = parseJSON(&getReturn, stdout.String())
if err != nil {
return "", err
}
var ex_ip string
if len(getReturn.Status.LoadBalancer.Ingress) != 0 {
ex_ip = getReturn.Status.LoadBalancer.Ingress[0].IP
}
return ex_ip, nil
}
func DeleteKubeRC(name, external_port string) (error) {
if external_port != "" {
err := deleteLoadBalanacerService(name)
if err != nil {
return err
}
}
delete_replicacontrolers := exec.Command("kubectl", "delete", "deployments", name)
var stdout, stderr bytes.Buffer
delete_replicacontrolers.Stdout = &stdout
delete_replicacontrolers.Stderr = &stderr
err := delete_replicacontrolers.Run()
if err != nil {
for i := 0; i <= 10; i++ {
time.Sleep(5 * time.Second)
err := delete_replicacontrolers.Run()
if err == nil {
break
} else if i == 10 && err != nil {
return fmt.Errorf("Error deleting our replicacontroler: %q and stdout: %q", stderr.String(), stdout.String())
}
}
}
return nil
}
func deleteLoadBalanacerService(name string) (error) {
delete_loadbalancer_service := exec.Command("kubectl", "delete", "svc", name)
var stdout, stderr bytes.Buffer
delete_loadbalancer_service.Stdout = &stdout
delete_loadbalancer_service.Stderr = &stderr
err := delete_loadbalancer_service.Run()
if err != nil {
return fmt.Errorf("Error deleting service: %q with stdout: %q", stderr.String(), stdout.String())
}
return nil
}