|
| 1 | +package osimagestream |
| 2 | + |
| 3 | +import ( |
| 4 | + "maps" |
| 5 | + "slices" |
| 6 | + |
| 7 | + "github.com/openshift/api/machineconfiguration/v1alpha1" |
| 8 | + "k8s.io/klog/v2" |
| 9 | +) |
| 10 | + |
| 11 | +const ( |
| 12 | + // coreOSLabelStreamClass is the container image label that identifies the OS stream name. |
| 13 | + coreOSLabelStreamClass = "io.openshift.os.streamclass" |
| 14 | + // coreOSLabelDiscriminator is the container image label that distinguishes OS images from Extensions images. |
| 15 | + coreOSLabelDiscriminator = "ostree.linux" |
| 16 | +) |
| 17 | + |
| 18 | +// ImageType indicates whether a container image is an OS image or an extensions image. |
| 19 | +type ImageType int |
| 20 | + |
| 21 | +const ( |
| 22 | + // ImageTypeOS represents a base operating system image. |
| 23 | + ImageTypeOS = iota |
| 24 | + // ImageTypeExtensions represents an OS extensions image. |
| 25 | + ImageTypeExtensions |
| 26 | +) |
| 27 | + |
| 28 | +// ImageDataExtractor extracts metadata from container image labels to determine |
| 29 | +// the image type and associated OS stream. |
| 30 | +type ImageDataExtractor interface { |
| 31 | + // GetImageData analyzes container image labels and returns structured metadata, |
| 32 | + // or nil if the image is not an OS or extensions image. |
| 33 | + GetImageData(image string, labels map[string]string) *ImageData |
| 34 | +} |
| 35 | + |
| 36 | +// ImageData contains metadata extracted from a container image. |
| 37 | +type ImageData struct { |
| 38 | + Image string // Container image reference |
| 39 | + Type ImageType // Image type (OS or extensions) |
| 40 | + Stream string // OS stream name |
| 41 | +} |
| 42 | + |
| 43 | +// ImageStreamExtractorImpl implements ImageDataExtractor using container image labels |
| 44 | +// to identify and classify OS and extensions images. |
| 45 | +type ImageStreamExtractorImpl struct { |
| 46 | + imagesInspector ImagesInspector |
| 47 | + streamLabels []string |
| 48 | + osImageDiscriminator string |
| 49 | +} |
| 50 | + |
| 51 | +// NewImageStreamExtractor creates a new ImageDataExtractor configured to recognize |
| 52 | +// CoreOS images using standard image labels. |
| 53 | +func NewImageStreamExtractor() ImageDataExtractor { |
| 54 | + // The type is thought to allow future extra label addition for |
| 55 | + // i.e. Allow a customer to bring their own images with their own labels (defining a selector) |
| 56 | + return &ImageStreamExtractorImpl{ |
| 57 | + streamLabels: []string{coreOSLabelStreamClass}, |
| 58 | + osImageDiscriminator: coreOSLabelDiscriminator, |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +// GetImageData analyzes container image labels to extract OS stream metadata. |
| 63 | +// Returns nil if the image does not have the required stream label. |
| 64 | +// Distinguishes between OS and extensions images based on the presence of the ostree discriminator label. |
| 65 | +func (e *ImageStreamExtractorImpl) GetImageData(image string, labels map[string]string) *ImageData { |
| 66 | + imageData := &ImageData{ |
| 67 | + Image: image, |
| 68 | + Stream: findLabelValue(labels, e.streamLabels...), |
| 69 | + } |
| 70 | + if imageData.Stream == "" { |
| 71 | + // Not an OS/extensions image |
| 72 | + return nil |
| 73 | + } |
| 74 | + |
| 75 | + if findLabelValue(labels, e.osImageDiscriminator) != "" { |
| 76 | + imageData.Type = ImageTypeOS |
| 77 | + } else { |
| 78 | + imageData.Type = ImageTypeExtensions |
| 79 | + } |
| 80 | + |
| 81 | + return imageData |
| 82 | +} |
| 83 | + |
| 84 | +// findLabelValue searches for the first matching key in the map and returns its value. |
| 85 | +// Returns an empty string if none of the keys are found. |
| 86 | +func findLabelValue(m map[string]string, keys ...string) string { |
| 87 | + for _, k := range keys { |
| 88 | + if v, ok := m[k]; ok { |
| 89 | + return v |
| 90 | + } |
| 91 | + } |
| 92 | + return "" |
| 93 | +} |
| 94 | + |
| 95 | +// GroupOSContainerImageMetadataToStream groups OS and extensions images by stream name. |
| 96 | +// Combines pairs of OS and extensions images that share the same stream name into OSImageStreamSet objects. |
| 97 | +// If multiple images are found for the same stream and type, the last one wins. |
| 98 | +func GroupOSContainerImageMetadataToStream(imagesMetadata []*ImageData) []*v1alpha1.OSImageStreamSet { |
| 99 | + streamMaps := make(map[string]*v1alpha1.OSImageStreamSet) |
| 100 | + for _, imageMetadata := range imagesMetadata { |
| 101 | + streamURLSet, exists := streamMaps[imageMetadata.Stream] |
| 102 | + if !exists { |
| 103 | + streamMaps[imageMetadata.Stream] = NewOSImageStreamURLSetFromImageMetadata(imageMetadata) |
| 104 | + continue |
| 105 | + } |
| 106 | + |
| 107 | + // The stream already exists. Maybe it has not both urls yet |
| 108 | + imageName := v1alpha1.ImageDigestFormat(imageMetadata.Image) |
| 109 | + if imageMetadata.Type == ImageTypeOS { |
| 110 | + if streamURLSet.OSImage != "" && streamURLSet.OSImage != imageName { |
| 111 | + // Looks like we have a conflict. Log it and override |
| 112 | + klog.V(4).Infof("multiple OS images for the same %s stream. Previous one was %s. Overriding with %s", streamURLSet.Name, streamURLSet.OSImage, imageName) |
| 113 | + } |
| 114 | + streamURLSet.OSImage = imageName |
| 115 | + } else { |
| 116 | + if streamURLSet.OSExtensionsImage != "" && streamURLSet.OSExtensionsImage != imageName { |
| 117 | + // Looks like we have a conflict. Log it and override |
| 118 | + klog.V(4).Infof("multiple OS Extensions images for the same %s stream. Previous one was %s. Overriding with %s", streamURLSet.Name, streamURLSet.OSImage, imageName) |
| 119 | + } |
| 120 | + streamURLSet.OSExtensionsImage = imageName |
| 121 | + } |
| 122 | + } |
| 123 | + return slices.Collect(maps.Values(streamMaps)) |
| 124 | +} |
| 125 | + |
| 126 | +// NewOSImageStreamURLSetFromImageMetadata creates an OSImageStreamSet from image metadata. |
| 127 | +// Populates either the OSImage or OSExtensionsImage field based on the image type. |
| 128 | +func NewOSImageStreamURLSetFromImageMetadata(imageMetadata *ImageData) *v1alpha1.OSImageStreamSet { |
| 129 | + urlSet := &v1alpha1.OSImageStreamSet{ |
| 130 | + Name: imageMetadata.Stream, |
| 131 | + } |
| 132 | + imageName := v1alpha1.ImageDigestFormat(imageMetadata.Image) |
| 133 | + if imageMetadata.Type == ImageTypeOS { |
| 134 | + urlSet.OSImage = imageName |
| 135 | + } else { |
| 136 | + urlSet.OSExtensionsImage = imageName |
| 137 | + } |
| 138 | + return urlSet |
| 139 | +} |
0 commit comments