annotations.go RawHistoryFolder
1package manifest
2
3import (
4 "encoding/json"
5 "fmt"
6)
7
8// ParseManifestAnnotations extracts annotations from manifest data
9func ParseManifestAnnotations(manifestData []byte, contentType string) (map[string]string, error) {
10 switch contentType {
11 case "application/vnd.oci.image.manifest.v1+json":
12 return parseOCIManifestAnnotations(manifestData)
13 case "application/vnd.docker.distribution.manifest.v2+json":
14 // Docker v2.2 manifests don't support annotations at manifest level
15 return map[string]string{}, nil
16 case "application/vnd.oci.image.index.v1+json",
17 "application/vnd.docker.distribution.manifest.list.v2+json":
18 return parseManifestListAnnotations(manifestData)
19 default:
20 return map[string]string{}, fmt.Errorf("unsupported manifest media type: %s", contentType)
21 }
22}
23
24// parseOCIManifestAnnotations parses annotations from an OCI image manifest
25func parseOCIManifestAnnotations(manifestData []byte) (map[string]string, error) {
26 var manifest OCIManifest
27 if err := json.Unmarshal(manifestData, &manifest); err != nil {
28 return nil, fmt.Errorf("failed to parse OCI manifest: %w", err)
29 }
30
31 if manifest.Annotations == nil {
32 return map[string]string{}, nil
33 }
34
35 return manifest.Annotations, nil
36}
37
38// parseManifestListAnnotations parses annotations from a manifest list
39func parseManifestListAnnotations(manifestData []byte) (map[string]string, error) {
40 var manifestList ManifestList
41 if err := json.Unmarshal(manifestData, &manifestList); err != nil {
42 return nil, fmt.Errorf("failed to parse manifest list: %w", err)
43 }
44
45 if manifestList.Annotations == nil {
46 return map[string]string{}, nil
47 }
48
49 return manifestList.Annotations, nil
50}
51
52// IsVersionedDeployment checks if manifest contains version annotations for deployment
53func IsVersionedDeployment(annotations map[string]string) bool {
54 // Just check for the "version" annotation - keep it simple
55 _, exists := annotations["version"]
56 return exists
57}
58
59// GetDeploymentVersion extracts the deployment version from annotations
60func GetDeploymentVersion(annotations map[string]string) string {
61 // Just use the "version" annotation - keep it simple
62 if version, exists := annotations["version"]; exists && version != "" {
63 return version
64 }
65
66 return ""
67}
68