forked from cilium/cilium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
123 lines (105 loc) · 3.48 KB
/
types.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
// Copyright 2016-2019 Authors of Cilium
//
// 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 types
import (
"encoding/json"
"fmt"
"io/ioutil"
"net"
ciliumv2 "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2"
cniTypes "github.com/containernetworking/cni/pkg/types"
"github.com/containernetworking/cni/pkg/types/current"
"github.com/containernetworking/cni/pkg/version"
)
// NetConf is the Cilium specific CNI network configuration
type NetConf struct {
cniTypes.NetConf
MTU int `json:"mtu"`
Args Args `json:"args"`
ENI ciliumv2.ENISpec `json:"eni,omitempty"`
}
// NetConfList is a CNI chaining configuration
type NetConfList struct {
Plugins []*NetConf `json:"plugins,omitempty"`
}
func parsePrevResult(n *NetConf) (*NetConf, error) {
if n.RawPrevResult != nil {
resultBytes, err := json.Marshal(n.RawPrevResult)
if err != nil {
return nil, fmt.Errorf("could not serialize prevResult: %v", err)
}
res, err := version.NewResult(n.CNIVersion, resultBytes)
if err != nil {
return nil, fmt.Errorf("could not parse prevResult: %v", err)
}
n.PrevResult, err = current.NewResultFromResult(res)
if err != nil {
return nil, fmt.Errorf("could not convert result to current version: %v", err)
}
}
return n, nil
}
// ReadNetConf reads a CNI configuration file and returns the corresponding
// NetConf structure
func ReadNetConf(path string) (*NetConf, error) {
b, err := ioutil.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("Unable to read CNI configuration '%s': %s", path, err)
}
netConfList := &NetConfList{}
if err := json.Unmarshal(b, netConfList); err == nil {
for _, plugin := range netConfList.Plugins {
if plugin.Type == "cilium-cni" {
return parsePrevResult(plugin)
}
}
}
return LoadNetConf(b)
}
// LoadNetConf unmarshals a Cilium network configuration from JSON and returns
// a NetConf together with the CNI version
func LoadNetConf(bytes []byte) (*NetConf, error) {
n := &NetConf{}
if err := json.Unmarshal(bytes, n); err != nil {
return nil, fmt.Errorf("failed to load netconf: %s", err)
}
return parsePrevResult(n)
}
// ArgsSpec is the specification of additional arguments of the CNI ADD call
type ArgsSpec struct {
cniTypes.CommonArgs
IP net.IP
K8S_POD_NAME cniTypes.UnmarshallableString
K8S_POD_NAMESPACE cniTypes.UnmarshallableString
K8S_POD_INFRA_CONTAINER_ID cniTypes.UnmarshallableString
}
// Args contains arbitrary information a scheduler
// can pass to the cni plugin
type Args struct {
Mesos Mesos `json:"org.apache.mesos,omitempty"`
}
// Mesos contains network-specific information from the scheduler to the cni plugin
type Mesos struct {
NetworkInfo NetworkInfo `json:"network_info"`
}
// NetworkInfo supports passing only labels from mesos
type NetworkInfo struct {
Name string `json:"name"`
Labels struct {
Labels []struct {
Key string `json:"key"`
Value string `json:"value"`
} `json:"labels,omitempty"`
} `json:"labels,omitempty"`
}