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 2f4bef9..0eaf117 100644 --- a/pkg/schema/v1/pod.go +++ b/pkg/schema/v1/pod.go @@ -37,15 +37,17 @@ 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:"-"` + 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 } @@ -137,6 +139,8 @@ 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) @@ -387,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 @@ -399,6 +405,8 @@ 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.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 acaad28..1a41b63 100644 --- a/schema/mysql/schema.sql +++ b/schema/mysql/schema.sql @@ -55,6 +55,43 @@ 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 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,