Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setup public config #43

Merged
merged 1 commit into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmds/modules/netlightd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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 {
Expand Down
17 changes: 15 additions & 2 deletions cmds/modules/noded/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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")
}
Expand All @@ -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)
Expand Down
78 changes: 78 additions & 0 deletions cmds/modules/noded/public.go
Original file line number Diff line number Diff line change
@@ -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)
}
Loading