-
Notifications
You must be signed in to change notification settings - Fork 37
/
main.go
122 lines (97 loc) · 3.03 KB
/
main.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
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"github.com/genuinetools/netns/bridge"
"github.com/genuinetools/netns/network"
"github.com/genuinetools/netns/version"
"github.com/genuinetools/pkg/cli"
"github.com/opencontainers/runc/libcontainer/configs"
"github.com/sirupsen/logrus"
)
const (
defaultBridgeName = "netns0"
defaultBridgeIP = "172.19.0.1/16"
defaultStateDir = "/run/github.com/genuinetools/netns"
)
var (
ipfile string
staticip string
netOpt network.Opt
brOpt bridge.Opt
debug bool
client *network.Client
)
func main() {
// Create a new cli program.
p := cli.NewProgram()
p.Name = "netns"
p.Description = "Runc hook for setting up default bridge networking"
// Set the GitCommit and Version.
p.GitCommit = version.GITCOMMIT
p.Version = version.VERSION
// Build the list of available commands.
p.Commands = []cli.Command{
&createCommand{},
&listCommand{},
&removeCommand{},
}
// Setup the global flags.
p.FlagSet = flag.NewFlagSet("global", flag.ExitOnError)
p.FlagSet.StringVar(&ipfile, "ipfile", ".ip", "file in which to save the containers ip address")
p.FlagSet.StringVar(&netOpt.ContainerInterface, "iface", network.DefaultContainerInterface, "name of interface in the namespace")
p.FlagSet.StringVar(&netOpt.StateDir, "state-dir", defaultStateDir, "directory for saving state, used for ip allocation")
p.FlagSet.StringVar(&brOpt.Name, "bridge", defaultBridgeName, "name for bridge")
p.FlagSet.StringVar(&brOpt.IPAddr, "ip", defaultBridgeIP, "ip address for bridge")
p.FlagSet.IntVar(&brOpt.MTU, "mtu", bridge.DefaultMTU, "mtu for bridge")
p.FlagSet.BoolVar(&debug, "d", false, "enable debug logging")
p.FlagSet.StringVar(&staticip, "static-ip", "", "Enable static IP Address")
// Set the before function.
p.Before = func(ctx context.Context) error {
// Set the log level.
if debug {
logrus.SetLevel(logrus.DebugLevel)
}
netOpt.BridgeName = brOpt.Name
// Create the network client.
var err error
client, err = network.New(netOpt)
return err
}
// Set the main program action.
p.Action = func(ctx context.Context, args []string) error {
hook, err := readHookData()
if err != nil {
return err
}
ip, err := client.Create(hook, brOpt, staticip)
if err != nil {
return err
}
// Save the ip to a file so other hooks can use it.
if err := ioutil.WriteFile(ipfile, []byte(ip.String()), 0755); err != nil {
return fmt.Errorf("saving allocated ip address for container to %s failed: %v", ipfile, err)
}
return nil
}
// Run our program.
p.Run()
}
// readHookData decodes stdin as HookState.
func readHookData() (hook configs.HookState, err error) {
// Read hook data from stdin.
b, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return hook, fmt.Errorf("reading hook data from stdin failed: %v", err)
}
// Umarshal the hook state.
if err := json.Unmarshal(b, &hook); err != nil {
return hook, fmt.Errorf("unmarshaling stdin as HookState failed: %v", err)
}
logrus.Debugf("hooks state: %#v", hook)
return hook, nil
}