Skip to content

Commit

Permalink
Add K3S_DATA_DIR as env var for --data-dir flag
Browse files Browse the repository at this point in the history
Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
  • Loading branch information
brandond committed Jul 30, 2024
1 parent 5b9fa7e commit fb4e66f
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 11 deletions.
7 changes: 5 additions & 2 deletions cmd/k3s/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,14 @@ func main() {
}
}

// findDataDir reads data-dir settings from the CLI args and config file.
// findDataDir reads data-dir settings from the environment, CLI args, and config file.
// If not found, the default will be used, which varies depending on whether
// k3s is being run as root or not.
func findDataDir(args []string) string {
var dataDir string
dataDir := os.Getenv(version.ProgramUpper + "_DATA_DIR")
if dataDir != "" {
return dataDir
}
fs := pflag.NewFlagSet("data-dir-set", pflag.ContinueOnError)
fs.ParseErrorsWhitelist.UnknownFlags = true
fs.SetOutput(io.Discard)
Expand Down
3 changes: 3 additions & 0 deletions pkg/cli/cmds/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,14 @@ func NewAgentCommand(action func(ctx *cli.Context) error) cli.Command {
EnvVar: version.ProgramUpper + "_URL",
Destination: &AgentConfig.ServerURL,
},
// Note that this is different from DataDirFlag used elswhere in the CLI,
// as this is bound to AgentConfig instead of ServerConfig.
&cli.StringFlag{
Name: "data-dir,d",
Usage: "(agent/data) Folder to hold state",
Destination: &AgentConfig.DataDir,
Value: "/var/lib/rancher/" + version.Program + "",
EnvVar: version.ProgramUpper + "_DATA_DIR",
},
NodeNameFlag,
WithNodeIDFlag,
Expand Down
6 changes: 1 addition & 5 deletions pkg/cli/cmds/certs.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,14 @@ var (
},
}
CertRotateCACommandFlags = []cli.Flag{
DataDirFlag,
cli.StringFlag{
Name: "server,s",
Usage: "(cluster) Server to connect to",
EnvVar: version.ProgramUpper + "_URL",
Value: "https://127.0.0.1:6443",
Destination: &ServerConfig.ServerURL,
},
cli.StringFlag{
Name: "data-dir,d",
Usage: "(data) Folder to hold state default /var/lib/rancher/" + version.Program + " or ${HOME}/.rancher/" + version.Program + " if not root",
Destination: &ServerConfig.DataDir,
},
cli.StringFlag{
Name: "path",
Usage: "Path to directory containing new CA certificates",
Expand Down
5 changes: 1 addition & 4 deletions pkg/cli/cmds/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ func NewApp() *cli.App {
}
app.Flags = []cli.Flag{
DebugFlag,
&cli.StringFlag{
Name: "data-dir,d",
Usage: "(data) Folder to hold state (default: /var/lib/rancher/" + version.Program + " or ${HOME}/.rancher/" + version.Program + " if not root)",
},
DataDirFlag,
}

return app
Expand Down
1 change: 1 addition & 0 deletions pkg/cli/cmds/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ var (
Name: "data-dir,d",
Usage: "(data) Folder to hold state default /var/lib/rancher/" + version.Program + " or ${HOME}/.rancher/" + version.Program + " if not root",
Destination: &ServerConfig.DataDir,
EnvVar: version.ProgramUpper + "_DATA_DIR",
}
ServerToken = &cli.StringFlag{
Name: "token,t",
Expand Down

0 comments on commit fb4e66f

Please sign in to comment.