如何提取Kubernetes Pod命令执行结果的属性?

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

How to extract kubernetes pod command execution result attributes

问题

我正在使用client-Go连接到pod,并且我想获取文件目录的属性。

func GetPodFiles(c *gin.Context)  {
    client, _ := Init.ClusterID(c)
    path := c.DefaultQuery("path", "/")
    cmd := []string{
        "sh",
        "-c",
        fmt.Sprintf("ls -l %s", path),
    }
    config, _ := Init.ClusterCfg(c)
    req := client.CoreV1().RESTClient().Post().
        Resource("pods").
        Name("nacos-0").
        Namespace("default").SubResource("exec").Param("container", "nacos")
    req.VersionedParams(
        &v1.PodExecOptions{
            Command: cmd,
            Stdin:   false,
            Stdout:  true,
            Stderr:  true,
            TTY:     false,
        },
        scheme.ParameterCodec,
    )

    var stdout, stderr bytes.Buffer
    exec, err := remotecommand.NewSPDYExecutor(config, "POST", req.URL())
    if err != nil {
        response.FailWithMessage(response.InternalServerError, err.Error(), c)
        return
    }
    err = exec.Stream(remotecommand.StreamOptions{
        Stdin:  nil,
        Stdout: &stdout,
        Stderr: &stderr,
    })
    if err != nil {
        response.FailWithMessage(response.InternalServerError, "Error obtaining file", c)
        return
    }

    fmt.Println(stdout.String())
}

执行结果输出

total 0
lrwxrwxrwx   1 root root   7 Jun  1  2018 bin -> usr/bin
drwxr-xr-x   5 root root 360 Feb 16 16:39 dev
lrwxrwxrwx   1 root root   8 Jun  1  2018 sbin -> usr/sbin
drwxr-xr-x   2 root root   6 Apr 11  2018 srv

期望的结果

"data": [
        {
            "perm": "drwxr-xr-x",
            "mod_time": "2022-03-02 15:02:15",
            "kind": "d",
            "name": "temp",
            "size": ""
        },
]

有没有一种好的方法或者Go语言的第三方库来处理它。请告诉我。谢谢。

英文:

I am connecting to pod via client-Go and I want to get the properties of the file directory

func GetPodFiles(c *gin.Context)  {
	client, _ := Init.ClusterID(c)
	path := c.DefaultQuery("path", "/")
	cmd := []string{
		"sh",
		"-c",
		fmt.Sprintf("ls -l %s", path),
	}
	config, _ := Init.ClusterCfg(c)
	req := client.CoreV1().RESTClient().Post().
		Resource("pods").
		Name("nacos-0").
		Namespace("default").SubResource("exec").Param("container", "nacos")
	req.VersionedParams(
		&v1.PodExecOptions{
			Command: cmd,
			Stdin:   false,
			Stdout:  true,
			Stderr:  true,
			TTY:     false,
		},
		scheme.ParameterCodec,
	)

	var stdout, stderr bytes.Buffer
	exec, err := remotecommand.NewSPDYExecutor(config, "POST", req.URL())
	if err != nil {
		response.FailWithMessage(response.InternalServerError, err.Error(), c)
		return
	}
	err = exec.Stream(remotecommand.StreamOptions{
		Stdin:  nil,
		Stdout: &stdout,
		Stderr: &stderr,
	})
	if err != nil {
		response.FailWithMessage(response.InternalServerError, "Error obtaining file", c)
		return
	}

	fmt.Println(stdout.String())
}

Execution Result Output

total 0
lrwxrwxrwx   1 root root   7 Jun  1  2018 bin -> usr/bin
drwxr-xr-x   5 root root 360 Feb 16 16:39 dev
lrwxrwxrwx   1 root root   8 Jun  1  2018 sbin -> usr/sbin
drwxr-xr-x   2 root root   6 Apr 11  2018 srv

Expect the result

"data": [
        {
            "perm": "drwxr-xr-x",
            "mod_time": "2022-03-02 15:02:15",
            "kind": "d",
            "name": "temp",
            "size": ""
        },
]

Is there a good way or a golang third-party library to handle it. Please let me know. Thank you

答案1

得分: 1

在Kubernetes pod中,您可以执行stat Linux命令,而不是ls命令。

$ stat yourFileOrDirName

该命令的默认输出如下所示:

  File: yourFileOrDirName
  Size: 346             Blocks: 0          IO Block: 4096   directory
Device: 51h/82d Inode: 40431       Links: 1
Access: (0755/drwxr-xr-x)  Uid: ( 1000/ username)   Gid: ( 1000/ groupname)
Access: 2022-03-02 11:59:07.384821351 +0100
Modify: 2022-03-02 11:58:48.733821177 +0100
Change: 2022-03-02 11:58:48.733821177 +0100
 Birth: 2021-12-21 11:12:05.571841723 +0100

但是,您可以像这样调整其输出:

$ stat --printf="%n,%A,%y,%s" yourFileOrDirName

其中 %n - 文件名,%A - 权限位和文件类型(以人类可读的形式),%y - 最后修改时间(人类可读),%s - 总大小(以字节为单位)。您还可以选择任何字符作为分隔符,而不是逗号。

输出将会是:

yourFileOrDirName,drwxr-xr-x,2022-03-02 11:58:48.733821177 +0100,346

有关stat命令的更多信息,请参阅此处

在获得这样的输出后,如果您确实需要,我相信您可以轻松地将其“转换”为JSON格式。

此外,您可以像这样运行stat命令:

$ stat --printf="{\"data\":[{\"name\":\"%n\",\"perm\":\"%A\",\"mod_time\":\"%y\",\"size\":\"%s\"}]}" yourFileOrDirName

或者如@mdaniel建议的那样,由于该命令不包含任何shell变量,也没有',更简洁的命令是:

stat --printf='{"data":[{"name":"%n","perm":"%A","mod_time":"%y","size":"%s"}]}' yourFileOrDirName

然后获得DIY的JSON输出:

{"data":[{"name":"yourFileOrDirName","perm":"drwxrwxr-x","mod_time":"2022-02-04 15:17:27.000000000 +0000","size":"4096"}]}
英文:

In a Kubernetes pod you can execute the stat linux command instead of ls command.

$ stat yourFileOrDirName

The output of this command by default is like this:

  File: yourFileOrDirName
  Size: 346             Blocks: 0          IO Block: 4096   directory
Device: 51h/82d Inode: 40431       Links: 1
Access: (0755/drwxr-xr-x)  Uid: ( 1000/ username)   Gid: ( 1000/ groupname)
Access: 2022-03-02 11:59:07.384821351 +0100
Modify: 2022-03-02 11:58:48.733821177 +0100
Change: 2022-03-02 11:58:48.733821177 +0100
 Birth: 2021-12-21 11:12:05.571841723 +0100

But you can tweak its output like this:

$ stat --printf="%n,%A,%y,%s" yourFileOrDirName

where %n - file name, %A - permission bits and file type in human readable form, %y - time of last data modification human-readable, %s - total size, in bytes. You can also choose any character as a delimiter instead of comma.

the output will be:

yourFileOrDirName,drwxr-xr-x,2022-03-02 11:58:48.733821177 +0100,346

See more info about the stat command here.

After you get such output, I believe you can easily 'convert' it to json format if you really need it.

Furthermore, you can run the stat command like this:

$ stat --printf="{\"data\":[{\"name\":\"%n\",\"perm\":\"%A\",\"mod_time\":\"%y\",\"size\":\"%s\"}]}" yourFileOrDirName

Or as @mdaniel suggested, since the command does not contain any shell variables, nor a ', the cleaner command is:

stat --printf='{"data":[{"name":"%n","perm":"%A","mod_time":"%y","size":"%s"}]}' yourFileOrDirName

and get the DIY json output:

{"data":[{"name":"yourFileOrDirName","perm":"drwxrwxr-x","mod_time":"2022-02-04 15:17:27.000000000 +0000","size":"4096"}]}

huangapple
  • 本文由 发表于 2022年3月3日 09:36:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/71331142.html
匿名

发表评论

匿名网友

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

确定