@@ -166,12 +166,25 @@ func (np *NetworkDriver) prepareResourceClaim(ctx context.Context, claim *resour
166
166
podCfg .Network .Interface .Name = ifName
167
167
}
168
168
169
- // If there is no custom addresses then use the existing ones
170
- if len (podCfg .Network .Interface .Addresses ) == 0 {
169
+ // If DHCP is requested, do a DHCP request to gather the network parameters (IPs and Routes)
170
+ // ... but we DO NOT apply them in the root namespace
171
+ if podCfg .Network .Interface .DHCP != nil && * podCfg .Network .Interface .DHCP {
172
+ klog .V (2 ).Infof ("trying to get network configuration via DHCP" )
173
+ contextCancel , cancel := context .WithTimeout (ctx , 5 * time .Second )
174
+ defer cancel ()
175
+ ip , routes , err := getDHCP (contextCancel , ifName )
176
+ if err != nil {
177
+ errorList = append (errorList , fmt .Errorf ("fail to get configuration via DHCP for %s: %w" , ifName , err ))
178
+ } else {
179
+ podCfg .Network .Interface .Addresses = []string {ip }
180
+ podCfg .Network .Routes = append (podCfg .Network .Routes , routes ... )
181
+ }
182
+ } else if len (podCfg .Network .Interface .Addresses ) == 0 {
183
+ // If there is no custom addresses and no DHCP, then use the existing ones
171
184
// get the existing IP addresses
172
185
nlAddresses , err := nlHandle .AddrList (link , netlink .FAMILY_ALL )
173
186
if err != nil && ! errors .Is (err , netlink .ErrDumpInterrupted ) {
174
- klog . Infof ("fail to get ip addresses for interface %s : %v " , ifName , err )
187
+ errorList = append ( errorList , fmt . Errorf ("fail to get ip addresses for interface %s : %w " , ifName , err ) )
175
188
} else {
176
189
for _ , address := range nlAddresses {
177
190
// Only move IP addresses with global scope because those are not host-specific, auto-configured,
@@ -185,21 +198,6 @@ func (np *NetworkDriver) prepareResourceClaim(ctx context.Context, claim *resour
185
198
}
186
199
}
187
200
188
- // If there are no addresses configured on the interface and the user is not setting them
189
- // this may be an interface that uses DHCP, so we bring it up if necessary and do a DHCP
190
- // request to gather the network parameters (IPs and Routes) ... but we DO NOT apply them
191
- // in the root namespace
192
- if len (podCfg .Network .Interface .Addresses ) == 0 {
193
- klog .V (2 ).Infof ("trying to get network configuration via DHCP" )
194
- ip , routes , err := getDHCP (ifName )
195
- if err != nil {
196
- klog .Infof ("fail to get configuration via DHCP: %v" , err )
197
- } else {
198
- podCfg .Network .Interface .Addresses = []string {ip }
199
- podCfg .Network .Routes = append (podCfg .Network .Routes , routes ... )
200
- }
201
- }
202
-
203
201
// Obtain the existing supported ethtool features and validate the config
204
202
if podCfg .Network .Ethtool != nil {
205
203
client , err := newEthtoolClient (0 )
0 commit comments