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

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

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中添加了新的变量:

Location string `json:"location"`

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

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

Location string `json:"location"`

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

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函数中添加了:

out.Location = in.Location

函数如下:

func autoConvert_v1_NodeSystemInfo_To_api_NodeSystemInfo(in *NodeSystemInfo, out *api.NodeSystemInfo, s conversion.Scope) error {
    out.MachineID = in.MachineID
    out.SystemUUID = in.SystemUUID
    out.Location = in.Location
    out.BootID = in.BootID
    out.KernelVersion = in.KernelVersion
    out.OSImage = in.OSImage
    out.ContainerRuntimeVersion = in.ContainerRuntimeVersion
    out.KubeletVersion = in.KubeletVersion
    out.KubeProxyVersion = in.KubeProxyVersion
    out.OperatingSystem = in.OperatingSystem
    out.Architecture = in.Architecture
    return nil
}

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

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

函数如下:

func describeNode(node *api.Node, nodeNonTerminatedPodsList *api.PodList, events *api.EventList, canViewPods bool) (string, error) {
    return tabbedString(func(out io.Writer) error {
        fmt.Fprintf(out, "Name:\t%s\n", node.Name)
        fmt.Fprintf(out, "Role:\t%s\n", findNodeRole(node))
        printLabelsMultiline(out, "Labels", node.Labels)
        printTaintsInAnnotationMultiline(out, "Taints", node.Annotations)
        fmt.Fprintf(out, "CreationTimestamp:\t%s\n", node.CreationTimestamp.Time.Format(time.RFC1123Z))
        fmt.Fprintf(out, "Phase:\t%v\n", node.Status.Phase)
        if len(node.Status.Conditions) > 0 {
            fmt.Fprint(out, "Conditions:\n  Type\tStatus\tLastHeartbeatTime\tLastTransitionTime\tReason\tMessage\n")
            fmt.Fprint(out, "  ----\t------\t-----------------\t------------------\t------\t-------\n")
            for _, c := range node.Status.Conditions {
                fmt.Fprintf(out, "  %v \t%v \t%s \t%s \t%v \t%v\n",
                    c.Type,
                    c.Status,
                    c.LastHeartbeatTime.Time.Format(time.RFC1123Z),
                    c.LastTransitionTime.Time.Format(time.RFC1123Z),
                    c.Reason,
                    c.Message)
            }
        }
        addresses := make([]string, 0, len(node.Status.Addresses))
        for _, address := range node.Status.Addresses {
            addresses = append(addresses, address.Address)
        }

        printResourceList := func(resourceList api.ResourceList) {
            resources := make([]api.ResourceName, 0, len(resourceList))
            for resource := range resourceList {
                resources = append(resources, resource)
            }
            sort.Sort(SortableResourceNames(resources))
            for _, resource := range resources {
                value := resourceList[resource]
                fmt.Fprintf(out, " %s:\t%s\n", resource, value.String())
            }
        }

        fmt.Fprintf(out, "Addresses:\t%s\n", strings.Join(addresses, ","))
        if len(node.Status.Capacity) > 0 {
            fmt.Fprintf(out, "Capacity:\n")
            printResourceList(node.Status.Capacity)
        }
        if len(node.Status.Allocatable) > 0 {
            fmt.Fprintf(out, "Allocatable:\n")
            printResourceList(node.Status.Allocatable)
        }

        fmt.Fprintf(out, "System Info:\n")
        fmt.Fprintf(out, " Machine ID:\t%s\n", node.Status.NodeInfo.MachineID)
        fmt.Fprintf(out, " System UUID:\t%s\n", node.Status.NodeInfo.SystemUUID)
        fmt.Fprintf(out, " Location:\t%s\n", node.Status.NodeInfo.Location)
        fmt.Fprintf(out, " Boot ID:\t%s\n", node.Status.NodeInfo.BootID)
        fmt.Fprintf(out, " Kernel Version:\t%s\n", node.Status.NodeInfo.KernelVersion)
        fmt.Fprintf(out, " OS Image:\t%s\n", node.Status.NodeInfo.OSImage)
        fmt.Fprintf(out, " Operating System:\t%s\n", node.Status.NodeInfo.OperatingSystem)
        fmt.Fprintf(out, " Architecture:\t%s\n", node.Status.NodeInfo.Architecture)
        fmt.Fprintf(out, " Container Runtime Version:\t%s\n", node.Status.NodeInfo.ContainerRuntimeVersion)
        fmt.Fprintf(out, " Kubelet Version:\t%s\n", node.Status.NodeInfo.KubeletVersion)
        fmt.Fprintf(out, " Kube-Proxy Version:\t%s\n", node.Status.NodeInfo.KubeProxyVersion)

        if len(node.Spec.PodCIDR) > 0 {
            fmt.Fprintf(out, "PodCIDR:\t%s\n", node.Spec.PodCIDR)
        }
        if len(node.Spec.ExternalID) > 0 {
            fmt.Fprintf(out, "ExternalID:\t%s\n", node.Spec.ExternalID)
        }
        if canViewPods && nodeNonTerminatedPodsList != nil {
            if err := describeNodeResource(nodeNonTerminatedPodsList, node, out); err != nil {
                return err
            }
        } else {
            fmt.Fprintf(out, "Pods:\tnot authorized\n")
        }
        if events != nil {
            DescribeEvents(events, out)
        }
        return nil
    })
}
英文:

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

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

Location string `json:"location"`

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

and

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

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

I added

out.Location = in.Location

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

func autoConvert_v1_NodeSystemInfo_To_api_NodeSystemInfo(in *NodeSystemInfo, out *api.NodeSystemInfo, s conversion.Scope) error {
    out.MachineID = in.MachineID
    out.SystemUUID = in.SystemUUID
    out.Location = in.Location
    out.BootID = in.BootID
    out.KernelVersion = in.KernelVersion
    out.OSImage = in.OSImage
    out.ContainerRuntimeVersion = in.ContainerRuntimeVersion
    out.KubeletVersion = in.KubeletVersion
    out.KubeProxyVersion = in.KubeProxyVersion
    out.OperatingSystem = in.OperatingSystem
    out.Architecture = in.Architecture
    return nil
}

and add

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

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

func describeNode(node *api.Node, nodeNonTerminatedPodsList *api.PodList, events *api.EventList, canViewPods bool) (string, error) {
return tabbedString(func(out io.Writer) error {
	fmt.Fprintf(out, "Name:\t%s\n", node.Name)
	fmt.Fprintf(out, "Role:\t%s\n", findNodeRole(node))
	printLabelsMultiline(out, "Labels", node.Labels)
	printTaintsInAnnotationMultiline(out, "Taints", node.Annotations)
	fmt.Fprintf(out, "CreationTimestamp:\t%s\n", node.CreationTimestamp.Time.Format(time.RFC1123Z))
	fmt.Fprintf(out, "Phase:\t%v\n", node.Status.Phase)
	if len(node.Status.Conditions) > 0 {
		fmt.Fprint(out, "Conditions:\n  Type\tStatus\tLastHeartbeatTime\tLastTransitionTime\tReason\tMessage\n")
		fmt.Fprint(out, "  ----\t------\t-----------------\t------------------\t------\t-------\n")
		for _, c := range node.Status.Conditions {
			fmt.Fprintf(out, "  %v \t%v \t%s \t%s \t%v \t%v\n",
				c.Type,
				c.Status,
				c.LastHeartbeatTime.Time.Format(time.RFC1123Z),
				c.LastTransitionTime.Time.Format(time.RFC1123Z),
				c.Reason,
				c.Message)
		}
	}
	addresses := make([]string, 0, len(node.Status.Addresses))
	for _, address := range node.Status.Addresses {
		addresses = append(addresses, address.Address)
	}

	printResourceList := func(resourceList api.ResourceList) {
		resources := make([]api.ResourceName, 0, len(resourceList))
		for resource := range resourceList {
			resources = append(resources, resource)
		}
		sort.Sort(SortableResourceNames(resources))
		for _, resource := range resources {
			value := resourceList[resource]
			fmt.Fprintf(out, " %s:\t%s\n", resource, value.String())
		}
	}

	fmt.Fprintf(out, "Addresses:\t%s\n", strings.Join(addresses, ","))
	if len(node.Status.Capacity) > 0 {
		fmt.Fprintf(out, "Capacity:\n")
		printResourceList(node.Status.Capacity)
	}
	if len(node.Status.Allocatable) > 0 {
		fmt.Fprintf(out, "Allocatable:\n")
		printResourceList(node.Status.Allocatable)
	}

	fmt.Fprintf(out, "System Info:\n")
	fmt.Fprintf(out, " Machine ID:\t%s\n", node.Status.NodeInfo.MachineID)
	fmt.Fprintf(out, " System UUID:\t%s\n", node.Status.NodeInfo.SystemUUID)
	fmt.Fprintf(out, " Location:\t%s\n", node.Status.NodeInfo.Location)
	fmt.Fprintf(out, " Boot ID:\t%s\n", node.Status.NodeInfo.BootID)
	fmt.Fprintf(out, " Kernel Version:\t%s\n", node.Status.NodeInfo.KernelVersion)
	fmt.Fprintf(out, " OS Image:\t%s\n", node.Status.NodeInfo.OSImage)
	fmt.Fprintf(out, " Operating System:\t%s\n", node.Status.NodeInfo.OperatingSystem)
	fmt.Fprintf(out, " Architecture:\t%s\n", node.Status.NodeInfo.Architecture)
	fmt.Fprintf(out, " Container Runtime Version:\t%s\n", node.Status.NodeInfo.ContainerRuntimeVersion)
	fmt.Fprintf(out, " Kubelet Version:\t%s\n", node.Status.NodeInfo.KubeletVersion)
	fmt.Fprintf(out, " Kube-Proxy Version:\t%s\n", node.Status.NodeInfo.KubeProxyVersion)

	if len(node.Spec.PodCIDR) > 0 {
		fmt.Fprintf(out, "PodCIDR:\t%s\n", node.Spec.PodCIDR)
	}
	if len(node.Spec.ExternalID) > 0 {
		fmt.Fprintf(out, "ExternalID:\t%s\n", node.Spec.ExternalID)
	}
	if canViewPods && nodeNonTerminatedPodsList != nil {
		if err := describeNodeResource(nodeNonTerminatedPodsList, node, out); err != nil {
			return err
		}
	} else {
		fmt.Fprintf(out, "Pods:\tnot authorized\n")
	}
	if events != nil {
		DescribeEvents(events, out)
	}
	return nil
})
}

答案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:

确定