-
Notifications
You must be signed in to change notification settings - Fork 177
/
contivnet.go
188 lines (152 loc) · 4.47 KB
/
contivnet.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/***
Copyright 2014 Cisco Systems Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// this package is a simple binary to invoke netdcli to ensure
// it can be self sufficient to run as network plugin for Kubernetes
// networking replacement
package main
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"strings"
"github.com/contiv/netplugin/core"
"github.com/contiv/netplugin/netmaster/intent"
log "github.com/Sirupsen/logrus"
)
const (
logFile = "/tmp/xx"
tmpNetdcliFile = "/tmp/netdcli.cfg"
netdcliBin = "/root/go/bin/netdcli"
)
func getHostLabel() (string, error) {
cmdStr := "ps ef -C netplugin "
output, err := exec.Command("/bin/bash", "-c", cmdStr).Output()
if err != nil {
return "", err
}
fields := strings.Split(strings.TrimSpace(string(output)), " ")
for idx, field := range fields {
if field == "-host-label" && len(fields) > idx {
return fields[idx+1], nil
}
}
return "", core.Errorf("couldn't find host label")
}
func printUsage(arg0 string) {
usageString := "" +
arg0 + " init" + "\n" +
arg0 + " setup <pod-name> <pod-namespace> <infra-container-uuid>" +
"\n" +
arg0 + " teardown <pod-name> <pod-namespace> <infra-container-uuid>" +
"\n" +
arg0 + " help" + "\n"
log.Fatalf("Usage: \n%s\n", usageString)
}
func initPlugin() error {
log.Infof("initializing the driver \n")
return nil
}
func setUpPod(podNameSpace, podName, attachUUID string) error {
hostLabel, err := getHostLabel()
if err != nil {
log.Fatalf("error %s getting host label \n", err)
}
epCfg := []intent.ConfigEP{
{Host: hostLabel, Container: podName, AttachUUID: attachUUID}}
bytes, err := json.Marshal(epCfg)
if err != nil {
log.Errorf("error '%s' marshaling endpoint information \n", err)
return err
}
jsonStr := string(bytes)
jsonStr = strings.Replace(jsonStr, "\"", "\\\"", -1)
cmdStr := fmt.Sprintf("echo \"%s\" > %s", jsonStr, tmpNetdcliFile)
output, err := exec.Command("/bin/bash", "-c", cmdStr).Output()
if err != nil {
log.Errorf("error '%s' marshaling endpoint information output \n%s\n",
err, output)
return err
}
cmdStr = netdcliBin + " -host-bindings-cfg " + tmpNetdcliFile + " 2>&1"
output, err = exec.Command("/bin/bash", "-c", cmdStr).Output()
if err != nil {
log.Errorf("error '%s' executing host bindings, output \n%s\n",
err, output)
return err
}
return nil
}
func tearDownPod(podNameSpace, podName, attachUUID string) error {
hostLabel, err := getHostLabel()
if err != nil {
log.Fatalf("error %s getting host label \n", err)
}
epCfg := []intent.ConfigEP{
{Host: hostLabel, Container: podName}}
bytes, err := json.Marshal(epCfg)
if err != nil {
log.Errorf("error '%s' marshaling endpoint information \n", err)
return err
}
jsonStr := string(bytes)
jsonStr = strings.Replace(jsonStr, "\"", "\\\"", -1)
cmdStr := fmt.Sprintf("echo \"%s\" > %s", jsonStr, tmpNetdcliFile)
output, err := exec.Command("/bin/bash", "-c", cmdStr).Output()
if err != nil {
log.Errorf("error '%s' marshaling endpoint information output \n%s\n",
err, output)
return err
}
cmdStr = netdcliBin + " -host-bindings-cfg " + tmpNetdcliFile + " 2>&1"
output, err = exec.Command("/bin/bash", "-c", cmdStr).Output()
if err != nil {
log.Errorf("error '%s' executing host bindings, output \n%s\n",
err, output)
return err
}
return nil
}
func main() {
var err error
of, err := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err == nil {
log.SetOutput(of)
}
defer of.Close()
if len(os.Args) < 2 {
printUsage(os.Args[0])
}
log.Debugf("%s\n===============\n", os.Args)
switch os.Args[1] {
case "init":
err = initPlugin()
case "help":
printUsage(os.Args[0])
case "setup":
if len(os.Args) < 5 {
printUsage(os.Args[0])
}
err = setUpPod(os.Args[2], os.Args[3], os.Args[4])
case "teardown":
if len(os.Args) < 5 {
printUsage(os.Args[0])
}
err = tearDownPod(os.Args[2], os.Args[3], os.Args[4])
}
if err != nil {
log.Errorf("error '%s' executing %s \n", err, os.Args)
os.Exit(2)
}
os.Exit(0)
}