@@ -501,7 +501,6 @@ func (e *CNIEnv) writeNetworkConfig(net *NetworkConfig) error {
501
501
502
502
// networkConfigList loads config from dir if dir exists.
503
503
func (e * CNIEnv ) networkConfigList () ([]* NetworkConfig , error ) {
504
- l := []* NetworkConfig {}
505
504
common , err := libcni .ConfFiles (e .NetconfPath , []string {".conf" , ".conflist" , ".json" })
506
505
if err != nil {
507
506
return nil , err
@@ -513,34 +512,53 @@ func (e *CNIEnv) networkConfigList() ([]*NetworkConfig, error) {
513
512
return nil , err
514
513
}
515
514
}
516
- fileNames := append (common , namespaced ... )
515
+ return cniLoad (append (common , namespaced ... ))
516
+ }
517
+
518
+ func wrapCNIError (fileName string , err error ) error {
519
+ return fmt .Errorf ("failed marshalling json out of network configuration file %q: %w\n " +
520
+ "For details on the schema, see https://pkg.go.dev/github.com/containernetworking/cni/libcni#NetworkConfigList" , fileName , err )
521
+ }
522
+
523
+ func cniLoad (fileNames []string ) (configList []* NetworkConfig , err error ) {
524
+ var fileName string
525
+
517
526
sort .Strings (fileNames )
518
- for _ , fileName := range fileNames {
519
- var lcl * libcni.NetworkConfigList
527
+
528
+ for _ , fileName = range fileNames {
529
+ var bytes []byte
530
+ bytes , err = os .ReadFile (fileName )
531
+ if err != nil {
532
+ return nil , fmt .Errorf ("error reading %s: %w" , fileName , err )
533
+ }
534
+
535
+ var netConfigList * libcni.NetworkConfigList
520
536
if strings .HasSuffix (fileName , ".conflist" ) {
521
- lcl , err = libcni .ConfListFromFile ( fileName )
537
+ netConfigList , err = libcni .ConfListFromBytes ( bytes )
522
538
if err != nil {
523
- return nil , err
539
+ return nil , wrapCNIError ( fileName , err )
524
540
}
525
541
} else {
526
- lc , err := libcni .ConfFromFile (fileName )
542
+ var netConfig * libcni.NetworkConfig
543
+ netConfig , err = libcni .ConfFromBytes (bytes )
527
544
if err != nil {
528
- return nil , err
545
+ return nil , wrapCNIError ( fileName , err )
529
546
}
530
- lcl , err = libcni .ConfListFromConf (lc )
547
+ netConfigList , err = libcni .ConfListFromConf (netConfig )
531
548
if err != nil {
532
- return nil , err
549
+ return nil , wrapCNIError ( fileName , err )
533
550
}
534
551
}
535
- id , lbls := nerdctlIDLabels (lcl .Bytes )
536
- l = append (l , & NetworkConfig {
537
- NetworkConfigList : lcl ,
552
+ id , nerdctlLabels := nerdctlIDLabels (netConfigList .Bytes )
553
+ configList = append (configList , & NetworkConfig {
554
+ NetworkConfigList : netConfigList ,
538
555
NerdctlID : id ,
539
- NerdctlLabels : lbls ,
556
+ NerdctlLabels : nerdctlLabels ,
540
557
File : fileName ,
541
558
})
542
559
}
543
- return l , nil
560
+
561
+ return configList , nil
544
562
}
545
563
546
564
func nerdctlIDLabels (b []byte ) (* string , * map [string ]string ) {
0 commit comments