@@ -35,6 +35,7 @@ import (
35
35
"github.com/containerd/nerdctl/pkg/rootlessutil"
36
36
"github.com/containerd/nerdctl/pkg/strutil"
37
37
"github.com/opencontainers/runtime-spec/specs-go"
38
+ "github.com/sirupsen/logrus"
38
39
"github.com/spf13/cobra"
39
40
)
40
41
@@ -104,21 +105,29 @@ func withCustomHosts(src string) func(context.Context, oci.Client, *containers.C
104
105
}
105
106
}
106
107
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 ) {
108
109
opts := []oci.SpecOpts {}
109
110
portSlice , err := cmd .Flags ().GetStringSlice ("publish" )
110
111
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
112
117
}
113
118
netSlice , err := getNetworkSlice (cmd )
114
119
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 )
116
125
}
117
126
118
127
ports := make ([]gocni.PortMapping , 0 )
119
128
netType , err := nettype .Detect (netSlice )
120
129
if err != nil {
121
- return nil , nil , nil , err
130
+ return nil , nil , "" , nil , err
122
131
}
123
132
124
133
switch netType {
@@ -131,44 +140,44 @@ func generateNetOpts(cmd *cobra.Command, dataStore, stateDir, ns, id string) ([]
131
140
// The actual network is configured in the oci hook.
132
141
cniPath , err := cmd .Flags ().GetString ("cni-path" )
133
142
if err != nil {
134
- return nil , nil , nil , err
143
+ return nil , nil , "" , nil , err
135
144
}
136
145
cniNetconfpath , err := cmd .Flags ().GetString ("cni-netconfpath" )
137
146
if err != nil {
138
- return nil , nil , nil , err
147
+ return nil , nil , "" , nil , err
139
148
}
140
149
e , err := netutil .NewCNIEnv (cniPath , cniNetconfpath )
141
150
if err != nil {
142
- return nil , nil , nil , err
151
+ return nil , nil , "" , nil , err
143
152
}
144
153
netMap := e .NetworkMap ()
145
154
for _ , netstr := range netSlice {
146
155
_ , ok := netMap [netstr ]
147
156
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 )
149
158
}
150
159
}
151
160
152
161
resolvConfPath := filepath .Join (stateDir , "resolv.conf" )
153
162
dnsValue , err := cmd .Flags ().GetStringSlice ("dns" )
154
163
if err != nil {
155
- return nil , nil , nil , err
164
+ return nil , nil , "" , nil , err
156
165
}
157
166
if runtime .GOOS == "linux" {
158
167
conf , err := resolvconf .Get ()
159
168
if err != nil {
160
- return nil , nil , nil , err
169
+ return nil , nil , "" , nil , err
161
170
}
162
171
slirp4Dns := []string {}
163
172
if rootlessutil .IsRootlessChild () {
164
173
slirp4Dns , err = dnsutil .GetSlirp4netnsDns ()
165
174
if err != nil {
166
- return nil , nil , nil , err
175
+ return nil , nil , "" , nil , err
167
176
}
168
177
}
169
178
conf , err = resolvconf .FilterResolvDNS (conf .Content , true )
170
179
if err != nil {
171
- return nil , nil , nil , err
180
+ return nil , nil , "" , nil , err
172
181
}
173
182
searchDomains := resolvconf .GetSearchDomains (conf .Content )
174
183
dnsOptions := resolvconf .GetOptions (conf .Content )
@@ -177,25 +186,25 @@ func generateNetOpts(cmd *cobra.Command, dataStore, stateDir, ns, id string) ([]
177
186
nameServers = resolvconf .GetNameservers (conf .Content , resolvconf .IPv4 )
178
187
}
179
188
if _ , err := resolvconf .Build (resolvConfPath , append (slirp4Dns , nameServers ... ), searchDomains , dnsOptions ); err != nil {
180
- return nil , nil , nil , err
189
+ return nil , nil , "" , nil , err
181
190
}
182
191
183
192
// the content of /etc/hosts is created in OCI Hook
184
193
etcHostsPath , err := hostsstore .AllocHostsFile (dataStore , ns , id )
185
194
if err != nil {
186
- return nil , nil , nil , err
195
+ return nil , nil , "" , nil , err
187
196
}
188
197
opts = append (opts , withCustomResolvConf (resolvConfPath ), withCustomHosts (etcHostsPath ))
189
198
for _ , p := range portSlice {
190
199
pm , err := portutil .ParseFlagP (p )
191
200
if err != nil {
192
- return nil , nil , pm , err
201
+ return nil , nil , "" , pm , err
193
202
}
194
203
ports = append (ports , pm ... )
195
204
}
196
205
}
197
206
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 )
199
208
}
200
- return opts , netSlice , ports , nil
209
+ return opts , netSlice , ipAddress , ports , nil
201
210
}
0 commit comments