Skip to content

Commit c37e2d2

Browse files
authored
Merge pull request #1124 from yyb196/nw_improve
feature: add plugin point before endpoint creating
2 parents 108689e + cfda4e0 commit c37e2d2

File tree

5 files changed

+55
-19
lines changed

5 files changed

+55
-19
lines changed

apis/plugins/ContainerPlugin.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,9 @@ type ContainerPlugin interface {
1111
// PreStart returns an array of priority and args which will pass to runc, the every priority
1212
// used to sort the pre start array that pass to runc, network plugin hook always has priority value 0.
1313
PreStart(interface{}) ([]int, [][]string, error)
14+
15+
//NetworkGenericParams accepts the container id and env of this container and returns the priority of this endpoint
16+
// and if this endpoint should enable resolver and a map which will be used as generic params to create endpoints of
17+
// this container
18+
PreCreateEndpoint(string, []string) (priority int, disableResolver bool, genericParam map[string]interface{})
1419
}

daemon/mgr/container.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1525,7 +1525,7 @@ func (mgr *ContainerManager) detachVolumes(ctx context.Context, c *ContainerMeta
15251525
}
15261526

15271527
func (mgr *ContainerManager) buildContainerEndpoint(c *ContainerMeta) *networktypes.Endpoint {
1528-
return &networktypes.Endpoint{
1528+
ep := &networktypes.Endpoint{
15291529
Owner: c.ID,
15301530
Hostname: c.Config.Hostname,
15311531
Domainname: c.Config.Domainname,
@@ -1544,6 +1544,12 @@ func (mgr *ContainerManager) buildContainerEndpoint(c *ContainerMeta) *networkty
15441544
PortBindings: c.HostConfig.PortBindings,
15451545
NetworkConfig: c.NetworkSettings,
15461546
}
1547+
1548+
if mgr.containerPlugin != nil {
1549+
ep.Priority, ep.DisableResolver, ep.GenericParams = mgr.containerPlugin.PreCreateEndpoint(c.ID, c.Config.Env)
1550+
}
1551+
1552+
return ep
15471553
}
15481554

15491555
// setBaseFS keeps container basefs in meta

daemon/mgr/network.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ func (nm *NetworkManager) EndpointCreate(ctx context.Context, endpoint *types.En
257257
}
258258

259259
// create endpoint
260-
epOptions, err := endpointOptions(n, networkConfig, endpointConfig)
260+
epOptions, err := endpointOptions(n, endpoint)
261261
if err != nil {
262262
return "", err
263263
}
@@ -293,7 +293,7 @@ func (nm *NetworkManager) EndpointCreate(ctx context.Context, endpoint *types.En
293293
networkConfig.SandboxKey = sb.Key()
294294

295295
// endpoint joins into sandbox
296-
joinOptions, err := joinOptions(endpointConfig)
296+
joinOptions, err := joinOptions(endpoint)
297297
if err != nil {
298298
return "", err
299299
}
@@ -493,9 +493,9 @@ func initNetworkLog(cfg *config.Config) {
493493
netlog.SetFormatter(formatter)
494494
}
495495

496-
func endpointOptions(n libnetwork.Network, nwconfig *apitypes.NetworkSettings, epConfig *apitypes.EndpointSettings) ([]libnetwork.EndpointOption, error) {
496+
func endpointOptions(n libnetwork.Network, endpoint *types.Endpoint) ([]libnetwork.EndpointOption, error) {
497497
var createOptions []libnetwork.EndpointOption
498-
498+
epConfig := endpoint.EndpointConfig
499499
if epConfig != nil {
500500
ipam := epConfig.IPAMConfig
501501
if ipam != nil && (ipam.IPV4Address != "" || ipam.IPV6Address != "" || len(ipam.LinkLocalIps) > 0) {
@@ -517,6 +517,13 @@ func endpointOptions(n libnetwork.Network, nwconfig *apitypes.NetworkSettings, e
517517
genericOption := options.Generic{}
518518
createOptions = append(createOptions, libnetwork.EndpointOptionGeneric(genericOption))
519519

520+
if len(endpoint.GenericParams) > 0 {
521+
createOptions = append(createOptions, libnetwork.EndpointOptionGeneric(endpoint.GenericParams))
522+
}
523+
if endpoint.DisableResolver {
524+
createOptions = append(createOptions, libnetwork.CreateOptionDisableResolution())
525+
}
526+
520527
return createOptions, nil
521528
}
522529

@@ -653,9 +660,11 @@ func (nm *NetworkManager) cleanEndpointConfig(epConfig *apitypes.EndpointSetting
653660
epConfig.MacAddress = ""
654661
}
655662

656-
func joinOptions(epConfig *apitypes.EndpointSettings) ([]libnetwork.EndpointOption, error) {
663+
func joinOptions(endpoint *types.Endpoint) ([]libnetwork.EndpointOption, error) {
657664
var joinOptions []libnetwork.EndpointOption
658665
// TODO: parse endpoint's links
659666

667+
// set priority option
668+
joinOptions = append(joinOptions, libnetwork.JoinOptionPriority(nil, endpoint.Priority))
660669
return joinOptions, nil
661670
}

docs/features/pouch_with_plugin.md

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
# Pouch with plugin
22

3-
In order to run custom code provided by users which will be triggered at some point, we support a plugin framework which introduced from golang 1.8. At this time in this plugin framework we enable users to add custom code at four point:
3+
In order to run custom code provided by users which will be triggered at some point, we support a plugin framework which introduced from golang 1.8. At this time in this plugin framework we enable users to add custom code at file points:
44

55
* pre-start daemon point
66
* pre-stop daemon point
77
* pre-create container point
88
* pre-start container point
9+
* pre-create-endapoint container point
910

1011
Above four points are organized by two Plugin interfaces, which are DaemonPlugin and ContainerPlugin, defined as follow:
1112

@@ -21,15 +22,20 @@ type DaemonPlugin interface {
2122
PreStopHook() error
2223
}
2324
24-
// ContainerPlugin defines in which place a plugin will be triggered in container lifecycle
25+
// ContainerPlugin defines places where a plugin will be triggered in container lifecycle
2526
type ContainerPlugin interface {
26-
// PreCreate defines plugin point where receives an container create request, in this plugin point user
27-
// could change the container create body passed-in by http request body
28-
PreCreate(io.ReadCloser) (io.ReadCloser, error)
29-
30-
// PreStart returns an array of priority and args which will pass to runc, the every priority
31-
// used to sort the pre start array that pass to runc, network plugin hook always has priority value 0.
32-
PreStart(interface{}) ([]int, [][]string, error)
27+
// PreCreate defines plugin point where receives an container create request, in this plugin point user
28+
// could change the container create body passed-in by http request body
29+
PreCreate(io.ReadCloser) (io.ReadCloser, error)
30+
31+
// PreStart returns an array of priority and args which will pass to runc, the every priority
32+
// used to sort the pre start array that pass to runc, network plugin hook always has priority value 0.
33+
PreStart(interface{}) ([]int, [][]string, error)
34+
35+
//NetworkGenericParams accepts the container id and env of this container and returns the priority of this endpoint
36+
// and if this endpoint should enable resolver and a map which will be used as generic params to create endpoints of
37+
// this container
38+
PreCreateEndpoint(string, []string) (priority int, disableResolver bool, genericParam map[string]interface{})
3339
}
3440
3541
```
@@ -83,6 +89,11 @@ func (c ContPlugin) PreStart(interface{}) ([]int, [][]string, error) {
8389
return []int{-4}, [][]string{{"/usr/bin/touch", "touch", "/tmp/pre_start_hook"}}, nil
8490
}
8591
92+
func (c ContPlugin) PreCreateEndpoint(string, []string) (priority int, disableResolver bool, genericParam map[string]interface{}) {
93+
fmt.Println("pre create endpoint")
94+
return
95+
}
96+
8697
func main() {
8798
fmt.Println(ContainerPlugin, DaemonPlugin)
8899
}
@@ -130,7 +141,8 @@ and if you use the exact code above, every time you start a container the file a
130141

131142
## usage
132143

133-
at pre-start daemon point you can start assist processes like network plugins and dfget proxy which need by pouchd and whose life cycle is the same as pouchd.
134-
at pre-stop daemon point you can stop the assist processes gracefully, but the trigger of this point is not a promise, because pouchd may be killed by SIGKILL.
135-
at pre-create container point you can change the input stream by some rules, in some company they have some stale orchestration system who use env to pass-in some limit which is an attribute in pouch, then you can use this point to convert value in env to attribute in ContainerConfig or HostConfig of pouch create api.
136-
at pre-start container point you can set more pre-start hooks to oci spec, where you can do some special thing before container entrypoint start, priority decide the order of executing of the hook. libnetwork hook has priority 0, so if the hook is expected to run before network in container setup you should set priority to a value big then 0, and vice versa.
144+
* at pre-start daemon point you can start assist processes like network plugins and dfget proxy which need by pouchd and whose life cycle is the same as pouchd.
145+
* at pre-stop daemon point you can stop the assist processes gracefully, but the trigger of this point is not a promise, because pouchd may be killed by SIGKILL.
146+
* at pre-create container point you can change the input stream by some rules, in some company they have some stale orchestration system who use env to pass-in some limit which is an attribute in pouch, then you can use this point to convert value in env to attribute in ContainerConfig or HostConfig of pouch create api.
147+
* at pre-start container point you can set more pre-start hooks to oci spec, where you can do some special thing before container entrypoint start, priority decide the order of executing of the hook. libnetwork hook has priority 0, so if the hook is expected to run before network in container setup you should set priority to a value big then 0, and vice versa.
148+
* at pre-create-endpoint container point you can return the priority of this endpoint and if this endpoint need enable resolver and the generic params of this endpoint.

network/types/endpoint.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,8 @@ type Endpoint struct {
3131

3232
NetworkConfig *types.NetworkSettings
3333
EndpointConfig *types.EndpointSettings
34+
35+
GenericParams map[string]interface{}
36+
Priority int
37+
DisableResolver bool
3438
}

0 commit comments

Comments
 (0)