2019-01-09 11:54:15 -05:00
|
|
|
package agent
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
2019-03-01 19:07:55 -05:00
|
|
|
"io/ioutil"
|
2019-01-09 11:54:15 -05:00
|
|
|
"os"
|
2019-03-01 19:07:55 -05:00
|
|
|
"strings"
|
|
|
|
|
"time"
|
2019-01-09 11:54:15 -05:00
|
|
|
|
2019-05-09 18:05:51 -04:00
|
|
|
"github.com/rancher/wrangler/pkg/signals"
|
|
|
|
|
|
2019-01-09 11:54:15 -05:00
|
|
|
"github.com/rancher/k3s/pkg/agent"
|
|
|
|
|
"github.com/rancher/k3s/pkg/cli/cmds"
|
2019-03-08 17:47:44 -05:00
|
|
|
"github.com/rancher/k3s/pkg/datadir"
|
2019-02-07 23:28:09 -05:00
|
|
|
"github.com/sirupsen/logrus"
|
2019-01-09 11:54:15 -05:00
|
|
|
"github.com/urfave/cli"
|
|
|
|
|
)
|
|
|
|
|
|
2019-03-01 19:07:55 -05:00
|
|
|
func readToken(path string) (string, error) {
|
|
|
|
|
if path == "" {
|
|
|
|
|
return "", nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
tokenBytes, err := ioutil.ReadFile(path)
|
|
|
|
|
if err == nil {
|
|
|
|
|
return strings.TrimSpace(string(tokenBytes)), nil
|
|
|
|
|
} else if os.IsNotExist(err) {
|
|
|
|
|
logrus.Infof("Waiting for %s to be available\n", path)
|
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
|
} else {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-09 11:54:15 -05:00
|
|
|
func Run(ctx *cli.Context) error {
|
|
|
|
|
if os.Getuid() != 0 {
|
|
|
|
|
return fmt.Errorf("agent must be ran as root")
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-01 19:07:55 -05:00
|
|
|
if cmds.AgentConfig.TokenFile != "" {
|
|
|
|
|
token, err := readToken(cmds.AgentConfig.TokenFile)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
cmds.AgentConfig.Token = token
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-22 16:14:58 -05:00
|
|
|
if cmds.AgentConfig.Token == "" && cmds.AgentConfig.ClusterSecret == "" {
|
2019-01-09 11:54:15 -05:00
|
|
|
return fmt.Errorf("--token is required")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if cmds.AgentConfig.ServerURL == "" {
|
|
|
|
|
return fmt.Errorf("--server is required")
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-07 23:28:09 -05:00
|
|
|
logrus.Infof("Starting k3s agent %s", ctx.App.Version)
|
|
|
|
|
|
2019-03-08 17:47:44 -05:00
|
|
|
dataDir, err := datadir.LocalHome(cmds.AgentConfig.DataDir, cmds.AgentConfig.Rootless)
|
2019-01-09 11:54:15 -05:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cfg := cmds.AgentConfig
|
2019-02-07 23:12:49 -05:00
|
|
|
cfg.Debug = ctx.GlobalBool("debug")
|
2019-01-09 11:54:15 -05:00
|
|
|
cfg.DataDir = dataDir
|
2019-05-07 19:47:07 -04:00
|
|
|
cfg.Labels = append(cfg.Labels, "node-role.kubernetes.io/worker=true")
|
2019-01-09 11:54:15 -05:00
|
|
|
|
2019-05-09 18:05:51 -04:00
|
|
|
contextCtx := signals.SetupSignalHandler(context.Background())
|
2019-02-07 23:28:09 -05:00
|
|
|
|
2019-01-09 11:54:15 -05:00
|
|
|
return agent.Run(contextCtx, cfg)
|
|
|
|
|
}
|