Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add pod cpu memory funcs #54

Merged
merged 2 commits into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions kubernetes/cel_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ func Library() []cel.EnvOption {
celPodProperties(),
celNodeProperties(),
celk8sLabels(),
celPodResourceLimits(),
celPodResourceRequests(),
}
}
103 changes: 88 additions & 15 deletions kubernetes/topology.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kubernetes

import (
"fmt"
"strings"

"github.com/flanksource/gomplate/v3/conv"
Expand Down Expand Up @@ -59,6 +60,92 @@ func celPodProperties() cel.EnvOption {
)
}

func celPodResourceLimits() cel.EnvOption {
return cel.Function("k8s.getResourcesLimit",
cel.Overload("k8s.getResourcesLimit_obj_str_int",
[]*cel.Type{cel.AnyType, cel.StringType},
cel.AnyType,
cel.BinaryBinding(func(obj, resourceType ref.Val) ref.Val {
val := getPodResources(obj.Value(), fmt.Sprint(resourceType.Value()), "limits")
return types.Int(val)
}),
),
)
}

func celPodResourceRequests() cel.EnvOption {
return cel.Function("k8s.getResourcesRequests",
cel.Overload("k8s.getResourcesRequests_obj_str_int",
[]*cel.Type{cel.AnyType, cel.StringType},
cel.AnyType,
cel.BinaryBinding(func(obj, resourceType ref.Val) ref.Val {
val := getPodResources(obj.Value(), fmt.Sprint(resourceType.Value()), "requests")
return types.Int(val)
}),
),
)
}

func getPodResources(input any, resourceType string, allocType string) int64 {
obj := GetUnstructured(input)
if obj == nil {
return 0
}

var pod corev1.Pod
err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &pod)
if err != nil {
return 0
}

if resourceType == "memory" && allocType == "limits" {
var totalMemBytes int64
for _, container := range pod.Spec.Containers {
mem := container.Resources.Limits.Memory()
if mem != nil {
totalMemBytes += _k8sMemoryAsBytes(mem.String())
}
}
return totalMemBytes
}

if resourceType == "memory" && allocType == "requests" {
var totalMemBytes int64
for _, container := range pod.Spec.Containers {
mem := container.Resources.Requests.Memory()
if mem != nil {
totalMemBytes += _k8sMemoryAsBytes(mem.String())
}
}
return totalMemBytes
}

if resourceType == "cpu" && allocType == "limits" {
var totalCPU int64
for _, container := range pod.Spec.Containers {
cpu := container.Resources.Limits.Cpu()
if cpu != nil {
totalCPU += _k8sCPUAsMillicores(cpu.String())
}
}
return totalCPU

}

if resourceType == "cpu" && allocType == "requests" {
var totalCPU int64
for _, container := range pod.Spec.Containers {
cpu := container.Resources.Requests.Cpu()
if cpu != nil {
totalCPU += _k8sCPUAsMillicores(cpu.String())
}
}
return totalCPU
}

return 0
}

func PodComponentProperties(input any) []map[string]any {
obj := GetUnstructured(input)
if obj == nil {
Expand Down Expand Up @@ -94,7 +181,7 @@ func PodComponentProperties(input any) []map[string]any {
{"name": "memory", "max": totalMemBytes, "unit": "bytes", "headline": true},
{"name": "node", "text": pod.Spec.NodeName},
{"name": "created_at", "text": pod.ObjectMeta.CreationTimestamp.String()},
{"name": "ips", "text": pod.Status.PodIP},
{"name": "namespace", "text": pod.ObjectMeta.Namespace},
}
}

Expand Down Expand Up @@ -127,24 +214,10 @@ func NodeComponentProperties(input any) []map[string]any {
totalMemBytes := _k8sMemoryAsBytes(node.Status.Allocatable.Memory().String())
totalStorage := _k8sMemoryAsBytes(node.Status.Allocatable.StorageEphemeral().String())

var internalIP, externalIP string
for _, addr := range node.Status.Addresses {
if addr.Type == corev1.NodeInternalIP {
internalIP = addr.Address
}
if addr.Type == corev1.NodeExternalIP {
externalIP = addr.Address
}
}

return []map[string]any{
{"name": "cpu", "max": totalCPU, "unit": "millicores", "headline": true},
{"name": "memory", "max": totalMemBytes, "unit": "bytes", "headline": true},
{"name": "ephemeral-storage", "max": totalStorage, "unit": "bytes", "headline": true},
{"name": "zone", "text": node.GetLabels()["topology.kubernetes.io/zone"]},
{"name": "instance-type", "text": node.GetLabels()["node.kubernetes.io/instance-type"]},
{"name": "os-image", "text": node.Status.NodeInfo.OSImage},
{"name": "internal-ip", "text": internalIP},
{"name": "external-ip", "text": externalIP},
}
}
Loading