@@ -501,7 +501,6 @@ func (e *CNIEnv) writeNetworkConfig(net *NetworkConfig) error {
501501
502502// networkConfigList loads config from dir if dir exists.
503503func (e * CNIEnv ) networkConfigList () ([]* NetworkConfig , error ) {
504- l := []* NetworkConfig {}
505504 common , err := libcni .ConfFiles (e .NetconfPath , []string {".conf" , ".conflist" , ".json" })
506505 if err != nil {
507506 return nil , err
@@ -513,34 +512,53 @@ func (e *CNIEnv) networkConfigList() ([]*NetworkConfig, error) {
513512 return nil , err
514513 }
515514 }
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+
517526 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
520536 if strings .HasSuffix (fileName , ".conflist" ) {
521- lcl , err = libcni .ConfListFromFile ( fileName )
537+ netConfigList , err = libcni .ConfListFromBytes ( bytes )
522538 if err != nil {
523- return nil , err
539+ return nil , wrapCNIError ( fileName , err )
524540 }
525541 } else {
526- lc , err := libcni .ConfFromFile (fileName )
542+ var netConfig * libcni.NetworkConfig
543+ netConfig , err = libcni .ConfFromBytes (bytes )
527544 if err != nil {
528- return nil , err
545+ return nil , wrapCNIError ( fileName , err )
529546 }
530- lcl , err = libcni .ConfListFromConf (lc )
547+ netConfigList , err = libcni .ConfListFromConf (netConfig )
531548 if err != nil {
532- return nil , err
549+ return nil , wrapCNIError ( fileName , err )
533550 }
534551 }
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 ,
538555 NerdctlID : id ,
539- NerdctlLabels : lbls ,
556+ NerdctlLabels : nerdctlLabels ,
540557 File : fileName ,
541558 })
542559 }
543- return l , nil
560+
561+ return configList , nil
544562}
545563
546564func nerdctlIDLabels (b []byte ) (* string , * map [string ]string ) {
0 commit comments