Skip to content

Commit

Permalink
Add Kubernetes service type and ports
Browse files Browse the repository at this point in the history
  • Loading branch information
bboreham committed Feb 24, 2018
1 parent 4407604 commit 64570f1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
4 changes: 4 additions & 0 deletions probe/kubernetes/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const (
Replicas = report.KubernetesReplicas
DesiredReplicas = report.KubernetesDesiredReplicas
NodeType = report.KubernetesNodeType
Type = report.KubernetesType
Ports = report.KubernetesPorts
)

// Exposed for testing
Expand All @@ -44,6 +46,8 @@ var (
PublicIP: {ID: PublicIP, Label: "Public IP", From: report.FromLatest, Datatype: report.IP, Priority: 4},
IP: {ID: IP, Label: "Internal IP", From: report.FromLatest, Datatype: report.IP, Priority: 5},
report.Pod: {ID: report.Pod, Label: "# Pods", From: report.FromCounters, Datatype: report.Number, Priority: 6},
Type: {ID: Type, Label: "Type", From: report.FromLatest, Priority: 7},
Ports: {ID: Ports, Label: "Ports", From: report.FromLatest, Priority: 8},
}

ServiceMetricTemplates = PodMetricTemplates
Expand Down
22 changes: 21 additions & 1 deletion probe/kubernetes/service.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package kubernetes

import (
"fmt"

"github.com/weaveworks/scope/report"

apiv1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -37,11 +39,29 @@ func (s *service) Selector() labels.Selector {
return labels.SelectorFromSet(labels.Set(s.Spec.Selector))
}

// human-readable version of a Kubernetes ServicePort
func servicePortString(p apiv1.ServicePort) string {
if p.NodePort == 0 {
return fmt.Sprintf("%d/%s", p.Port, p.Protocol)
}
return fmt.Sprintf("%d:%d/%s", p.Port, p.NodePort, p.Protocol)
}

func (s *service) GetNode() report.Node {
latest := map[string]string{IP: s.Spec.ClusterIP}
latest := map[string]string{
IP: s.Spec.ClusterIP,
Type: string(s.Spec.Type),
}
if s.Spec.LoadBalancerIP != "" {
latest[PublicIP] = s.Spec.LoadBalancerIP
}
if len(s.Spec.Ports) != 0 {
portStr := ""
for _, p := range s.Spec.Ports {
portStr = portStr + servicePortString(p) + ","
}
latest[Ports] = portStr[:len(portStr)-1]
}
return s.MetaNode(report.MakeServiceNodeID(s.UID())).WithLatests(latest)
}

Expand Down
4 changes: 4 additions & 0 deletions report/map_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ const (
KubernetesLastScheduled = "kubernetes_last_scheduled"
KubernetesActiveJobs = "kubernetes_active_jobs"
KubernetesStateDeleted = "deleted"
KubernetesType = "kubernetes_type"
KubernetesPorts = "kubernetes_ports"
// probe/awsecs
ECSCluster = "ecs_cluster"
ECSCreatedAt = "ecs_created_at"
Expand Down Expand Up @@ -173,6 +175,8 @@ var commonKeys = map[string]string{
KubernetesSuspended: KubernetesSuspended,
KubernetesLastScheduled: KubernetesLastScheduled,
KubernetesActiveJobs: KubernetesActiveJobs,
KubernetesType: KubernetesType,
KubernetesPorts: KubernetesPorts,

ECSCluster: ECSCluster,
ECSCreatedAt: ECSCreatedAt,
Expand Down

0 comments on commit 64570f1

Please sign in to comment.