From ebb4bb05130a32f5c090cea7a31e4698fe6c2956 Mon Sep 17 00:00:00 2001 From: Omar Abdulaziz Date: Tue, 28 Jan 2025 14:40:28 +0200 Subject: [PATCH] setup public config - setup the persistance file in the netlight module - add the chain events listner to catch PublicConfigStored events for the node - run the redis consumer to act on the stored event --- cmds/modules/netlightd/main.go | 3 ++ cmds/modules/noded/main.go | 17 +++++++- cmds/modules/noded/public.go | 78 ++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 cmds/modules/noded/public.go diff --git a/cmds/modules/netlightd/main.go b/cmds/modules/netlightd/main.go index 6bf24c62e..6ea55ce94 100644 --- a/cmds/modules/netlightd/main.go +++ b/cmds/modules/netlightd/main.go @@ -14,6 +14,7 @@ import ( "github.com/threefoldtech/zosbase/pkg/netlight/bridge" "github.com/threefoldtech/zosbase/pkg/netlight/ifaceutil" "github.com/threefoldtech/zosbase/pkg/netlight/nft" + "github.com/threefoldtech/zosbase/pkg/netlight/public" "github.com/threefoldtech/zosbase/pkg/netlight/resource" "github.com/urfave/cli/v2" @@ -73,6 +74,8 @@ func action(cli *cli.Context) error { return errors.Wrap(err, "fail to create module root") } + public.SetPersistence(root) + waitMyceliumBin() if err := bootstrap.DefaultBridgeValid(); err != nil { diff --git a/cmds/modules/noded/main.go b/cmds/modules/noded/main.go index e7b18e8af..2c03f48f5 100644 --- a/cmds/modules/noded/main.go +++ b/cmds/modules/noded/main.go @@ -59,7 +59,6 @@ var Module cli.Command = cli.Command{ } func registerationServer(ctx context.Context, msgBrokerCon string, env environment.Environment, info registrar.RegistrationInfo) error { - redis, err := zbus.NewRedisClient(msgBrokerCon) if err != nil { return errors.Wrap(err, "fail to connect to message broker server") @@ -92,6 +91,11 @@ func action(cli *cli.Context) error { return errors.Wrap(err, "fail to connect to message broker server") } + consumer, err := events.NewConsumer(msgBrokerCon, module) + if err != nil { + return errors.Wrap(err, "failed to to create event consumer") + } + if printID { sysCl := stubs.NewSystemMonitorStub(redis) fmt.Println(sysCl.NodeID(cli.Context)) @@ -216,7 +220,7 @@ func action(cli *cli.Context) error { } go events.Start(ctx) - system, err := monitord.NewSystemMonitor(node, 2*time.Second) + system, err := monitord.NewSystemMonitor(node, 2*time.Second, redis) if err != nil { log.Fatal().Err(err).Msg("failed to initialize system monitor") } @@ -232,6 +236,15 @@ func action(cli *cli.Context) error { log.Info().Uint32("node", node).Uint32("twin", twin).Msg("node registered") + go func() { + for { + if err := startPublicConfigWatcher(ctx, node, redis, consumer); err != nil { + log.Error().Err(err).Msg("setting public config failed") + <-time.After(10 * time.Second) + } + } + }() + log.Info().Uint32("twin", twin).Msg("node has been registered") idStub := stubs.NewIdentityManagerStub(redis) fetchCtx, cancel := context.WithTimeout(ctx, 30*time.Second) diff --git a/cmds/modules/noded/public.go b/cmds/modules/noded/public.go new file mode 100644 index 000000000..96953e57e --- /dev/null +++ b/cmds/modules/noded/public.go @@ -0,0 +1,78 @@ +package noded + +import ( + "context" + "time" + + "github.com/pkg/errors" + "github.com/rs/zerolog/log" + substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go" + "github.com/threefoldtech/zbus" + "github.com/threefoldtech/zosbase/pkg" + "github.com/threefoldtech/zosbase/pkg/events" + "github.com/threefoldtech/zosbase/pkg/stubs" +) + +// public sets and watches changes to public config on chain and tries to apply the provided setup +func startPublicConfigWatcher(ctx context.Context, nodeID uint32, cl zbus.Client, events *events.RedisConsumer) error { + ch, err := events.PublicConfig(ctx) + if err != nil { + return errors.Wrap(err, "failed to subscribe to node events") + } + + substrateGateway := stubs.NewSubstrateGatewayStub(cl) + +reapply: + for { + node, err := substrateGateway.GetNode(ctx, nodeID) + if err != nil { + return errors.Wrap(err, "failed to get node public config") + } + + var cfg *substrate.PublicConfig + if node.PublicConfig.HasValue { + cfg = &node.PublicConfig.AsValue + } + + if err := setPublicConfig(ctx, cl, cfg); err != nil { + return errors.Wrap(err, "failed to set public config (reapply)") + } + + for { + select { + case <-ctx.Done(): + return nil + case event := <-ch: + log.Info().Msgf("got a public config update: %+v", event.PublicConfig) + var cfg *substrate.PublicConfig + if event.PublicConfig.HasValue { + cfg = &event.PublicConfig.AsValue + } + if err := setPublicConfig(ctx, cl, cfg); err != nil { + return errors.Wrap(err, "failed to set public config") + } + case <-time.After(2 * time.Hour): + // last resort, if none of the events + // was received, it will be a good idea to just + // check every 2 hours for changes. + continue reapply + } + } + } +} + +func setPublicConfig(ctx context.Context, cl zbus.Client, cfg *substrate.PublicConfig) error { + log.Info().Msg("setting node public config") + netMgr := stubs.NewNetworkerLightStub(cl) + + if cfg == nil { + return netMgr.UnSetPublicConfig(ctx) + } + + pub, err := pkg.PublicConfigFrom(*cfg) + if err != nil { + return errors.Wrap(err, "failed to create public config from setup") + } + + return netMgr.SetPublicConfig(ctx, pub) +}