This repository has been archived by the owner on Dec 5, 2023. It is now read-only.
/
deploy.go
150 lines (138 loc) · 4.08 KB
/
deploy.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
package docker
import (
"context"
"crypto/sha256"
"fmt"
"time"
"github.com/miRemid/cqless/pkg/cninetwork"
"github.com/miRemid/cqless/pkg/pb"
"github.com/miRemid/cqless/pkg/types"
dtypes "github.com/docker/docker/api/types" // docker types
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters"
"k8s.io/apimachinery/pkg/api/resource"
)
// type pullResponse struct {
// Status string `json:"status"`
// ID string `json:"id,omitempty"`
// Error string `json:"error"`
// Progress string `json:"progress"`
// ProgressDetail struct {
// Current int `json:"current"`
// Total int `json:"total"`
// } `json:"progressDetail"`
// }
// func (r *pullResponse) String() string {
// // status:
// format := r.Status
// if r.ID != "" {
// format += " " + "id: " + r.ID
// } else {
// format += "\r"
// return format
// }
// if r.ProgressDetail != nil {
// format += fmt.Sprintf(" %d/%d", r.ProgressDetail.Current, r.ProgressDetail.Total)
// }
// return format + "\r"
// }
func (p *DockerProvider) pull(ctx context.Context, req *pb.Function) error {
// 1. check local
filter := filters.NewArgs(filters.Arg("reference", req.Image))
img, err := p.cli.ImageList(ctx, dtypes.ImageListOptions{
Filters: filter,
})
if err != nil {
return err
}
if len(img) > 0 {
return nil
}
body, err := p.cli.ImagePull(ctx, req.Image, dtypes.ImagePullOptions{})
if err != nil {
return err
}
// buffer := bytes.NewBuffer(data)
defer body.Close()
// TODO: 进度条
// reader := bufio.NewReader(body)
// for {
// data, err := reader.ReadBytes('\n')
// if err != nil {
// if err != io.EOF {
// return err
// }
// break
// }
// pullResponse := new(pullResponse)
// if err := json.Unmarshal(data, pullResponse); err != nil {
// return err
// }
// fmt.Println(pullResponse)
// }
// io.Copy(os.Stdout, body)
return nil
}
func (p *DockerProvider) Deploy(ctx context.Context, req *pb.CreateFunctionRequest, cni *cninetwork.CNIManager) (*pb.Function, error) {
function := req.Function
p.log.Printf("start to pull %s\n", function.Image)
err := p.pull(ctx, function)
if err != nil {
return nil, err
}
p.log.Printf("start to deploy function: %s\n", function.Name)
labels := function.Labels
if labels == nil {
labels = make(map[string]string)
}
labels[types.DEFAULT_FUNCTION_NAME_LABEL] = function.Name // 添加一个 DEFAULT_FUNCTION_NAME_LABEL = FunctionName 用于后续查找
if function.WatchDogPort == "" {
function.WatchDogPort = types.DEFAULT_WATCHDOG_PORT
}
labels[types.DEFAULT_FUNCTION_PORT_LABEL] = function.WatchDogPort // 添加一个WatchPort标签,用于Resolve
// 计算一个Hash Label,用于区分
hashKey := fmt.Sprintf("%s%v", function.Name, time.Now())
hashValue := sha256.Sum256([]byte(hashKey))
containerName := fmt.Sprintf("%s-%x", function.Name, hashValue[:4])
envs := types.BuildEnv(function)
var containerResources container.Resources
if function.Limits != nil && len(function.Limits.Memory) > 0 {
qty, err := resource.ParseQuantity(function.Limits.Memory)
if err != nil {
p.log.Printf("error parsing (%q) as quantity: %s", function.Limits.Memory, err.Error())
}
containerResources.Memory = qty.Value()
}
resp, err := p.cli.ContainerCreate(ctx,
&container.Config{
Env: envs,
Labels: labels,
Hostname: function.Name,
Image: function.Image,
},
&container.HostConfig{
// Mounts: mounts,
Resources: containerResources,
NetworkMode: "none", // 我们将使用cni来为container提供网络
},
nil, nil, containerName)
if err != nil {
return nil, err
}
p.log.Printf("create function container, id: %s\n", resp.ID)
if err := p.cli.ContainerStart(ctx, resp.ID, dtypes.ContainerStartOptions{}); err != nil {
return nil, err
}
p.log.Printf("start function container, id: %s\n", resp.ID)
info, err := p.cli.ContainerInspect(ctx, resp.ID)
if err != nil {
return nil, err
}
fn := p.createFunction(info)
_, err = cni.CreateCNINetwork(ctx, fn)
if err != nil {
return nil, err
}
fn.Scheme = function.Scheme
return fn, nil
}