Skip to content

Commit 513ef78

Browse files
author
Mrunal Patel
authored
Merge pull request #72 from dcbw/contexts
ocicni: add new functions that take a Context
2 parents b197cd1 + 7ea2e7d commit 513ef78

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

pkg/ocicni/ocicni.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,10 @@ func bringUpLoopback(netns string) error {
548548
}
549549

550550
func (plugin *cniNetworkPlugin) SetUpPod(podNetwork PodNetwork) ([]NetResult, error) {
551+
return plugin.SetUpPodWithContext(context.Background(), podNetwork)
552+
}
553+
554+
func (plugin *cniNetworkPlugin) SetUpPodWithContext(ctx context.Context, podNetwork PodNetwork) ([]NetResult, error) {
551555
if err := plugin.networksAvailable(&podNetwork); err != nil {
552556
return nil, err
553557
}
@@ -563,7 +567,7 @@ func (plugin *cniNetworkPlugin) SetUpPod(podNetwork PodNetwork) ([]NetResult, er
563567

564568
results := make([]NetResult, 0)
565569
if err := plugin.forEachNetwork(&podNetwork, false, func(network *cniNetwork, podNetwork *PodNetwork, rt *libcni.RuntimeConf) error {
566-
result, err := network.addToNetwork(rt, plugin.cniConfig)
570+
result, err := network.addToNetwork(ctx, rt, plugin.cniConfig)
567571
if err != nil {
568572
logrus.Errorf("Error while adding pod to CNI network %q: %s", network.name, err)
569573
return err
@@ -668,6 +672,10 @@ func tearDownLoopback(netns string) error {
668672
// TearDownPod tears down pod networks. Prefers cached pod attachment information
669673
// but falls back to given network attachment information.
670674
func (plugin *cniNetworkPlugin) TearDownPod(podNetwork PodNetwork) error {
675+
return plugin.TearDownPodWithContext(context.Background(), podNetwork)
676+
}
677+
678+
func (plugin *cniNetworkPlugin) TearDownPodWithContext(ctx context.Context, podNetwork PodNetwork) error {
671679
if len(podNetwork.Networks) == 0 {
672680
attachments, err := plugin.getCachedNetworkInfo(podNetwork.ID)
673681
if err == nil && len(attachments) > 0 {
@@ -688,7 +696,7 @@ func (plugin *cniNetworkPlugin) TearDownPod(podNetwork PodNetwork) error {
688696
}
689697

690698
return plugin.forEachNetwork(&podNetwork, true, func(network *cniNetwork, podNetwork *PodNetwork, rt *libcni.RuntimeConf) error {
691-
if err := network.deleteFromNetwork(rt, plugin.cniConfig); err != nil {
699+
if err := network.deleteFromNetwork(ctx, rt, plugin.cniConfig); err != nil {
692700
logrus.Errorf("Error while removing pod from CNI network %q: %s", network.name, err)
693701
return err
694702
}
@@ -718,6 +726,12 @@ func checkLoopback(netns string) error {
718726
// GetPodNetworkStatus returns IP addressing and interface details for all
719727
// networks attached to the pod.
720728
func (plugin *cniNetworkPlugin) GetPodNetworkStatus(podNetwork PodNetwork) ([]NetResult, error) {
729+
return plugin.GetPodNetworkStatusWithContext(context.Background(), podNetwork)
730+
}
731+
732+
// GetPodNetworkStatusWithContext returns IP addressing and interface details for all
733+
// networks attached to the pod.
734+
func (plugin *cniNetworkPlugin) GetPodNetworkStatusWithContext(ctx context.Context, podNetwork PodNetwork) ([]NetResult, error) {
721735
plugin.podLock(podNetwork).Lock()
722736
defer plugin.podUnlock(podNetwork)
723737

@@ -728,7 +742,7 @@ func (plugin *cniNetworkPlugin) GetPodNetworkStatus(podNetwork PodNetwork) ([]Ne
728742

729743
results := make([]NetResult, 0)
730744
if err := plugin.forEachNetwork(&podNetwork, true, func(network *cniNetwork, podNetwork *PodNetwork, rt *libcni.RuntimeConf) error {
731-
result, err := network.checkNetwork(rt, plugin.cniConfig, plugin.nsManager, podNetwork.NetNS)
745+
result, err := network.checkNetwork(ctx, rt, plugin.cniConfig, plugin.nsManager, podNetwork.NetNS)
732746
if err != nil {
733747
logrus.Errorf("Error while checking pod to CNI network %q: %s", network.name, err)
734748
return err
@@ -750,9 +764,9 @@ func (plugin *cniNetworkPlugin) GetPodNetworkStatus(podNetwork PodNetwork) ([]Ne
750764
return results, nil
751765
}
752766

753-
func (network *cniNetwork) addToNetwork(rt *libcni.RuntimeConf, cni *libcni.CNIConfig) (cnitypes.Result, error) {
767+
func (network *cniNetwork) addToNetwork(ctx context.Context, rt *libcni.RuntimeConf, cni *libcni.CNIConfig) (cnitypes.Result, error) {
754768
logrus.Infof("About to add CNI network %s (type=%v)", network.name, network.config.Plugins[0].Network.Type)
755-
res, err := cni.AddNetworkList(context.Background(), network.config, rt)
769+
res, err := cni.AddNetworkList(ctx, network.config, rt)
756770
if err != nil {
757771
logrus.Errorf("Error adding network: %v", err)
758772
return nil, err
@@ -761,7 +775,7 @@ func (network *cniNetwork) addToNetwork(rt *libcni.RuntimeConf, cni *libcni.CNIC
761775
return res, nil
762776
}
763777

764-
func (network *cniNetwork) checkNetwork(rt *libcni.RuntimeConf, cni *libcni.CNIConfig, nsManager *nsManager, netns string) (cnitypes.Result, error) {
778+
func (network *cniNetwork) checkNetwork(ctx context.Context, rt *libcni.RuntimeConf, cni *libcni.CNIConfig, nsManager *nsManager, netns string) (cnitypes.Result, error) {
765779
logrus.Infof("About to check CNI network %s (type=%v)", network.name, network.config.Plugins[0].Network.Type)
766780

767781
gtet, err := cniversion.GreaterThanOrEqualTo(network.config.CNIVersion, "0.4.0")
@@ -773,7 +787,7 @@ func (network *cniNetwork) checkNetwork(rt *libcni.RuntimeConf, cni *libcni.CNIC
773787

774788
// When CNIVersion supports Check, use it. Otherwise fall back on what was done initially.
775789
if gtet {
776-
err = cni.CheckNetworkList(context.Background(), network.config, rt)
790+
err = cni.CheckNetworkList(ctx, network.config, rt)
777791
logrus.Infof("Checking CNI network %s (config version=%v)", network.name, network.config.CNIVersion)
778792
if err != nil {
779793
logrus.Errorf("Error checking network: %v", err)
@@ -833,9 +847,9 @@ func (network *cniNetwork) checkNetwork(rt *libcni.RuntimeConf, cni *libcni.CNIC
833847
return converted, nil
834848
}
835849

836-
func (network *cniNetwork) deleteFromNetwork(rt *libcni.RuntimeConf, cni *libcni.CNIConfig) error {
850+
func (network *cniNetwork) deleteFromNetwork(ctx context.Context, rt *libcni.RuntimeConf, cni *libcni.CNIConfig) error {
837851
logrus.Infof("About to del CNI network %s (type=%v)", network.name, network.config.Plugins[0].Network.Type)
838-
if err := cni.DelNetworkList(context.Background(), network.config, rt); err != nil {
852+
if err := cni.DelNetworkList(ctx, network.config, rt); err != nil {
839853
logrus.Errorf("Error deleting network: %v", err)
840854
return err
841855
}

pkg/ocicni/types.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package ocicni
22

33
import (
4+
"context"
5+
46
"github.com/containernetworking/cni/pkg/types"
57
)
68

@@ -122,12 +124,21 @@ type CNIPlugin interface {
122124
// pod are launched.
123125
SetUpPod(network PodNetwork) ([]NetResult, error)
124126

127+
// SetUpPodWithContext is the same as SetUpPod but takes a context
128+
SetUpPodWithContext(ctx context.Context, network PodNetwork) ([]NetResult, error)
129+
125130
// TearDownPod is the method called before a pod's sandbox container will be deleted
126131
TearDownPod(network PodNetwork) error
127132

128-
// Status is the method called to obtain the ipv4 or ipv6 addresses of the pod sandbox
133+
// TearDownPodWithContext is the same as TearDownPod but takes a context
134+
TearDownPodWithContext(ctx context.Context, network PodNetwork) error
135+
136+
// GetPodNetworkStatus is the method called to obtain the ipv4 or ipv6 addresses of the pod sandbox
129137
GetPodNetworkStatus(network PodNetwork) ([]NetResult, error)
130138

139+
// GetPodNetworkStatusWithContext is the same as GetPodNetworkStatus but takes a context
140+
GetPodNetworkStatusWithContext(ctx context.Context, network PodNetwork) ([]NetResult, error)
141+
131142
// NetworkStatus returns error if the network plugin is in error state
132143
Status() error
133144

0 commit comments

Comments
 (0)