Skip to content

Monitoring mode #16

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
23 changes: 11 additions & 12 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
# Build the manager binary
FROM golang:1.21 as manager
# syntax=docker/dockerfile:1.2

WORKDIR /
FROM golang:1.21 as builder

WORKDIR /app

COPY go.mod go.mod
COPY go.sum go.sum
RUN go mod download
COPY go.mod go.sum ./
# Cache go modules
RUN --mount=type=cache,target=/go/pkg/mod go mod download

# Copy the go source
COPY cmd cmd
COPY pkg pkg

RUN CGO_ENABLED=0 go build -a -o manager cmd/main.go

FROM d3fk/kubectl:latest as kubectl
ARG GO_BUILD_FLAGS=""
RUN CGO_ENABLED=0 go build ${GO_BUILD_FLAGS} -o manager cmd/main.go

FROM alpine:latest
WORKDIR /
COPY --from=manager /manager .
COPY --from=kubectl /kubectl /usr/bin/kubectl
COPY --from=builder /app/manager .
COPY --from=d3fk/kubectl:latest /kubectl /usr/bin/kubectl

ENTRYPOINT ["/manager"]
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
IMG ?= kondense/kondense:1.0.1
GO_BUILD_FLAGS ?=

# Enable Docker BuildKit
export DOCKER_BUILDKIT=1

all: build

build:
docker build -t ${IMG} .
docker build --build-arg GO_BUILD_FLAGS="${GO_BUILD_FLAGS}" -t ${IMG} .

load:
minikube image load ${IMG}
Expand All @@ -15,4 +19,6 @@ undeploy:
kubectl delete -f manifests

push:
docker push ${IMG}
docker push ${IMG}

.PHONY: all build load deploy undeploy push
5 changes: 4 additions & 1 deletion manifests/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@ spec:
resources:
limits:
cpu: 80m
memory: 100M
memory: 100M
env:
- name: STRESS-NG_MODE
value: "cpu" #"cpu" || "memory" || "all"
83 changes: 52 additions & 31 deletions pkg/controller/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@ import (
corev1 "k8s.io/api/core/v1"
)

func (r *Reconciler) ReconcileContainer(pod *corev1.Pod, container corev1.Container, wg *sync.WaitGroup) {
func (r *Reconciler) ReconcileContainer(pod *corev1.Pod, kondenseContainer *corev1.Container, container corev1.Container, wg *sync.WaitGroup) {
defer wg.Done()

exclude := utils.ContainersToExclude()
if slices.Contains(exclude, container.Name) {
return
}

err := r.UpdateStats(pod, container)
mode = utils.GetMonitorMode(r.KondenseContainer(), &container)

err := r.UpdateStats(pod, container, mode)
if err != nil {
log.Error().Err(err)
return
Expand All @@ -37,55 +39,74 @@ func (r *Reconciler) ReconcileContainer(pod *corev1.Pod, container corev1.Contai
}
}

func (r *Reconciler) UpdateStats(pod *corev1.Pod, container corev1.Container) error {
func (r *Reconciler) UpdateStats(pod *corev1.Pod, container corev1.Container, mode string) error {
var err error
var output []byte
for i := 0; i < 3; i++ {
cmd := exec.Command("kubectl", "exec", "-i", r.Name, "-c", container.Name, "--", "cat", "/sys/fs/cgroup/memory.pressure", "/sys/fs/cgroup/cpu.stat")
// we don't need kubectl for kondense container.
if strings.ToLower(container.Name) == "kondense" {
cmd = exec.Command("cat", "/sys/fs/cgroup/memory.pressure", "/sys/fs/cgroup/cpu.stat")
}
output, err = cmd.Output()
if err == nil {
r.CStats[container.Name].LastUpdate = time.Now()
break
}
time.Sleep(50 * time.Millisecond)
}

output, err = r.executeStatsCmd(container)
if err != nil {
return err
}

r.CStats[container.Name].LastUpdate = time.Now()

txt := strings.Split(string(output), " ")
if len(txt) != 15 {
return fmt.Errorf("error got unexpected stats for container %s: %s", container.Name, txt)
}

err = r.UpdateMemStats(container.Name, txt)
if err != nil {
return err
if mode == "memory" || mode == "all" {
err = r.UpdateMemStats(container.Name, txt)
if err != nil {
return err
}
}

err = r.UpdateCPUStats(container.Name, txt)
if err != nil {
return err
if mode == "cpu" || mode == "all" {
err = r.UpdateCPUStats(container.Name, txt)
if err != nil {
return err
}
}

s := r.CStats[container.Name]
log.Info().
Str("container", container.Name).
Int64("memory_limit", s.Mem.Limit).
Uint64("memory_time to decrease", s.Mem.GraceTicks).
Uint64("memory_total", s.Mem.PrevTotal).
Uint64("integral", s.Mem.Integral).
Int64("cpu_limit", s.Cpu.Limit).
Uint64("cpu_average", s.Cpu.Avg).
Msg("updated stats")

logEvent := log.Info().Str("container", container.Name)
if mode == "memory" || mode == "all" {
logEvent = logEvent.Int64("memory_limit", s.Mem.Limit).
Uint64("memory_time to decrease", s.Mem.GraceTicks).
Uint64("memory_total", s.Mem.PrevTotal).
Uint64("integral", s.Mem.Integral)
}
if mode == "cpu" || mode == "all" {
logEvent = logEvent.Int64("cpu_limit", s.Cpu.Limit).
Uint64("cpu_average", s.Cpu.Avg)
}
logEvent.Msg("updated stats")
return nil
}

func (r *Reconciler) executeStatsCmd(container corev1.Container) ([]byte, error) {
var cmd *exec.Cmd
var err error
var output []byte
retryNb := 3

if strings.ToLower(container.Name) == "kondense" {
cmd = exec.Command("cat", "/sys/fs/cgroup/memory.pressure", "/sys/fs/cgroup/cpu.stat")
} else {
cmd = exec.Command("kubectl", "exec", "-i", r.Name, "-c", container.Name, "--", "cat", "/sys/fs/cgroup/memory.pressure", "/sys/fs/cgroup/cpu.stat")
}
for i := 0; i < retryNb; i++ {
output, err = cmd.Output()
if err == nil {
return output, nil
}
time.Sleep(50 * time.Millisecond * time.Duration(i+1))
}
return nil, err
}

func (r *Reconciler) UpdateMemStats(containerName string, txt []string) error {
s := r.CStats[containerName]

Expand Down
1 change: 1 addition & 0 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func (r *Reconciler) Reconcile() {
var wg sync.WaitGroup
wg.Add(len(pod.Spec.Containers))


for _, container := range pod.Spec.Containers {
go r.ReconcileContainer(pod, container, &wg)
}
Expand Down
12 changes: 12 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ import (
"net/http"
"os"
"strings"
"slices"

"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"

corev1 "k8s.io/api/core/v1"
)

func ContainersToExclude() []string {
Expand Down Expand Up @@ -59,3 +62,12 @@ func GetBearerToken() (string, error) {

return "Bearer " + string(token), nil
}

func GetMonitorMode(kondenseContainer *corev1.Container, container *corev1.Container) (string) {
for _, envVar := range container.Env {
if envVar.Name == strings.ToUpper(container.Name) + "_MODE" {
return envVar.Value
}
}
return "all";
}