2019-01-09 16:54:15 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
2024-04-25 01:02:05 +00:00
|
|
|
"context"
|
2022-12-08 23:59:21 +00:00
|
|
|
"crypto/tls"
|
2019-01-09 16:54:15 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2022-12-08 23:59:21 +00:00
|
|
|
"path/filepath"
|
2020-04-19 06:38:16 +00:00
|
|
|
"runtime"
|
2019-01-09 16:54:15 +00:00
|
|
|
|
2023-12-06 00:25:49 +00:00
|
|
|
"github.com/gorilla/mux"
|
2022-03-02 23:47:27 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/agent"
|
2024-04-25 01:02:05 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/agent/https"
|
2022-03-02 23:47:27 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/cli/cmds"
|
2024-04-25 01:02:05 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/daemons/config"
|
2022-03-02 23:47:27 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/datadir"
|
2024-04-25 01:02:05 +00:00
|
|
|
k3smetrics "github.com/k3s-io/k3s/pkg/metrics"
|
2024-05-06 16:42:27 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/proctitle"
|
2024-04-25 01:02:05 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/profile"
|
2023-12-06 00:25:49 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/spegel"
|
2023-05-04 14:49:16 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/util"
|
2022-03-02 23:47:27 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/version"
|
2022-09-01 17:20:32 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/vpn"
|
2024-05-06 16:42:27 +00:00
|
|
|
"github.com/rancher/wrangler/v3/pkg/signals"
|
2019-02-08 04:28:09 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2020-08-29 19:46:55 +00:00
|
|
|
"github.com/urfave/cli"
|
2019-01-09 16:54:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func Run(ctx *cli.Context) error {
|
2023-12-14 00:44:58 +00:00
|
|
|
// Validate build env
|
|
|
|
cmds.MustValidateGolang()
|
|
|
|
|
2020-07-27 19:09:21 +00:00
|
|
|
// hide process arguments from ps output, since they may contain
|
|
|
|
// database credentials or other secrets.
|
2024-05-06 16:42:27 +00:00
|
|
|
proctitle.SetProcTitle(os.Args[0] + " agent")
|
2020-07-27 19:09:21 +00:00
|
|
|
|
2021-10-08 19:47:20 +00:00
|
|
|
// Evacuate cgroup v2 before doing anything else that may fork.
|
|
|
|
if err := cmds.EvacuateCgroup2(); err != nil {
|
2021-09-27 19:44:11 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-08 19:47:20 +00:00
|
|
|
// Initialize logging, and subprocess reaping if necessary.
|
|
|
|
// Log output redirection and subprocess reaping both require forking.
|
2020-08-29 19:46:55 +00:00
|
|
|
if err := cmds.InitLogging(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-09-27 19:44:11 +00:00
|
|
|
|
2022-11-15 08:44:35 +00:00
|
|
|
if runtime.GOOS != "windows" && os.Getuid() != 0 && !cmds.AgentConfig.Rootless {
|
|
|
|
return fmt.Errorf("agent must be run as root, or with --rootless")
|
2019-01-09 16:54:15 +00:00
|
|
|
}
|
|
|
|
|
2019-03-02 00:07:55 +00:00
|
|
|
if cmds.AgentConfig.TokenFile != "" {
|
2022-09-01 17:20:32 +00:00
|
|
|
token, err := util.ReadFile(cmds.AgentConfig.TokenFile)
|
2019-03-02 00:07:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cmds.AgentConfig.Token = token
|
|
|
|
}
|
|
|
|
|
2022-12-08 23:59:21 +00:00
|
|
|
clientKubeletCert := filepath.Join(cmds.AgentConfig.DataDir, "agent", "client-kubelet.crt")
|
|
|
|
clientKubeletKey := filepath.Join(cmds.AgentConfig.DataDir, "agent", "client-kubelet.key")
|
|
|
|
_, err := tls.LoadX509KeyPair(clientKubeletCert, clientKubeletKey)
|
|
|
|
|
|
|
|
if err != nil && cmds.AgentConfig.Token == "" {
|
2019-01-09 16:54:15 +00:00
|
|
|
return fmt.Errorf("--token is required")
|
|
|
|
}
|
|
|
|
|
|
|
|
if cmds.AgentConfig.ServerURL == "" {
|
|
|
|
return fmt.Errorf("--server is required")
|
|
|
|
}
|
|
|
|
|
2021-04-21 22:56:20 +00:00
|
|
|
if cmds.AgentConfig.FlannelIface != "" && len(cmds.AgentConfig.NodeIP) == 0 {
|
2023-09-26 09:09:03 +00:00
|
|
|
ip, err := util.GetIPFromInterface(cmds.AgentConfig.FlannelIface)
|
2023-09-13 13:55:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cmds.AgentConfig.NodeIP.Set(ip)
|
2019-07-08 23:02:06 +00:00
|
|
|
}
|
|
|
|
|
2020-09-21 16:56:03 +00:00
|
|
|
logrus.Info("Starting " + version.Program + " agent " + ctx.App.Version)
|
2019-02-08 04:28:09 +00:00
|
|
|
|
2019-03-08 22:47:44 +00:00
|
|
|
dataDir, err := datadir.LocalHome(cmds.AgentConfig.DataDir, cmds.AgentConfig.Rootless)
|
2019-01-09 16:54:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg := cmds.AgentConfig
|
2020-08-29 19:46:55 +00:00
|
|
|
cfg.Debug = ctx.GlobalBool("debug")
|
2019-01-09 16:54:15 +00:00
|
|
|
cfg.DataDir = dataDir
|
|
|
|
|
2021-11-08 15:32:43 +00:00
|
|
|
contextCtx := signals.SetupSignalContext()
|
2019-02-08 04:28:09 +00:00
|
|
|
|
2023-06-05 21:15:17 +00:00
|
|
|
go cmds.WriteCoverage(contextCtx)
|
2024-05-07 09:29:47 +00:00
|
|
|
if cfg.VPNAuthFile != "" {
|
|
|
|
cfg.VPNAuth, err = util.ReadFile(cfg.VPNAuthFile)
|
2022-09-01 17:20:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Starts the VPN in the agent if config was set up
|
2024-05-07 09:29:47 +00:00
|
|
|
if cfg.VPNAuth != "" {
|
|
|
|
err := vpn.StartVPN(cfg.VPNAuth)
|
2022-09-01 17:20:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-06-05 21:15:17 +00:00
|
|
|
|
2023-12-06 00:25:49 +00:00
|
|
|
// Until the agent is run and retrieves config from the server, we won't know
|
|
|
|
// if the embedded registry is enabled. If it is not enabled, these are not
|
|
|
|
// used as the registry is never started.
|
2024-04-25 01:02:05 +00:00
|
|
|
registry := spegel.DefaultRegistry
|
|
|
|
registry.Bootstrapper = spegel.NewAgentBootstrapper(cfg.ServerURL, cfg.Token, cfg.DataDir)
|
|
|
|
registry.Router = func(ctx context.Context, nodeConfig *config.Node) (*mux.Router, error) {
|
|
|
|
return https.Start(ctx, nodeConfig, nil)
|
|
|
|
}
|
2023-12-06 00:25:49 +00:00
|
|
|
|
2024-04-25 01:02:05 +00:00
|
|
|
// same deal for metrics - these are not used if the extra metrics listener is not enabled.
|
|
|
|
metrics := k3smetrics.DefaultMetrics
|
|
|
|
metrics.Router = func(ctx context.Context, nodeConfig *config.Node) (*mux.Router, error) {
|
|
|
|
return https.Start(ctx, nodeConfig, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// and for pprof as well
|
|
|
|
pprof := profile.DefaultProfiler
|
|
|
|
pprof.Router = func(ctx context.Context, nodeConfig *config.Node) (*mux.Router, error) {
|
|
|
|
return https.Start(ctx, nodeConfig, nil)
|
2023-12-06 00:25:49 +00:00
|
|
|
}
|
|
|
|
|
2019-01-09 16:54:15 +00:00
|
|
|
return agent.Run(contextCtx, cfg)
|
|
|
|
}
|