Files
weave-scope/probe/kubernetes/service.go
Akash Srivastava 029bbbcec8 Add describe control to all k8s resources
This commit will add a new control i.e. Describe
which will describe a k8s resource similar to kubectl describe.

Signed-off-by: Akash Srivastava <akashsrivastava4927@gmail.com>
2019-04-01 11:22:12 +05:30

74 lines
1.6 KiB
Go

package kubernetes
import (
"fmt"
"github.com/weaveworks/scope/report"
apiv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
)
// These constants are keys used in node metadata
const (
PublicIP = report.KubernetesPublicIP
)
// Service represents a Kubernetes service
type Service interface {
Meta
GetNode(probeID string) report.Node
Selector() labels.Selector
ClusterIP() string
}
type service struct {
*apiv1.Service
Meta
}
// NewService creates a new Service
func NewService(s *apiv1.Service) Service {
return &service{Service: s, Meta: meta{s.ObjectMeta}}
}
func (s *service) Selector() labels.Selector {
if s.Spec.Selector == nil {
return labels.Nothing()
}
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(probeID string) report.Node {
latest := map[string]string{
IP: s.Spec.ClusterIP,
Type: string(s.Spec.Type),
report.ControlProbeID: probeID,
}
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).
WithLatestActiveControls(Describe)
}
func (s *service) ClusterIP() string {
return s.Spec.ClusterIP
}