Skip to content

Commit 3443593

Browse files
committed
Add more annotations and set the actual version in the yaml
Signed-off-by: David Gageot <david.gageot@docker.com>
1 parent 3002610 commit 3443593

1 file changed

Lines changed: 32 additions & 6 deletions

File tree

pkg/oci/package.go

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
11
package oci
22

33
import (
4+
"context"
45
"fmt"
56
"os"
67
"path/filepath"
78
"strings"
89
"time"
910

11+
latest "github.com/docker/cagent/pkg/config/v2"
12+
"github.com/goccy/go-yaml"
1013
v1 "github.com/google/go-containerregistry/pkg/v1"
1114
"github.com/google/go-containerregistry/pkg/v1/empty"
1215
"github.com/google/go-containerregistry/pkg/v1/mutate"
1316
"github.com/google/go-containerregistry/pkg/v1/static"
1417
"github.com/google/go-containerregistry/pkg/v1/types"
1518

19+
"github.com/docker/cagent/pkg/config"
1620
"github.com/docker/cagent/pkg/content"
1721
"github.com/docker/cagent/pkg/path"
22+
"github.com/docker/cagent/pkg/version"
1823
)
1924

2025
// PackageFileAsOCIToStore creates an OCI artifact from a file and stores it in the content store
21-
func PackageFileAsOCIToStore(filePath, artifactRef string, store *content.Store) (string, error) {
26+
func PackageFileAsOCIToStore(ctx context.Context, filePath, artifactRef string, store *content.Store) (string, error) {
2227
if !strings.Contains(artifactRef, ":") {
2328
artifactRef += ":latest"
2429
}
@@ -34,6 +39,31 @@ func PackageFileAsOCIToStore(filePath, artifactRef string, store *content.Store)
3439
return "", fmt.Errorf("reading file: %w", err)
3540
}
3641

42+
cfg, err := config.LoadConfigBytes(ctx, data)
43+
if err != nil {
44+
return "", fmt.Errorf("loading config: %w", err)
45+
}
46+
47+
// Make sure we push a yaml with the latest version
48+
cfg.Version = latest.Version
49+
data, err = yaml.MarshalWithOptions(cfg, yaml.Indent(2))
50+
if err != nil {
51+
return "", fmt.Errorf("marshaling config: %w", err)
52+
}
53+
54+
// Prepare OCI annotations
55+
annotations := map[string]string{
56+
"io.docker.cagent.version": version.Version,
57+
"org.opencontainers.image.created": time.Now().Format(time.RFC3339),
58+
"org.opencontainers.image.description": fmt.Sprintf("OCI artifact containing %s", filepath.Base(validatedPath)),
59+
}
60+
if author := cfg.Metadata.Author; author != "" {
61+
annotations["org.opencontainers.image.authors"] = author
62+
}
63+
if license := cfg.Metadata.License; license != "" {
64+
annotations["org.opencontainers.image.licenses"] = license
65+
}
66+
3767
layer := static.NewLayer(data, types.OCIUncompressedLayer)
3868
img, err := mutate.AppendLayers(empty.Image, layer)
3969
if err != nil {
@@ -42,11 +72,7 @@ func PackageFileAsOCIToStore(filePath, artifactRef string, store *content.Store)
4272

4373
// Convert to OCI manifest format to support annotations
4474
img = mutate.MediaType(img, types.OCIManifestSchema1)
45-
46-
img = mutate.Annotations(img, map[string]string{
47-
"org.opencontainers.image.created": time.Now().Format(time.RFC3339),
48-
"org.opencontainers.image.description": fmt.Sprintf("OCI artifact containing %s", filepath.Base(validatedPath)),
49-
}).(v1.Image)
75+
img = mutate.Annotations(img, annotations).(v1.Image)
5076

5177
digest, err := store.StoreArtifact(img, artifactRef)
5278
if err != nil {

0 commit comments

Comments
 (0)