Skip to content

Commit

Permalink
fix: request previous IP address in discovery
Browse files Browse the repository at this point in the history
This ensures that even in the event of a DHCP downtime that exceeds the
lease time, the current IP can be maintained.

Signed-off-by: TomyLobo <[email protected]>
Signed-off-by: Andrey Smirnov <[email protected]>
  • Loading branch information
TomyLobo authored and smira committed Jan 9, 2025
1 parent cc84caf commit 499695e
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions internal/app/machined/pkg/controllers/network/operator/dhcp4.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,12 @@ func (d *DHCP4) knownHostname(hostname network.HostnameStatusSpec) bool {
// be assigned to the associated so that unicast operations can bind successfully.
func (d *DHCP4) waitForNetworkReady(ctx context.Context) error {
// If an IP address has been registered, wait for the address association to be ready
if len(d.addresses) > 0 {
if addresses := d.AddressSpecs(); len(addresses) > 0 {
_, err := d.state.WatchFor(ctx,
resource.NewMetadata(
network.NamespaceName,
network.AddressStatusType,
network.AddressID(d.linkName, d.addresses[0].Address),
network.AddressID(d.linkName, addresses[0].Address),
resource.VersionUndefined,
),
state.WithPhases(resource.PhaseRunning),
Expand Down Expand Up @@ -159,7 +159,7 @@ func (d *DHCP4) Run(ctx context.Context, notifyCh chan<- struct{}) {
// Perform a lease request or renewal
leaseTime, err := d.requestRenew(ctx, hostname)
if err != nil && !errors.Is(err, context.Canceled) {
d.logger.Warn("request/renew failed", zap.Error(err), zap.String("link", d.linkName))
d.logger.Warn("DHCP request/renew failed", zap.Error(err), zap.String("link", d.linkName))
}

if err == nil {
Expand Down Expand Up @@ -235,7 +235,7 @@ func (d *DHCP4) Run(ctx context.Context, notifyCh chan<- struct{}) {
d.lease = nil

d.logger.Debug("restarting DHCP sequence due to hostname change",
zap.Strings("dhcp_hostname", xslices.Map(d.hostname, func(spec network.HostnameSpecSpec) string {
zap.Strings("dhcp_hostname", xslices.Map(d.HostnameSpecs(), func(spec network.HostnameSpecSpec) string {
return spec.Hostname
})),
)
Expand Down Expand Up @@ -532,13 +532,23 @@ func (d *DHCP4) requestRenew(ctx context.Context, hostname network.HostnameStatu
//nolint:errcheck
defer client.Close()

addresses := d.AddressSpecs()

switch {
case d.lease != nil && !d.lease.ACK.ServerIPAddr.IsUnspecified():
d.logger.Debug("DHCP RENEW", zap.String("link", d.linkName))
d.lease, err = client.Renew(ctx, d.lease, mods...)
case d.lease != nil && d.lease.Offer != nil:
d.logger.Debug("DHCP REQUEST FROM OFFER", zap.String("link", d.linkName))
d.lease, err = client.RequestFromOffer(ctx, d.lease.Offer, mods...)
case len(addresses) >= 1:
previousIPAddress := net.IP(addresses[0].Address.Addr().AsSlice())

d.logger.Debug("DHCP REQUEST with previous IP", zap.String("link", d.linkName), zap.Stringer("previous_ip", previousIPAddress))

d.lease, err = client.Request(ctx, dhcpv4.PrependModifiers(mods,
dhcpv4.WithOption(dhcpv4.OptRequestedIPAddress(previousIPAddress)),
)...)
default:
d.logger.Debug("DHCP REQUEST", zap.String("link", d.linkName))
d.lease, err = client.Request(ctx, mods...)
Expand Down

0 comments on commit 499695e

Please sign in to comment.