@@ -35,6 +35,7 @@ import (
3535 "github.com/containerd/nerdctl/pkg/rootlessutil"
3636 "github.com/containerd/nerdctl/pkg/strutil"
3737 "github.com/opencontainers/runtime-spec/specs-go"
38+ "github.com/sirupsen/logrus"
3839 "github.com/spf13/cobra"
3940)
4041
@@ -104,21 +105,29 @@ func withCustomHosts(src string) func(context.Context, oci.Client, *containers.C
104105 }
105106}
106107
107- func generateNetOpts (cmd * cobra.Command , dataStore , stateDir , ns , id string ) ([]oci.SpecOpts , []string , []gocni.PortMapping , error ) {
108+ func generateNetOpts (cmd * cobra.Command , dataStore , stateDir , ns , id string ) ([]oci.SpecOpts , []string , string , []gocni.PortMapping , error ) {
108109 opts := []oci.SpecOpts {}
109110 portSlice , err := cmd .Flags ().GetStringSlice ("publish" )
110111 if err != nil {
111- return nil , nil , nil , err
112+ return nil , nil , "" , nil , err
113+ }
114+ ipAddress , err := cmd .Flags ().GetString ("ip" )
115+ if err != nil {
116+ return nil , nil , "" , nil , err
112117 }
113118 netSlice , err := getNetworkSlice (cmd )
114119 if err != nil {
115- return nil , nil , nil , err
120+ return nil , nil , "" , nil , err
121+ }
122+
123+ if (netSlice == nil || len (netSlice ) == 0 ) && (ipAddress != "" ) {
124+ logrus .Warnf ("You have assign an IP address %s but no network, So we will use the default network" , ipAddress )
116125 }
117126
118127 ports := make ([]gocni.PortMapping , 0 )
119128 netType , err := nettype .Detect (netSlice )
120129 if err != nil {
121- return nil , nil , nil , err
130+ return nil , nil , "" , nil , err
122131 }
123132
124133 switch netType {
@@ -131,44 +140,44 @@ func generateNetOpts(cmd *cobra.Command, dataStore, stateDir, ns, id string) ([]
131140 // The actual network is configured in the oci hook.
132141 cniPath , err := cmd .Flags ().GetString ("cni-path" )
133142 if err != nil {
134- return nil , nil , nil , err
143+ return nil , nil , "" , nil , err
135144 }
136145 cniNetconfpath , err := cmd .Flags ().GetString ("cni-netconfpath" )
137146 if err != nil {
138- return nil , nil , nil , err
147+ return nil , nil , "" , nil , err
139148 }
140149 e , err := netutil .NewCNIEnv (cniPath , cniNetconfpath )
141150 if err != nil {
142- return nil , nil , nil , err
151+ return nil , nil , "" , nil , err
143152 }
144153 netMap := e .NetworkMap ()
145154 for _ , netstr := range netSlice {
146155 _ , ok := netMap [netstr ]
147156 if ! ok {
148- return nil , nil , nil , fmt .Errorf ("network %s not found" , netstr )
157+ return nil , nil , "" , nil , fmt .Errorf ("network %s not found" , netstr )
149158 }
150159 }
151160
152161 resolvConfPath := filepath .Join (stateDir , "resolv.conf" )
153162 dnsValue , err := cmd .Flags ().GetStringSlice ("dns" )
154163 if err != nil {
155- return nil , nil , nil , err
164+ return nil , nil , "" , nil , err
156165 }
157166 if runtime .GOOS == "linux" {
158167 conf , err := resolvconf .Get ()
159168 if err != nil {
160- return nil , nil , nil , err
169+ return nil , nil , "" , nil , err
161170 }
162171 slirp4Dns := []string {}
163172 if rootlessutil .IsRootlessChild () {
164173 slirp4Dns , err = dnsutil .GetSlirp4netnsDns ()
165174 if err != nil {
166- return nil , nil , nil , err
175+ return nil , nil , "" , nil , err
167176 }
168177 }
169178 conf , err = resolvconf .FilterResolvDNS (conf .Content , true )
170179 if err != nil {
171- return nil , nil , nil , err
180+ return nil , nil , "" , nil , err
172181 }
173182 searchDomains := resolvconf .GetSearchDomains (conf .Content )
174183 dnsOptions := resolvconf .GetOptions (conf .Content )
@@ -177,25 +186,25 @@ func generateNetOpts(cmd *cobra.Command, dataStore, stateDir, ns, id string) ([]
177186 nameServers = resolvconf .GetNameservers (conf .Content , resolvconf .IPv4 )
178187 }
179188 if _ , err := resolvconf .Build (resolvConfPath , append (slirp4Dns , nameServers ... ), searchDomains , dnsOptions ); err != nil {
180- return nil , nil , nil , err
189+ return nil , nil , "" , nil , err
181190 }
182191
183192 // the content of /etc/hosts is created in OCI Hook
184193 etcHostsPath , err := hostsstore .AllocHostsFile (dataStore , ns , id )
185194 if err != nil {
186- return nil , nil , nil , err
195+ return nil , nil , "" , nil , err
187196 }
188197 opts = append (opts , withCustomResolvConf (resolvConfPath ), withCustomHosts (etcHostsPath ))
189198 for _ , p := range portSlice {
190199 pm , err := portutil .ParseFlagP (p )
191200 if err != nil {
192- return nil , nil , pm , err
201+ return nil , nil , "" , pm , err
193202 }
194203 ports = append (ports , pm ... )
195204 }
196205 }
197206 default :
198- return nil , nil , nil , fmt .Errorf ("unexpected network type %v" , netType )
207+ return nil , nil , "" , nil , fmt .Errorf ("unexpected network type %v" , netType )
199208 }
200- return opts , netSlice , ports , nil
209+ return opts , netSlice , ipAddress , ports , nil
201210}
0 commit comments