2019-01-09 16:54:15 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2020-04-19 06:38:16 +00:00
|
|
|
"runtime"
|
2019-01-09 16:54:15 +00:00
|
|
|
|
|
|
|
"github.com/rancher/k3s/pkg/agent"
|
|
|
|
"github.com/rancher/k3s/pkg/cli/cmds"
|
2019-03-08 22:47:44 +00:00
|
|
|
"github.com/rancher/k3s/pkg/datadir"
|
2019-07-08 23:02:06 +00:00
|
|
|
"github.com/rancher/k3s/pkg/netutil"
|
2019-10-27 05:53:25 +00:00
|
|
|
"github.com/rancher/k3s/pkg/token"
|
2020-05-05 22:09:04 +00:00
|
|
|
"github.com/rancher/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"
|
2019-01-09 16:54:15 +00:00
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Run(ctx *cli.Context) error {
|
2019-08-08 05:45:54 +00:00
|
|
|
if err := cmds.InitLogging(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
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 != "" {
|
|
|
|
cmds.AgentConfig.Token = cmds.AgentConfig.ClusterSecret
|
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2019-07-08 23:02:06 +00:00
|
|
|
if cmds.AgentConfig.FlannelIface != "" && cmds.AgentConfig.NodeIP == "" {
|
|
|
|
cmds.AgentConfig.NodeIP = netutil.GetIPFromInterface(cmds.AgentConfig.FlannelIface)
|
|
|
|
}
|
|
|
|
|
2020-05-05 22:09:04 +00:00
|
|
|
logrus.Infof("Starting "+version.Program+" agent %s", 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
|
2019-02-08 04:12:49 +00:00
|
|
|
cfg.Debug = ctx.GlobalBool("debug")
|
2019-01-09 16:54:15 +00:00
|
|
|
cfg.DataDir = dataDir
|
|
|
|
|
2019-05-09 22:05:51 +00:00
|
|
|
contextCtx := signals.SetupSignalHandler(context.Background())
|
2019-02-08 04:28:09 +00:00
|
|
|
|
2019-01-09 16:54:15 +00:00
|
|
|
return agent.Run(contextCtx, cfg)
|
|
|
|
}
|