2019-01-09 16:54:15 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2020-04-19 06:38:16 +00:00
|
|
|
"runtime"
|
2019-01-09 16:54:15 +00:00
|
|
|
|
2020-07-27 19:09:21 +00:00
|
|
|
"github.com/erikdubbelboer/gspt"
|
2022-03-02 23:47:27 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/agent"
|
|
|
|
"github.com/k3s-io/k3s/pkg/cli/cmds"
|
|
|
|
"github.com/k3s-io/k3s/pkg/datadir"
|
|
|
|
"github.com/k3s-io/k3s/pkg/netutil"
|
|
|
|
"github.com/k3s-io/k3s/pkg/token"
|
|
|
|
"github.com/k3s-io/k3s/pkg/version"
|
2019-07-18 13:40:39 +00:00
|
|
|
"github.com/rancher/wrangler/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 {
|
2020-07-27 19:09:21 +00:00
|
|
|
// hide process arguments from ps output, since they may contain
|
|
|
|
// database credentials or other secrets.
|
|
|
|
gspt.SetProcTitle(os.Args[0] + " agent")
|
|
|
|
|
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
|
|
|
|
2020-04-19 06:38:16 +00:00
|
|
|
if os.Getuid() != 0 && runtime.GOOS != "windows" {
|
2019-01-09 16:54:15 +00:00
|
|
|
return fmt.Errorf("agent must be ran as root")
|
|
|
|
}
|
|
|
|
|
2019-03-02 00:07:55 +00:00
|
|
|
if cmds.AgentConfig.TokenFile != "" {
|
2019-10-27 05:53:25 +00:00
|
|
|
token, err := token.ReadFile(cmds.AgentConfig.TokenFile)
|
2019-03-02 00:07:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cmds.AgentConfig.Token = token
|
|
|
|
}
|
|
|
|
|
2019-11-14 19:42:42 +00:00
|
|
|
if cmds.AgentConfig.Token == "" && cmds.AgentConfig.ClusterSecret != "" {
|
2022-09-01 16:33:59 +00:00
|
|
|
logrus.Fatal("cluster-secret is deprecated. Use --token instead.")
|
2019-11-14 19:42:42 +00:00
|
|
|
}
|
|
|
|
|
2019-10-27 05:53:25 +00:00
|
|
|
if 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 {
|
|
|
|
cmds.AgentConfig.NodeIP.Set(netutil.GetIPFromInterface(cmds.AgentConfig.FlannelIface))
|
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
|
|
|
|
2019-01-09 16:54:15 +00:00
|
|
|
return agent.Run(contextCtx, cfg)
|
|
|
|
}
|