|
| 1 | +package container |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "os/exec" |
| 7 | + "strings" |
| 8 | +) |
| 9 | + |
| 10 | +// ImageLabels represents the labels from a container image |
| 11 | +type ImageLabels struct { |
| 12 | + Version string |
| 13 | + Revision string |
| 14 | +} |
| 15 | + |
| 16 | +// GetImageLabelsFromRegistry retrieves image labels from a registry using skopeo |
| 17 | +func GetImageLabelsFromRegistry(imageURL string) (*ImageLabels, error) { |
| 18 | + // Always add the docker:// prefix for skopeo |
| 19 | + imageURL = "docker://" + imageURL |
| 20 | + |
| 21 | + cmd := exec.Command("skopeo", "inspect", "--override-os", "linux", "--override-arch", "amd64", imageURL) |
| 22 | + output, err := cmd.Output() |
| 23 | + if err != nil { |
| 24 | + return nil, fmt.Errorf("failed to inspect image %s with skopeo: %w", imageURL, err) |
| 25 | + } |
| 26 | + |
| 27 | + var imageInfo struct { |
| 28 | + Labels map[string]string `json:"Labels"` |
| 29 | + } |
| 30 | + |
| 31 | + if err := json.Unmarshal(output, &imageInfo); err != nil { |
| 32 | + return nil, fmt.Errorf("failed to parse skopeo output: %w", err) |
| 33 | + } |
| 34 | + |
| 35 | + // Get both label sets |
| 36 | + jumpstarterVersion := imageInfo.Labels["jumpstarter.version"] |
| 37 | + jumpstarterRevision := imageInfo.Labels["jumpstarter.revision"] |
| 38 | + ociVersion := imageInfo.Labels["org.opencontainers.image.version"] |
| 39 | + ociRevision := imageInfo.Labels["org.opencontainers.image.revision"] |
| 40 | + |
| 41 | + // Use jumpstarter labels if both exist, otherwise fall back to OCI labels |
| 42 | + var version, revision string |
| 43 | + if jumpstarterVersion != "" && jumpstarterRevision != "" { |
| 44 | + version = jumpstarterVersion |
| 45 | + revision = jumpstarterRevision |
| 46 | + } else { |
| 47 | + version = ociVersion |
| 48 | + revision = ociRevision |
| 49 | + } |
| 50 | + |
| 51 | + // Only return empty labels if BOTH version and revision are completely missing |
| 52 | + // (neither jumpstarter nor OCI labels exist) |
| 53 | + return &ImageLabels{ |
| 54 | + Version: version, |
| 55 | + Revision: revision, |
| 56 | + }, nil |
| 57 | +} |
| 58 | + |
| 59 | +// GetRunningContainerLabels retrieves labels from a running container using podman inspect |
| 60 | +func GetRunningContainerLabels(serviceName string) (*ImageLabels, error) { |
| 61 | + cmd := exec.Command("podman", "inspect", "--format", |
| 62 | + "{{index .Config.Labels \"jumpstarter.version\"}} {{index .Config.Labels \"jumpstarter.revision\"}}", |
| 63 | + serviceName) |
| 64 | + output, err := cmd.Output() |
| 65 | + if err != nil { |
| 66 | + return nil, fmt.Errorf("failed to inspect container %s: %w", serviceName, err) |
| 67 | + } |
| 68 | + |
| 69 | + parts := strings.Fields(strings.TrimSpace(string(output))) |
| 70 | + if len(parts) < 2 { |
| 71 | + return &ImageLabels{}, nil // Return empty labels if not found |
| 72 | + } |
| 73 | + |
| 74 | + return &ImageLabels{ |
| 75 | + Version: parts[0], |
| 76 | + Revision: parts[1], |
| 77 | + }, nil |
| 78 | +} |
| 79 | + |
| 80 | +// CompareVersions compares two ImageLabels and returns true if they match |
| 81 | +func (il *ImageLabels) Matches(other *ImageLabels) bool { |
| 82 | + return il.Version == other.Version && il.Revision == other.Revision |
| 83 | +} |
| 84 | + |
| 85 | +// IsEmpty returns true if both version and revision are empty |
| 86 | +func (il *ImageLabels) IsEmpty() bool { |
| 87 | + return il.Version == "" && il.Revision == "" |
| 88 | +} |
| 89 | + |
| 90 | +// String returns a string representation of the image labels |
| 91 | +func (il *ImageLabels) String() string { |
| 92 | + if il.IsEmpty() { |
| 93 | + return "no version info" |
| 94 | + } |
| 95 | + return fmt.Sprintf("version=%s revision=%s", il.Version, il.Revision) |
| 96 | +} |
0 commit comments