在Kubernetes节点信息中添加位置,并让kubectl将其打印出来。

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

Add location in kubernetes node infomation and let kubectl print it out

问题

我想在Kubernetes节点的信息中添加位置信息,并让kubectl命令的"describe node"打印出节点的位置。然而,我无法打印出位置的值。输出结果如图所示。我做错了什么或者我漏掉了什么?非常感谢任何帮助。

我已经在kubernetes/vendor/github.com/google/cadvisor/info/v1/machine.go中添加了新的变量:

  1. Location string `json:"location"`

并修改了设置和传递Location值的相应文件。

我还在/home/william/kubernetes/pkg/api/types.go中添加了新的变量:

  1. Location string `json:"location"`

以及在/home/william/kubernetes/pkg/api/v1/types.go中添加了:

  1. Location string `json:"location" protobuf:"bytes,2,opt,name=location"`

我在/home/william/kubernetes/pkg/api/v1/zz_generated.conversion.go中的autoConvert_v1_NodeSystemInfo_To_api_NodeSystemInfo函数中添加了:

  1. out.Location = in.Location

函数如下:

  1. func autoConvert_v1_NodeSystemInfo_To_api_NodeSystemInfo(in *NodeSystemInfo, out *api.NodeSystemInfo, s conversion.Scope) error {
  2. out.MachineID = in.MachineID
  3. out.SystemUUID = in.SystemUUID
  4. out.Location = in.Location
  5. out.BootID = in.BootID
  6. out.KernelVersion = in.KernelVersion
  7. out.OSImage = in.OSImage
  8. out.ContainerRuntimeVersion = in.ContainerRuntimeVersion
  9. out.KubeletVersion = in.KubeletVersion
  10. out.KubeProxyVersion = in.KubeProxyVersion
  11. out.OperatingSystem = in.OperatingSystem
  12. out.Architecture = in.Architecture
  13. return nil
  14. }

并在/home/william/kubernetes/pkg/kubectl/describe.go中的describeNode函数中添加了:

  1. fmt.Fprintf(out, " Location:\t%s\n", node.Status.NodeInfo.Location)

函数如下:

  1. func describeNode(node *api.Node, nodeNonTerminatedPodsList *api.PodList, events *api.EventList, canViewPods bool) (string, error) {
  2. return tabbedString(func(out io.Writer) error {
  3. fmt.Fprintf(out, "Name:\t%s\n", node.Name)
  4. fmt.Fprintf(out, "Role:\t%s\n", findNodeRole(node))
  5. printLabelsMultiline(out, "Labels", node.Labels)
  6. printTaintsInAnnotationMultiline(out, "Taints", node.Annotations)
  7. fmt.Fprintf(out, "CreationTimestamp:\t%s\n", node.CreationTimestamp.Time.Format(time.RFC1123Z))
  8. fmt.Fprintf(out, "Phase:\t%v\n", node.Status.Phase)
  9. if len(node.Status.Conditions) > 0 {
  10. fmt.Fprint(out, "Conditions:\n Type\tStatus\tLastHeartbeatTime\tLastTransitionTime\tReason\tMessage\n")
  11. fmt.Fprint(out, " ----\t------\t-----------------\t------------------\t------\t-------\n")
  12. for _, c := range node.Status.Conditions {
  13. fmt.Fprintf(out, " %v \t%v \t%s \t%s \t%v \t%v\n",
  14. c.Type,
  15. c.Status,
  16. c.LastHeartbeatTime.Time.Format(time.RFC1123Z),
  17. c.LastTransitionTime.Time.Format(time.RFC1123Z),
  18. c.Reason,
  19. c.Message)
  20. }
  21. }
  22. addresses := make([]string, 0, len(node.Status.Addresses))
  23. for _, address := range node.Status.Addresses {
  24. addresses = append(addresses, address.Address)
  25. }
  26. printResourceList := func(resourceList api.ResourceList) {
  27. resources := make([]api.ResourceName, 0, len(resourceList))
  28. for resource := range resourceList {
  29. resources = append(resources, resource)
  30. }
  31. sort.Sort(SortableResourceNames(resources))
  32. for _, resource := range resources {
  33. value := resourceList[resource]
  34. fmt.Fprintf(out, " %s:\t%s\n", resource, value.String())
  35. }
  36. }
  37. fmt.Fprintf(out, "Addresses:\t%s\n", strings.Join(addresses, ","))
  38. if len(node.Status.Capacity) > 0 {
  39. fmt.Fprintf(out, "Capacity:\n")
  40. printResourceList(node.Status.Capacity)
  41. }
  42. if len(node.Status.Allocatable) > 0 {
  43. fmt.Fprintf(out, "Allocatable:\n")
  44. printResourceList(node.Status.Allocatable)
  45. }
  46. fmt.Fprintf(out, "System Info:\n")
  47. fmt.Fprintf(out, " Machine ID:\t%s\n", node.Status.NodeInfo.MachineID)
  48. fmt.Fprintf(out, " System UUID:\t%s\n", node.Status.NodeInfo.SystemUUID)
  49. fmt.Fprintf(out, " Location:\t%s\n", node.Status.NodeInfo.Location)
  50. fmt.Fprintf(out, " Boot ID:\t%s\n", node.Status.NodeInfo.BootID)
  51. fmt.Fprintf(out, " Kernel Version:\t%s\n", node.Status.NodeInfo.KernelVersion)
  52. fmt.Fprintf(out, " OS Image:\t%s\n", node.Status.NodeInfo.OSImage)
  53. fmt.Fprintf(out, " Operating System:\t%s\n", node.Status.NodeInfo.OperatingSystem)
  54. fmt.Fprintf(out, " Architecture:\t%s\n", node.Status.NodeInfo.Architecture)
  55. fmt.Fprintf(out, " Container Runtime Version:\t%s\n", node.Status.NodeInfo.ContainerRuntimeVersion)
  56. fmt.Fprintf(out, " Kubelet Version:\t%s\n", node.Status.NodeInfo.KubeletVersion)
  57. fmt.Fprintf(out, " Kube-Proxy Version:\t%s\n", node.Status.NodeInfo.KubeProxyVersion)
  58. if len(node.Spec.PodCIDR) > 0 {
  59. fmt.Fprintf(out, "PodCIDR:\t%s\n", node.Spec.PodCIDR)
  60. }
  61. if len(node.Spec.ExternalID) > 0 {
  62. fmt.Fprintf(out, "ExternalID:\t%s\n", node.Spec.ExternalID)
  63. }
  64. if canViewPods && nodeNonTerminatedPodsList != nil {
  65. if err := describeNodeResource(nodeNonTerminatedPodsList, node, out); err != nil {
  66. return err
  67. }
  68. } else {
  69. fmt.Fprintf(out, "Pods:\tnot authorized\n")
  70. }
  71. if events != nil {
  72. DescribeEvents(events, out)
  73. }
  74. return nil
  75. })
  76. }
英文:

I want to add location information in kubernetes nodes' information and let kubectl command "describe node" print out the location of the node. However, I can't print out the value of Location.
The output is look like the image.
output of current code
What did I do wrong or what should I do but I missed it?
Any help will be very much appreciated

I already added new variable

  1. Location string `json:"location"`

in kubernetes/vendor/github.com/google/cadvisor/info/v1/machine.go

and modified corresponding file that set and pass the value of Location.

I also added new variable

  1. Location string `json:"location"`

in /home/william/kubernetes/pkg/api/types.go

and

  1. Location string `json:"location" protobuf:"bytes,2,opt,name=location"`

in /home/william/kubernetes/pkg/api/v1/types.go

I added

  1. out.Location = in.Location

in function autoConvert_v1_NodeSystemInfo_To_api_NodeSystemInfo in /home/william/kubernetes/pkg/api/v1/zz_generated.conversion.go

  1. func autoConvert_v1_NodeSystemInfo_To_api_NodeSystemInfo(in *NodeSystemInfo, out *api.NodeSystemInfo, s conversion.Scope) error {
  2. out.MachineID = in.MachineID
  3. out.SystemUUID = in.SystemUUID
  4. out.Location = in.Location
  5. out.BootID = in.BootID
  6. out.KernelVersion = in.KernelVersion
  7. out.OSImage = in.OSImage
  8. out.ContainerRuntimeVersion = in.ContainerRuntimeVersion
  9. out.KubeletVersion = in.KubeletVersion
  10. out.KubeProxyVersion = in.KubeProxyVersion
  11. out.OperatingSystem = in.OperatingSystem
  12. out.Architecture = in.Architecture
  13. return nil
  14. }

and add

  1. fmt.Fprintf(out, " Location:\t%s\n", node.Status.NodeInfo.Location)

in function describeNode in /home/william/kubernetes/pkg/kubectl/describe.go

  1. func describeNode(node *api.Node, nodeNonTerminatedPodsList *api.PodList, events *api.EventList, canViewPods bool) (string, error) {
  2. return tabbedString(func(out io.Writer) error {
  3. fmt.Fprintf(out, "Name:\t%s\n", node.Name)
  4. fmt.Fprintf(out, "Role:\t%s\n", findNodeRole(node))
  5. printLabelsMultiline(out, "Labels", node.Labels)
  6. printTaintsInAnnotationMultiline(out, "Taints", node.Annotations)
  7. fmt.Fprintf(out, "CreationTimestamp:\t%s\n", node.CreationTimestamp.Time.Format(time.RFC1123Z))
  8. fmt.Fprintf(out, "Phase:\t%v\n", node.Status.Phase)
  9. if len(node.Status.Conditions) > 0 {
  10. fmt.Fprint(out, "Conditions:\n Type\tStatus\tLastHeartbeatTime\tLastTransitionTime\tReason\tMessage\n")
  11. fmt.Fprint(out, " ----\t------\t-----------------\t------------------\t------\t-------\n")
  12. for _, c := range node.Status.Conditions {
  13. fmt.Fprintf(out, " %v \t%v \t%s \t%s \t%v \t%v\n",
  14. c.Type,
  15. c.Status,
  16. c.LastHeartbeatTime.Time.Format(time.RFC1123Z),
  17. c.LastTransitionTime.Time.Format(time.RFC1123Z),
  18. c.Reason,
  19. c.Message)
  20. }
  21. }
  22. addresses := make([]string, 0, len(node.Status.Addresses))
  23. for _, address := range node.Status.Addresses {
  24. addresses = append(addresses, address.Address)
  25. }
  26. printResourceList := func(resourceList api.ResourceList) {
  27. resources := make([]api.ResourceName, 0, len(resourceList))
  28. for resource := range resourceList {
  29. resources = append(resources, resource)
  30. }
  31. sort.Sort(SortableResourceNames(resources))
  32. for _, resource := range resources {
  33. value := resourceList[resource]
  34. fmt.Fprintf(out, " %s:\t%s\n", resource, value.String())
  35. }
  36. }
  37. fmt.Fprintf(out, "Addresses:\t%s\n", strings.Join(addresses, ","))
  38. if len(node.Status.Capacity) > 0 {
  39. fmt.Fprintf(out, "Capacity:\n")
  40. printResourceList(node.Status.Capacity)
  41. }
  42. if len(node.Status.Allocatable) > 0 {
  43. fmt.Fprintf(out, "Allocatable:\n")
  44. printResourceList(node.Status.Allocatable)
  45. }
  46. fmt.Fprintf(out, "System Info:\n")
  47. fmt.Fprintf(out, " Machine ID:\t%s\n", node.Status.NodeInfo.MachineID)
  48. fmt.Fprintf(out, " System UUID:\t%s\n", node.Status.NodeInfo.SystemUUID)
  49. fmt.Fprintf(out, " Location:\t%s\n", node.Status.NodeInfo.Location)
  50. fmt.Fprintf(out, " Boot ID:\t%s\n", node.Status.NodeInfo.BootID)
  51. fmt.Fprintf(out, " Kernel Version:\t%s\n", node.Status.NodeInfo.KernelVersion)
  52. fmt.Fprintf(out, " OS Image:\t%s\n", node.Status.NodeInfo.OSImage)
  53. fmt.Fprintf(out, " Operating System:\t%s\n", node.Status.NodeInfo.OperatingSystem)
  54. fmt.Fprintf(out, " Architecture:\t%s\n", node.Status.NodeInfo.Architecture)
  55. fmt.Fprintf(out, " Container Runtime Version:\t%s\n", node.Status.NodeInfo.ContainerRuntimeVersion)
  56. fmt.Fprintf(out, " Kubelet Version:\t%s\n", node.Status.NodeInfo.KubeletVersion)
  57. fmt.Fprintf(out, " Kube-Proxy Version:\t%s\n", node.Status.NodeInfo.KubeProxyVersion)
  58. if len(node.Spec.PodCIDR) > 0 {
  59. fmt.Fprintf(out, "PodCIDR:\t%s\n", node.Spec.PodCIDR)
  60. }
  61. if len(node.Spec.ExternalID) > 0 {
  62. fmt.Fprintf(out, "ExternalID:\t%s\n", node.Spec.ExternalID)
  63. }
  64. if canViewPods && nodeNonTerminatedPodsList != nil {
  65. if err := describeNodeResource(nodeNonTerminatedPodsList, node, out); err != nil {
  66. return err
  67. }
  68. } else {
  69. fmt.Fprintf(out, "Pods:\tnot authorized\n")
  70. }
  71. if events != nil {
  72. DescribeEvents(events, out)
  73. }
  74. return nil
  75. })
  76. }

答案1

得分: 0

编写一个程序,在每个节点上运行,该程序读取节点的位置,然后更新 Kubernetes 节点 API 中的节点标签或注释。

您不需要修改 Kubernetes 源代码来实现此目的。如果您修改了 Kubernetes 源代码,将更难捕捉到未来的更改,并且您将无法使用标准版本发布。

英文:

Write a program that runs on each node which reads the nodes location and then updates the node's label or (annotation) in the kubernetes node API.

You don't need to modify the kubernetes source to accomplish this. If you modify the kubernetes source, it will be harder to pickup future changes and you won't be able to use standard releases..

huangapple
  • 本文由 发表于 2017年8月6日 22:06:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/45532809.html
匿名

发表评论

匿名网友

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

确定