Skip to content

Commit

Permalink
Integrate mycelium in zos (#2342)
Browse files Browse the repository at this point in the history
* set up mycelium in a namespace

* add mycelium to networkd

* run mycelium in networkd

* add mycelium to networkder

* add subnet function

* rename dummy device

* download flists in namespace

* fix flist tests

* add mycelium adr

* fix flist failure

* add mycelium support for zdbs

* fix mycelium ip returns as publicIp

* log failure in ensuring mycelium for zdb

* rename mycelium constants

* remove zdb ensure mycelium

* run ensure container setup even if container is running
  • Loading branch information
Eslam-Nawara authored Jun 10, 2024
1 parent 8cc357d commit fd7d15f
Show file tree
Hide file tree
Showing 18 changed files with 1,216 additions and 154 deletions.
1 change: 1 addition & 0 deletions bins/packages/vector/files/vector.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ sources:
- b-*
- br-*
- dumdum
- mydumdum
- dummy*
- tozos*
filesystem:
Expand Down
125 changes: 91 additions & 34 deletions cmds/modules/networkd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/pkg/errors"
"github.com/threefoldtech/zos/pkg/environment"
"github.com/threefoldtech/zos/pkg/network/dhcp"
"github.com/threefoldtech/zos/pkg/network/mycelium"
"github.com/threefoldtech/zos/pkg/network/public"
"github.com/threefoldtech/zos/pkg/network/types"
"github.com/threefoldtech/zos/pkg/zinit"
Expand Down Expand Up @@ -110,47 +111,25 @@ func action(cli *cli.Context) error {
if err := dmz.Create(ctx); err != nil {
return errors.Wrap(err, "failed to create ndmz")
}
log.Debug().Msg("starting yggdrasil")
yggNamespace := dmz.Namespace()

namespace := dmz.Namespace()
if public.HasPublicSetup() {
yggNamespace = public.PublicNamespace
namespace = public.PublicNamespace
}

yggNs, err := yggdrasil.NewYggdrasilNamespace(yggNamespace)
log.Debug().Msg("starting yggdrasil")
ygg, err := setupYgg(ctx, namespace, dmz.Namespace(), identity.PrivateKey(cli.Context))
if err != nil {
return errors.Wrap(err, "failed to create yggdrasil namespace")
return err
}

ygg, err := yggdrasil.EnsureYggdrasil(ctx, identity.PrivateKey(cli.Context), yggNs)
log.Debug().Msg("starting mycelium")
mycelium, err := setupMycelium(ctx, namespace, dmz.Namespace(), identity.PrivateKey(cli.Context))
if err != nil {
return errors.Wrap(err, "failed to start yggdrasil")
}

if public.HasPublicSetup() {
// if yggdrasil is living inside public namespace
// we still need to setup ndmz to also have yggdrasil but we set the yggdrasil interface
// a different Ip that lives inside the yggdrasil range.
dmzYgg, err := yggdrasil.NewYggdrasilNamespace(dmz.Namespace())
if err != nil {
return errors.Wrap(err, "failed to setup ygg for dmz namespace")
}

ip, err := ygg.SubnetFor([]byte(fmt.Sprintf("ygg:%s", dmz.Namespace())))
if err != nil {
return errors.Wrap(err, "failed to calculate ip for ygg inside dmz")
}

gw, err := ygg.Gateway()
if err != nil {
return err
}

if err := dmzYgg.SetYggIP(ip, gw.IP); err != nil {
return errors.Wrap(err, "failed to set yggdrasil ip for dmz")
}
return err
}

networker, err := network.NewNetworker(identity, dmz, ygg)
networker, err := network.NewNetworker(identity, dmz, ygg, mycelium)
if err != nil {
return errors.Wrap(err, "error creating network manager")
}
Expand All @@ -164,7 +143,6 @@ func action(cli *cli.Context) error {
}

func startZBusServer(ctx context.Context, broker string, networker pkg.Networker) error {

server, err := zbus.NewRedisServer(module, broker, 1)
if err != nil {
log.Error().Err(err).Msgf("fail to connect to message broker server")
Expand All @@ -187,7 +165,7 @@ func startZBusServer(ctx context.Context, broker string, networker pkg.Networker
func waitYggdrasilBin() {
log.Info().Msg("wait for yggdrasil binary to be available")
bo := backoff.NewExponentialBackOff()
bo.MaxElapsedTime = 0 //forever
bo.MaxElapsedTime = 0 // forever
_ = backoff.RetryNotify(func() error {
_, err := exec.LookPath("yggdrasil")
return err
Expand All @@ -212,3 +190,82 @@ func migrateOlderDHCPService() error {

return nil
}

func setupYgg(ctx context.Context, namespace, dmzNs string, privateKey []byte) (ygg *yggdrasil.YggServer, err error) {
yggNs, err := yggdrasil.NewYggdrasilNamespace(namespace)
if err != nil {
return ygg, errors.Wrap(err, "failed to create yggdrasil namespace")
}

ygg, err = yggdrasil.EnsureYggdrasil(ctx, privateKey, yggNs)
if err != nil {
return ygg, errors.Wrap(err, "failed to start yggdrasil")
}

if public.HasPublicSetup() {
// if yggdrasil is living inside public namespace
// we still need to setup ndmz to also have yggdrasil but we set the yggdrasil interface
// a different Ip that lives inside the yggdrasil range.
dmzYgg, err := yggdrasil.NewYggdrasilNamespace(dmzNs)
if err != nil {
return ygg, errors.Wrap(err, "failed to setup ygg for dmz namespace")
}

ip, err := ygg.SubnetFor([]byte(fmt.Sprintf("ygg:%s", dmzNs)))
if err != nil {
return ygg, errors.Wrap(err, "failed to calculate ip for ygg inside dmz")
}

gw, err := ygg.Gateway()
if err != nil {
return ygg, err
}

if err := dmzYgg.SetYggIP(ip, gw.IP); err != nil {
return ygg, errors.Wrap(err, "failed to set yggdrasil ip for dmz")
}
}
return
}

func setupMycelium(ctx context.Context, namespace, dmzNs string, privateKey []byte) (myc *mycelium.MyceliumServer, err error) {
myNs, err := mycelium.NewMyNamespace(namespace)
if err != nil {
return myc, errors.Wrap(err, "failed to create mycelium namespace")
}

myc, err = mycelium.EnsureMycelium(ctx, privateKey, myNs)
if err != nil {
return myc, errors.Wrap(err, "failed to start mycelium")
}

if public.HasPublicSetup() {
// if mycelium is living inside public namespace
// we still need to setup ndmz to also have mycelium but we set the mycelium interface
// a different Ip that lives inside the mycelium range.
dmzMy, err := mycelium.NewMyNamespace(dmzNs)
if err != nil {
return myc, errors.Wrap(err, "failed to setup mycelium for dmz namespace")
}

inspcet, err := myc.InspectMycelium()
if err != nil {
return myc, err
}

ip, err := inspcet.IPFor([]byte(fmt.Sprintf("my:%s", dmzNs)))
if err != nil {
return myc, errors.Wrap(err, "failed to calculate ip for mycelium inside dmz")
}

gw, err := inspcet.Gateway()
if err != nil {
return myc, err
}

if err := dmzMy.SetMyIP(ip, gw.IP); err != nil {
return myc, errors.Wrap(err, "failed to set mycelium ip for dmz")
}
}
return
}
19 changes: 19 additions & 0 deletions docs/architecture/decisions/0010-mycelium.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# 1. Mycelium

Date: 2024-05-29

## Status

Accepted

## Context

Support mycelium network for zdbs and zos host to get flists over mycelium

## Decision

Integrate mycelium in zos and allow zos host to have mycelium IPs, mount flists over mycelium, and support mycelium on zdbs

## Consequences

Using mycelium IP is optional. Old clients should work normally without breakage.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ require (
github.com/moby/sys/mountinfo v0.6.2 // indirect
github.com/ncruces/go-strftime v0.1.9 // indirect
github.com/nsf/termbox-go v1.1.1 // indirect
github.com/oasisprotocol/curve25519-voi v0.0.0-20230904125328-1f23a7beb09a // indirect
github.com/onsi/ginkgo v1.16.4 // indirect
github.com/onsi/gomega v1.16.0 // indirect
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,8 @@ github.com/nsf/termbox-go v1.1.1/go.mod h1:T0cTdVuOwf7pHQNtfhnEbzHbcNyCEcVU4YPpo
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
github.com/oasisprotocol/curve25519-voi v0.0.0-20230904125328-1f23a7beb09a h1:dlRvE5fWabOchtH7znfiFCcOvmIYgOeAS5ifBXBlh9Q=
github.com/oasisprotocol/curve25519-voi v0.0.0-20230904125328-1f23a7beb09a/go.mod h1:hVoHR2EVESiICEMbg137etN/Lx+lSrHPTD39Z/uE+2s=
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=
Expand Down
Loading

0 comments on commit fd7d15f

Please sign in to comment.