|
1 | 1 | package discovery |
2 | 2 |
|
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "slices" |
| 6 | +) |
| 7 | + |
3 | 8 | // Manifest represents the discovered components for a given application. |
4 | 9 | type Manifest struct { |
5 | 10 | DiscoveredImages []DiscoveredImage |
6 | 11 | } |
7 | 12 |
|
8 | | -// DiscoveredImage is a container image that' been discovered based on the |
9 | | -// workload watchers. |
| 13 | +func (m *Manifest) Equal(m1 Manifest) bool { |
| 14 | + if len(m.DiscoveredImages) != len(m1.DiscoveredImages) { |
| 15 | + return false |
| 16 | + } |
| 17 | + for idx := range m.DiscoveredImages { |
| 18 | + if !m.DiscoveredImages[idx].Equal(m1.DiscoveredImages[idx]) { |
| 19 | + return false |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + return true |
| 24 | +} |
| 25 | + |
| 26 | +// Insert inserts images into the manifest. If an image is already present in |
| 27 | +// the manifest, the containers from the images to the inserted images are |
| 28 | +// inserted into the existing image in the manifest instead, avoiding the |
| 29 | +// creation of duplicate images in the manifest. |
| 30 | +func (m *Manifest) Insert(images ...DiscoveredImage) { |
| 31 | + for _, image := range images { |
| 32 | + idx := slices.IndexFunc(m.DiscoveredImages, func(i DiscoveredImage) bool { |
| 33 | + return i.Image == image.Image |
| 34 | + }) |
| 35 | + |
| 36 | + if idx == -1 { |
| 37 | + m.DiscoveredImages = append(m.DiscoveredImages, image) |
| 38 | + } else { |
| 39 | + for _, container := range image.Containers { |
| 40 | + containerExists := slices.ContainsFunc(m.DiscoveredImages[idx].Containers, func(c DiscoveredContainer) bool { |
| 41 | + return c.Equal(container) |
| 42 | + }) |
| 43 | + |
| 44 | + if !containerExists { |
| 45 | + m.DiscoveredImages[idx].Containers = append(m.DiscoveredImages[idx].Containers, container) |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +// DiscoveredImage is a container image which was discovered in one or more workloads. |
10 | 53 | type DiscoveredImage struct { |
11 | | - // PodName is the pod.metadata.name value where the image was discovered. |
12 | | - PodName string |
13 | | - // ContainerName represents the container in the pod that had the image. |
14 | | - ContainerName string |
15 | 54 | // Image is a fully qualified container image name and tag or digest. |
16 | 55 | Image string |
| 56 | + |
| 57 | + // Containers is a list of DiscoveredContainer objects which are using |
| 58 | + // the discovered image. |
| 59 | + Containers []DiscoveredContainer |
| 60 | +} |
| 61 | + |
| 62 | +func (i *DiscoveredImage) Equal(i1 DiscoveredImage) bool { |
| 63 | + return i.Image == i1.Image && slices.Equal(i.Containers, i1.Containers) |
| 64 | +} |
| 65 | + |
| 66 | +// DiscoveredContainer is a container which was observed during the discovery process. |
| 67 | +type DiscoveredContainer struct { |
| 68 | + // Name is the name of a container in a pod. |
| 69 | + Name string |
| 70 | + |
| 71 | + // Type is the ContainerType of the container in its pod. |
| 72 | + Type ContainerType |
| 73 | + |
| 74 | + // Pod is the DiscoveredPod which this container is a part of. |
| 75 | + Pod DiscoveredPod |
| 76 | +} |
| 77 | + |
| 78 | +func (c *DiscoveredContainer) Equal(c1 DiscoveredContainer) bool { |
| 79 | + return c.Name == c1.Name && c.Type == c1.Type && c.Pod.Name == c1.Pod.Name && c.Pod.Namespace == c1.Pod.Namespace |
| 80 | +} |
| 81 | + |
| 82 | +// ContainerType is the type of a container in a pod. |
| 83 | +type ContainerType int |
| 84 | + |
| 85 | +const ( |
| 86 | + Container ContainerType = iota |
| 87 | + InitContainer |
| 88 | + EphemeralContainer |
| 89 | +) |
| 90 | + |
| 91 | +func (c ContainerType) String() string { |
| 92 | + switch c { |
| 93 | + case Container: |
| 94 | + return "Container" |
| 95 | + case InitContainer: |
| 96 | + return "InitContainer" |
| 97 | + case EphemeralContainer: |
| 98 | + return "EphemeralContainer" |
| 99 | + } |
| 100 | + |
| 101 | + return "" |
| 102 | +} |
| 103 | + |
| 104 | +func (c ContainerType) MarshalJSON() ([]byte, error) { |
| 105 | + return json.Marshal(c.String()) |
| 106 | +} |
| 107 | + |
| 108 | +// DiscoveredPod is a pod that contains a discovered image. |
| 109 | +type DiscoveredPod struct { |
| 110 | + // Name is the pod.metadata.name value where the image was discovered. |
| 111 | + Name string |
| 112 | + |
| 113 | + // Namespace is the pod.metadata.namespace value of the pod. |
| 114 | + Namespace string |
17 | 115 | } |
0 commit comments