Files
kairos-agent/internal/role/auto.go
Ettore Di Giacinto 63cd28d1cb Split off cli into separate binaries (#37)
* 🎨 Split off cli into separate binaries

This commit splits off the cli into 3 binaries:
- agent
- cli
- provider

The provider now is a separate component that can be tested by itself
and have its own lifecycle. This paves the way to a ligher c3os variant,
HA support and other features that can be provided on runtime.

This is working, but still there are low hanging fruit to care about.

Fixes #14

* 🤖 Add provider bin to releases

* ⚙️ Handle signals

* ⚙️ Reduce buildsize footprint

* 🎨 Scan for providers also in /system/providers

* 🤖 Run goreleaser

* 🎨 Refactoring
2022-07-04 22:39:34 +02:00

58 lines
1.4 KiB
Go

package role
import (
"github.com/c3os-io/c3os/pkg/config"
utils "github.com/mudler/edgevpn/pkg/utils"
service "github.com/mudler/edgevpn/api/client/service"
)
func contains(slice []string, elem string) bool {
for _, s := range slice {
if elem == s {
return true
}
}
return false
}
func Auto(cc *config.Config) Role {
return func(c *service.RoleConfig) error {
advertizing, _ := c.Client.AdvertizingNodes()
actives, _ := c.Client.ActiveNodes()
c.Logger.Info("Active nodes:", actives)
c.Logger.Info("Advertizing nodes:", advertizing)
if len(advertizing) < 2 {
c.Logger.Info("Not enough nodes")
return nil
}
// first get available nodes
nodes := advertizing
shouldBeLeader := utils.Leader(advertizing)
lead, _ := c.Client.Get("auto", "leader")
// From now on, only the leader keeps processing
// TODO: Make this more reliable with consensus
if shouldBeLeader != c.UUID && lead != c.UUID {
c.Logger.Infof("<%s> not a leader, leader is '%s', sleeping", c.UUID, shouldBeLeader)
return nil
}
if shouldBeLeader == c.UUID && (lead == "" || !contains(nodes, lead)) {
c.Client.Set("auto", "leader", c.UUID)
c.Logger.Info("Announcing ourselves as leader, backing off")
return nil
}
if lead != c.UUID {
c.Logger.Info("Backing off, as we are not currently flagged as leader")
return nil
}
return scheduleRoles(nodes, c, cc)
}
}