Unverified Commit 49d0f20e authored by Darren Shepherd's avatar Darren Shepherd Committed by GitHub

Merge pull request #110 from ibuildthecloud/tokenfile

Add --token-file support
parents a317cda8 8acc17fc
......@@ -3,7 +3,10 @@ package agent
import (
"context"
"fmt"
"io/ioutil"
"os"
"strings"
"time"
"github.com/rancher/k3s/pkg/agent"
"github.com/rancher/k3s/pkg/cli/cmds"
......@@ -13,11 +16,37 @@ import (
"github.com/urfave/cli"
)
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
}
}
}
func Run(ctx *cli.Context) error {
if os.Getuid() != 0 {
return fmt.Errorf("agent must be ran as root")
}
if cmds.AgentConfig.TokenFile != "" {
token, err := readToken(cmds.AgentConfig.TokenFile)
if err != nil {
return err
}
cmds.AgentConfig.Token = token
}
if cmds.AgentConfig.Token == "" && cmds.AgentConfig.ClusterSecret == "" {
return fmt.Errorf("--token is required")
}
......
......@@ -9,7 +9,8 @@ import (
type Agent struct {
Token string
ServerURL string
TokenFile string
ServerURL string
DataDir string
NodeIP string
NodeName string
......@@ -70,6 +71,12 @@ func NewAgentCommand(action func(ctx *cli.Context) error) cli.Command {
Destination: &AgentConfig.Token,
},
cli.StringFlag{
Name: "token-file",
Usage: "Token file to use for authentication",
EnvVar: "K3S_TOKEN_FILE",
Destination: &AgentConfig.TokenFile,
},
cli.StringFlag{
Name: "server,s",
Usage: "Server to connect to",
EnvVar: "K3S_URL",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment