Skip to content

Commit

Permalink
chore: account for resource sorting in dns upstream resource
Browse files Browse the repository at this point in the history
`List` returns a sorted (by id) list of resources. This doesn't work when the order of dns upstreams is important. Because of that
we need to rework "DNSUpstreams.net.talos.dev" resource ID from destination host to "dns №<num>".

Fixes siderolabs#9274

Signed-off-by: Dmitriy Matrenichev <dmitry.matrenichev@siderolabs.com>
  • Loading branch information
DmitriyMV committed Sep 11, 2024
1 parent e17fafa commit 413a7db
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
13 changes: 11 additions & 2 deletions internal/app/machined/pkg/controllers/network/dns_upstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package network

import (
"context"
"fmt"
"net"
"time"

Expand Down Expand Up @@ -103,17 +104,25 @@ func (ctrl *DNSUpstreamController) run(ctx context.Context, r controller.Runtime
return err
}

for _, s := range rs.TypedSpec().DNSServers {
for i, s := range rs.TypedSpec().DNSServers {
remoteAddr := s.String()

if err = safe.WriterModify[*network.DNSUpstream](
ctx,
r,
network.NewDNSUpstream(remoteAddr),
network.NewDNSUpstream(fmt.Sprintf("dns upstream №%d", i+1)),
func(u *network.DNSUpstream) error {
touchedIDs[u.Metadata().ID()] = struct{}{}

if u.TypedSpec().Value.Prx != nil {
if u.TypedSpec().Value.Prx.Addr() == net.JoinHostPort(remoteAddr, "53") {
// existing upstream in this position is already correct
return nil
}

// existing upstream in this position is incorrect, stop it
u.TypedSpec().Value.Prx.Stop()

return nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ func (ctrl *ResolverMergeController) Run(ctx context.Context, r controller.Runti
}

if final.DNSServers != nil {
if err = r.Modify(ctx, network.NewResolverSpec(network.NamespaceName, network.ResolverID), func(res resource.Resource) error {
spec := res.(*network.ResolverSpec) //nolint:errcheck,forcetypeassert

if err = safe.WriterModify(ctx, r, network.NewResolverSpec(network.NamespaceName, network.ResolverID), func(spec *network.ResolverSpec) error {
*spec.TypedSpec() = final

return nil
Expand Down Expand Up @@ -150,9 +148,9 @@ func mergeDNSServers(dst *[]netip.Addr, src []netip.Addr) {
// and same vice versa for IPv6
switch {
case dstHasV4 && !srcHasV4:
*dst = append(slices.Clone(src), filterIPFamily(*dst, true)...)
*dst = slices.Concat(src, filterIPFamily(*dst, true))
case dstHasV6 && !srcHasV6:
*dst = append(slices.Clone(src), filterIPFamily(*dst, false)...)
*dst = slices.Concat(src, filterIPFamily(*dst, false))
default:
*dst = src
}
Expand Down

0 comments on commit 413a7db

Please sign in to comment.