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

8/6/2017

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
})
}
-- 陳冠維
go
kubernetes

1 Answer

8/7/2017

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

-- Eric Tune
Source: StackOverflow