Skip to content

Commit cb48fad

Browse files
committed
support fixed-ip in anchor-cni
1 parent 451f1a2 commit cb48fad

4 files changed

Lines changed: 66 additions & 29 deletions

File tree

app/cnicmd.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ import (
99

1010
"github.com/opencontainers/runtime-spec/specs-go"
1111
"github.com/pkg/errors"
12+
log "github.com/sirupsen/logrus"
13+
"github.com/urfave/cli/v2"
14+
1215
"github.com/projecteru2/docker-cni/cni"
1316
"github.com/projecteru2/docker-cni/config"
1417
"github.com/projecteru2/docker-cni/handler"
15-
log "github.com/sirupsen/logrus"
16-
"github.com/urfave/cli/v2"
1718
)
1819

1920
func runCNI(handler handler.Handler) func(*cli.Context) error {
@@ -59,14 +60,15 @@ func runCNI(handler handler.Handler) func(*cli.Context) error {
5960
}
6061

6162
cniToolConfig := cni.CNIToolConfig{
62-
CNIPath: conf.CNIBinDir,
63-
NetConfPath: conf.CNIConfDir,
64-
NetNS: netns,
65-
Args: os.Getenv("CNI_ARGS"),
66-
IfName: conf.CNIIfname,
67-
Cmd: c.String("command"),
68-
ContainerID: state.ID,
69-
Handler: handler.HandleCNIConfig,
63+
CNIPath: conf.CNIBinDir,
64+
NetConfPath: conf.CNIConfDir,
65+
NetNS: netns,
66+
Args: os.Getenv("CNI_ARGS"),
67+
CapabilityArgs: os.Getenv("CAP_ARGS"),
68+
IfName: conf.CNIIfname,
69+
Cmd: c.String("command"),
70+
ContainerID: state.ID,
71+
Handler: handler.HandleCNIConfig,
7072
}
7173

7274
log.Infof("[hook] docker-cni running: %+v", cniToolConfig)

cni/cniitool.go renamed to cni/cnitool.go

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cni
33

44
import (
55
"context"
6+
"encoding/json"
67
"fmt"
78
"io/ioutil"
89
"path/filepath"
@@ -21,14 +22,15 @@ const (
2122

2223
// CNIToolConfig .
2324
type CNIToolConfig struct {
24-
CNIPath string `json:"cni_path"`
25-
NetConfPath string `json:"net_conf_path"`
26-
NetNS string `json:"net_ns"`
27-
Args string `json:"args"`
28-
IfName string `json:"if_name"`
29-
Cmd string `json:"cmd"`
30-
ContainerID string `json:"container_id"`
31-
Handler func([]byte) ([]byte, error)
25+
CNIPath string `json:"cni_path"`
26+
NetConfPath string `json:"net_conf_path"`
27+
NetNS string `json:"net_ns"`
28+
Args string `json:"args"`
29+
CapabilityArgs string `json:"capability_args"`
30+
IfName string `json:"if_name"`
31+
Cmd string `json:"cmd"`
32+
ContainerID string `json:"container_id"`
33+
Handler func([]byte) ([]byte, error)
3234
}
3335

3436
func parseArgs(args string) ([][2]string, error) {
@@ -144,13 +146,21 @@ func Run(config CNIToolConfig) error {
144146
}
145147
}
146148

149+
var capabilityArgs map[string]interface{}
150+
if len(config.CapabilityArgs) > 0 {
151+
if err = json.Unmarshal([]byte(config.CapabilityArgs), &capabilityArgs); err != nil {
152+
return err
153+
}
154+
}
155+
147156
cninet := libcni.NewCNIConfig(filepath.SplitList(config.CNIPath), nil)
148157

149158
rt := &libcni.RuntimeConf{
150-
ContainerID: config.ContainerID,
151-
NetNS: config.NetNS,
152-
IfName: config.IfName,
153-
Args: cniArgs,
159+
ContainerID: config.ContainerID,
160+
NetNS: config.NetNS,
161+
IfName: config.IfName,
162+
Args: cniArgs,
163+
CapabilityArgs: capabilityArgs,
154164
}
155165

156166
switch config.Cmd {

handler/cni/create.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package cni
22

33
import (
4+
"encoding/json"
5+
"fmt"
46
"strings"
57

68
"github.com/projecteru2/docker-cni/config"
@@ -19,16 +21,29 @@ func (h *CNIHandler) HandleCreate(conf config.Config, containerMeta *oci.Contain
1921

2022
func (h *CNIHandler) AddCNIStartHook(conf config.Config, containerMeta *oci.ContainerMeta) (err error) {
2123
env := []string{}
22-
cniArgs := []string{"IgnoreUnknown=true"}
24+
cniArgs := []string{
25+
"IgnoreUnknown=true",
26+
"K8S_POD_NAMESPACE=default",
27+
fmt.Sprintf("K8S_POD_NAME=%s", containerMeta.ID),
28+
}
2329
if containerMeta.RequiresSpecificIPPool() {
2430
cniArgs = append(cniArgs, "IPPOOL="+containerMeta.SpecificIPPool())
2531
}
2632
if containerMeta.RequiresSpecificIP() {
2733
cniArgs = append(cniArgs, "IP="+containerMeta.SpecificIP())
2834
}
29-
if len(cniArgs) != 0 {
30-
env = append(env, "CNI_ARGS="+strings.Join(cniArgs, ";"))
35+
env = append(env, "CNI_ARGS="+strings.Join(cniArgs, ";"))
36+
37+
if containerMeta.RequiresFixedIP() {
38+
capArgs := map[string]map[string]string{
39+
"io.kubernetes.cri.pod-annotations": {
40+
"shopee.com/cni.ip-mod": "static",
41+
},
42+
}
43+
capArgsJson, _ := json.Marshal(capArgs)
44+
env = append(env, "CAP_ARGS="+string(capArgsJson))
3145
}
46+
3247
containerMeta.AppendHook("prestart",
3348
conf.BinPathname,
3449
[]string{conf.BinPathname, "cni", "--config", conf.Filename, "--command", "add"}, // args

oci/container.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (c *ContainerMeta) Save() (err error) {
3434
return errors.WithStack(ioutil.WriteFile(c.BundlePath, data, 0644))
3535
}
3636

37-
func (c ContainerMeta) SpecificIP() string {
37+
func (c *ContainerMeta) SpecificIP() string {
3838
for _, env := range c.Process.Env {
3939
parts := strings.Split(env, "=")
4040
if len(parts) == 2 && parts[0] == "IPV4" && parts[1] != "" {
@@ -44,11 +44,11 @@ func (c ContainerMeta) SpecificIP() string {
4444
return ""
4545
}
4646

47-
func (c ContainerMeta) RequiresSpecificIP() bool {
47+
func (c *ContainerMeta) RequiresSpecificIP() bool {
4848
return c.SpecificIP() != ""
4949
}
5050

51-
func (c ContainerMeta) SpecificIPPool() string {
51+
func (c *ContainerMeta) SpecificIPPool() string {
5252
for _, env := range c.Process.Env {
5353
parts := strings.Split(env, "=")
5454
if len(parts) == 2 && parts[0] == "IPPOOL" && parts[1] != "" {
@@ -58,6 +58,16 @@ func (c ContainerMeta) SpecificIPPool() string {
5858
return ""
5959
}
6060

61-
func (c ContainerMeta) RequiresSpecificIPPool() bool {
61+
func (c *ContainerMeta) RequiresSpecificIPPool() bool {
6262
return c.SpecificIPPool() != ""
6363
}
64+
65+
func (c *ContainerMeta) RequiresFixedIP() bool {
66+
for _, env := range c.Process.Env {
67+
parts := strings.Split(env, "=")
68+
if len(parts) == 2 && parts[0] == "FIXED_IP" && parts[1] != "0" {
69+
return true
70+
}
71+
}
72+
return false
73+
}

0 commit comments

Comments
 (0)