Skip to content

Commit fd6b783

Browse files
committed
Merge branch 'entrypoint' into 'master'
你们的幺蛾子啊害我要改这个 entrypoint 可以写 `after_start: "curl 127.0.0.1:7001"` 之类的, 不验证运行结果; `before_stop: "curl -XPOST localhost:8818/rabbitmq/consumer.stop"`之类的, 同样不验证运行结果, 当做 hook 用吧. @platform See merge request !30
2 parents 4675520 + 38537c3 commit fd6b783

File tree

5 files changed

+45
-0
lines changed

5 files changed

+45
-0
lines changed

cluster/calcium/cluster.go

+5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ type calcium struct {
2020
source source.Source
2121
}
2222

23+
const (
24+
AFTER_START = "after_start"
25+
BEFORE_STOP = "before_stop"
26+
)
27+
2328
func New(config types.Config) (*calcium, error) {
2429
var err error
2530
store, err := etcdstore.New(config)

cluster/calcium/create_container.go

+16
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ func (c *calcium) doCreateContainerWithCPUPeriod(nodename string, connum int, qu
163163
continue
164164
}
165165

166+
// after start
167+
runExec(node.Engine, info, AFTER_START)
168+
166169
_, err = c.store.AddContainer(info.ID, opts.Podname, node.Name, containerName, nil, opts.Memory)
167170
if err != nil {
168171
ms[i].Error = err.Error()
@@ -435,6 +438,9 @@ func (c *calcium) doCreateContainerWithScheduler(nodename string, cpumap []types
435438
continue
436439
}
437440

441+
// after start
442+
runExec(node.Engine, info, AFTER_START)
443+
438444
_, err = c.store.AddContainer(info.ID, opts.Podname, node.Name, containerName, quota, opts.Memory)
439445
if err != nil {
440446
ms[i].Error = err.Error()
@@ -586,6 +592,10 @@ func (c *calcium) makeContainerOptions(quota map[string]int, specs types.Specs,
586592
if entry.HealthCheck == "tcp" || entry.HealthCheck == "http" {
587593
containerLabels["healthcheck"] = entry.HealthCheck
588594
}
595+
// 要把after_start和before_stop写进去
596+
containerLabels[AFTER_START] = entry.AfterStart
597+
containerLabels[BEFORE_STOP] = entry.BeforeStop
598+
// 接下来是meta
589599
for key, value := range specs.Meta {
590600
containerLabels[key] = value
591601
}
@@ -759,6 +769,9 @@ func (c *calcium) doUpgradeContainer(containers []*types.Container, image string
759769
// TODO in the future I hope `makeContainerConfig` will make a deep copy of config
760770
imageToDelete := info.Config.Image
761771

772+
// before stop old container
773+
runExec(engine, info, BEFORE_STOP)
774+
762775
// stops the old container
763776
timeout := 5 * time.Second
764777
err = engine.ContainerStop(context.Background(), info.ID, &timeout)
@@ -820,6 +833,9 @@ func (c *calcium) doUpgradeContainer(containers []*types.Container, image string
820833
continue
821834
}
822835

836+
// after start
837+
runExec(engine, newInfo, AFTER_START)
838+
823839
// if so, add a new container in etcd
824840
_, err = c.store.AddContainer(newInfo.ID, container.Podname, container.Nodename, containerName, container.CPU, container.Memory)
825841
if err != nil {

cluster/calcium/helper.go

+19
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ import (
77
"path/filepath"
88
"strings"
99

10+
engineapi "github.com/docker/engine-api/client"
1011
enginetypes "github.com/docker/engine-api/types"
1112
enginecontainer "github.com/docker/engine-api/types/container"
1213
enginenetwork "github.com/docker/engine-api/types/network"
1314
"gitlab.ricebook.net/platform/core/types"
1415
"gitlab.ricebook.net/platform/core/utils"
16+
"golang.org/x/net/context"
1517
)
1618

1719
// As the name says,
@@ -130,3 +132,20 @@ func makeMountPaths(specs types.Specs, config types.Config) ([]string, map[strin
130132
binds = append(binds, "/proc/sys:/writable-proc/sys:ro")
131133
return binds, volumes
132134
}
135+
136+
// 跑存在labels里的exec
137+
// 为什么要存labels呢, 因为下线容器的时候根本不知道entrypoint是啥
138+
func runExec(client *engineapi.Client, container enginetypes.ContainerJSON, label string) error {
139+
cmd, ok := container.Config.Labels[label]
140+
if !ok {
141+
return fmt.Errorf("No %q found in container %q", label, container.ID)
142+
}
143+
144+
cmds := utils.MakeCommandLineArgs(cmd)
145+
execConfig := enginetypes.ExecConfig{User: container.Config.User, Cmd: cmds}
146+
resp, err := client.ContainerExecCreate(context.Background(), container.ID, execConfig)
147+
if err != nil {
148+
return err
149+
}
150+
return client.ContainerExecStart(context.Background(), resp.ID, enginetypes.ExecStartCheck{})
151+
}

cluster/calcium/remove_container.go

+3
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ func (c *calcium) removeOneContainer(container *types.Container) error {
8383
return err
8484
}
8585

86+
// before stop
87+
runExec(container.Engine, info, BEFORE_STOP)
88+
8689
timeout := 5 * time.Second
8790
err = container.Engine.ContainerStop(context.Background(), info.ID, &timeout)
8891
if err != nil {

types/specs.go

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ type Specs struct {
2323
// single entrypoint
2424
type Entrypoint struct {
2525
Command string `yaml:"cmd,omitempty"`
26+
AfterStart string `yaml:"after_start,omitempty"`
27+
BeforeStop string `yaml:"before_stop,omitempty"`
2628
Ports []Port `yaml:"ports,omitempty,flow"`
2729
Exposes []Expose `yaml:"exposes,omitempty,flow"`
2830
NetworkMode string `yaml:"network_mode,omitempty"`

0 commit comments

Comments
 (0)