Skip to content

Commit 6bebcb7

Browse files
committed
feature: add plugin point of endpoint creating which enables users to
setup priority of endpoint and decide whether enable libnetwork resolver to this endpoint and setup generic params of this endpoint. Signed-off-by: Frank Yang <[email protected]>
1 parent 7a3a988 commit 6bebcb7

File tree

5 files changed

+50
-18
lines changed

5 files changed

+50
-18
lines changed

apis/plugins/ContainerPlugin.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,7 @@ 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 env of this container and returns the priority of this endpoint and if this endpoint should enable resolver and a map which will be used as generic params to create endpoints of this container
16+
PreCreateEndpoint([]string) (priority int, disableResolver bool, genericParam map[string]interface{})
1417
}

daemon/mgr/container.go

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

15261526
func (mgr *ContainerManager) buildContainerEndpoint(c *ContainerMeta) *networktypes.Endpoint {
1527-
return &networktypes.Endpoint{
1527+
ep := &networktypes.Endpoint{
15281528
Owner: c.ID,
15291529
Hostname: c.Config.Hostname,
15301530
Domainname: c.Config.Domainname,
@@ -1543,6 +1543,12 @@ func (mgr *ContainerManager) buildContainerEndpoint(c *ContainerMeta) *networkty
15431543
PortBindings: c.HostConfig.PortBindings,
15441544
NetworkConfig: c.NetworkSettings,
15451545
}
1546+
1547+
if mgr.containerPlugin != nil {
1548+
ep.Priority, ep.DisableResolver, ep.GenericParams = mgr.containerPlugin.PreCreateEndpoint(c.Config.Env)
1549+
}
1550+
1551+
return ep
15461552
}
15471553

15481554
// 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: 22 additions & 12 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,18 @@ 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)
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)
2930
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)
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 env of this container and returns the priority of this endpoint and if this endpoint should enable resolver and a map which will be used as generic params to create endpoints of this container
36+
PreCreateEndpoint([]string) (priority int, disableResolver bool, genericParam map[string]interface{})
3337
}
3438
3539
```
@@ -83,6 +87,11 @@ func (c ContPlugin) PreStart(interface{}) ([]int, [][]string, error) {
8387
return []int{-4}, [][]string{{"/usr/bin/touch", "touch", "/tmp/pre_start_hook"}}, nil
8488
}
8589
90+
func (c ContPlugin) PreCreateEndpoint([]string) (priority int, disableResolver bool, genericParam map[string]interface{}) {
91+
fmt.Println("pre create endpoint")
92+
return
93+
}
94+
8695
func main() {
8796
fmt.Println(ContainerPlugin, DaemonPlugin)
8897
}
@@ -130,7 +139,8 @@ and if you use the exact code above, every time you start a container the file a
130139

131140
## usage
132141

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.
142+
* 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.
143+
* 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.
144+
* 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.
145+
* 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.
146+
* 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)