@@ -60,8 +60,17 @@ func (e *CNIEnv) ListNetworksMatch(reqs []string, allowPseudoNetwork bool) (list
6060
6161 list = make (map [string ][]* NetworkConfig )
6262 for _ , req := range reqs {
63- if ! allowPseudoNetwork && (req == "host" || req == "none" ) {
64- errs = append (errs , fmt .Errorf ("pseudo network not allowed: %s" , req ))
63+ if req == "host" || req == "none" {
64+ if ! allowPseudoNetwork {
65+ errs = append (errs , fmt .Errorf ("pseudo network not allowed: %s" , req ))
66+ continue
67+ }
68+ cfg , err := newPseudoNetworkConfig (req )
69+ if err != nil {
70+ errs = append (errs , err )
71+ continue
72+ }
73+ list [req ] = []* NetworkConfig {cfg }
6574 continue
6675 }
6776
@@ -88,6 +97,30 @@ func (e *CNIEnv) ListNetworksMatch(reqs []string, allowPseudoNetwork bool) (list
8897 return list , errs
8998}
9099
100+ func newPseudoNetworkConfig (name string ) (* NetworkConfig , error ) {
101+ confJSON , err := json .Marshal (& cniNetworkConfig {
102+ CNIVersion : "1.0.0" ,
103+ Name : name ,
104+ // Pseudo networks are not backed by real CNI config files. We still need a
105+ // parseable config object so network inspect can render them consistently.
106+ Plugins : []CNIPlugin {
107+ & pseudoNetworkPlugin {PluginType : "nerdctl-pseudo" },
108+ },
109+ })
110+ if err != nil {
111+ return nil , err
112+ }
113+
114+ confList , err := libcni .ConfListFromBytes (confJSON )
115+ if err != nil {
116+ return nil , err
117+ }
118+
119+ return & NetworkConfig {
120+ NetworkConfigList : confList ,
121+ }, nil
122+ }
123+
91124func UsedNetworks (ctx context.Context , client * containerd.Client ) (map [string ][]string , error ) {
92125 nsService := client .NamespaceService ()
93126 nsList , err := nsService .List (ctx )
@@ -288,8 +321,8 @@ type NetworkConfig struct {
288321type cniNetworkConfig struct {
289322 CNIVersion string `json:"cniVersion"`
290323 Name string `json:"name"`
291- ID string `json:"nerdctlID"`
292- Labels map [string ]string `json:"nerdctlLabels"`
324+ ID string `json:"nerdctlID,omitempty "`
325+ Labels map [string ]string `json:"nerdctlLabels,omitempty "`
293326 Plugins []CNIPlugin `json:"plugins"`
294327}
295328
0 commit comments