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

Use kairos logger #637

Merged
merged 4 commits into from
Sep 18, 2024
Merged
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
2 changes: 1 addition & 1 deletion Earthfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
VERSION 0.6

# renovate: datasource=docker depName=golang
ARG GO_VERSION=1.22
ARG GO_VERSION=1.23
ARG TARGETARCH
ARG CGO_ENABLED=0

Expand Down
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/kairos-io/provider-kairos/v2

go 1.22.5
go 1.23.1

replace github.com/elastic/gosigar => github.com/mudler/gosigar v0.14.3-0.20220502202347-34be910bdaaf

Expand All @@ -10,7 +10,7 @@ require (
github.com/ipfs/go-log/v2 v2.5.1
github.com/kairos-io/go-nodepair v0.2.0
github.com/kairos-io/kairos-agent/v2 v2.14.0
github.com/kairos-io/kairos-sdk v0.4.2
github.com/kairos-io/kairos-sdk v0.4.4
github.com/mudler/edgevpn v0.28.3
github.com/mudler/go-pluggable v0.0.0-20230126220627-7710299a0ae5
github.com/mudler/go-processmanager v0.0.0-20240820160718-8b802d3ecf82
Expand Down Expand Up @@ -49,7 +49,7 @@ require (
github.com/cloudflare/circl v1.3.7 // indirect
github.com/containerd/cgroups v1.1.0 // indirect
github.com/containerd/console v1.0.4-0.20230706203907-8f6c4e4faef5 // indirect
github.com/containerd/containerd v1.7.21 // indirect
github.com/containerd/containerd v1.7.22 // indirect
github.com/containerd/continuity v0.4.2 // indirect
github.com/containerd/errdefs v0.1.0 // indirect
github.com/containerd/log v0.1.0 // indirect
Expand All @@ -68,7 +68,7 @@ require (
github.com/djherbis/times v1.6.0 // indirect
github.com/docker/cli v27.1.1+incompatible // indirect
github.com/docker/distribution v2.8.3+incompatible // indirect
github.com/docker/docker v27.2.0+incompatible // indirect
github.com/docker/docker v27.2.1+incompatible // indirect
github.com/docker/docker-credential-helpers v0.7.0 // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
Expand Down Expand Up @@ -296,7 +296,7 @@ require (
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa // indirect
golang.org/x/image v0.18.0 // indirect
golang.org/x/mod v0.20.0 // indirect
golang.org/x/mod v0.21.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.25.0 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ github.com/containerd/containerd v1.7.20 h1:Sl6jQYk3TRavaU83h66QMbI2Nqg9Jm6qzwX5
github.com/containerd/containerd v1.7.20/go.mod h1:52GsS5CwquuqPuLncsXwG0t2CiUce+KsNHJZQJvAgR0=
github.com/containerd/containerd v1.7.21 h1:USGXRK1eOC/SX0L195YgxTHb0a00anxajOzgfN0qrCA=
github.com/containerd/containerd v1.7.21/go.mod h1:e3Jz1rYRUZ2Lt51YrH9Rz0zPyJBOlSvB3ghr2jbVD8g=
github.com/containerd/containerd v1.7.22 h1:nZuNnNRA6T6jB975rx2RRNqqH2k6ELYKDZfqTHqwyy0=
github.com/containerd/containerd v1.7.22/go.mod h1:e3Jz1rYRUZ2Lt51YrH9Rz0zPyJBOlSvB3ghr2jbVD8g=
github.com/containerd/continuity v0.4.2 h1:v3y/4Yz5jwnvqPKJJ+7Wf93fyWoCB3F5EclWG023MDM=
github.com/containerd/continuity v0.4.2/go.mod h1:F6PTNCKepoxEaXLQp3wDAjygEnImnZ/7o4JzpodfroQ=
github.com/containerd/errdefs v0.1.0 h1:m0wCRBiu1WJT/Fr+iOoQHMQS/eP5myQ8lCv4Dz5ZURM=
Expand Down Expand Up @@ -180,6 +182,8 @@ github.com/docker/docker v27.1.2+incompatible h1:AhGzR1xaQIy53qCkxARaFluI00WPGtX
github.com/docker/docker v27.1.2+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
github.com/docker/docker v27.2.0+incompatible h1:Rk9nIVdfH3+Vz4cyI/uhbINhEZ/oLmc+CBXmH6fbNk4=
github.com/docker/docker v27.2.0+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
github.com/docker/docker v27.2.1+incompatible h1:fQdiLfW7VLscyoeYEBz7/J8soYFDZV1u6VW6gJEjNMI=
github.com/docker/docker v27.2.1+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
github.com/docker/docker-credential-helpers v0.7.0 h1:xtCHsjxogADNZcdv1pKUHXryefjlVRqWqIhk/uXJp0A=
github.com/docker/docker-credential-helpers v0.7.0/go.mod h1:rETQfLdHNT3foU5kuNkFR1R1V12OJRRO5lzt2D1b5X0=
github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKohAFqRJQ=
Expand Down Expand Up @@ -445,6 +449,8 @@ github.com/kairos-io/kairos-sdk v0.4.1 h1:WF+X30URojMxV7AlzVj0uejvPWG4zq1WNga0sw
github.com/kairos-io/kairos-sdk v0.4.1/go.mod h1:lgQAYkh0aWIZg4/CQcC+OPQp95ONs2PzkMIcAq8w6OY=
github.com/kairos-io/kairos-sdk v0.4.2 h1:tq0a6R9qSotatFt20q/+oIHJSN+XTQWji4XgHk5Vvlw=
github.com/kairos-io/kairos-sdk v0.4.2/go.mod h1:1x8VaVt1ppXxQUlNyDRksoDglR423hxfEtjx6qWDO7w=
github.com/kairos-io/kairos-sdk v0.4.4 h1:FolUGqpdTnYYdLwFEOND97QEp2H/htVjW7pHUOmtxNI=
github.com/kairos-io/kairos-sdk v0.4.4/go.mod h1:OIJYihhuiUOeBXHYj9V3R381SAgc/EaKrFepcBxGroM=
github.com/kbinani/screenshot v0.0.0-20230812210009-b87d31814237 h1:YOp8St+CM/AQ9Vp4XYm4272E77MptJDHkwypQHIRl9Q=
github.com/kbinani/screenshot v0.0.0-20230812210009-b87d31814237/go.mod h1:e7qQlOY68wOz4b82D7n+DdaptZAi+SHW0+yKiWZzEYE=
github.com/kendru/darwin/go/depgraph v0.0.0-20221105232959-877d6a81060c h1:eKb4PqwAMhlqwXw0W3atpKaYaPGlXE/Fwh+xpCEYaPk=
Expand Down Expand Up @@ -1154,6 +1160,8 @@ golang.org/x/mod v0.19.0 h1:fEdghXQSo20giMthA7cd28ZC+jts4amQ3YMXiP5oMQ8=
golang.org/x/mod v0.19.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/mod v0.20.0 h1:utOm6MM3R3dnawAiJgn0y+xvuYRsm1RKM/4giyfDgV0=
golang.org/x/mod v0.20.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/mod v0.21.0 h1:vvrHzRwRfVKSiLrG+d4FMl/Qi4ukBCE6kZlTUkDYRT0=
golang.org/x/mod v0.21.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down
79 changes: 30 additions & 49 deletions internal/provider/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,16 @@ import (
"fmt"
"strings"

logging "github.com/ipfs/go-log/v2"
edgeVPNClient "github.com/mudler/edgevpn/api/client"
"go.uber.org/zap"

"github.com/kairos-io/kairos-sdk/bus"
"github.com/kairos-io/kairos-sdk/machine"
"github.com/kairos-io/kairos-sdk/machine/openrc"
"github.com/kairos-io/kairos-sdk/machine/systemd"
"github.com/kairos-io/kairos-sdk/types"
"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"
p2p "github.com/kairos-io/provider-kairos/v2/internal/role/p2p"
edgeVPNClient "github.com/mudler/edgevpn/api/client"

"github.com/kairos-io/provider-kairos/v2/internal/services"

Expand All @@ -34,23 +32,23 @@ func Bootstrap(e *pluggable.Event) pluggable.EventResponse {
}

c := &config.Config{}
providerConfig := &providerConfig.Config{}
prvConfig := &providerConfig.Config{}
err = config.FromString(cfg.Config, c)
if err != nil {
return ErrorEvent("Failed reading JSON input: %s input '%s'", err.Error(), cfg.Config)
}

err = config.FromString(cfg.Config, providerConfig)
err = config.FromString(cfg.Config, prvConfig)
if err != nil {
return ErrorEvent("Failed reading JSON input: %s input '%s'", err.Error(), cfg.Config)
}
// TODO: this belong to a systemd service that is started instead

p2pBlockDefined := providerConfig.P2P != nil
tokenNotDefined := ((p2pBlockDefined && providerConfig.P2P.NetworkToken == "") || !p2pBlockDefined)
skipAuto := (p2pBlockDefined && !providerConfig.P2P.Auto.IsEnabled())
p2pBlockDefined := prvConfig.P2P != nil
Itxaka marked this conversation as resolved.
Show resolved Hide resolved
tokenNotDefined := (p2pBlockDefined && prvConfig.P2P.NetworkToken == "") || !p2pBlockDefined
skipAuto := p2pBlockDefined && !prvConfig.P2P.Auto.IsEnabled()

if providerConfig.P2P == nil && !providerConfig.K3s.Enabled && !providerConfig.K3sAgent.Enabled {
if prvConfig.P2P == nil && !prvConfig.K3s.Enabled && !prvConfig.K3sAgent.Enabled {
return pluggable.EventResponse{State: fmt.Sprintf("no kairos or k3s configuration. nothing to do: %s", cfg.Config)}
}

Expand All @@ -59,35 +57,18 @@ func Bootstrap(e *pluggable.Event) pluggable.EventResponse {

logLevel := "debug"

if p2pBlockDefined && providerConfig.P2P.LogLevel != "" {
logLevel = providerConfig.P2P.LogLevel
}

lvl, err := logging.LevelFromString(logLevel)
if err != nil {
return ErrorEvent("Failed setup logger: %s", err.Error())
if p2pBlockDefined && prvConfig.P2P.LogLevel != "" {
logLevel = prvConfig.P2P.LogLevel
}

// TODO: Fixup Logging to file
loggerCfg := zap.NewProductionConfig()
loggerCfg.OutputPaths = []string{
cfg.Logfile,
}
logger, err := loggerCfg.Build()
if err != nil {
return ErrorEvent("Failed setup logger: %s", err.Error())
}

logging.SetAllLoggers(lvl)

log := &logging.ZapEventLogger{SugaredLogger: *logger.Sugar()}
logger := types.NewKairosLogger("provider", logLevel, false)

// Do onetimebootstrap if K3s or K3s-agent are enabled.
// Those blocks are not required to be enabled in case of a kairos
// full automated setup. Otherwise, they must be explicitly enabled.
if (tokenNotDefined && (providerConfig.K3s.Enabled || providerConfig.K3sAgent.Enabled)) || skipAuto {
err := oneTimeBootstrap(log, providerConfig, func() error {
return SetupVPN(services.EdgeVPNDefaultInstance, cfg.APIAddress, "/", true, providerConfig)
if (tokenNotDefined && (prvConfig.K3s.Enabled || prvConfig.K3sAgent.Enabled)) || skipAuto {
err := oneTimeBootstrap(logger, prvConfig, func() error {
return SetupVPN(services.EdgeVPNDefaultInstance, cfg.APIAddress, "/", true, prvConfig)
})
if err != nil {
return ErrorEvent("Failed setup: %s", err.Error())
Expand All @@ -100,63 +81,63 @@ func Bootstrap(e *pluggable.Event) pluggable.EventResponse {
}

// We might still want a VPN, but not to route traffic into
if providerConfig.P2P.VPNNeedsCreation() {
if prvConfig.P2P.VPNNeedsCreation() {
logger.Info("Configuring VPN")
if err := SetupVPN(services.EdgeVPNDefaultInstance, cfg.APIAddress, "/", true, providerConfig); err != nil {
if err := SetupVPN(services.EdgeVPNDefaultInstance, cfg.APIAddress, "/", true, prvConfig); err != nil {
return ErrorEvent("Failed setup VPN: %s", err.Error())
}
} else { // We need at least the API to co-ordinate
logger.Info("Configuring API")
if err := SetupAPI(cfg.APIAddress, "/", true, providerConfig); err != nil {
if err := SetupAPI(cfg.APIAddress, "/", true, prvConfig); err != nil {
return ErrorEvent("Failed setup VPN: %s", err.Error())
}
}

networkID := "kairos"

if p2pBlockDefined && providerConfig.P2P.NetworkID != "" {
networkID = providerConfig.P2P.NetworkID
if p2pBlockDefined && prvConfig.P2P.NetworkID != "" {
networkID = prvConfig.P2P.NetworkID
}

cc := service.NewClient(
networkID,
edgeVPNClient.NewClient(edgeVPNClient.WithHost(cfg.APIAddress)))

nodeOpts := []service.Option{
service.WithMinNodes(providerConfig.P2P.MinimumNodes),
service.WithLogger(log),
service.WithMinNodes(prvConfig.P2P.MinimumNodes),
service.WithLogger(logger),
service.WithClient(cc),
service.WithUUID(machine.UUID()),
service.WithStateDir("/usr/local/.kairos/state"),
service.WithNetworkToken(providerConfig.P2P.NetworkToken),
service.WithNetworkToken(prvConfig.P2P.NetworkToken),
service.WithPersistentRoles("auto"),
service.WithRoles(
service.RoleKey{
Role: "master",
RoleHandler: p2p.Master(c, providerConfig, false, false, "master"),
RoleHandler: p2p.Master(c, prvConfig, false, false, "master"),
},
service.RoleKey{
Role: "master/clusterinit",
RoleHandler: p2p.Master(c, providerConfig, true, true, "master/clusterinit"),
RoleHandler: p2p.Master(c, prvConfig, true, true, "master/clusterinit"),
},
service.RoleKey{
Role: "master/ha",
RoleHandler: p2p.Master(c, providerConfig, false, true, "master/ha"),
RoleHandler: p2p.Master(c, prvConfig, false, true, "master/ha"),
},
service.RoleKey{
Role: "worker",
RoleHandler: p2p.Worker(c, providerConfig),
RoleHandler: p2p.Worker(c, prvConfig),
},
service.RoleKey{
Role: "auto",
RoleHandler: role.Auto(c, providerConfig),
RoleHandler: role.Auto(c, prvConfig),
},
),
}

// Optionally set up a specific node role if the user has defined so
if providerConfig.P2P.Role != "" {
nodeOpts = append(nodeOpts, service.WithDefaultRoles(providerConfig.P2P.Role))
if prvConfig.P2P.Role != "" {
nodeOpts = append(nodeOpts, service.WithDefaultRoles(prvConfig.P2P.Role))
}

k, err := service.NewNode(nodeOpts...)
Expand All @@ -175,7 +156,7 @@ func Bootstrap(e *pluggable.Event) pluggable.EventResponse {
}
}

func oneTimeBootstrap(l logging.StandardLogger, c *providerConfig.Config, vpnSetupFN func() error) error {
func oneTimeBootstrap(l types.KairosLogger, c *providerConfig.Config, vpnSetupFN func() error) error {
var err error
if role.SentinelExist() {
l.Info("Sentinel exists, nothing to do. exiting.")
Expand Down
54 changes: 0 additions & 54 deletions internal/provider/bootstrap_test.go

This file was deleted.

Loading