/
common.go
85 lines (74 loc) · 2.71 KB
/
common.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023-Present The UDS Authors
// Package bundler defines behavior for bundling packages
package bundler
import (
"errors"
"fmt"
"strings"
"github.com/defenseunicorns/pkg/helpers"
"github.com/defenseunicorns/pkg/oci"
"github.com/defenseunicorns/uds-cli/src/pkg/utils"
"github.com/defenseunicorns/uds-cli/src/types"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/zarf/src/pkg/zoci"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras-go/v2/registry"
)
// copied from: https://github.com/defenseunicorns/zarf/blob/main/src/pkg/oci/push.go
func manifestAnnotationsFromMetadata(metadata *types.UDSMetadata) map[string]string {
annotations := map[string]string{
ocispec.AnnotationDescription: metadata.Description,
}
if url := metadata.URL; url != "" {
annotations[ocispec.AnnotationURL] = url
}
if authors := metadata.Authors; authors != "" {
annotations[ocispec.AnnotationAuthors] = authors
}
if documentation := metadata.Documentation; documentation != "" {
annotations[ocispec.AnnotationDocumentation] = documentation
}
if source := metadata.Source; source != "" {
annotations[ocispec.AnnotationSource] = source
}
if vendor := metadata.Vendor; vendor != "" {
annotations[ocispec.AnnotationVendor] = vendor
}
return annotations
}
// copied from: https://github.com/defenseunicorns/zarf/blob/main/src/pkg/oci/push.go
func pushManifestConfigFromMetadata(r *oci.OrasRemote, metadata *types.UDSMetadata, build *types.UDSBuildData) (ocispec.Descriptor, error) {
annotations := map[string]string{
ocispec.AnnotationTitle: metadata.Name,
ocispec.AnnotationDescription: metadata.Description,
}
manifestConfig := oci.ConfigPartial{
Architecture: build.Architecture,
OCIVersion: "1.0.1",
Annotations: annotations,
}
manifestConfigDesc, err := utils.ToOCIRemote(manifestConfig, zoci.ZarfLayerMediaTypeBlob, r)
if err != nil {
return ocispec.Descriptor{}, err
}
return *manifestConfigDesc, nil
}
// copied from: https://github.com/defenseunicorns/zarf/blob/main/src/pkg/oci/utils.go
func referenceFromMetadata(registryLocation string, metadata *types.UDSMetadata) (string, error) {
ver := metadata.Version
if len(ver) == 0 {
return "", errors.New("version is required for publishing")
}
if !strings.HasSuffix(registryLocation, "/") {
registryLocation = registryLocation + "/"
}
registryLocation = strings.TrimPrefix(registryLocation, helpers.OCIURLPrefix)
raw := fmt.Sprintf("%s%s:%s", registryLocation, metadata.Name, ver)
message.Debug("Raw OCI reference from metadata:", raw)
ref, err := registry.ParseReference(raw)
if err != nil {
return "", err
}
return ref.String(), nil
}