在Go语言中,设置绝对路径在kubeconfig中不起作用。

huangapple go评论89阅读模式
英文:

Setting absolute path does not work in Go for kubeconfig

问题

我正在使用Go模块调用kubectl,代码如下:

getNsCmd := cmd.NewCmd("kubectl", "--kubeconfig", "~/.kube/<kube-config-file>", "get", "ns")

如果我像这样设置路径,它就能正常工作:

getNsCmd := cmd.NewCmd("kubectl", "--kubeconfig", "../../../../.kube/<kube-config-file>", "get", "ns")

我正在使用Go的cmd包。

目前,该模块位于另一个存储库中,这就是为什么它必须向上导航四个级别的原因。我认为这是因为该命令是从该文件的角度运行的,但它似乎不应该这样。如果它只是作为一个cli命令简单地运行,第一个命令(我认为)应该可以工作。

当我手动从cli运行此命令时,它可以正常工作:

$ kubectl --kubeconfig ~/.kube/<kube-config-file> get ns
英文:

I am making a call to kubectl from with a Go module like so:

getNsCmd := cmd.NewCmd(&quot;kubectl&quot;, &quot;--kubeconfig&quot;, &quot;~/.kube/&lt;kube-config-file&gt;&quot;, &quot;get&quot;, &quot;ns&quot;)

It works if I set the path like this:

getNsCmd := cmd.NewCmd(&quot;kubectl&quot;, &quot;--kubeconfig&quot;, &quot;../../../../.kube/&lt;kube-config-file&gt;&quot;, &quot;get&quot;, &quot;ns&quot;)

I am using the Go cmd package

Currently this module lives in another repo and that's why it has to navigate up four levels. I figure this is because the command is running from the perspective of this file, but it doesn't seem like it should have to. If it's simple running it as a cli command the first one (I would think) should work.

When I run this command from the cli manually it works just fine:

$ kubectl --kubeconfig ~/.kube/&lt;kube-config-file&gt; get ns

答案1

得分: 0

使用~作为快捷方式是特定于bash的内容。~只是获取用户的$HOME目录,但不适用于所有shell的实现。这是可以从终端使用的内容,但不一定适用于代码(它不是与shell无关的)。然而,Go语言也可以使用os.UserHomeDir()来找到当前用户的主目录。

https://pkg.go.dev/os#UserHomeDir

类似下面的代码应该可以工作:

package main

import (
	"fmt"
	"os"
)

func main() {

	homeDir, _ := os.UserHomeDir()

	fmt.Printf("%s/.kube/<kube-config-file>", homeDir)
}

输出:/root/.kube/<kube-config-file>

英文:

Using ~ as a shortcut is something specific to bash. ~ just grabs the user's $HOME directory, but does not work across all implementations of the shell. This is something that can be used from the terminal but not always from code (it's not shell-independent). However, go can also find the current user's home directory using os.UserHomeDir().

https://pkg.go.dev/os#UserHomeDir

Something like this should work

package main

import (
	&quot;fmt&quot;
	&quot;os&quot;
)

func main() {

	homeDir, _ := os.UserHomeDir()

	fmt.Printf(&quot;%s/.kube/&lt;kube-config-file&gt;&quot;, homeDir)
}

Output: /root/.kube/&lt;kube-config-file&gt;

答案2

得分: 0

由于您使用的软件包提到它包装了Go的os/exec软件包,这里是它们文档的一部分摘录(文档链接):

与C和其他语言中的“system”库调用不同,os/exec软件包有意不调用系统shell,也不展开任何通配符模式或处理其他由shell通常完成的扩展、管道或重定向。该软件包的行为更像是C的“exec”函数系列。要展开通配符模式,可以直接调用shell,注意转义任何危险输入,或者使用path/filepath软件包的Glob函数。要展开环境变量,请使用os软件包的ExpandEnv函数。

所以我认为您可能想尝试这样的代码:

import "os"

getNsCmd := cmd.NewCmd("kubectl", "--kubeconfig", os.ExpandEnv("$HOME/.kube/<kube-config-file>"), "get", "ns")

请注意,这只是一个示例代码,您需要将<kube-config-file>替换为实际的kube配置文件名。

英文:

Since the package you're using mentions that it wraps the Go's os/exec package, here is an excerpt from their documentation:

> Unlike the "system" library call from C and other languages, the
> os/exec package intentionally does not invoke the system shell and
> does not expand any glob patterns or handle other expansions,
> pipelines, or redirections typically done by shells. The package
> behaves more like C's "exec" family of functions. To expand glob
> patterns, either call the shell directly, taking care to escape any
> dangerous input, or use the path/filepath package's Glob function. To
> expand environment variables, use package os's ExpandEnv.

So I think you would want to try something like this:

import &quot;os&quot;

getNsCmd := cmd.NewCmd(&quot;kubectl&quot;, &quot;--kubeconfig&quot;, os.ExpandEnv(&quot;$HOME/.kube/&lt;kube-config-file&gt;&quot;), &quot;get&quot;, &quot;ns&quot;)

答案3

得分: 0

如其他人所提到的,我需要获取主目录环境变量。然而,我是这样实现的:

homePath, _ := os.LookupEnv("HOME")

//

getNsCmd := cmd.NewCmd("kubectl", "--kubeconfig", homePath+"/.kube/<kube-config-file>", "get", "ns")

请注意,这是一个代码片段,用于在Go语言中获取主目录环境变量并执行一些命令。

英文:

As others have mentioned I needed to grab the home directory environment var.
However I implemented it like this:

	homePath, _ := os.LookupEnv(&quot;HOME&quot;)

//

getNsCmd := cmd.NewCmd(&quot;kubectl&quot;, &quot;--kubeconfig&quot;, homePath+&quot;/.kube/&lt;kube-config-file&gt;&quot;, &quot;get&quot;, &quot;ns&quot;)

huangapple
  • 本文由 发表于 2022年4月30日 01:24:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/72061651.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定