Skip to content
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
41 changes: 35 additions & 6 deletions discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,42 @@ type Manifest struct {
DiscoveredImages []DiscoveredImage
}

// DiscoveredImage is a container image that' been discovered based on the
// workload watchers.
// DiscoveredImage is a container image which was discovered in one or more workloads.
type DiscoveredImage struct {
// PodName is the pod.metadata.name value where the image was discovered.
PodName string
// ContainerName represents the container in the pod that had the image.
ContainerName string
// Image is a fully qualified container image name and tag or digest.
Image string

// Containers is a list of DiscoveredContainer objects which are using
// the discovered image.
Containers []DiscoveredContainer
}

// DiscoveredContainer is a container which was observed during the discovery process.
type DiscoveredContainer struct {
// Name is the name of a container in a pod.
Name string

// Type is the ContainerType of the container in its pod.
Type ContainerType

// Pod is the DiscoveredPod which this container is a part of.
Pod DiscoveredPod
}

// ContainerType is the type of a container in a pod.
type ContainerType = string

const (
ContainerTypeStandard ContainerType = "Container"
ContainerTypeInit ContainerType = "InitContainer"
ContainerTypeEphemeral ContainerType = "EphemeralContainer"
)

// DiscoveredPod is a pod that contains a discovered image.
type DiscoveredPod struct {
// Name is the pod.metadata.name value where the image was discovered.
Name string

// Namespace is the pod.metadata.namespace value of the pod.
Namespace string
}
70 changes: 60 additions & 10 deletions internal/discover/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"log/slog"
"slices"

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

Expand All @@ -28,7 +29,7 @@ func NewManifestJSONProcessorFn(out io.Writer) ProcessingFunction {
continueRunning = false
break
}
m.DiscoveredImages = append(m.DiscoveredImages, processContainers(p, logger)...)
m = appendToManifest(m, processContainers(p, logger)...)
case <-ctx.Done():
logger.Debug("processorFn completing because the context completed")
continueRunning = false
Expand Down Expand Up @@ -64,9 +65,17 @@ func processContainers(
found = append(
found,
discovery.DiscoveredImage{
PodName: p.Name,
ContainerName: c.Name,
Image: c.Image,
Image: c.Image,
Containers: []discovery.DiscoveredContainer{
{
Name: c.Name,
Type: discovery.ContainerTypeStandard,
Pod: discovery.DiscoveredPod{
Name: p.Name,
Namespace: p.Namespace,
},
},
},
},
)
}
Expand All @@ -76,9 +85,17 @@ func processContainers(
found = append(
found,
discovery.DiscoveredImage{
PodName: p.Name,
ContainerName: c.Name,
Image: c.Image,
Image: c.Image,
Containers: []discovery.DiscoveredContainer{
{
Name: c.Name,
Type: discovery.ContainerTypeInit,
Pod: discovery.DiscoveredPod{
Name: p.Name,
Namespace: p.Namespace,
},
},
},
},
)
}
Expand All @@ -88,12 +105,45 @@ func processContainers(
found = append(
found,
discovery.DiscoveredImage{
PodName: p.Name,
ContainerName: c.Name,
Image: c.Image,
Image: c.Image,
Containers: []discovery.DiscoveredContainer{
{
Name: c.Name,
Type: discovery.ContainerTypeEphemeral,
Pod: discovery.DiscoveredPod{
Name: p.Name,
Namespace: p.Namespace,
},
},
},
},
)
}

return found
}

func appendToManifest(m discovery.Manifest, images ...discovery.DiscoveredImage) discovery.Manifest {
for _, image := range images {
idx := slices.IndexFunc(m.DiscoveredImages, func(i discovery.DiscoveredImage) bool {
return i.Image == image.Image
})

if idx == -1 {
m.DiscoveredImages = append(m.DiscoveredImages, image)
continue
}

for _, container := range image.Containers {
if !slices.Contains(m.DiscoveredImages[idx].Containers, container) {
m.DiscoveredImages[idx].Containers = append(m.DiscoveredImages[idx].Containers, container)
}
}
}

return m
}

func imagesEqual(i1, i2 discovery.DiscoveredImage) bool {
return i1.Image == i2.Image && slices.Equal(i1.Containers, i2.Containers)
}
Loading