|
| 1 | +package features |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "strconv" |
| 8 | + "strings" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/olekukonko/tablewriter" |
| 12 | + corev1 "k8s.io/api/core/v1" |
| 13 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 14 | +) |
| 15 | + |
| 16 | +func ShowServiceByFilter(services *corev1.ServiceList) { |
| 17 | + table := tablewriter.NewWriter(os.Stdout) |
| 18 | + table.SetHeader([]string{ |
| 19 | + "NAME", |
| 20 | + "TYPE", |
| 21 | + "CLUSTER-IP", |
| 22 | + "EXTERNAL-IP(S)", |
| 23 | + "PORT(S)", |
| 24 | + "AGE", |
| 25 | + }) |
| 26 | + |
| 27 | + table.SetAutoFormatHeaders(false) |
| 28 | + table.SetAutoWrapText(false) |
| 29 | + |
| 30 | + for _, service := range services.Items { |
| 31 | + var externalIPs string |
| 32 | + if service.Spec.Type == corev1.ServiceTypeLoadBalancer && len(service.Status.LoadBalancer.Ingress) > 0 { |
| 33 | + if service.Status.LoadBalancer.Ingress[0].IP != "" { |
| 34 | + externalIPs = service.Status.LoadBalancer.Ingress[0].IP |
| 35 | + } else if service.Status.LoadBalancer.Ingress[0].Hostname != "" { |
| 36 | + externalIPs = service.Status.LoadBalancer.Ingress[0].Hostname |
| 37 | + } else { |
| 38 | + externalIPs = "<pending>" |
| 39 | + } |
| 40 | + } else if len(service.Spec.ExternalIPs) > 0 { |
| 41 | + externalIPs = strings.Join(service.Spec.ExternalIPs, ", ") |
| 42 | + } else { |
| 43 | + externalIPs = "<none>" |
| 44 | + } |
| 45 | + age := HumanReadableDuration(time.Since(service.ObjectMeta.CreationTimestamp.Time)) |
| 46 | + ports := make([]string, len(service.Spec.Ports)) |
| 47 | + for i, port := range service.Spec.Ports { |
| 48 | + protocolName := string(port.Protocol) |
| 49 | + if port.Port != port.TargetPort.IntVal { |
| 50 | + // The port is not named, so try to find the corresponding named port |
| 51 | + for _, namedPort := range service.Spec.Ports { |
| 52 | + if namedPort.Name == port.Name { |
| 53 | + protocolName = string(namedPort.Protocol) |
| 54 | + break |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + if port.Port == 0 { |
| 59 | + ports[i] = fmt.Sprintf("%s", protocolName) |
| 60 | + } else { |
| 61 | + ports[i] = fmt.Sprintf("%d", port.Port) |
| 62 | + } |
| 63 | + if port.TargetPort.String() != "0" { |
| 64 | + ports[i] += ":" + port.TargetPort.String() |
| 65 | + } |
| 66 | + ports[i] += "/" + protocolName |
| 67 | + } |
| 68 | + |
| 69 | + table.Append([]string{ |
| 70 | + service.Name, |
| 71 | + string(service.Spec.Type), |
| 72 | + service.Spec.ClusterIP, |
| 73 | + externalIPs, |
| 74 | + strings.Join(ports, ", "), |
| 75 | + age, |
| 76 | + }) |
| 77 | + } |
| 78 | + table.Render() |
| 79 | +} |
| 80 | + |
| 81 | +func ShowEndpointByFilter(endpoints *corev1.EndpointsList) { |
| 82 | + table := tablewriter.NewWriter(os.Stdout) |
| 83 | + table.SetHeader([]string{ |
| 84 | + "NAME", |
| 85 | + "ENDPOINTS TARGET", |
| 86 | + "ENDPOINTS PORT(S)", |
| 87 | + // "ENDPOINTS NAME", |
| 88 | + "AGE", |
| 89 | + }) |
| 90 | + |
| 91 | + table.SetAutoFormatHeaders(false) |
| 92 | + table.SetAutoWrapText(false) |
| 93 | + |
| 94 | + for _, ep := range endpoints.Items { |
| 95 | + serviceName := ep.ObjectMeta.Name |
| 96 | + age := HumanReadableDuration(time.Since(ep.ObjectMeta.CreationTimestamp.Time)) |
| 97 | + |
| 98 | + for _, subset := range ep.Subsets { |
| 99 | + addresses := make([]string, len(subset.Addresses)) |
| 100 | + ports := make([]string, len(subset.Ports)) |
| 101 | + for i, addr := range subset.Addresses { |
| 102 | + target := addr.TargetRef.Name |
| 103 | + if addr.TargetRef.Kind == "Pod" { |
| 104 | + pod, err := GetPod(addr.TargetRef.Namespace, target) |
| 105 | + if err == nil { |
| 106 | + target = fmt.Sprintf("%s (%s)", target, pod.Status.PodIP) |
| 107 | + } |
| 108 | + } |
| 109 | + addresses[i] = target |
| 110 | + } |
| 111 | + for i, port := range subset.Ports { |
| 112 | + portNumber := strconv.Itoa(int(port.Port)) |
| 113 | + if int(port.Port) == 0 { |
| 114 | + portNumber = port.Name |
| 115 | + } |
| 116 | + ports[i] = portNumber |
| 117 | + } |
| 118 | + table.Append([]string{ |
| 119 | + serviceName, |
| 120 | + strings.Join(addresses, ", "), |
| 121 | + strings.Join(ports, ", "), |
| 122 | + age, |
| 123 | + }) |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + table.Render() |
| 128 | +} |
| 129 | + |
| 130 | +func GetPod(namespace string, name string) (*corev1.Pod, error) { |
| 131 | + clientset, err := GetClientSet(kubeconfig) |
| 132 | + if err != nil { |
| 133 | + return nil, err |
| 134 | + } |
| 135 | + |
| 136 | + ctx := context.Background() |
| 137 | + pod, err := clientset.CoreV1().Pods(namespace).Get(ctx, name, metav1.GetOptions{}) |
| 138 | + if err != nil { |
| 139 | + return nil, err |
| 140 | + } |
| 141 | + |
| 142 | + return pod, nil |
| 143 | +} |
0 commit comments