Skip to content

Commit

Permalink
Merge pull request #197 from amosproj/feat/#43-collect-pod-informatio…
Browse files Browse the repository at this point in the history
…n-from-kubernetes-cluster

Feat/#43 Collect pod information from Kubernetes cluster
  • Loading branch information
smnws authored Jul 3, 2023
2 parents f807c51 + 677c0f4 commit cc786f6
Show file tree
Hide file tree
Showing 5 changed files with 444 additions and 177 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ repos:
rev: v4.4.0
hooks:
- id: trailing-whitespace
exclude: ^DB/testdata_init\.sql$
- id: end-of-file-fixer
- id: check-yaml
- repo: https://github.com/alessandrojcm/commitlint-pre-commit-hook
Expand Down
28 changes: 22 additions & 6 deletions DB/kubernetes-schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,23 @@ CREATE TABLE pods(
"node_name" text,
"namespace" text,
"status_phase" text,
"data" json NOT NULL
"data" json NOT NULL,
"host_ip" text,
"pod_ip" text,
"pod_ips" text ARRAY,
"start_time" timestamp,
"qos_class" text
);

CREATE TABLE pod_status_conditions(
"id" int GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
"pod_id" int REFERENCES pods(id) ON DELETE CASCADE,
"type" text,
"status" text,
"last_probe_time" timestamp,
"last_transition_time" timestamp,
"reason" text,
"message" text
);

CREATE TABLE "container_states"(
Expand Down Expand Up @@ -69,20 +85,20 @@ CREATE TABLE containers(
"ready" bool,
"restart_count" int,
"started" bool,
"state_id" int REFERENCES "container_states" (id),
"last_state_id" int REFERENCES "container_states" (id)
"state_id" int REFERENCES "container_states"(id),
"last_state_id" int REFERENCES "container_states"(id)
);

CREATE TABLE "volume_devices"(
"id" int GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
"container_id" int REFERENCES "containers" ("id") NOT NULL,
"container_id" int REFERENCES "containers"("id") NOT NULL,
"device_path" text,
"name" text
);

CREATE TABLE "volume_mounts"(
"id" int GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
"container_id" int REFERENCES "containers" ("id") NOT NULL,
"container_id" int REFERENCES "containers"("id") NOT NULL,
"mount_path" text,
"mount_propagation" text,
"name" text,
Expand All @@ -93,7 +109,7 @@ CREATE TABLE "volume_mounts"(

CREATE TABLE "container_ports"(
"id" int GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
"container_id" int REFERENCES "containers" ("id") NOT NULL,
"container_id" int REFERENCES "containers"("id") NOT NULL,
"container_port" int,
"host_ip" text,
"host_port" int,
Expand Down
526 changes: 362 additions & 164 deletions DB/testdata_init.sql

Large diffs are not rendered by default.

48 changes: 42 additions & 6 deletions Proxy/internal/cluster/pod_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cluster
import (
"context"
"encoding/json"
"time"

model "github.com/amosproj/amos2023ss04-kubernetes-inventory-taker/Proxy/internal/database/model"
"github.com/uptrace/bun"
Expand All @@ -19,29 +20,64 @@ func ProcessPod(event Event, bunDB *bun.DB) {
return
}

insertPod(podNew, bunDB, event.timestamp)

// This adds the containers inside the pod
ProcessContainer(podNew, bunDB, event.timestamp)
}

func insertPod(podNew *corev1.Pod, bunDB *bun.DB, eventTimestamp time.Time) {
jsonData, err := json.Marshal(podNew)
if err != nil {
klog.Error("Error converting Node to JSON:", err)
klog.Error(err)
}

podStatus := podNew.Status
podIPs := make([]string, len(podStatus.PodIPs))

for i, podIP := range podStatus.PodIPs {
podIPs[i] = podIP.IP
}

podDB := &model.Pod{
ID: 0,
PodResourceVersion: podNew.ResourceVersion,
PodID: string(podNew.UID),
Timestamp: event.timestamp,
Timestamp: eventTimestamp,
NodeName: podNew.Spec.NodeName,
Name: podNew.Name,
Namespace: podNew.Namespace,
StatusPhase: string(podNew.Status.Phase),
Data: string(jsonData),
HostIP: podStatus.HostIP,
PodIP: podStatus.PodIP,
PodIPs: podIPs,
StartTime: podStatus.StartTime.Time,
QOSClass: string(podStatus.QOSClass),
}

// Insert the pod into the database
_, err = bunDB.NewInsert().Model(podDB).Exec(context.Background())
if err != nil {
klog.Error(err)
}

// This adds the containers inside the pod
ProcessContainer(podNew, bunDB, event.timestamp)
insertPodStatusConditions(podStatus, podDB.ID, bunDB)
}

func insertPodStatusConditions(podStatus corev1.PodStatus, podID int, bunDB *bun.DB) {
for _, condition := range podStatus.Conditions {
podStatusConditionDB := &model.PodStatusCondition{
PodID: podID,
Type: string(condition.Type),
Status: string(condition.Status),
LastProbeTime: condition.LastProbeTime.Time,
LastTransitionTime: condition.LastTransitionTime.Time,
Reason: condition.Reason,
Message: condition.Message,
}

_, err := bunDB.NewInsert().Model(podStatusConditionDB).Exec(context.Background())
if err != nil {
klog.Error(err)
}
}
}
18 changes: 17 additions & 1 deletion Proxy/internal/database/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Service struct {
}

type Pod struct {
ID int `bun:"id,autoincrement"`
ID int `bun:"id,autoincrement,pk"`
PodResourceVersion string `bun:"pod_resource_version,type:text,notnull"`
PodID string `bun:"pod_id,type:uuid,notnull"`
Timestamp time.Time `bun:"timestamp,type:timestamp,notnull"`
Expand All @@ -34,6 +34,22 @@ type Pod struct {
Namespace string `bun:"namespace,type:text"`
StatusPhase string `bun:"status_phase,type:text"`
Data string `bun:"data,type:json"`
HostIP string `bun:"host_ip"`
PodIP string `bun:"pod_ip"`
PodIPs []string `bun:"pod_ips,array"`
StartTime time.Time `bun:"start_time"`
QOSClass string `bun:"qos_class"`
}

type PodStatusCondition struct {
ID int `bun:"id,autoincrement,notnull,pk"`
PodID int `bun:"pod_id"`
Type string `bun:"type"`
Status string `bun:"status"`
LastProbeTime time.Time `bun:"last_probe_time,type:timestamp,nullzero"`
LastTransitionTime time.Time `bun:"last_transition_time,type:timestamp"`
Reason string `bun:"reason"`
Message string `bun:"message"`
}

type Node struct {
Expand Down

0 comments on commit cc786f6

Please sign in to comment.