Skip to content
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

Sync init containers #131

Merged
merged 2 commits into from
Sep 26, 2024
Merged
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
10 changes: 10 additions & 0 deletions pkg/schema/v1/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
28 changes: 18 additions & 10 deletions pkg/schema/v1/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand All @@ -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),
Expand Down
37 changes: 37 additions & 0 deletions schema/mysql/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading