forked from yamt/midonet-kubernetes
/
converter.go
120 lines (112 loc) · 3.29 KB
/
converter.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
// Copyright (C) 2018 Midokura SARL.
// 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.
package pod
import (
"fmt"
"net"
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
"k8s.io/api/core/v1"
"k8s.io/client-go/tools/cache"
"github.com/midonet/midonet-kubernetes/pkg/converter"
"github.com/midonet/midonet-kubernetes/pkg/midonet"
)
func idForKey(key string, config *converter.Config) uuid.UUID {
return converter.IDForKey("Pod", key, config)
}
type podConverter struct {
nodeGetter cache.KeyGetter
}
func newPodConverter(nodeInformer cache.SharedIndexInformer) converter.Converter {
return &podConverter{nodeInformer.GetIndexer()}
}
func (c *podConverter) Convert(key converter.Key, obj interface{}, config *converter.Config) ([]converter.BackendResource, converter.SubResourceMap, error) {
subs := make(converter.SubResourceMap)
clog := log.WithField("key", key)
baseID := idForKey(key.Key(), config)
bridgePortID := baseID
spec := obj.(*v1.Pod).Spec
meta := obj.(*v1.Pod).ObjectMeta
status := obj.(*v1.Pod).Status
nodeName := spec.NodeName
if nodeName == "" {
clog.Info("NodeName is not set")
return nil, nil, nil
}
if spec.HostNetwork {
clog.Debug("hostNetwork")
return nil, nil, nil
}
if status.Phase == v1.PodSucceeded || status.Phase == v1.PodFailed {
clog.Debug("Terminated pod")
return nil, nil, nil
}
bridgeID := converter.IDForKey("Node", nodeName, config)
nodeObj, exists, err := c.nodeGetter.GetByKey(nodeName)
if err != nil {
return nil, nil, err
}
if !exists {
return nil, nil, fmt.Errorf("node %s is not known yet", nodeName)
}
node := nodeObj.(*v1.Node)
hostID, err := uuid.Parse(node.ObjectMeta.Annotations[converter.HostIDAnnotation])
if err != nil {
// Retry later. Note: we don't listen Node events.
return nil, nil, err
}
res := []converter.BackendResource{
&midonet.Port{
Parent: midonet.Parent{ID: &bridgeID},
ID: &bridgePortID,
Type: "Bridge",
},
&midonet.HostInterfacePort{
Parent: midonet.Parent{ID: &hostID},
HostID: &hostID,
PortID: &bridgePortID,
InterfaceName: IFNameForKey(key.Key()),
},
}
macStr, exists := meta.Annotations[converter.MACAnnotation]
if exists {
mac, err := net.ParseMAC(macStr)
if err != nil {
return nil, nil, err
}
skey := converter.Key{
Kind: "Pod-MAC",
Name: fmt.Sprintf("%s/mac/%s", key.Name, DNSifyMAC(mac)),
}
subs[skey] = &PortMAC{
BridgeID: bridgeID,
PortID: bridgePortID,
MAC: mac,
}
ip := net.ParseIP(status.PodIP)
if ip != nil {
skey := converter.Key{
Kind: "Pod-ARP",
Name: fmt.Sprintf("%s/ip/%s/%s", key.Name, ip, DNSifyMAC(mac)),
}
subs[skey] = &PortARP{
BridgeID: bridgeID,
IP: ip,
MAC: mac,
}
}
}
return res, subs, nil
}