forked from openshift/ovn-kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cni.go
186 lines (166 loc) · 5.23 KB
/
cni.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
package cni
import (
"encoding/json"
"fmt"
"net"
"time"
"github.com/sirupsen/logrus"
"github.com/containernetworking/cni/pkg/types/current"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/util/wait"
"github.com/openvswitch/ovn-kubernetes/go-controller/pkg/config"
"github.com/openvswitch/ovn-kubernetes/go-controller/pkg/kube"
"github.com/openvswitch/ovn-kubernetes/go-controller/pkg/util"
)
var minRsrc = resource.MustParse("1k")
var maxRsrc = resource.MustParse("1P")
func validateBandwidthIsReasonable(rsrc *resource.Quantity) error {
if rsrc.Value() < minRsrc.Value() {
return fmt.Errorf("resource is unreasonably small (< 1kbit)")
}
if rsrc.Value() > maxRsrc.Value() {
return fmt.Errorf("resoruce is unreasonably large (> 1Pbit)")
}
return nil
}
func extractPodBandwidthResources(podAnnotations map[string]string) (int64, int64, error) {
ingress := int64(-1)
egress := int64(-1)
str, found := podAnnotations["kubernetes.io/ingress-bandwidth"]
if found {
ingressVal, err := resource.ParseQuantity(str)
if err != nil {
return -1, -1, err
}
if err := validateBandwidthIsReasonable(&ingressVal); err != nil {
return -1, -1, err
}
ingress = ingressVal.Value()
}
str, found = podAnnotations["kubernetes.io/egress-bandwidth"]
if found {
egressVal, err := resource.ParseQuantity(str)
if err != nil {
return -1, -1, err
}
if err := validateBandwidthIsReasonable(&egressVal); err != nil {
return -1, -1, err
}
egress = egressVal.Value()
}
return ingress, egress, nil
}
func (pr *PodRequest) cmdAdd() *PodResult {
namespace := pr.PodNamespace
podName := pr.PodName
if namespace == "" || podName == "" {
logrus.Errorf("required CNI variable missing")
return nil
}
clientset, err := util.NewClientset(&config.Kubernetes)
if err != nil {
logrus.Errorf("Could not create clientset for kubernetes: %v", err)
return nil
}
kubecli := &kube.Kube{KClient: clientset}
// Get the IP address and MAC address from the API server.
// Exponential back off ~32 seconds + 7* t(api call)
var annotationBackoff = wait.Backoff{Duration: 1 * time.Second, Steps: 7, 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["ovn"]; ok {
return true, nil
}
return false, nil
}); err != nil {
logrus.Errorf("failed to get pod annotation - %v", err)
return nil
}
ovnAnnotation, ok := annotation["ovn"]
if !ok {
logrus.Errorf("failed to get ovn annotation from pod")
return nil
}
var ovnAnnotatedMap map[string]string
err = json.Unmarshal([]byte(ovnAnnotation), &ovnAnnotatedMap)
if err != nil {
logrus.Errorf("unmarshal ovn annotation failed")
return nil
}
ipAddress := ovnAnnotatedMap["ip_address"]
macAddress := ovnAnnotatedMap["mac_address"]
gatewayIP := ovnAnnotatedMap["gateway_ip"]
if ipAddress == "" || macAddress == "" || gatewayIP == "" {
logrus.Errorf("failed in pod annotation key extract")
return nil
}
ingress, egress, err := extractPodBandwidthResources(annotation)
if err != nil {
logrus.Errorf("failed to parse bandwidth request: %v", err)
return nil
}
var interfacesArray []*current.Interface
interfacesArray, err = pr.ConfigureInterface(namespace, podName, macAddress, ipAddress, gatewayIP, config.Default.MTU, ingress, egress)
if err != nil {
logrus.Errorf("Failed to configure interface in pod: %v", err)
return nil
}
// Build the result structure to pass back to the runtime
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"
}
result := ¤t.Result{
Interfaces: interfacesArray,
IPs: []*current.IPConfig{
{
Version: ipVersion,
Interface: current.Int(1),
Address: net.IPNet{IP: addr, Mask: addrNet.Mask},
Gateway: net.ParseIP(gatewayIP),
},
},
}
podResult := &PodResult{}
//versionedResult, _ := result.GetAsVersion(pr.CNIConf.CNIVersion)
podResult.Response, _ = json.Marshal(result)
return podResult
}
func (pr *PodRequest) cmdDel() *PodResult {
err := pr.PlatformSpecificCleanup()
if err != nil {
logrus.Error("Teardown error: %v", err)
}
return &PodResult{}
}
// HandleCNIRequest is the callback for all the requests
// coming to the cniserver after being procesed into PodRequest objects
// Argument '*PodRequest' encapsulates all the necessary information
// Return value is the actual bytes to be sent back without further processing.
func HandleCNIRequest(request *PodRequest) ([]byte, error) {
logrus.Infof("Dispatching pod network request %v", request)
var result *PodResult
switch request.Command {
case CNIAdd:
result = request.cmdAdd()
case CNIDel:
result = request.cmdDel()
default:
}
if result == nil {
return PodResult{}.Response, fmt.Errorf("Nil response to CNI request")
}
logrus.Infof("Returning pod network request %v, result %s err %v", request, string(result.Response), result.Err)
return result.Response, result.Err
}