-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Operator endpoints for cluster insights (#1312)
- Loading branch information
1 parent
bb2f76a
commit 9822b61
Showing
4 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package cluster | ||
|
||
import ( | ||
"context" | ||
|
||
"connectrpc.com/connect" | ||
api_cluster "github.com/rigdev/rig-go-api/operator/api/v1/cluster" | ||
"github.com/rigdev/rig-go-api/operator/api/v1/cluster/clusterconnect" | ||
"github.com/rigdev/rig/pkg/service/cluster" | ||
) | ||
|
||
func NewHandler( | ||
cluster cluster.Service, | ||
) clusterconnect.ServiceHandler { | ||
return &handler{ | ||
cluster: cluster, | ||
} | ||
} | ||
|
||
type handler struct { | ||
cluster cluster.Service | ||
} | ||
|
||
func (h *handler) GetNodes( | ||
ctx context.Context, _ *connect.Request[api_cluster.GetNodesRequest], | ||
) (*connect.Response[api_cluster.GetNodesResponse], error) { | ||
nodes, err := h.cluster.GetNodes(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return connect.NewResponse(&api_cluster.GetNodesResponse{ | ||
Nodes: nodes, | ||
}), nil | ||
} | ||
|
||
func (h *handler) GetNodePods( | ||
ctx context.Context, req *connect.Request[api_cluster.GetNodePodsRequest], | ||
) (*connect.Response[api_cluster.GetNodePodsResponse], error) { | ||
pods, err := h.cluster.GetNodePods(ctx, req.Msg.GetNodeName()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return connect.NewResponse(&api_cluster.GetNodePodsResponse{ | ||
Pods: pods, | ||
}), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package cluster | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"maps" | ||
"slices" | ||
|
||
"github.com/rigdev/rig-go-api/model" | ||
api_cluster "github.com/rigdev/rig-go-api/operator/api/v1/cluster" | ||
"github.com/rigdev/rig/pkg/pipeline" | ||
corev1 "k8s.io/api/core/v1" | ||
metricsv1beta1 "k8s.io/metrics/pkg/apis/metrics/v1beta1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
type Service interface { | ||
GetNodes(ctx context.Context) ([]*api_cluster.Node, error) | ||
GetNodePods(ctx context.Context, nodeName string) ([]*api_cluster.Pod, error) | ||
} | ||
|
||
func New(client client.Client) Service { | ||
return &service{ | ||
client: client, | ||
} | ||
} | ||
|
||
type service struct { | ||
client client.Client | ||
} | ||
|
||
func (s *service) GetNodes(ctx context.Context) ([]*api_cluster.Node, error) { | ||
listReq := corev1.NodeList{} | ||
if err := s.client.List(ctx, &listReq); err != nil { | ||
return nil, fmt.Errorf("failed to list nodes: %w", err) | ||
} | ||
|
||
nodes := map[string]*api_cluster.Node{} | ||
|
||
for _, node := range listReq.Items { | ||
nodes[node.GetName()] = &api_cluster.Node{ | ||
NodeName: node.GetName(), | ||
Allocateable: &model.Resources{ | ||
CpuMillis: uint64(node.Status.Allocatable.Cpu().MilliValue()), | ||
MemoryBytes: uint64(node.Status.Allocatable.Memory().Value()), | ||
}, | ||
MaxPods: uint64(node.Status.Allocatable.Pods().Value()), | ||
} | ||
} | ||
|
||
list := metricsv1beta1.NodeMetricsList{} | ||
if err := s.client.List(ctx, &list); err != nil { | ||
return nil, fmt.Errorf("failed to list node metrics: %w", err) | ||
} | ||
|
||
for _, node := range list.Items { | ||
n, ok := nodes[node.GetName()] | ||
if !ok { | ||
n = &api_cluster.Node{ | ||
NodeName: node.GetName(), | ||
Allocateable: &model.Resources{}, | ||
Usage: &model.Resources{}, | ||
MaxPods: 0, | ||
} | ||
nodes[node.GetName()] = n | ||
} | ||
n.Usage = &model.Resources{ | ||
CpuMillis: uint64(node.Usage.Cpu().MilliValue()), | ||
MemoryBytes: uint64(node.Usage.Memory().Value()), | ||
} | ||
} | ||
|
||
keys := slices.Sorted((maps.Keys(nodes))) | ||
var res []*api_cluster.Node | ||
for _, k := range keys { | ||
res = append(res, nodes[k]) | ||
} | ||
return res, nil | ||
} | ||
|
||
func (s *service) GetNodePods(ctx context.Context, nodeName string) ([]*api_cluster.Pod, error) { | ||
listReq := corev1.PodList{} | ||
if err := s.client.List(ctx, &listReq, client.MatchingFields{ | ||
"spec.nodeName": nodeName, | ||
}); err != nil { | ||
return nil, err | ||
} | ||
|
||
var res []*api_cluster.Pod | ||
for _, pod := range listReq.Items { | ||
req := &model.Resources{} | ||
for _, c := range pod.Spec.Containers { | ||
req.CpuMillis += uint64(c.Resources.Requests.Cpu().MilliValue()) | ||
req.MemoryBytes += uint64(c.Resources.Requests.Memory().Value()) | ||
} | ||
res = append(res, &api_cluster.Pod{ | ||
PodName: pod.GetName(), | ||
Namespace: pod.GetNamespace(), | ||
Requested: req, | ||
CapsuleName: pod.Labels[pipeline.LabelCapsule], | ||
}) | ||
} | ||
|
||
return res, nil | ||
} |