From 394fc98ca46472e33982dc9012f2834cb28657f1 Mon Sep 17 00:00:00 2001 From: Jonada Hoxha Date: Wed, 25 Sep 2024 13:37:09 +0200 Subject: [PATCH 1/2] Sync init containers --- pkg/schema/v1/pod.go | 21 ++++++++++++--------- schema/mysql/schema.sql | 17 +++++++++++++++++ 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/pkg/schema/v1/pod.go b/pkg/schema/v1/pod.go index 2f4bef9..44a9936 100644 --- a/pkg/schema/v1/pod.go +++ b/pkg/schema/v1/pod.go @@ -37,15 +37,16 @@ type Pod struct { Qos sql.NullString RestartPolicy string Yaml string - Conditions []PodCondition `db:"-"` - Containers []*Container `db:"-"` - Owners []PodOwner `db:"-"` - Labels []Label `db:"-"` - PodLabels []PodLabel `db:"-"` - Annotations []Annotation `db:"-"` - PodAnnotations []PodAnnotation `db:"-"` - Pvcs []PodPvc `db:"-"` - Volumes []PodVolume `db:"-"` + Conditions []PodCondition `db:"-"` + Containers []*Container `db:"-"` + InitContainers []*InitContainer `db:"-"` + Owners []PodOwner `db:"-"` + Labels []Label `db:"-"` + PodLabels []PodLabel `db:"-"` + Annotations []Annotation `db:"-"` + PodAnnotations []PodAnnotation `db:"-"` + Pvcs []PodPvc `db:"-"` + Volumes []PodVolume `db:"-"` factory *PodFactory } @@ -137,6 +138,7 @@ func (p *Pod) Obtain(k8s kmetav1.Object) { } p.Containers = NewContainers[Container](p, pod.Spec.Containers, pod.Status.ContainerStatuses, NewContainer) + p.InitContainers = NewContainers[InitContainer](p, pod.Spec.InitContainers, pod.Status.InitContainerStatuses, NewInitContainer) p.IcingaState, p.IcingaStateReason = p.getIcingaState(pod) @@ -399,6 +401,7 @@ func (p *Pod) Relations() []database.Relation { return []database.Relation{ database.HasMany(p.Conditions, fk), database.HasMany(p.Containers, database.WithoutCascadeDelete()), + database.HasMany(p.InitContainers, database.WithoutCascadeDelete()), database.HasMany(p.Owners, fk), database.HasMany(p.Labels, database.WithoutCascadeDelete()), database.HasMany(p.PodLabels, fk), diff --git a/schema/mysql/schema.sql b/schema/mysql/schema.sql index acaad28..c2e5cbf 100644 --- a/schema/mysql/schema.sql +++ b/schema/mysql/schema.sql @@ -55,6 +55,23 @@ CREATE TABLE container ( PRIMARY KEY (uuid) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; +CREATE TABLE init_container ( + uuid binary(16) NOT NULL, + pod_uuid binary(16) NOT NULL, + name varchar(63) COLLATE utf8mb4_unicode_ci NOT NULL, + image varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, + image_pull_policy enum('Always', 'Never', 'IfNotPresent') COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, + cpu_limits bigint unsigned NULL DEFAULT NULL, + cpu_requests bigint unsigned NULL DEFAULT NULL, + memory_limits bigint unsigned NULL DEFAULT NULL, + memory_requests bigint unsigned NULL DEFAULT NULL, + state enum('Waiting', 'Running', 'Terminated') COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, + state_details longtext NULL DEFAULT NULL, + icinga_state enum('unknown', 'pending', 'ok', 'warning', 'critical') COLLATE utf8mb4_unicode_ci NOT NULL, + icinga_state_reason text NULL DEFAULT NULL, + PRIMARY KEY (uuid) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; + CREATE TABLE container_device ( container_uuid binary(16) NOT NULL, pod_uuid binary(16) NOT NULL, From fa4e2529a5e2d914d16ee56b4230c93d02f4d26d Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Thu, 26 Sep 2024 11:36:26 +0200 Subject: [PATCH 2/2] Account sidecar containers --- pkg/schema/v1/container.go | 10 ++++++++++ pkg/schema/v1/pod.go | 27 ++++++++++++++++----------- schema/mysql/schema.sql | 20 ++++++++++++++++++++ 3 files changed, 46 insertions(+), 11 deletions(-) diff --git a/pkg/schema/v1/container.go b/pkg/schema/v1/container.go index 183320f..94e60bb 100644 --- a/pkg/schema/v1/container.go +++ b/pkg/schema/v1/container.go @@ -180,6 +180,11 @@ type InitContainer struct { } func NewInitContainer(podUuid types.UUID, container kcorev1.Container, status kcorev1.ContainerStatus) *InitContainer { + if container.RestartPolicy != nil && *container.RestartPolicy == kcorev1.ContainerRestartPolicyAlways { + // Sidecar container. + return nil + } + c := &InitContainer{} c.ContainerCommon.Obtain(podUuid, container, status) c.ContainerResources.Obtain(container) @@ -194,6 +199,11 @@ type SidecarContainer struct { } func NewSidecarContainer(podUuid types.UUID, container kcorev1.Container, status kcorev1.ContainerStatus) *SidecarContainer { + if container.RestartPolicy == nil || *container.RestartPolicy != kcorev1.ContainerRestartPolicyAlways { + // Init container. + return nil + } + c := &SidecarContainer{} c.ContainerCommon.Obtain(podUuid, container, status) c.ContainerResources.Obtain(container) diff --git a/pkg/schema/v1/pod.go b/pkg/schema/v1/pod.go index 44a9936..0eaf117 100644 --- a/pkg/schema/v1/pod.go +++ b/pkg/schema/v1/pod.go @@ -37,16 +37,17 @@ type Pod struct { Qos sql.NullString RestartPolicy string Yaml string - Conditions []PodCondition `db:"-"` - Containers []*Container `db:"-"` - InitContainers []*InitContainer `db:"-"` - Owners []PodOwner `db:"-"` - Labels []Label `db:"-"` - PodLabels []PodLabel `db:"-"` - Annotations []Annotation `db:"-"` - PodAnnotations []PodAnnotation `db:"-"` - Pvcs []PodPvc `db:"-"` - Volumes []PodVolume `db:"-"` + Conditions []PodCondition `db:"-"` + Containers []*Container `db:"-"` + InitContainers []*InitContainer `db:"-"` + SidecarContainers []*SidecarContainer `db:"-"` + Owners []PodOwner `db:"-"` + Labels []Label `db:"-"` + PodLabels []PodLabel `db:"-"` + Annotations []Annotation `db:"-"` + PodAnnotations []PodAnnotation `db:"-"` + Pvcs []PodPvc `db:"-"` + Volumes []PodVolume `db:"-"` factory *PodFactory } @@ -139,6 +140,7 @@ func (p *Pod) Obtain(k8s kmetav1.Object) { p.Containers = NewContainers[Container](p, pod.Spec.Containers, pod.Status.ContainerStatuses, NewContainer) p.InitContainers = NewContainers[InitContainer](p, pod.Spec.InitContainers, pod.Status.InitContainerStatuses, NewInitContainer) + p.SidecarContainers = NewContainers[SidecarContainer](p, pod.Spec.InitContainers, pod.Status.InitContainerStatuses, NewSidecarContainer) p.IcingaState, p.IcingaStateReason = p.getIcingaState(pod) @@ -389,7 +391,9 @@ func NewContainers[T any]( } for _, container := range containers { - obtained = append(obtained, factory(p.Uuid, container, statusesIdx[container.Name])) + if c := factory(p.Uuid, container, statusesIdx[container.Name]); c != nil { + obtained = append(obtained, c) + } } return obtained @@ -402,6 +406,7 @@ func (p *Pod) Relations() []database.Relation { database.HasMany(p.Conditions, fk), database.HasMany(p.Containers, database.WithoutCascadeDelete()), database.HasMany(p.InitContainers, database.WithoutCascadeDelete()), + database.HasMany(p.SidecarContainers, database.WithoutCascadeDelete()), database.HasMany(p.Owners, fk), database.HasMany(p.Labels, database.WithoutCascadeDelete()), database.HasMany(p.PodLabels, fk), diff --git a/schema/mysql/schema.sql b/schema/mysql/schema.sql index c2e5cbf..1a41b63 100644 --- a/schema/mysql/schema.sql +++ b/schema/mysql/schema.sql @@ -72,6 +72,26 @@ CREATE TABLE init_container ( PRIMARY KEY (uuid) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; +CREATE TABLE sidecar_container ( + uuid binary(16) NOT NULL, + pod_uuid binary(16) NOT NULL, + name varchar(63) COLLATE utf8mb4_unicode_ci NOT NULL, + image varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, + image_pull_policy enum('Always', 'Never', 'IfNotPresent') COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, + cpu_limits bigint unsigned NULL DEFAULT NULL, + cpu_requests bigint unsigned NULL DEFAULT NULL, + memory_limits bigint unsigned NULL DEFAULT NULL, + memory_requests bigint unsigned NULL DEFAULT NULL, + state enum('Waiting', 'Running', 'Terminated') COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, + state_details longtext NULL DEFAULT NULL, + ready enum('n', 'y') COLLATE utf8mb4_unicode_ci NOT NULL, + started enum('n', 'y') COLLATE utf8mb4_unicode_ci NOT NULL, + restart_count int unsigned NOT NULL, + icinga_state enum('unknown', 'pending', 'ok', 'warning', 'critical') COLLATE utf8mb4_unicode_ci NOT NULL, + icinga_state_reason text NULL DEFAULT NULL, + PRIMARY KEY (uuid) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; + CREATE TABLE container_device ( container_uuid binary(16) NOT NULL, pod_uuid binary(16) NOT NULL,