From b2a3963a5e21bdfbbbcfdee06adf0ca27f512980 Mon Sep 17 00:00:00 2001 From: Mauro Morales Date: Thu, 20 Mar 2025 22:25:41 +0100 Subject: [PATCH] Move common Signed-off-by: Mauro Morales --- internal/provider/bootstrap.go | 19 ++++++++++--------- internal/role/common.go | 11 +++++++++-- internal/role/p2p/common.go | 8 -------- internal/role/p2p/k0s.go | 3 ++- internal/role/p2p/k3s.go | 5 +++-- internal/role/p2p/worker.go | 3 ++- internal/role/schedule.go | 10 ++++------ 7 files changed, 30 insertions(+), 29 deletions(-) diff --git a/internal/provider/bootstrap.go b/internal/provider/bootstrap.go index 6458e14..7ec0f59 100644 --- a/internal/provider/bootstrap.go +++ b/internal/provider/bootstrap.go @@ -14,6 +14,7 @@ import ( "github.com/kairos-io/kairos-sdk/utils" providerConfig "github.com/kairos-io/provider-kairos/v2/internal/provider/config" "github.com/kairos-io/provider-kairos/v2/internal/role" + common "github.com/kairos-io/provider-kairos/v2/internal/role" p2p "github.com/kairos-io/provider-kairos/v2/internal/role/p2p" edgeVPNClient "github.com/mudler/edgevpn/api/client" @@ -111,26 +112,26 @@ func Bootstrap(e *pluggable.Event) pluggable.EventResponse { service.WithUUID(machine.UUID()), service.WithStateDir("/usr/local/.kairos/state"), service.WithNetworkToken(prvConfig.P2P.NetworkToken), - service.WithPersistentRoles(p2p.RoleAuto), + service.WithPersistentRoles(common.RoleAuto), service.WithRoles( service.RoleKey{ - Role: p2p.RoleControlPlane, - RoleHandler: p2p.ControlPlane(c, prvConfig, p2p.RoleControlPlane), + Role: common.RoleControlPlane, + RoleHandler: p2p.ControlPlane(c, prvConfig, common.RoleControlPlane), }, service.RoleKey{ - Role: p2p.RoleControlPlaneClusterInit, - RoleHandler: p2p.ControlPlane(c, prvConfig, p2p.RoleControlPlaneClusterInit), + Role: common.RoleControlPlaneClusterInit, + RoleHandler: p2p.ControlPlane(c, prvConfig, common.RoleControlPlaneClusterInit), }, service.RoleKey{ - Role: p2p.RoleControlPlaneHA, - RoleHandler: p2p.ControlPlane(c, prvConfig, p2p.RoleControlPlaneHA), + Role: common.RoleControlPlaneHA, + RoleHandler: p2p.ControlPlane(c, prvConfig, common.RoleControlPlaneHA), }, service.RoleKey{ - Role: p2p.RoleWorker, + Role: common.RoleWorker, RoleHandler: p2p.Worker(c, prvConfig), }, service.RoleKey{ - Role: p2p.RoleAuto, + Role: common.RoleAuto, RoleHandler: role.Auto(c, prvConfig), }, ), diff --git a/internal/role/common.go b/internal/role/common.go index bf7fde2..b32e995 100644 --- a/internal/role/common.go +++ b/internal/role/common.go @@ -1,12 +1,19 @@ package role import ( - "io/ioutil" // nolint "os" service "github.com/mudler/edgevpn/api/client/service" ) +const ( + RoleWorker = "worker" + RoleControlPlane = "master" + RoleControlPlaneHA = "master/ha" + RoleControlPlaneClusterInit = "master/clusterinit" + RoleAuto = "auto" +) + type Role func(*service.RoleConfig) error func SentinelExist() bool { @@ -17,7 +24,7 @@ func SentinelExist() bool { } func CreateSentinel() error { - return ioutil.WriteFile("/usr/local/.kairos/deployed", []byte{}, os.ModePerm) + return os.WriteFile("/usr/local/.kairos/deployed", []byte{}, os.ModePerm) } func getRoles(client *service.Client, nodes []string) ([]string, map[string]string) { diff --git a/internal/role/p2p/common.go b/internal/role/p2p/common.go index 44bfe3b..bb83fbd 100644 --- a/internal/role/p2p/common.go +++ b/internal/role/p2p/common.go @@ -7,14 +7,6 @@ import ( providerConfig "github.com/kairos-io/provider-kairos/v2/internal/provider/config" ) -const ( - RoleWorker = "worker" - RoleControlPlane = "master" - RoleControlPlaneHA = "master/ha" - RoleControlPlaneClusterInit = "master/clusterinit" - RoleAuto = "auto" -) - func guessInterface(pconfig *providerConfig.Config) string { if pconfig.KubeVIP.Interface != "" { return pconfig.KubeVIP.Interface diff --git a/internal/role/p2p/k0s.go b/internal/role/p2p/k0s.go index 3266f9e..88b0601 100644 --- a/internal/role/p2p/k0s.go +++ b/internal/role/p2p/k0s.go @@ -9,6 +9,7 @@ import ( "github.com/kairos-io/kairos-sdk/machine" "github.com/kairos-io/kairos-sdk/utils" providerConfig "github.com/kairos-io/provider-kairos/v2/internal/provider/config" + common "github.com/kairos-io/provider-kairos/v2/internal/role" service "github.com/mudler/edgevpn/api/client/service" "gopkg.in/yaml.v2" ) @@ -213,7 +214,7 @@ func (k *K0sWorker) RoleConfig() *service.RoleConfig { } func (k *K0sControlPlane) HA() bool { - return k.role == RoleControlPlaneHA + return k.role == common.RoleControlPlaneHA } func (k *K0sControlPlane) ClusterInit() bool { diff --git a/internal/role/p2p/k3s.go b/internal/role/p2p/k3s.go index c3ee2c8..4824a87 100644 --- a/internal/role/p2p/k3s.go +++ b/internal/role/p2p/k3s.go @@ -10,6 +10,7 @@ import ( "github.com/kairos-io/kairos-sdk/machine" "github.com/kairos-io/kairos-sdk/utils" providerConfig "github.com/kairos-io/provider-kairos/v2/internal/provider/config" + common "github.com/kairos-io/provider-kairos/v2/internal/role" service "github.com/mudler/edgevpn/api/client/service" ) @@ -176,11 +177,11 @@ func (k *K3sWorker) RoleConfig() *service.RoleConfig { } func (k *K3sControlPlane) HA() bool { - return k.role == RoleControlPlaneHA + return k.role == common.RoleControlPlaneHA } func (k *K3sControlPlane) ClusterInit() bool { - return k.role == RoleControlPlaneClusterInit + return k.role == common.RoleControlPlaneClusterInit } func (k *K3sControlPlane) IP() string { diff --git a/internal/role/p2p/worker.go b/internal/role/p2p/worker.go index d00ccfd..b79c8b6 100644 --- a/internal/role/p2p/worker.go +++ b/internal/role/p2p/worker.go @@ -9,6 +9,7 @@ import ( providerConfig "github.com/kairos-io/provider-kairos/v2/internal/provider/config" "github.com/kairos-io/provider-kairos/v2/internal/role" + common "github.com/kairos-io/provider-kairos/v2/internal/role" service "github.com/mudler/edgevpn/api/client/service" ) @@ -41,7 +42,7 @@ func Worker(cc *config.Config, pconfig *providerConfig.Config) role.Role { //nol } ip := guessIP(pconfig) - worker.SetRole(RoleWorker) + worker.SetRole(common.RoleWorker) worker.SetRoleConfig(c) worker.SetIP(ip) diff --git a/internal/role/schedule.go b/internal/role/schedule.go index e6c645c..2190a66 100644 --- a/internal/role/schedule.go +++ b/internal/role/schedule.go @@ -34,8 +34,6 @@ func scheduleRoles(nodes []string, c *service.RoleConfig, cc *config.Config, pco hasControlPlane := false controlPlaneRole := "master" - workerRole := "worker" - controlPlaneHA := "master/ha" if pconfig.P2P.Auto.HA.IsEnabled() { controlPlaneRole = "master/clusterinit" @@ -46,7 +44,7 @@ func scheduleRoles(nodes []string, c *service.RoleConfig, cc *config.Config, pco switch r { case controlPlaneRole: hasControlPlane = true - case controlPlaneHA: + case RoleControlPlaneHA: controlPlaneCounter++ } } @@ -87,7 +85,7 @@ func scheduleRoles(nodes []string, c *service.RoleConfig, cc *config.Config, pco if pconfig.P2P.Auto.HA.IsEnabled() && pconfig.P2P.Auto.HA.ExtraControlPlanes != nil && *pconfig.P2P.Auto.HA.ExtraControlPlanes() != controlPlaneCounter { if len(unassignedNodes) > 0 { - if err := c.Client.Set("role", unassignedNodes[0], controlPlaneHA); err != nil { + if err := c.Client.Set("role", unassignedNodes[0], RoleControlPlaneHA); err != nil { c.Logger.Error(err) return err } @@ -99,11 +97,11 @@ func scheduleRoles(nodes []string, c *service.RoleConfig, cc *config.Config, pco // cycle all empty roles and assign worker roles for _, uuid := range unassignedNodes { - if err := c.Client.Set("role", uuid, workerRole); err != nil { + if err := c.Client.Set("role", uuid, RoleWorker); err != nil { c.Logger.Error(err) return err } - c.Logger.Infof("-> Set %s to %s", workerRole, uuid) + c.Logger.Infof("-> Set %s to %s", RoleWorker, uuid) } c.Logger.Info("Done scheduling")