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 for zos light #3

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
23 changes: 12 additions & 11 deletions pkg/gateway_light/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/threefoldtech/zosbase/pkg/cache"
"github.com/threefoldtech/zosbase/pkg/gridtypes"
"github.com/threefoldtech/zosbase/pkg/gridtypes/zos"
"github.com/threefoldtech/zosbase/pkg/kernel"
"github.com/threefoldtech/zosbase/pkg/netlight/types"
"github.com/threefoldtech/zosbase/pkg/stubs"
"github.com/threefoldtech/zosbase/pkg/zinit"
Expand Down Expand Up @@ -342,8 +341,11 @@ func (g *gatewayModule) validateNameContracts() error {
ctx, cancel := context.WithTimeout(context.Background(), validationPeriod/2)
defer cancel()
e := stubs.NewProvisionStub(g.cl)
baseDomain, found := kernel.GetParams().GetOne("domain")
if !found {

netStub := stubs.NewNetworkerLightStub(g.cl)
config, err := netStub.LoadPublicConfig(context.Background())
baseDomain := config.Domain
if baseDomain == "" || err == nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a bit confusing
u r calling config.domain without checking the error returned from loadPublicConfig the method returns nil in case of error which will result in an error when calling config.domain
so plz check the error first then u can call config.Domain
this line

if baseDomain == "" || err == nil {

should be

if baseDomain == "" || err != nil {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, this is wrong, it was a wip comment fixed in zoslight repo but didn't do the commit here https://github.com/threefoldtech/zoslight/blob/ff8a7a63a0c9c717a3e2ceed73112e8fbf99da83/pkg/gateway/gateway.go#L348

in case an err, config.Domain will be empty anyway. but you are right cleaner to check for the error first

// domain doesn't exist so no name workloads exist
return nil
}
Expand Down Expand Up @@ -425,12 +427,12 @@ func (g *gatewayModule) traefikBinary(ctx context.Context, z *zinit.Client) (str
// ensureGateway makes sure that gateway infrastructure is in place and
// that it is supported.
func (g *gatewayModule) ensureGateway(ctx context.Context, forceResstart bool) (string, error) {
var (
flistd = stubs.NewFlisterStub(g.cl)
)
flistd := stubs.NewFlisterStub(g.cl)

domain, found := kernel.GetParams().GetOne("domain")
if !found {
netStub := stubs.NewNetworkerLightStub(g.cl)
config, err := netStub.LoadPublicConfig(context.Background())
domain := config.Domain
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as the above comment

if domain == "" || err != nil {
return "", fmt.Errorf("gateway is not supported on this node, domain is not set")
}

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

//other wise we start traefik
// other wise we start traefik
return domain, g.startTraefik(z)
}

func (g *gatewayModule) verifyDomainDestination(ctx context.Context, domain string) error {
networker := stubs.NewNetworkerLightStub(g.cl)

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

func (g *gatewayModule) startTraefik(z *zinit.Client) error {

cmd := fmt.Sprintf(
"%s --configfile %s",
g.binPath,
Expand Down Expand Up @@ -540,7 +542,6 @@ func (g *gatewayModule) configPath(name string) string {
}

func (g *gatewayModule) validateNameContract(name string, twinID uint32) error {

contractID, subErr := g.substrateGateway.GetContractIDByNameRegistration(context.Background(), name)
if subErr.IsCode(pkg.CodeNotFound) {
return ErrContractNotReserved
Expand Down
14 changes: 10 additions & 4 deletions pkg/monitord/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import (
"github.com/shirou/gopsutil/disk"
"github.com/shirou/gopsutil/mem"
"github.com/shirou/gopsutil/net"
"github.com/threefoldtech/zbus"
"github.com/threefoldtech/zosbase/pkg"
"github.com/threefoldtech/zosbase/pkg/gridtypes/zos"
"github.com/threefoldtech/zosbase/pkg/kernel"
"github.com/threefoldtech/zosbase/pkg/stubs"
)

var _ pkg.SystemMonitor = (*systemMonitor)(nil)
Expand All @@ -20,15 +22,16 @@ var _ pkg.SystemMonitor = (*systemMonitor)(nil)
type systemMonitor struct {
duration time.Duration
node uint32
cl zbus.Client
}

// NewSystemMonitor creates new system of system monitor
func NewSystemMonitor(node uint32, duration time.Duration) (pkg.SystemMonitor, error) {
func NewSystemMonitor(node uint32, duration time.Duration, cl zbus.Client) (pkg.SystemMonitor, error) {
if duration == 0 {
duration = 2 * time.Second
}

return &systemMonitor{duration: duration, node: node}, nil
return &systemMonitor{duration: duration, node: node, cl: cl}, nil
}

func (m *systemMonitor) NodeID() uint32 {
Expand Down Expand Up @@ -215,8 +218,11 @@ func (n *systemMonitor) GetNodeFeatures() []pkg.NodeFeature {
pkg.NodeFeature("mycelium"),
}
feat = append(feat, zosLightFeat...)
_, found := kernel.GetParams().GetOne("domain")
if found {

netStub := stubs.NewNetworkerLightStub(n.cl)
config, err := netStub.LoadPublicConfig(context.Background())

if config.Domain != "" && err == nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as first comment

feat = append(feat, pkg.NodeFeature("gateway"))
}
return feat
Expand Down
44 changes: 37 additions & 7 deletions pkg/netlight/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/threefoldtech/zosbase/pkg/netlight/ipam"
"github.com/threefoldtech/zosbase/pkg/netlight/namespace"
"github.com/threefoldtech/zosbase/pkg/netlight/options"
"github.com/threefoldtech/zosbase/pkg/netlight/public"
"github.com/threefoldtech/zosbase/pkg/netlight/resource"
"github.com/threefoldtech/zosbase/pkg/versioned"
"github.com/vishvananda/netlink"
Expand All @@ -39,12 +40,10 @@ const (
networkDir = "networks"
)

var (
NDMZGwIP = &net.IPNet{
IP: net.ParseIP("100.127.0.1"),
Mask: net.CIDRMask(16, 32),
}
)
var NDMZGwIP = &net.IPNet{
IP: net.ParseIP("100.127.0.1"),
Mask: net.CIDRMask(16, 32),
}

var NetworkSchemaLatestVersion = semver.MustParse("0.1.0")

Expand Down Expand Up @@ -90,7 +89,6 @@ func (n *networker) Delete(name string) error {
}

return resource.Delete(name)

}

func (n *networker) AttachPrivate(name, id string, vmIp net.IP) (device localPkg.TapDevice, err error) {
Expand Down Expand Up @@ -391,6 +389,38 @@ func (n *networker) Interfaces(iface string, netns string) (pkg.Interfaces, erro
return pkg.Interfaces{Interfaces: interfaces}, nil
}

func (n *networker) UnSetPublicConfig() error {
return public.DeletePublicConfig()
}

// Set node public namespace config
func (n *networker) SetPublicConfig(cfg pkg.PublicConfig) error {
if cfg.Equal(pkg.PublicConfig{}) {
return fmt.Errorf("public config cannot be unset, only modified")
}

current, err := public.LoadPublicConfig()
if err != nil && err != public.ErrNoPublicConfig {
return errors.Wrapf(err, "failed to load current public configuration")
}

if current != nil && current.Equal(cfg) {
// nothing to do
return nil
}

if err := public.SavePublicConfig(cfg); err != nil {
return errors.Wrap(err, "failed to store public config")
}

return nil
}

func (n *networker) LoadPublicConfig() (pkg.PublicConfig, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need for this method u already calling public.loadPublicConfig without changes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cfg, err := public.LoadPublicConfig()
return *cfg, err
}

func CreateNDMZBridge() (*netlink.Bridge, error) {
return createNDMZBridge(NDMZBridge, NDMZGw)
}
Expand Down
6 changes: 2 additions & 4 deletions pkg/netlight/public/persist.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ const (
publicConfigFile = "public-config.json"
)

var (
// persistencePath is path to config file.
persistencePath = ""
)
// persistencePath is path to config file.
var persistencePath = ""

func SetPersistence(path string) {
stat, err := os.Stat(path)
Expand Down
3 changes: 3 additions & 0 deletions pkg/network_light.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ type NetworkerLight interface {
Ready() error
ZOSAddresses(ctx context.Context) <-chan NetlinkAddresses
GetSubnet(networkID NetID) (net.IPNet, error)
SetPublicConfig(cfg PublicConfig) error
UnSetPublicConfig() error
LoadPublicConfig() (PublicConfig, error)
}

type TapDevice struct {
Expand Down
47 changes: 47 additions & 0 deletions pkg/stubs/network_light_stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,23 @@ func (s *NetworkerLightStub) Interfaces(ctx context.Context, arg0 string, arg1 s
return
}

func (s *NetworkerLightStub) LoadPublicConfig(ctx context.Context) (ret0 pkg.PublicConfig, ret1 error) {
args := []interface{}{}
result, err := s.client.RequestContext(ctx, s.module, s.object, "LoadPublicConfig", args...)
if err != nil {
panic(err)
}
result.PanicOnError()
ret1 = result.CallError()
loader := zbus.Loader{
&ret0,
}
if err := result.Unmarshal(&loader); err != nil {
panic(err)
}
return
}

func (s *NetworkerLightStub) Namespace(ctx context.Context, arg0 string) (ret0 string) {
args := []interface{}{arg0}
result, err := s.client.RequestContext(ctx, s.module, s.object, "Namespace", args...)
Expand Down Expand Up @@ -190,6 +207,36 @@ func (s *NetworkerLightStub) Ready(ctx context.Context) (ret0 error) {
return
}

func (s *NetworkerLightStub) SetPublicConfig(ctx context.Context, arg0 pkg.PublicConfig) (ret0 error) {
args := []interface{}{arg0}
result, err := s.client.RequestContext(ctx, s.module, s.object, "SetPublicConfig", args...)
if err != nil {
panic(err)
}
result.PanicOnError()
ret0 = result.CallError()
loader := zbus.Loader{}
if err := result.Unmarshal(&loader); err != nil {
panic(err)
}
return
}

func (s *NetworkerLightStub) UnSetPublicConfig(ctx context.Context) (ret0 error) {
args := []interface{}{}
result, err := s.client.RequestContext(ctx, s.module, s.object, "UnSetPublicConfig", args...)
if err != nil {
panic(err)
}
result.PanicOnError()
ret0 = result.CallError()
loader := zbus.Loader{}
if err := result.Unmarshal(&loader); err != nil {
panic(err)
}
return
}

func (s *NetworkerLightStub) ZDBIPs(ctx context.Context, arg0 string) (ret0 [][]uint8, ret1 error) {
args := []interface{}{arg0}
result, err := s.client.RequestContext(ctx, s.module, s.object, "ZDBIPs", args...)
Expand Down