Skip to content

Commit

Permalink
feat: add pod cpu memory funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
yashmehrotra committed Apr 5, 2024
1 parent fdefe42 commit 2b84774
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 15 deletions.
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(),
celPodMaxMemoryBytes(),
celPodMaxCPUMillicores(),
}
}
84 changes: 69 additions & 15 deletions kubernetes/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,74 @@ func celPodProperties() cel.EnvOption {
)
}

func celPodMaxMemoryBytes() cel.EnvOption {
return cel.Function("k8s.podMaxMemoryBytes",
cel.Overload("k8s.podMaxMemoryBytes_obj_int",
[]*cel.Type{cel.AnyType},
cel.AnyType,
cel.UnaryBinding(func(obj ref.Val) ref.Val {
val := conv.ToInt(podMaxMemory(obj.Value()))
return types.Int(val)
}),
),
)
}

func podMaxMemory(input any) 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
}
var totalMemBytes int64
for _, container := range pod.Spec.Containers {
mem := container.Resources.Limits.Memory()
if mem != nil {
totalMemBytes += _k8sMemoryAsBytes(mem.String())
}
}
return totalMemBytes
}

func celPodMaxCPUMillicores() cel.EnvOption {
return cel.Function("k8s.podMaxCPUMillicores",
cel.Overload("k8s.podMaxCPUMillicores_obj_int",
[]*cel.Type{cel.AnyType},
cel.AnyType,
cel.UnaryBinding(func(obj ref.Val) ref.Val {
val := conv.ToInt(podMaxCPU(obj.Value()))
return types.Int(val)
}),
),
)
}

func podMaxCPU(input any) 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
}
var totalCPU int64
for _, container := range pod.Spec.Containers {
cpu := container.Resources.Limits.Cpu()
if cpu != nil {
totalCPU += _k8sCPUAsMillicores(cpu.String())
}
}
return totalCPU
}

func PodComponentProperties(input any) []map[string]any {
obj := GetUnstructured(input)
if obj == nil {
Expand Down Expand Up @@ -94,7 +162,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 +195,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},
}
}

0 comments on commit 2b84774

Please sign in to comment.