Skip to content

Commit 48effe4

Browse files
GabriFedi97NiccoloFei
authored andcommitted
feat: rely on image annotations instead of labels in the dagger module
Signed-off-by: Gabriele Fedi <[email protected]>
1 parent 117f4c9 commit 48effe4

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

dagger/maintenance/image.go

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,61 @@
11
package main
22

33
import (
4+
"bytes"
45
"fmt"
56
"regexp"
67
"strconv"
78

89
"github.com/google/go-containerregistry/pkg/name"
10+
containerregistryv1 "github.com/google/go-containerregistry/pkg/v1"
911
"github.com/google/go-containerregistry/pkg/v1/remote"
12+
ocispecv1 "github.com/opencontainers/image-spec/specs-go/v1"
1013
)
1114

1215
const (
1316
DefaultPgMajor = 18
1417
DefaultDistribution = "trixie"
1518
)
1619

17-
// getImageLabels returns the OCI labels given an image ref.
18-
func getImageLabels(imageRef string) (map[string]string, error) {
20+
// getImageAnnotations returns the OCI annotations given an image ref.
21+
func getImageAnnotations(imageRef string) (map[string]string, error) {
1922
ref, err := name.ParseReference(imageRef)
2023
if err != nil {
2124
return nil, err
2225
}
2326

24-
desc, err := remote.Get(ref)
27+
head, err := remote.Get(ref)
2528
if err != nil {
2629
return nil, err
2730
}
2831

29-
img, err := desc.Image()
30-
if err != nil {
31-
return nil, err
32-
}
33-
34-
cfg, err := img.ConfigFile()
35-
if err != nil {
36-
return nil, err
32+
switch head.MediaType {
33+
case ocispecv1.MediaTypeImageIndex:
34+
indexManifest, err := containerregistryv1.ParseIndexManifest(bytes.NewReader(head.Manifest))
35+
if err != nil {
36+
return nil, err
37+
}
38+
return indexManifest.Annotations, nil
39+
case ocispecv1.MediaTypeImageManifest:
40+
manifest, err := containerregistryv1.ParseManifest(bytes.NewReader(head.Manifest))
41+
if err != nil {
42+
return nil, err
43+
}
44+
return manifest.Annotations, nil
3745
}
3846

39-
return cfg.Config.Labels, nil
47+
return nil, fmt.Errorf("unsupported media type: %s", head.MediaType)
4048
}
4149

4250
// getDefaultExtensionImage returns the default extension image for a given extension,
4351
// resolved from the metadata.
4452
func getDefaultExtensionImage(metadata *extensionMetadata) (string, error) {
4553
packageVersion := metadata.Versions[DefaultDistribution][strconv.Itoa(DefaultPgMajor)]
54+
if packageVersion == "" {
55+
return "", fmt.Errorf("no package version found for distribution %q and version %d",
56+
DefaultDistribution, DefaultPgMajor)
57+
}
58+
4659
re := regexp.MustCompile(`^(\d+(?:\.\d+)+)`)
4760
matches := re.FindStringSubmatch(packageVersion)
4861
if len(matches) < 2 {

dagger/maintenance/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func (m *Maintenance) GenerateTestingValues(
117117
}
118118
}
119119

120-
labels, err := getImageLabels(targetExtensionImage)
120+
annotations, err := getImageAnnotations(targetExtensionImage)
121121
if err != nil {
122122
return nil, err
123123
}
@@ -132,8 +132,8 @@ func (m *Maintenance) GenerateTestingValues(
132132
"dynamic_library_path": metadata.DynamicLibraryPath,
133133
"ld_library_path": metadata.LdLibraryPath,
134134
"extension_image": targetExtensionImage,
135-
"pg_image": labels["io.cloudnativepg.image.base.name"],
136-
"version": labels["org.opencontainers.image.version"],
135+
"pg_image": annotations["io.cloudnativepg.image.base.name"],
136+
"version": annotations["org.opencontainers.image.version"],
137137
}
138138
valuesYaml, err := yaml.Marshal(values)
139139
if err != nil {

0 commit comments

Comments
 (0)