forked from virtual-kubelet/virtual-kubelet
/
docker_client.go
113 lines (97 loc) · 3.12 KB
/
docker_client.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
package vic
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
"github.com/vmware/vic/pkg/trace"
)
type CreateResponse struct {
Id string `json:"Id"`
Warnings string `json:"Warnings"`
}
// Super simplistic docker client for the virtual kubelet to perform some operations
type DockerClient interface {
Ping(op trace.Operation) error
CreateContainer(op trace.Operation, config string) error
PullImage(op trace.Operation, image string) error
}
type VicDockerClient struct {
serverAddr string
}
func NewVicDockerClient(personaAddr string) DockerClient {
return &VicDockerClient{
serverAddr: personaAddr,
}
}
func (v *VicDockerClient) Ping(op trace.Operation) error {
personaServer := fmt.Sprintf("http://%s/v1.35/info", v.serverAddr)
resp, err := http.Get(personaServer)
if err != nil {
op.Errorf("Ping failed: error = %s", err.Error())
return err
}
if resp.StatusCode >= 300 {
op.Errorf("Ping failed: status = %d", resp.StatusCode)
return fmt.Errorf("Server Error")
}
return nil
}
func (v *VicDockerClient) CreateContainer(op trace.Operation, config string) error {
personaServer := fmt.Sprintf("http://%s/v1.35/containers/create", v.serverAddr)
reader := bytes.NewBuffer([]byte(config))
resp, err := http.Post(personaServer, "application/json", reader)
if err != nil {
op.Errorf("Error from from docker create: error = %s", err.Error())
return err
}
if resp.StatusCode >= 300 {
op.Errorf("Error from from docker create: status = %d", resp.StatusCode)
return fmt.Errorf("Image not found")
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
op.Infof("Response from docker create: status = %d", resp.StatusCode)
op.Infof("Response from docker create: body = %s", string(body))
var createResp CreateResponse
err = json.Unmarshal(body, &createResp)
if err != nil {
op.Errorf("Failed to unmarshal response from container create post")
return err
}
startContainerUrl := fmt.Sprintf("http://%s/v1.35/containers/%s/start", v.serverAddr, createResp.Id)
op.Infof("Starting container with request - %s", startContainerUrl)
_, err = http.Post(startContainerUrl, "", nil)
if err != nil {
op.Errorf("Failed to start container %s", createResp.Id)
return err
}
return nil
}
func (v *VicDockerClient) PullImage(op trace.Operation, image string) error {
pullClient := &http.Client{Timeout: 60 * time.Second}
personaServer := fmt.Sprintf("http://%s/v1.35/images/create?fromImage=%s", v.serverAddr, image)
op.Infof("POST %s", personaServer)
reader := bytes.NewBuffer([]byte(""))
resp, err := pullClient.Post(personaServer, "application/json", reader)
if err != nil {
op.Errorf("Error from docker pull: error = %s", err.Error())
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
msg := fmt.Sprintf("Error from docker pull: status = %d", resp.StatusCode)
op.Errorf(msg)
return fmt.Errorf(msg)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
msg := fmt.Sprintf("Error reading docker pull response: error = %s", err.Error())
op.Errorf(msg)
return fmt.Errorf(msg)
}
op.Infof("Response from docker pull: body = %s", string(body))
return nil
}