github.com/looshlee/cilium@v1.6.12/plugins/cilium-cni/types/types.go (about) 1 // Copyright 2016-2019 Authors of Cilium 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package types 16 17 import ( 18 "encoding/json" 19 "fmt" 20 "io/ioutil" 21 "net" 22 23 ciliumv2 "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2" 24 25 cniTypes "github.com/containernetworking/cni/pkg/types" 26 "github.com/containernetworking/cni/pkg/types/current" 27 "github.com/containernetworking/cni/pkg/version" 28 ) 29 30 // NetConf is the Cilium specific CNI network configuration 31 type NetConf struct { 32 cniTypes.NetConf 33 MTU int `json:"mtu"` 34 Args Args `json:"args"` 35 ENI ciliumv2.ENISpec `json:"eni,omitempty"` 36 } 37 38 // NetConfList is a CNI chaining configuration 39 type NetConfList struct { 40 Plugins []*NetConf `json:"plugins,omitempty"` 41 } 42 43 func parsePrevResult(n *NetConf) (*NetConf, error) { 44 if n.RawPrevResult != nil { 45 resultBytes, err := json.Marshal(n.RawPrevResult) 46 if err != nil { 47 return nil, fmt.Errorf("could not serialize prevResult: %v", err) 48 } 49 res, err := version.NewResult(n.CNIVersion, resultBytes) 50 if err != nil { 51 return nil, fmt.Errorf("could not parse prevResult: %v", err) 52 } 53 n.PrevResult, err = current.NewResultFromResult(res) 54 if err != nil { 55 return nil, fmt.Errorf("could not convert result to current version: %v", err) 56 } 57 } 58 59 return n, nil 60 } 61 62 // ReadNetConf reads a CNI configuration file and returns the corresponding 63 // NetConf structure 64 func ReadNetConf(path string) (*NetConf, error) { 65 b, err := ioutil.ReadFile(path) 66 if err != nil { 67 return nil, fmt.Errorf("Unable to read CNI configuration '%s': %s", path, err) 68 } 69 70 netConfList := &NetConfList{} 71 if err := json.Unmarshal(b, netConfList); err == nil { 72 for _, plugin := range netConfList.Plugins { 73 if plugin.Type == "cilium-cni" { 74 return parsePrevResult(plugin) 75 } 76 } 77 } 78 79 return LoadNetConf(b) 80 } 81 82 // LoadNetConf unmarshals a Cilium network configuration from JSON and returns 83 // a NetConf together with the CNI version 84 func LoadNetConf(bytes []byte) (*NetConf, error) { 85 n := &NetConf{} 86 if err := json.Unmarshal(bytes, n); err != nil { 87 return nil, fmt.Errorf("failed to load netconf: %s", err) 88 } 89 90 return parsePrevResult(n) 91 92 } 93 94 // ArgsSpec is the specification of additional arguments of the CNI ADD call 95 type ArgsSpec struct { 96 cniTypes.CommonArgs 97 IP net.IP 98 K8S_POD_NAME cniTypes.UnmarshallableString 99 K8S_POD_NAMESPACE cniTypes.UnmarshallableString 100 K8S_POD_INFRA_CONTAINER_ID cniTypes.UnmarshallableString 101 } 102 103 // Args contains arbitrary information a scheduler 104 // can pass to the cni plugin 105 type Args struct { 106 Mesos Mesos `json:"org.apache.mesos,omitempty"` 107 } 108 109 // Mesos contains network-specific information from the scheduler to the cni plugin 110 type Mesos struct { 111 NetworkInfo NetworkInfo `json:"network_info"` 112 } 113 114 // NetworkInfo supports passing only labels from mesos 115 type NetworkInfo struct { 116 Name string `json:"name"` 117 Labels struct { 118 Labels []struct { 119 Key string `json:"key"` 120 Value string `json:"value"` 121 } `json:"labels,omitempty"` 122 } `json:"labels,omitempty"` 123 }