Skip to content

Commit 57a61cb

Browse files
authored
Merge pull request #40 from threefoldtech/main_setPublicConfig
set public config in zoslight
2 parents 8701df0 + 1fe5826 commit 57a61cb

File tree

8 files changed

+211
-27
lines changed

8 files changed

+211
-27
lines changed

cmds/modules/netlightd/main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/threefoldtech/zos4/pkg/netlight/bridge"
1515
"github.com/threefoldtech/zos4/pkg/netlight/ifaceutil"
1616
"github.com/threefoldtech/zos4/pkg/netlight/nft"
17+
"github.com/threefoldtech/zos4/pkg/netlight/public"
1718
"github.com/threefoldtech/zos4/pkg/netlight/resource"
1819
"github.com/urfave/cli/v2"
1920

@@ -73,6 +74,8 @@ func action(cli *cli.Context) error {
7374
return errors.Wrap(err, "fail to create module root")
7475
}
7576

77+
public.SetPersistence(root)
78+
7679
waitMyceliumBin()
7780

7881
if err := bootstrap.DefaultBridgeValid(); err != nil {
@@ -135,7 +138,7 @@ func action(cli *cli.Context) error {
135138
return fmt.Errorf("failed to drop traffic to lan: %w", err)
136139
}
137140

138-
mod, err := netlight.NewNetworker()
141+
mod, err := netlight.NewNetworker(identity)
139142
if err != nil {
140143
return fmt.Errorf("failed to create Networker: %w", err)
141144
}

cmds/modules/noded/main.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ var Module cli.Command = cli.Command{
5959
}
6060

6161
func registerationServer(ctx context.Context, msgBrokerCon string, env environment.Environment, info registrar.RegistrationInfo) error {
62-
6362
redis, err := zbus.NewRedisClient(msgBrokerCon)
6463
if err != nil {
6564
return errors.Wrap(err, "fail to connect to message broker server")
@@ -85,13 +84,19 @@ func action(cli *cli.Context) error {
8584
printID bool = cli.Bool("id")
8685
printNet bool = cli.Bool("net")
8786
)
87+
8888
env := environment.MustGet()
8989

9090
redis, err := zbus.NewRedisClient(msgBrokerCon)
9191
if err != nil {
9292
return errors.Wrap(err, "fail to connect to message broker server")
9393
}
9494

95+
consumer, err := events.NewConsumer(msgBrokerCon, module)
96+
if err != nil {
97+
return errors.Wrap(err, "failed to to create event consumer")
98+
}
99+
95100
if printID {
96101
sysCl := stubs.NewSystemMonitorStub(redis)
97102
fmt.Println(sysCl.NodeID(cli.Context))
@@ -216,7 +221,7 @@ func action(cli *cli.Context) error {
216221
}
217222
go events.Start(ctx)
218223

219-
system, err := monitord.NewSystemMonitor(node, 2*time.Second)
224+
system, err := monitord.NewSystemMonitor(node, 2*time.Second, redis)
220225
if err != nil {
221226
log.Fatal().Err(err).Msg("failed to initialize system monitor")
222227
}
@@ -232,6 +237,15 @@ func action(cli *cli.Context) error {
232237

233238
log.Info().Uint32("node", node).Uint32("twin", twin).Msg("node registered")
234239

240+
go func() {
241+
for {
242+
if err := startPublicConfigWatcher(ctx, node, redis, consumer); err != nil {
243+
log.Error().Err(err).Msg("setting public config failed")
244+
<-time.After(10 * time.Second)
245+
}
246+
}
247+
}()
248+
235249
log.Info().Uint32("twin", twin).Msg("node has been registered")
236250
idStub := stubs.NewIdentityManagerStub(redis)
237251
fetchCtx, cancel := context.WithTimeout(ctx, 30*time.Second)

cmds/modules/noded/public.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package noded
2+
3+
import (
4+
"context"
5+
"time"
6+
7+
"github.com/pkg/errors"
8+
"github.com/rs/zerolog/log"
9+
substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go"
10+
"github.com/threefoldtech/zbus"
11+
"github.com/threefoldtech/zos4/pkg"
12+
"github.com/threefoldtech/zos4/pkg/events"
13+
"github.com/threefoldtech/zos4/pkg/stubs"
14+
)
15+
16+
// public sets and watches changes to public config on chain and tries to apply the provided setup
17+
func startPublicConfigWatcher(ctx context.Context, nodeID uint32, cl zbus.Client, events *events.RedisConsumer) error {
18+
ch, err := events.PublicConfig(ctx)
19+
if err != nil {
20+
return errors.Wrap(err, "failed to subscribe to node events")
21+
}
22+
23+
substrateGateway := stubs.NewSubstrateGatewayStub(cl)
24+
25+
reapply:
26+
for {
27+
node, err := substrateGateway.GetNode(ctx, nodeID)
28+
if err != nil {
29+
return errors.Wrap(err, "failed to get node public config")
30+
}
31+
32+
var cfg *substrate.PublicConfig
33+
if node.PublicConfig.HasValue {
34+
cfg = &node.PublicConfig.AsValue
35+
}
36+
37+
if err := setPublicConfig(ctx, cl, cfg); err != nil {
38+
return errors.Wrap(err, "failed to set public config (reapply)")
39+
}
40+
41+
for {
42+
select {
43+
case <-ctx.Done():
44+
return nil
45+
case event := <-ch:
46+
log.Info().Msgf("got a public config update: %+v", event.PublicConfig)
47+
var cfg *substrate.PublicConfig
48+
if event.PublicConfig.HasValue {
49+
cfg = &event.PublicConfig.AsValue
50+
}
51+
if err := setPublicConfig(ctx, cl, cfg); err != nil {
52+
return errors.Wrap(err, "failed to set public config")
53+
}
54+
case <-time.After(2 * time.Hour):
55+
// last resort, if none of the events
56+
// was received, it will be a good idea to just
57+
// check every 2 hours for changes.
58+
continue reapply
59+
}
60+
}
61+
}
62+
}
63+
64+
func setPublicConfig(ctx context.Context, cl zbus.Client, cfg *substrate.PublicConfig) error {
65+
log.Info().Msg("setting node public config")
66+
netMgr := stubs.NewNetworkerLightStub(cl)
67+
68+
if cfg == nil {
69+
return netMgr.UnSetPublicConfig(ctx)
70+
}
71+
72+
pub, err := pkg.PublicConfigFrom(*cfg)
73+
if err != nil {
74+
return errors.Wrap(err, "failed to create public config from setup")
75+
}
76+
77+
return netMgr.SetPublicConfig(ctx, pub)
78+
}

pkg/gateway/gateway.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"github.com/threefoldtech/zos4/pkg/cache"
2121
"github.com/threefoldtech/zos4/pkg/gridtypes"
2222
"github.com/threefoldtech/zos4/pkg/gridtypes/zos"
23-
"github.com/threefoldtech/zos4/pkg/kernel"
2423
"github.com/threefoldtech/zos4/pkg/netlight/types"
2524
"github.com/threefoldtech/zos4/pkg/stubs"
2625
"github.com/threefoldtech/zos4/pkg/zinit"
@@ -342,8 +341,11 @@ func (g *gatewayModule) validateNameContracts() error {
342341
ctx, cancel := context.WithTimeout(context.Background(), validationPeriod/2)
343342
defer cancel()
344343
e := stubs.NewProvisionStub(g.cl)
345-
baseDomain, found := kernel.GetParams().GetOne("domain")
346-
if !found {
344+
345+
netStub := stubs.NewNetworkerLightStub(g.cl)
346+
config, err := netStub.LoadPublicConfig(context.Background())
347+
baseDomain := config.Domain
348+
if baseDomain == "" || err == nil {
347349
// domain doesn't exist so no name workloads exist
348350
return nil
349351
}
@@ -425,12 +427,12 @@ func (g *gatewayModule) traefikBinary(ctx context.Context, z *zinit.Client) (str
425427
// ensureGateway makes sure that gateway infrastructure is in place and
426428
// that it is supported.
427429
func (g *gatewayModule) ensureGateway(ctx context.Context, forceResstart bool) (string, error) {
428-
var (
429-
flistd = stubs.NewFlisterStub(g.cl)
430-
)
430+
flistd := stubs.NewFlisterStub(g.cl)
431431

432-
domain, found := kernel.GetParams().GetOne("domain")
433-
if !found {
432+
netStub := stubs.NewNetworkerLightStub(g.cl)
433+
config, err := netStub.LoadPublicConfig(context.Background())
434+
domain := config.Domain
435+
if domain == "" || err != nil {
434436
return "", fmt.Errorf("gateway is not supported on this node, domain is not set")
435437
}
436438

@@ -478,9 +480,10 @@ func (g *gatewayModule) ensureGateway(ctx context.Context, forceResstart bool) (
478480
return domain, nil
479481
}
480482

481-
//other wise we start traefik
483+
// other wise we start traefik
482484
return domain, g.startTraefik(z)
483485
}
486+
484487
func (g *gatewayModule) verifyDomainDestination(ctx context.Context, domain string) error {
485488
networker := stubs.NewNetworkerLightStub(g.cl)
486489

@@ -508,7 +511,6 @@ func (g *gatewayModule) verifyDomainDestination(ctx context.Context, domain stri
508511
}
509512

510513
func (g *gatewayModule) startTraefik(z *zinit.Client) error {
511-
512514
cmd := fmt.Sprintf(
513515
"%s --configfile %s",
514516
g.binPath,
@@ -540,7 +542,6 @@ func (g *gatewayModule) configPath(name string) string {
540542
}
541543

542544
func (g *gatewayModule) validateNameContract(name string, twinID uint32) error {
543-
544545
contractID, subErr := g.substrateGateway.GetContractIDByNameRegistration(context.Background(), name)
545546
if subErr.IsCode(pkg.CodeNotFound) {
546547
return ErrContractNotReserved

pkg/monitord/system.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ import (
99
"github.com/shirou/gopsutil/disk"
1010
"github.com/shirou/gopsutil/mem"
1111
"github.com/shirou/gopsutil/net"
12+
"github.com/threefoldtech/zbus"
1213
"github.com/threefoldtech/zos4/pkg"
1314
"github.com/threefoldtech/zos4/pkg/gridtypes/zos"
14-
"github.com/threefoldtech/zos4/pkg/kernel"
15+
"github.com/threefoldtech/zos4/pkg/stubs"
1516
)
1617

1718
var _ pkg.SystemMonitor = (*systemMonitor)(nil)
@@ -20,15 +21,16 @@ var _ pkg.SystemMonitor = (*systemMonitor)(nil)
2021
type systemMonitor struct {
2122
duration time.Duration
2223
node uint32
24+
cl zbus.Client
2325
}
2426

2527
// NewSystemMonitor creates new system of system monitor
26-
func NewSystemMonitor(node uint32, duration time.Duration) (pkg.SystemMonitor, error) {
28+
func NewSystemMonitor(node uint32, duration time.Duration, cl zbus.Client) (pkg.SystemMonitor, error) {
2729
if duration == 0 {
2830
duration = 2 * time.Second
2931
}
3032

31-
return &systemMonitor{duration: duration, node: node}, nil
33+
return &systemMonitor{duration: duration, node: node, cl: cl}, nil
3234
}
3335

3436
func (m *systemMonitor) NodeID() uint32 {
@@ -212,8 +214,11 @@ func (n *systemMonitor) GetNodeFeatures() []pkg.NodeFeature {
212214
pkg.NodeFeature(zos.ZLogsType),
213215
pkg.NodeFeature("mycelium"),
214216
}
215-
_, found := kernel.GetParams().GetOne("domain")
216-
if found {
217+
218+
netStub := stubs.NewNetworkerLightStub(n.cl)
219+
config, err := netStub.LoadPublicConfig(context.Background())
220+
221+
if config.Domain != "" && err == nil {
217222
feat = append(feat, "gateway")
218223
}
219224

pkg/netlight/network.go

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ import (
2424
"github.com/threefoldtech/zos4/pkg/netlight/ipam"
2525
"github.com/threefoldtech/zos4/pkg/netlight/namespace"
2626
"github.com/threefoldtech/zos4/pkg/netlight/options"
27+
"github.com/threefoldtech/zos4/pkg/netlight/public"
2728
"github.com/threefoldtech/zos4/pkg/netlight/resource"
29+
"github.com/threefoldtech/zos4/pkg/stubs"
2830
"github.com/threefoldtech/zos4/pkg/versioned"
2931
"github.com/vishvananda/netlink"
3032
)
@@ -38,23 +40,22 @@ const (
3840
networkDir = "networks"
3941
)
4042

41-
var (
42-
NDMZGwIP = &net.IPNet{
43-
IP: net.ParseIP("100.127.0.1"),
44-
Mask: net.CIDRMask(16, 32),
45-
}
46-
)
43+
var NDMZGwIP = &net.IPNet{
44+
IP: net.ParseIP("100.127.0.1"),
45+
Mask: net.CIDRMask(16, 32),
46+
}
4747

4848
var NetworkSchemaLatestVersion = semver.MustParse("0.1.0")
4949

5050
type networker struct {
51+
identity *stubs.IdentityManagerStub
5152
ipamLease string
5253
networkDir string
5354
}
5455

5556
var _ pkg.NetworkerLight = (*networker)(nil)
5657

57-
func NewNetworker() (pkg.NetworkerLight, error) {
58+
func NewNetworker(identity *stubs.IdentityManagerStub) (pkg.NetworkerLight, error) {
5859
vd, err := cache.VolatileDir("networkd", 50*mib)
5960
if err != nil && !os.IsExist(err) {
6061
return nil, fmt.Errorf("failed to create networkd cache directory: %w", err)
@@ -64,6 +65,7 @@ func NewNetworker() (pkg.NetworkerLight, error) {
6465
runtimeDir := filepath.Join(vd, networkDir)
6566

6667
return &networker{
68+
identity: identity,
6769
ipamLease: ipamLease,
6870
networkDir: runtimeDir,
6971
}, nil
@@ -89,7 +91,6 @@ func (n *networker) Delete(name string) error {
8991
}
9092

9193
return resource.Delete(name)
92-
9394
}
9495

9596
func (n *networker) AttachPrivate(name, id string, vmIp net.IP) (device pkg.TapDevice, err error) {
@@ -390,6 +391,38 @@ func (n *networker) Interfaces(iface string, netns string) (pkg.Interfaces, erro
390391
return pkg.Interfaces{Interfaces: interfaces}, nil
391392
}
392393

394+
func (n *networker) UnSetPublicConfig() error {
395+
return public.DeletePublicConfig()
396+
}
397+
398+
// Set node public namespace config
399+
func (n *networker) SetPublicConfig(cfg pkg.PublicConfig) error {
400+
if cfg.Equal(pkg.PublicConfig{}) {
401+
return fmt.Errorf("public config cannot be unset, only modified")
402+
}
403+
404+
current, err := public.LoadPublicConfig()
405+
if err != nil && err != public.ErrNoPublicConfig {
406+
return errors.Wrapf(err, "failed to load current public configuration")
407+
}
408+
409+
if current != nil && current.Equal(cfg) {
410+
// nothing to do
411+
return nil
412+
}
413+
414+
if err := public.SavePublicConfig(cfg); err != nil {
415+
return errors.Wrap(err, "failed to store public config")
416+
}
417+
418+
return nil
419+
}
420+
421+
func (n *networker) LoadPublicConfig() (pkg.PublicConfig, error) {
422+
cfg, err := public.LoadPublicConfig()
423+
return *cfg, err
424+
}
425+
393426
func CreateNDMZBridge() (*netlink.Bridge, error) {
394427
return createNDMZBridge(NDMZBridge, NDMZGw)
395428
}

pkg/network_light.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ type NetworkerLight interface {
2929
Ready() error
3030
ZOSAddresses(ctx context.Context) <-chan NetlinkAddresses
3131
GetSubnet(networkID NetID) (net.IPNet, error)
32+
SetPublicConfig(cfg PublicConfig) error
33+
UnSetPublicConfig() error
34+
LoadPublicConfig() (PublicConfig, error)
3235
}
3336

3437
type TapDevice struct {

0 commit comments

Comments
 (0)