aboutsummaryrefslogtreecommitdiffstats
path: root/cmd/ovn4nfvk8s-cni/ovn4nfvk8s-cni.go
blob: 7c90d029b1455c9cbd1e35d148ceaaf6c1f09bf2 (plain)
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// +build linux

package main

import (
	"encoding/json"
	"fmt"
	"net"
	"os"
	"strconv"
	"strings"
	"time"

	"github.com/sirupsen/logrus"
	"github.com/urfave/cli"

	"github.com/containernetworking/cni/pkg/skel"
	"github.com/containernetworking/cni/pkg/types"
	"github.com/containernetworking/cni/pkg/types/current"
	"github.com/containernetworking/cni/pkg/version"
	"k8s.io/apimachinery/pkg/util/wait"

	"ovn4nfv-k8s-plugin/internal/pkg/kube"

	"ovn4nfv-k8s-plugin/cmd/ovn4nfvk8s-cni/app"
	"ovn4nfv-k8s-plugin/internal/pkg/config"
)

func argString2Map(args string) (map[string]string, error) {
	argsMap := make(map[string]string)

	pairs := strings.Split(args, ";")
	for _, pair := range pairs {
		kv := strings.Split(pair, "=")
		if len(kv) != 2 {
			return nil, fmt.Errorf("ARGS: invalid pair %q", pair)
		}
		keyString := kv[0]
		valueString := kv[1]
		argsMap[keyString] = valueString
	}

	return argsMap, nil
}

func parseOvnNetworkObject(ovnnetwork string) ([]map[string]string, error) {
	var ovnNet []map[string]string

	if ovnnetwork == "" {
		return nil, fmt.Errorf("parseOvnNetworkObject:error")
	}

	if err := json.Unmarshal([]byte(ovnnetwork), &ovnNet); err != nil {
		return nil, fmt.Errorf("parseOvnNetworkObject: failed to load ovn network err: %v | ovn network: %v", err, ovnnetwork)
	}

	return ovnNet, nil
}

func mergeWithResult(srcObj, dstObj types.Result) (types.Result, error) {

	if dstObj == nil {
		return srcObj, nil
	}
	src, err := current.NewResultFromResult(srcObj)
	if err != nil {
		return nil, fmt.Errorf("Couldn't convert old result to current version: %v", err)
	}
	dst, err := current.NewResultFromResult(dstObj)
	if err != nil {
		return nil, fmt.Errorf("Couldn't convert old result to current version: %v", err)
	}

	ifacesLength := len(dst.Interfaces)

	for _, iface := range src.Interfaces {
		dst.Interfaces = append(dst.Interfaces, iface)
	}
	for _, ip := range src.IPs {
		if ip.Interface != nil && *(ip.Interface) != -1 {
			ip.Interface = current.Int(*(ip.Interface) + ifacesLength)
		}
		dst.IPs = append(dst.IPs, ip)
	}
	for _, route := range src.Routes {
		dst.Routes = append(dst.Routes, route)
	}

	for _, ns := range src.DNS.Nameservers {
		dst.DNS.Nameservers = append(dst.DNS.Nameservers, ns)
	}
	for _, s := range src.DNS.Search {
		dst.DNS.Search = append(dst.DNS.Search, s)
	}
	for _, opt := range src.DNS.Options {
		dst.DNS.Options = append(dst.DNS.Options, opt)
	}
	// TODO: what about DNS.domain?
	return dst, nil
}

func prettyPrint(i interface{}) string {
	s, _ := json.MarshalIndent(i, "", "\t")
	return string(s)
}

func addMultipleInterfaces(args *skel.CmdArgs, ovnAnnotation, namespace, podName string) types.Result {
	logrus.Infof("ovn4nfvk8s-cni: addMultipleInterfaces ")

	var ovnAnnotatedMap []map[string]string
	ovnAnnotatedMap, err := parseOvnNetworkObject(ovnAnnotation)
	if err != nil {
		logrus.Errorf("addLogicalPort : Error Parsing Ovn Network List %v", ovnAnnotatedMap)
		return nil
	}
	if namespace == "" || podName == "" {
		logrus.Errorf("required CNI variable missing")
		return nil
	}
	var interfacesArray []*current.Interface
	var index int
	var result *current.Result
	var dstResult types.Result
	for _, ovnNet := range ovnAnnotatedMap {
		ipAddress := ovnNet["ip_address"]
		macAddress := ovnNet["mac_address"]
		gatewayIP := ovnNet["gateway_ip"]
		defaultGateway := ovnNet["defaultGateway"]

		if ipAddress == "" || macAddress == "" {
			logrus.Errorf("failed in pod annotation key extract")
			return nil
		}

		index++
		interfaceName := ovnNet["interface"]
		if interfaceName == "" {
			logrus.Errorf("addMultipleInterfaces: interface can't be null")
			return nil
		}
		logrus.Debugf("addMultipleInterfaces: ipAddress %v %v", ipAddress, interfaceName)
		interfacesArray, err = app.ConfigureInterface(args, namespace, podName, macAddress, ipAddress, gatewayIP, interfaceName, defaultGateway, config.Default.MTU)
		if err != nil {
			logrus.Errorf("Failed to configure interface in pod: %v", err)
			return nil
		}
		addr, addrNet, err := net.ParseCIDR(ipAddress)
		if err != nil {
			logrus.Errorf("failed to parse IP address %q: %v", ipAddress, err)
			return nil
		}
		ipVersion := "6"
		if addr.To4() != nil {
			ipVersion = "4"
		}
		var routes types.Route
		if defaultGateway == "true" {
			defaultAddr, defaultAddrNet, _ := net.ParseCIDR("0.0.0.0/0")
			routes = types.Route{Dst: net.IPNet{IP: defaultAddr, Mask: defaultAddrNet.Mask}, GW: net.ParseIP(gatewayIP)}

			result = &current.Result{
				Interfaces: interfacesArray,
				IPs: []*current.IPConfig{
					{
						Version:   ipVersion,
						Interface: current.Int(1),
						Address:   net.IPNet{IP: addr, Mask: addrNet.Mask},
						Gateway:   net.ParseIP(gatewayIP),
					},
				},
				Routes: []*types.Route{&routes},
			}
		} else {
			result = &current.Result{
				Interfaces: interfacesArray,
				IPs: []*current.IPConfig{
					{
						Version:   ipVersion,
						Interface: current.Int(1),
						Address:   net.IPNet{IP: addr, Mask: addrNet.Mask},
						Gateway:   net.ParseIP(gatewayIP),
					},
				},
			}

		}
		// Build the result structure to pass back to the runtime
		dstResult, err = mergeWithResult(types.Result(result), dstResult)
		if err != nil {
			logrus.Errorf("Failed to merge results: %v", err)
			return nil
		}
	}
	logrus.Infof("addMultipleInterfaces:  %s", prettyPrint(dstResult))
	return dstResult
}

func cmdAdd(args *skel.CmdArgs) error {
	logrus.Infof("ovn4nfvk8s-cni: cmdAdd ")
	conf := &types.NetConf{}
	if err := json.Unmarshal(args.StdinData, conf); err != nil {
		return fmt.Errorf("failed to load netconf: %v", err)
	}

	argsMap, err := argString2Map(args.Args)
	if err != nil {
		return err
	}

	namespace := argsMap["K8S_POD_NAMESPACE"]
	podName := argsMap["K8S_POD_NAME"]
	if namespace == "" || podName == "" {
		return fmt.Errorf("required CNI variable missing")
	}

	clientset, err := config.NewClientset(&config.Kubernetes)
	if err != nil {
		return fmt.Errorf("Could not create clientset for kubernetes: %v", err)
	}
	kubecli := &kube.Kube{KClient: clientset}

	// Get the IP address and MAC address from the API server.
	var annotationBackoff = wait.Backoff{Duration: 1 * time.Second, Steps: 14, Factor: 1.5, Jitter: 0.1}
	var annotation map[string]string
	if err := wait.ExponentialBackoff(annotationBackoff, func() (bool, error) {
		annotation, err = kubecli.GetAnnotationsOnPod(namespace, podName)
		if err != nil {
			// TODO: check if err is non recoverable
			logrus.Warningf("Error while obtaining pod annotations - %v", err)
			return false, nil
		}
		if _, ok := annotation["ovnIfaceList"]; ok {
			return true, nil
		}
		return false, nil
	}); err != nil {
		return fmt.Errorf("failed to get pod annotation - %v", err)
	}
	logrus.Infof("ovn4nfvk8s-cni: Annotation Found ")
	ovnAnnotation, ok := annotation["ovnIfaceList"]
	if !ok {
		return fmt.Errorf("Error while obtaining pod annotations")
	}
	result := addMultipleInterfaces(args, ovnAnnotation, namespace, podName)
	return result.Print()
}

func cmdDel(args *skel.CmdArgs) error {
	logrus.Infof("ovn4nfvk8s-cni: cmdDel ")
	for i := 0; i < 10; i++ {
		ifaceName := args.ContainerID[:14] + strconv.Itoa(i)
		done, err := app.PlatformSpecificCleanup(ifaceName)
		if err != nil {
			logrus.Errorf("Teardown error: %v", err)
		}
		if done {
			break
		}
	}
	return nil
}

func main() {
	logrus.Infof("ovn4nfvk8s-cni CNI Invoked by Multus")
	c := cli.NewApp()
	c.Name = "ovn4nfvk8s-cni"
	c.Usage = "a CNI plugin to set up or tear down a additional interfaces with OVN"
	c.Version = "0.0.2"
	c.Flags = config.Flags

	c.Action = func(ctx *cli.Context) error {
		if _, err := config.InitConfig(ctx); err != nil {
			return err
		}

		skel.PluginMain(cmdAdd, cmdDel, version.All)
		return nil
	}

	if err := c.Run(os.Args); err != nil {
		// Print the error to stdout in conformance with the CNI spec
		e, ok := err.(*types.Error)
		if !ok {
			e = &types.Error{Code: 100, Msg: err.Error()}
		}
		e.Print()
	}
}