Update OCICNI vendor to e617a611

Includes necessary changes for static IPs.

Signed-off-by: Matthew Heon <matthew.heon@gmail.com>
This commit is contained in:
Matthew Heon
2018-10-02 13:19:56 -04:00
parent 23c9816ba9
commit 6b80986194
3 changed files with 44 additions and 12 deletions

View File

@ -14,7 +14,7 @@ github.com/containers/image 7a1eac5d1df2dbd73d8b71853ebce32d989fcae3
github.com/containers/storage 41294c85d97bef688e18f710402895dbecde3308 github.com/containers/storage 41294c85d97bef688e18f710402895dbecde3308
github.com/containers/psgo 5dde6da0bc8831b35243a847625bcf18183bd1ee github.com/containers/psgo 5dde6da0bc8831b35243a847625bcf18183bd1ee
github.com/coreos/go-systemd v14 github.com/coreos/go-systemd v14
github.com/cri-o/ocicni master github.com/cri-o/ocicni e617a611e1755a5aa1014541d5074ff09352fe00
github.com/cyphar/filepath-securejoin v0.2.1 github.com/cyphar/filepath-securejoin v0.2.1
github.com/davecgh/go-spew v1.1.0 github.com/davecgh/go-spew v1.1.0
github.com/docker/distribution 7a8efe719e55bbfaff7bc5718cdf0ed51ca821df github.com/docker/distribution 7a8efe719e55bbfaff7bc5718cdf0ed51ca821df

View File

@ -3,6 +3,7 @@ package ocicni
import ( import (
"errors" "errors"
"fmt" "fmt"
"net"
"os" "os"
"path" "path"
"sort" "sort"
@ -351,14 +352,14 @@ func (plugin *cniNetworkPlugin) getNetwork(name string) (*cniNetwork, error) {
return net, nil return net, nil
} }
func (plugin *cniNetworkPlugin) getDefaultNetworkName() string { func (plugin *cniNetworkPlugin) GetDefaultNetworkName() string {
plugin.RLock() plugin.RLock()
defer plugin.RUnlock() defer plugin.RUnlock()
return plugin.defaultNetName return plugin.defaultNetName
} }
func (plugin *cniNetworkPlugin) getDefaultNetwork() *cniNetwork { func (plugin *cniNetworkPlugin) getDefaultNetwork() *cniNetwork {
defaultNetName := plugin.getDefaultNetworkName() defaultNetName := plugin.GetDefaultNetworkName()
if defaultNetName == "" { if defaultNetName == "" {
return nil return nil
} }
@ -383,7 +384,7 @@ func (plugin *cniNetworkPlugin) Name() string {
func (plugin *cniNetworkPlugin) forEachNetwork(podNetwork *PodNetwork, forEachFunc func(*cniNetwork, string, *PodNetwork) error) error { func (plugin *cniNetworkPlugin) forEachNetwork(podNetwork *PodNetwork, forEachFunc func(*cniNetwork, string, *PodNetwork) error) error {
networks := podNetwork.Networks networks := podNetwork.Networks
if len(networks) == 0 { if len(networks) == 0 {
networks = append(networks, plugin.getDefaultNetworkName()) networks = append(networks, plugin.GetDefaultNetworkName())
} }
for i, netName := range networks { for i, netName := range networks {
// Interface names start at "eth0" and count up for each network // Interface names start at "eth0" and count up for each network
@ -408,7 +409,7 @@ func (plugin *cniNetworkPlugin) SetUpPod(podNetwork PodNetwork) ([]cnitypes.Resu
plugin.podLock(podNetwork).Lock() plugin.podLock(podNetwork).Lock()
defer plugin.podUnlock(podNetwork) defer plugin.podUnlock(podNetwork)
_, err := plugin.loNetwork.addToNetwork(plugin.cacheDir, &podNetwork, "lo") _, err := plugin.loNetwork.addToNetwork(plugin.cacheDir, &podNetwork, "lo", "")
if err != nil { if err != nil {
logrus.Errorf("Error while adding to cni lo network: %s", err) logrus.Errorf("Error while adding to cni lo network: %s", err)
return nil, err return nil, err
@ -416,7 +417,12 @@ func (plugin *cniNetworkPlugin) SetUpPod(podNetwork PodNetwork) ([]cnitypes.Resu
results := make([]cnitypes.Result, 0) results := make([]cnitypes.Result, 0)
if err := plugin.forEachNetwork(&podNetwork, func(network *cniNetwork, ifName string, podNetwork *PodNetwork) error { if err := plugin.forEachNetwork(&podNetwork, func(network *cniNetwork, ifName string, podNetwork *PodNetwork) error {
result, err := network.addToNetwork(plugin.cacheDir, podNetwork, ifName) ip := ""
if conf, ok := podNetwork.NetworkConfig[network.name]; ok {
ip = conf.IP
}
result, err := network.addToNetwork(plugin.cacheDir, podNetwork, ifName, ip)
if err != nil { if err != nil {
logrus.Errorf("Error while adding pod to CNI network %q: %s", network.name, err) logrus.Errorf("Error while adding pod to CNI network %q: %s", network.name, err)
return err return err
@ -439,7 +445,12 @@ func (plugin *cniNetworkPlugin) TearDownPod(podNetwork PodNetwork) error {
defer plugin.podUnlock(podNetwork) defer plugin.podUnlock(podNetwork)
return plugin.forEachNetwork(&podNetwork, func(network *cniNetwork, ifName string, podNetwork *PodNetwork) error { return plugin.forEachNetwork(&podNetwork, func(network *cniNetwork, ifName string, podNetwork *PodNetwork) error {
if err := network.deleteFromNetwork(plugin.cacheDir, podNetwork, ifName); err != nil { ip := ""
if conf, ok := podNetwork.NetworkConfig[network.name]; ok {
ip = conf.IP
}
if err := network.deleteFromNetwork(plugin.cacheDir, podNetwork, ifName, ip); err != nil {
logrus.Errorf("Error while removing pod from CNI network %q: %s", network.name, err) logrus.Errorf("Error while removing pod from CNI network %q: %s", network.name, err)
return err return err
} }
@ -491,8 +502,8 @@ func (plugin *cniNetworkPlugin) GetPodNetworkStatus(podNetwork PodNetwork) ([]cn
return results, nil return results, nil
} }
func (network *cniNetwork) addToNetwork(cacheDir string, podNetwork *PodNetwork, ifName string) (cnitypes.Result, error) { func (network *cniNetwork) addToNetwork(cacheDir string, podNetwork *PodNetwork, ifName, ip string) (cnitypes.Result, error) {
rt, err := buildCNIRuntimeConf(cacheDir, podNetwork, ifName) rt, err := buildCNIRuntimeConf(cacheDir, podNetwork, ifName, ip)
if err != nil { if err != nil {
logrus.Errorf("Error adding network: %v", err) logrus.Errorf("Error adding network: %v", err)
return nil, err return nil, err
@ -509,8 +520,8 @@ func (network *cniNetwork) addToNetwork(cacheDir string, podNetwork *PodNetwork,
return res, nil return res, nil
} }
func (network *cniNetwork) deleteFromNetwork(cacheDir string, podNetwork *PodNetwork, ifName string) error { func (network *cniNetwork) deleteFromNetwork(cacheDir string, podNetwork *PodNetwork, ifName, ip string) error {
rt, err := buildCNIRuntimeConf(cacheDir, podNetwork, ifName) rt, err := buildCNIRuntimeConf(cacheDir, podNetwork, ifName, ip)
if err != nil { if err != nil {
logrus.Errorf("Error deleting network: %v", err) logrus.Errorf("Error deleting network: %v", err)
return err return err
@ -526,7 +537,7 @@ func (network *cniNetwork) deleteFromNetwork(cacheDir string, podNetwork *PodNet
return nil return nil
} }
func buildCNIRuntimeConf(cacheDir string, podNetwork *PodNetwork, ifName string) (*libcni.RuntimeConf, error) { func buildCNIRuntimeConf(cacheDir string, podNetwork *PodNetwork, ifName, ip string) (*libcni.RuntimeConf, error) {
logrus.Infof("Got pod network %+v", podNetwork) logrus.Infof("Got pod network %+v", podNetwork)
rt := &libcni.RuntimeConf{ rt := &libcni.RuntimeConf{
@ -542,6 +553,14 @@ func buildCNIRuntimeConf(cacheDir string, podNetwork *PodNetwork, ifName string)
}, },
} }
// Add requested static IP to CNI_ARGS
if ip != "" {
if tstIP := net.ParseIP(ip); tstIP == nil {
return nil, fmt.Errorf("unable to parse IP address %q", ip)
}
rt.Args = append(rt.Args, [2]string{"IP", ip})
}
if len(podNetwork.PortMappings) == 0 { if len(podNetwork.PortMappings) == 0 {
return rt, nil return rt, nil
} }

View File

@ -24,6 +24,14 @@ type PortMapping struct {
HostIP string `json:"hostIP"` HostIP string `json:"hostIP"`
} }
// NetworkConfig is additional configuration for a single CNI network.
type NetworkConfig struct {
// IP is a static IP to be specified in the network. Can only be used
// with the hostlocal IP allocator. If left unset, an IP will be
// dynamically allocated.
IP string
}
// PodNetwork configures the network of a pod sandbox. // PodNetwork configures the network of a pod sandbox.
type PodNetwork struct { type PodNetwork struct {
// Name is the name of the sandbox. // Name is the name of the sandbox.
@ -40,6 +48,11 @@ type PodNetwork struct {
// Networks is a list of CNI network names to attach to the sandbox // Networks is a list of CNI network names to attach to the sandbox
// Leave this list empty to attach the default network to the sandbox // Leave this list empty to attach the default network to the sandbox
Networks []string Networks []string
// NetworkConfig is configuration specific to a single CNI network.
// It is optional, and can be omitted for some or all specified networks
// without issue.
NetworkConfig map[string]NetworkConfig
} }
// CNIPlugin is the interface that needs to be implemented by a plugin // CNIPlugin is the interface that needs to be implemented by a plugin