Skip to content

Commit

Permalink
chore: use "handle-like" resource in DNSResolveCacheController
Browse files Browse the repository at this point in the history
Rework (and simplify) `DNSResolveCacheController` to use `DNSUpstream` "handle-like" resources.

Depends on cosi-project/runtime#400

Signed-off-by: Dmitriy Matrenichev <dmitry.matrenichev@siderolabs.com>
  • Loading branch information
DmitriyMV committed Feb 8, 2024
1 parent 013e130 commit afa71d6
Show file tree
Hide file tree
Showing 24 changed files with 981 additions and 1,019 deletions.
Binary file modified api/api.descriptors
Binary file not shown.
1 change: 0 additions & 1 deletion api/resource/definitions/network/network.proto
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ message DHCP6OperatorSpec {
// DNSResolveCacheSpec describes DNS servers status.
message DNSResolveCacheSpec {
string status = 1;
repeated common.NetIP servers = 2;
}

// HardwareAddrSpec describes spec for the link.
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ require (
github.com/containernetworking/plugins v1.4.0
github.com/coredns/coredns v1.11.1
github.com/coreos/go-iptables v0.7.0
github.com/cosi-project/runtime v0.3.20
github.com/cosi-project/runtime v0.4.0-alpha.4
github.com/distribution/reference v0.5.0
github.com/docker/docker v25.0.2+incompatible
github.com/docker/go-connections v0.5.0
Expand Down Expand Up @@ -235,7 +235,7 @@ require (
github.com/gorilla/websocket v1.5.0 // indirect
github.com/gosuri/uilive v0.0.4 // indirect
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.18.1 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-safetemp v1.0.0 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ github.com/coreos/go-semver v0.3.1 h1:yi21YpKnrx1gt5R+la8n5WgS0kCrsPp33dmEyHReZr
github.com/coreos/go-semver v0.3.1/go.mod h1:irMmmIw/7yzSRPWryHsK7EYSg09caPQL03VsM8rvUec=
github.com/coreos/go-systemd/v22 v22.5.0 h1:RrqgGjYQKalulkV8NGVIfkXQf6YYmOyiJKk8iXXhfZs=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/cosi-project/runtime v0.3.20 h1:pl8mwbHMFIRFYV8v0Glxw2ruhTXn/5ij7TSlO9nApi4=
github.com/cosi-project/runtime v0.3.20/go.mod h1:3DQsIr7zF/bmWfHOnpHmOQ9mDukFGi8AMoHx2rNsi+s=
github.com/cosi-project/runtime v0.4.0-alpha.4 h1:3TN+Y0NVKa/1QXqR3QTJ6ceWrcflyUeIZbQnrU2BprM=
github.com/cosi-project/runtime v0.4.0-alpha.4/go.mod h1:JE9yuyufGRCd28AyCWFkTNf3UMiZJT722bpfPEPnsNE=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/cpuguy83/go-md2man/v2 v2.0.3 h1:qMCsGGgs+MAzDFyp9LpAe1Lqy/fY/qCovCm0qnXZOBM=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
Expand Down Expand Up @@ -397,8 +397,8 @@ github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 h1:pdN6V1QBWet
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA=
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 h1:UH//fgunKIs4JdUbpDl1VZCDaL56wXCB/5+wF6uHfaI=
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0/go.mod h1:g5qyo/la0ALbONm6Vbp88Yd8NsDy6rZz+RcrMPxvld8=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.18.1 h1:6UKoz5ujsI55KNpsJH3UwCq3T8kKbZwNZBNPuTTje8U=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.18.1/go.mod h1:YvJ2f6MplWDhfxiUC3KpyTy76kYUZA4W3pTv/wdKQ9Y=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.0 h1:Wqo399gCIufwto+VfwCSvsnfGpF/w5E9CNxSwbpD6No=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.0/go.mod h1:qmOFXW2epJhM0qSnUUYpldc7gVz2KMQwJ/QYCDIa7XU=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
Expand Down
4 changes: 0 additions & 4 deletions hack/cloud-image-uploader/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.5.1 h1:sO0/P7g68FrryJzljemN+
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.5.1/go.mod h1:h8hyGFDsU5HMivxiS2iYFZsgDbU9OnnJ163x5UGVKYo=
github.com/Azure/azure-sdk-for-go/sdk/internal v1.5.1 h1:6oNBlSdi1QqM1PNW7FPA6xOGA5UNsXnkaYZz9vdPGhA=
github.com/Azure/azure-sdk-for-go/sdk/internal v1.5.1/go.mod h1:s4kgfzA0covAXNicZHDMN58jExvcng2mC/DepXiF1EI=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5 v5.4.0 h1:QfV5XZt6iNa2aWMAt96CZEbfJ7kgG/qYIpq465Shr5E=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5 v5.4.0/go.mod h1:uYt4CfhkJA9o0FN7jfE5minm/i4nUE4MjGUJkzB6Zs8=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5 v5.5.0 h1:MxA59PGoCFb+vCwRQi3PhQEwHj4+r2dhuv9HG+vM7iM=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5 v5.5.0/go.mod h1:uYt4CfhkJA9o0FN7jfE5minm/i4nUE4MjGUJkzB6Zs8=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/internal/v2 v2.0.0 h1:PTFGRSlMKCQelWwxUyYVEUqseBJVemLyqWJjvMyt0do=
Expand Down Expand Up @@ -47,8 +45,6 @@ github.com/Azure/go-autorest/tracing v0.6.0 h1:TYi4+3m5t6K48TGI9AUdb+IzbnSxvnvUM
github.com/Azure/go-autorest/tracing v0.6.0/go.mod h1:+vhtPC754Xsa23ID7GlGsrdKBpUA79WCAKPPZVC2DeU=
github.com/AzureAD/microsoft-authentication-library-for-go v1.2.1 h1:DzHpqpoJVaCgOUdVHxE8QB52S6NiVdDQvGlny1qvPqA=
github.com/AzureAD/microsoft-authentication-library-for-go v1.2.1/go.mod h1:wP83P5OoQ5p6ip3ScPr0BAq0BvuPAvacpEuSzyouqAI=
github.com/aws/aws-sdk-go v1.50.5 h1:H2Aadcgwr7a2aqS6ZwcE+l1mA6ZrTseYCvjw2QLmxIA=
github.com/aws/aws-sdk-go v1.50.5/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk=
github.com/aws/aws-sdk-go v1.50.8 h1:gY0WoOW+/Wz6XmYSgDH9ge3wnAevYDSQWPxxJvqAkP4=
github.com/aws/aws-sdk-go v1.50.8/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk=
github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ func (s *InspectServer) ControllerRuntimeDependencies(ctx context.Context, in *e
edgeType = inspectapi.DependencyEdgeType_INPUT_WEAK
case controller.EdgeInputDestroyReady:
edgeType = inspectapi.DependencyEdgeType_INPUT_DESTROY_READY
case controller.EdgeInputQPrimary,
controller.EdgeInputQMapped,
controller.EdgeInputQMappedDestroyReady:
return nil, fmt.Errorf("unexpected edge type: %v", graph.Edges[i].EdgeType)
}

edges = append(edges, &inspectapi.ControllerDependencyEdge{
Expand Down
63 changes: 22 additions & 41 deletions internal/app/machined/pkg/controllers/network/dns_resolve_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@ import (
"fmt"
"time"

"github.com/coredns/coredns/plugin/pkg/proxy"
"github.com/cosi-project/runtime/pkg/controller"
"github.com/cosi-project/runtime/pkg/resource"
"github.com/cosi-project/runtime/pkg/safe"
"github.com/cosi-project/runtime/pkg/state"
"github.com/siderolabs/gen/optional"
"go.uber.org/zap"

"github.com/siderolabs/talos/internal/pkg/ctxutil"
"github.com/siderolabs/talos/internal/pkg/dns"
"github.com/siderolabs/talos/pkg/machinery/resources/config"
"github.com/siderolabs/talos/pkg/machinery/resources/network"
)

Expand All @@ -36,18 +35,7 @@ func (ctrl *DNSResolveCacheController) Name() string {
// Inputs implements controller.Controller interface.
func (ctrl *DNSResolveCacheController) Inputs() []controller.Input {
return []controller.Input{
{
Namespace: network.NamespaceName,
Type: network.ResolverStatusType,
ID: optional.Some(network.ResolverID),
Kind: controller.InputWeak,
},
{
Namespace: config.NamespaceName,
Type: config.MachineConfigType,
ID: optional.Some(config.V1Alpha1ID),
Kind: controller.InputWeak,
},
safe.Input[*network.DNSUpstream](controller.InputWeak),
}
}

Expand All @@ -70,22 +58,18 @@ func (ctrl *DNSResolveCacheController) Run(ctx context.Context, r controller.Run
case <-r.EventCh():
}

mc, err := safe.ReaderGetByID[*config.MachineConfig](ctx, r, config.V1Alpha1ID)
upstreams, err := safe.ReaderListAll[*network.DNSUpstream](ctx, r)
if err != nil {
if state.IsNotFoundError(err) {
continue
}

return err
return fmt.Errorf("error getting resolver status: %w", err)
}

if !mc.Config().Machine().Features().LocalDNSEnabled() {
if upstreams.Len() == 0 {
continue
}

err = func() error {
ctrl.Logger.Info("starting dns cache resolve")
defer ctrl.Logger.Info("stopping dns cache resolve")
ctrl.Logger.Info("starting dns caching resolver")
defer ctrl.Logger.Info("stopping dns caching resolver")

return ctrl.runServer(ctx, r)
}()
Expand All @@ -95,10 +79,9 @@ func (ctrl *DNSResolveCacheController) Run(ctx context.Context, r controller.Run
}
}

func (ctrl *DNSResolveCacheController) writeDNSStatus(ctx context.Context, r controller.Runtime, net resource.ID, handler *dns.Handler) error {
func (ctrl *DNSResolveCacheController) writeDNSStatus(ctx context.Context, r controller.Runtime, net resource.ID) error {
return safe.WriterModify(ctx, r, network.NewDNSResolveCache(net), func(drc *network.DNSResolveCache) error {
drc.TypedSpec().Status = "running"
drc.TypedSpec().Servers = handler.ProxyList()

return nil
})
Expand Down Expand Up @@ -140,7 +123,7 @@ func (ctrl *DNSResolveCacheController) runServer(originCtx context.Context, r co

runner := dns.NewRunner(dns.NewServer(opt), l)

err := ctrl.writeDNSStatus(ctx, r, opt.Net, handler)
err := ctrl.writeDNSStatus(ctx, r, opt.Net)
if err != nil {
return err
}
Expand All @@ -164,33 +147,31 @@ func (ctrl *DNSResolveCacheController) runServer(originCtx context.Context, r co

eventCh = r.EventCh()

mc, err := safe.ReaderGetByID[*config.MachineConfig](ctx, r, config.V1Alpha1ID)
upstreams, err := safe.ReaderListAll[*network.DNSUpstream](ctx, r)
if err != nil {
return err
return fmt.Errorf("error getting resolver status: %w", err)
}

if !mc.Config().Machine().Features().LocalDNSEnabled() {
if upstreams.Len() == 0 {
return nil
}

resolverStatus, err := safe.ReaderGetByID[*network.ResolverStatus](ctx, r, network.ResolverID)
if err != nil {
if state.IsNotFoundError(err) {
continue
}
addrs := make([]string, 0, upstreams.Len())
prxs := make([]*proxy.Proxy, 0, len(addrs))

return fmt.Errorf("error getting resolver status: %w", err)
}
for it := upstreams.Iterator(); it.Next(); {
upstream := it.Value()

ctrl.Logger.Info("updating dns server nameservers", zap.Stringers("data", resolverStatus.TypedSpec().DNSServers))
addrs = append(addrs, upstream.TypedSpec().Value.Prx.Addr())
prxs = append(prxs, upstream.TypedSpec().Value.Prx.(*proxy.Proxy)) //nolint:forcetypeassert
}

err = handler.SetProxy(resolverStatus.TypedSpec().DNSServers)
if err != nil {
return fmt.Errorf("error setting dns server nameservers: %w", err)
if handler.SetProxy(prxs) {
ctrl.Logger.Info("updated dns server nameservers", zap.Strings("addrs", addrs))
}

for _, n := range []string{"udp", "tcp"} {
err = ctrl.writeDNSStatus(ctx, r, n, handler)
err = ctrl.writeDNSStatus(ctx, r, n)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ func (suite *DNSServer) TestResolving() {

rtestutils.AssertResources(suite.Ctx(), suite.T(), suite.State(), []resource.ID{"tcp", "udp"}, func(r *network.DNSResolveCache, assert *assert.Assertions) {
assert.Equal("running", r.TypedSpec().Status)
assert.Equal(dnsSlice, xslices.Map(r.TypedSpec().Servers, netip.Addr.String))
})

rtestutils.AssertLength[*network.DNSUpstream](suite.Ctx(), suite.T(), suite.State(), len(dnsSlice))

msg := &dns.Msg{
MsgHdr: dns.MsgHdr{
Id: dns.Id(),
Expand Down Expand Up @@ -111,9 +112,9 @@ func (suite *DNSServer) TestSetupStartStop() {

rtestutils.AssertResources(suite.Ctx(), suite.T(), suite.State(), []resource.ID{"tcp", "udp"}, func(r *network.DNSResolveCache, assert *assert.Assertions) {
assert.Equal("running", r.TypedSpec().Status)
assert.Equal(dnsSlice, xslices.Map(r.TypedSpec().Servers, netip.Addr.String))
})

rtestutils.AssertLength[*network.DNSUpstream](suite.Ctx(), suite.T(), suite.State(), len(dnsSlice))
// stop dns resolver

cfg.Container().RawV1Alpha1().MachineConfig.MachineFeatures.LocalDNS = pointer.To(false)
Expand All @@ -123,6 +124,10 @@ func (suite *DNSServer) TestSetupStartStop() {
ctest.AssertNoResource[*network.DNSResolveCache](suite, "tcp")
ctest.AssertNoResource[*network.DNSResolveCache](suite, "udp")

for _, d := range dnsSlice {
ctest.AssertNoResource[*network.DNSUpstream](suite, d)
}

// start dns resolver again

cfg.Container().RawV1Alpha1().MachineConfig.MachineFeatures.LocalDNS = pointer.To(true)
Expand All @@ -131,15 +136,17 @@ func (suite *DNSServer) TestSetupStartStop() {

rtestutils.AssertResources(suite.Ctx(), suite.T(), suite.State(), []resource.ID{"tcp", "udp"}, func(r *network.DNSResolveCache, assert *assert.Assertions) {
assert.Equal("running", r.TypedSpec().Status)
assert.Equal(dnsSlice, xslices.Map(r.TypedSpec().Servers, netip.Addr.String))
})

rtestutils.AssertLength[*network.DNSUpstream](suite.Ctx(), suite.T(), suite.State(), len(dnsSlice))
}

func TestDNSServer(t *testing.T) {
suite.Run(t, &DNSServer{
DefaultSuite: ctest.DefaultSuite{
Timeout: 10 * time.Second,
AfterSetup: func(suite *ctest.DefaultSuite) {
suite.Require().NoError(suite.Runtime().RegisterController(&netctrl.DNSUpstreamController{}))
suite.Require().NoError(suite.Runtime().RegisterController(&netctrl.DNSResolveCacheController{
Addr: ":10700",
Logger: zaptest.NewLogger(t),
Expand Down
Loading

0 comments on commit afa71d6

Please sign in to comment.