Skip to content

Commit 2811251

Browse files
author
Skyler Clark
committed
fixes log level, and adds a few tests
Signed-off-by: Skyler Clark <[email protected]>
1 parent 4841092 commit 2811251

File tree

2 files changed

+33
-20
lines changed

2 files changed

+33
-20
lines changed

pkg/ocicni/ocicni.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,12 @@ func newWatcher(confDir string) (*fsnotify.Watcher, error) {
124124
// Ensure plugin directory exists, because the following monitoring logic
125125
// relies on that.
126126
if err := os.MkdirAll(confDir, 0755); err != nil {
127-
return nil, fmt.Errorf("failed to create %q: %v", confDir, err)
127+
return nil, fmt.Errorf("failed to create directory %q: %v", confDir, err)
128128
}
129129

130130
watcher, err := fsnotify.NewWatcher()
131131
if err != nil {
132-
return nil, fmt.Errorf("could not create new watcher %v", err)
132+
return nil, fmt.Errorf("failed to create new watcher %v", err)
133133
}
134134
defer func() {
135135
// Close watcher on error
@@ -278,13 +278,13 @@ func loadNetworks(confDir string, cni *libcni.CNIConfig) (map[string]*cniNetwork
278278
if strings.HasSuffix(confFile, ".conflist") {
279279
confList, err = libcni.ConfListFromFile(confFile)
280280
if err != nil {
281-
logrus.Warningf("Error loading CNI config list file %s: %v", confFile, err)
281+
logrus.Errorf("Error loading CNI config list file %s: %v", confFile, err)
282282
continue
283283
}
284284
} else {
285285
conf, err := libcni.ConfFromFile(confFile)
286286
if err != nil {
287-
logrus.Warningf("Error loading CNI config file %s: %v", confFile, err)
287+
logrus.Errorf("Error loading CNI config file %s: %v", confFile, err)
288288
continue
289289
}
290290
if conf.Network.Type == "" {
@@ -293,7 +293,7 @@ func loadNetworks(confDir string, cni *libcni.CNIConfig) (map[string]*cniNetwork
293293
}
294294
confList, err = libcni.ConfListFromConf(conf)
295295
if err != nil {
296-
logrus.Warningf("Error converting CNI config file %s to list: %v", confFile, err)
296+
logrus.Errorf("Error converting CNI config file %s to list: %v", confFile, err)
297297
continue
298298
}
299299
}
@@ -324,7 +324,7 @@ func loadNetworks(confDir string, cni *libcni.CNIConfig) (map[string]*cniNetwork
324324
if _, ok := networks[confList.Name]; !ok {
325325
networks[confList.Name] = cniNet
326326
} else {
327-
logrus.Infof("Ignore CNI network %s (type=%v) at %s because already exists", confList.Name, confList.Plugins[0].Network.Type, confFile)
327+
logrus.Infof("Ignored CNI network %s (type=%v) at %s because already exists", confList.Name, confList.Plugins[0].Network.Type, confFile)
328328
}
329329

330330
if defaultNetName == "" {
@@ -351,9 +351,9 @@ func (plugin *cniNetworkPlugin) syncNetworkConfig() error {
351351
// Update defaultNetName if it is changeable
352352
if plugin.defaultNetName.changeable {
353353
plugin.defaultNetName.name = defaultNetName
354-
logrus.Infof("Update default CNI network name to %s", defaultNetName)
354+
logrus.Infof("Updated default CNI network name to %s", defaultNetName)
355355
} else {
356-
logrus.Warnf("Default CNI network name %s is unchangeable", plugin.defaultNetName.name)
356+
logrus.Warningf("Default CNI network name %s is unchangeable", plugin.defaultNetName.name)
357357
}
358358

359359
plugin.networks = networks
@@ -482,8 +482,8 @@ func (plugin *cniNetworkPlugin) forEachNetwork(podNetwork *PodNetwork, fromCache
482482
var newRt *libcni.RuntimeConf
483483
cniNet, newRt, err = plugin.loadNetworkFromCache(network.Name, rt)
484484
if err != nil {
485-
logrus.Debugf("error loading cached network config: %v", err)
486-
logrus.Debugf("falling back to loading from existing plugins on disk")
485+
logrus.Errorf("error loading cached network config: %v", err)
486+
logrus.Warningf("falling back to loading from existing plugins on disk")
487487
} else {
488488
// Use the updated RuntimeConf
489489
rt = newRt
@@ -519,10 +519,10 @@ func bringUpLoopback(netns string) error {
519519
return err
520520
}
521521
if len(v4Addrs) != 0 {
522-
// sanity check that this is a loopback address
522+
// check that this is a loopback address
523523
for _, addr := range v4Addrs {
524524
if !addr.IP.IsLoopback() {
525-
return fmt.Errorf("loopback interface found with non-loopback address %q", addr.IP)
525+
return fmt.Warningf("loopback interface found with non-loopback address %q", addr.IP)
526526
}
527527
}
528528
}
@@ -532,10 +532,10 @@ func bringUpLoopback(netns string) error {
532532
return err
533533
}
534534
if len(v6Addrs) != 0 {
535-
// sanity check that this is a loopback address
535+
// check that this is a loopback address
536536
for _, addr := range v6Addrs {
537537
if !addr.IP.IsLoopback() {
538-
return fmt.Errorf("loopback interface found with non-loopback address %q", addr.IP)
538+
return fmt.Warningf("loopback interface found with non-loopback address %q", addr.IP)
539539
}
540540
}
541541
}
@@ -616,7 +616,7 @@ func (plugin *cniNetworkPlugin) getCachedNetworkInfo(containerID string) ([]NetA
616616
cacheFile := filepath.Join(dirPath, fname)
617617
bytes, err := ioutil.ReadFile(cacheFile)
618618
if err != nil {
619-
logrus.Warningf("failed to read CNI cache file %s: %v", cacheFile, err)
619+
logrus.Errorf("failed to read CNI cache file %s: %v", cacheFile, err)
620620
continue
621621
}
622622

@@ -628,7 +628,7 @@ func (plugin *cniNetworkPlugin) getCachedNetworkInfo(containerID string) ([]NetA
628628
}{}
629629

630630
if err := json.Unmarshal(bytes, &cachedInfo); err != nil {
631-
logrus.Warningf("failed to unmarshal CNI cache file %s: %v", cacheFile, err)
631+
logrus.Errorf("failed to unmarshal CNI cache file %s: %v", cacheFile, err)
632632
continue
633633
}
634634
if cachedInfo.Kind != libcni.CNICacheV1 {
@@ -692,7 +692,7 @@ func (plugin *cniNetworkPlugin) TearDownPodWithContext(ctx context.Context, podN
692692

693693
if err := tearDownLoopback(podNetwork.NetNS); err != nil {
694694
// ignore error
695-
logrus.Errorf("Ignoring error tearing down loopback interface: %v", err)
695+
logrus.Warningf("Ignoring error tearing down loopback interface: %v", err)
696696
}
697697

698698
return plugin.forEachNetwork(&podNetwork, true, func(network *cniNetwork, podNetwork *PodNetwork, rt *libcni.RuntimeConf) error {
@@ -713,7 +713,7 @@ func checkLoopback(netns string) error {
713713
}
714714

715715
if link.Attrs().Flags&net.FlagUp != net.FlagUp {
716-
return fmt.Errorf("loopback interface is down")
716+
return fmt.Warningf("loopback interface is down")
717717
}
718718

719719
return nil
@@ -797,7 +797,7 @@ func (network *cniNetwork) checkNetwork(ctx context.Context, rt *libcni.RuntimeC
797797

798798
result, err = cni.GetNetworkListCachedResult(network.config, rt)
799799
if err != nil {
800-
logrus.Errorf("Error GetNetworkListCachedResult: %v", err)
800+
logrus.Errorf("Error getting network list cached result: %v", err)
801801
return nil, err
802802
} else if result != nil {
803803
return result, nil
@@ -850,7 +850,7 @@ func (network *cniNetwork) checkNetwork(ctx context.Context, rt *libcni.RuntimeC
850850
func (network *cniNetwork) deleteFromNetwork(ctx context.Context, rt *libcni.RuntimeConf, cni *libcni.CNIConfig) error {
851851
logrus.Infof("About to del CNI network %s (type=%v)", network.name, network.config.Plugins[0].Network.Type)
852852
if err := cni.DelNetworkList(ctx, network.config, rt); err != nil {
853-
logrus.Errorf("Error deleting network: %v", err)
853+
logrus.Errorf("Error deleting network %s: %v", network.name, err)
854854
return err
855855
}
856856
return nil

pkg/ocicni/ocicni_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,19 @@ var _ = Describe("ocicni operations", func() {
758758
Expect(err).NotTo(HaveOccurred())
759759
Expect(fake.delIndex).To(Equal(len(fake.plugins)))
760760
})
761+
It("verifies that network operations can be locked for a pod using cached networks", func() {
762+
podNet.Networks = []NetAttachment{}
763+
tmp := ocicni.(*cniNetworkPlugin)
764+
Expect(len(tmp.pods)).To(Equal(0))
765+
tmp.podLock(podNet)
766+
Expect(len(tmp.pods)).To(Equal(1))
767+
})
768+
It("verifies that network operations can be unlocked for a pod using cached networks", func() {
769+
podNet.Networks = []NetAttachment{}
770+
tmp := ocicni.(*cniNetworkPlugin)
771+
tmp.podUnlock(podNet)
772+
Expect(len(tmp.pods)).To(Equal(0))
773+
})
761774
})
762775

763776
It("tears down a pod using specified networks when cached info is missing", func() {

0 commit comments

Comments
 (0)