github.com/castai/kvisor@v1.7.1-0.20240516114728-b3572a2607b5/cmd/controller/kube/server.go (about)

     1  package kube
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	kubepb "github.com/castai/kvisor/api/v1/kube"
     8  	"google.golang.org/grpc/codes"
     9  	"google.golang.org/grpc/status"
    10  )
    11  
    12  func NewServer(client *Client) kubepb.KubeAPIServer {
    13  	return &Server{client: client}
    14  }
    15  
    16  type Server struct {
    17  	client *Client
    18  }
    19  
    20  func (s *Server) GetIPInfo(ctx context.Context, req *kubepb.GetIPInfoRequest) (*kubepb.GetIPInfoResponse, error) {
    21  	info, found := s.client.GetIPInfo(req.Ip)
    22  	if !found {
    23  		return nil, status.Errorf(codes.NotFound, fmt.Sprintf("pod by ip %s not found", req.Ip))
    24  	}
    25  	res := &kubepb.IPInfo{}
    26  	if owner := info.Owner; owner != nil {
    27  		res.WorkloadName = owner.Name
    28  		res.WorkloadKind = owner.Kind
    29  		res.WorkloadUid = string(owner.UID)
    30  	}
    31  	if info.Node != nil {
    32  		if zone, found := info.Node.Labels["topology.kubernetes.io/zone"]; found {
    33  			res.Zone = zone
    34  		}
    35  		if info.Pod == nil {
    36  			res.WorkloadKind = "Node"
    37  			res.WorkloadName = info.Node.Name
    38  		}
    39  	}
    40  	if pod := info.Pod; pod != nil {
    41  		res.PodUid = string(pod.UID)
    42  		res.PodName = pod.Name
    43  		res.Namespace = pod.Namespace
    44  	}
    45  	if svc := info.Service; svc != nil {
    46  		res.WorkloadKind = "Service"
    47  		res.WorkloadName = svc.Name
    48  		res.Namespace = svc.Namespace
    49  	}
    50  	if e := info.Endpoint; e != nil {
    51  		res.WorkloadKind = "Endpoint"
    52  		res.WorkloadName = e.Name
    53  		res.Namespace = e.Namespace
    54  	}
    55  
    56  	return &kubepb.GetIPInfoResponse{
    57  		Info: res,
    58  	}, nil
    59  }
    60  
    61  func (s *Server) GetClusterInfo(ctx context.Context, req *kubepb.GetClusterInfoRequest) (*kubepb.GetClusterInfoResponse, error) {
    62  	info, found := s.client.GetClusterInfo()
    63  	if !found {
    64  		return nil, status.Errorf(codes.NotFound, "cluster info found")
    65  	}
    66  	return &kubepb.GetClusterInfoResponse{
    67  		PodsCidr:    info.PodCidr,
    68  		ServiceCidr: info.ServiceCidr,
    69  	}, nil
    70  }
    71  
    72  func (s *Server) GetPod(ctx context.Context, req *kubepb.GetPodRequest) (*kubepb.GetPodResponse, error) {
    73  	owner, found := s.client.GetPodOwner(req.Uid)
    74  	if !found {
    75  		return nil, status.Errorf(codes.NotFound, "pod owner found")
    76  	}
    77  	return &kubepb.GetPodResponse{
    78  		Pod: &kubepb.Pod{
    79  			WorkloadName: owner.Name,
    80  			WorkloadKind: owner.Kind,
    81  		},
    82  	}, nil
    83  }