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

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

How to extract kubernetes pod command execution result attributes

问题

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

  1. func GetPodFiles(c *gin.Context) {
  2. client, _ := Init.ClusterID(c)
  3. path := c.DefaultQuery("path", "/")
  4. cmd := []string{
  5. "sh",
  6. "-c",
  7. fmt.Sprintf("ls -l %s", path),
  8. }
  9. config, _ := Init.ClusterCfg(c)
  10. req := client.CoreV1().RESTClient().Post().
  11. Resource("pods").
  12. Name("nacos-0").
  13. Namespace("default").SubResource("exec").Param("container", "nacos")
  14. req.VersionedParams(
  15. &v1.PodExecOptions{
  16. Command: cmd,
  17. Stdin: false,
  18. Stdout: true,
  19. Stderr: true,
  20. TTY: false,
  21. },
  22. scheme.ParameterCodec,
  23. )
  24. var stdout, stderr bytes.Buffer
  25. exec, err := remotecommand.NewSPDYExecutor(config, "POST", req.URL())
  26. if err != nil {
  27. response.FailWithMessage(response.InternalServerError, err.Error(), c)
  28. return
  29. }
  30. err = exec.Stream(remotecommand.StreamOptions{
  31. Stdin: nil,
  32. Stdout: &stdout,
  33. Stderr: &stderr,
  34. })
  35. if err != nil {
  36. response.FailWithMessage(response.InternalServerError, "Error obtaining file", c)
  37. return
  38. }
  39. fmt.Println(stdout.String())
  40. }

执行结果输出

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

期望的结果

  1. "data": [
  2. {
  3. "perm": "drwxr-xr-x",
  4. "mod_time": "2022-03-02 15:02:15",
  5. "kind": "d",
  6. "name": "temp",
  7. "size": ""
  8. },
  9. ]

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

英文:

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

  1. func GetPodFiles(c *gin.Context) {
  2. client, _ := Init.ClusterID(c)
  3. path := c.DefaultQuery("path", "/")
  4. cmd := []string{
  5. "sh",
  6. "-c",
  7. fmt.Sprintf("ls -l %s", path),
  8. }
  9. config, _ := Init.ClusterCfg(c)
  10. req := client.CoreV1().RESTClient().Post().
  11. Resource("pods").
  12. Name("nacos-0").
  13. Namespace("default").SubResource("exec").Param("container", "nacos")
  14. req.VersionedParams(
  15. &v1.PodExecOptions{
  16. Command: cmd,
  17. Stdin: false,
  18. Stdout: true,
  19. Stderr: true,
  20. TTY: false,
  21. },
  22. scheme.ParameterCodec,
  23. )
  24. var stdout, stderr bytes.Buffer
  25. exec, err := remotecommand.NewSPDYExecutor(config, "POST", req.URL())
  26. if err != nil {
  27. response.FailWithMessage(response.InternalServerError, err.Error(), c)
  28. return
  29. }
  30. err = exec.Stream(remotecommand.StreamOptions{
  31. Stdin: nil,
  32. Stdout: &stdout,
  33. Stderr: &stderr,
  34. })
  35. if err != nil {
  36. response.FailWithMessage(response.InternalServerError, "Error obtaining file", c)
  37. return
  38. }
  39. fmt.Println(stdout.String())
  40. }

Execution Result Output

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

Expect the result

  1. "data": [
  2. {
  3. "perm": "drwxr-xr-x",
  4. "mod_time": "2022-03-02 15:02:15",
  5. "kind": "d",
  6. "name": "temp",
  7. "size": ""
  8. },
  9. ]

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命令。

  1. $ stat yourFileOrDirName

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

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

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

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

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

输出将会是:

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

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

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

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

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

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

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

然后获得DIY的JSON输出:

  1. {"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.

  1. $ stat yourFileOrDirName

The output of this command by default is like this:

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

But you can tweak its output like this:

  1. $ 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:

  1. 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:

  1. $ 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:

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

and get the DIY json output:

  1. {"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:

确定