-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
manifest.go
229 lines (201 loc) · 8.34 KB
/
manifest.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package tunnel
import (
"context"
"fmt"
"slices"
"strings"
"github.com/containers/common/libimage/define"
"github.com/containers/image/v5/types"
"github.com/containers/podman/v5/pkg/bindings/images"
"github.com/containers/podman/v5/pkg/bindings/manifests"
"github.com/containers/podman/v5/pkg/domain/entities"
envLib "github.com/containers/podman/v5/pkg/env"
)
// ManifestCreate implements manifest create via ImageEngine
func (ir *ImageEngine) ManifestCreate(ctx context.Context, name string, images []string, opts entities.ManifestCreateOptions) (string, error) {
options := new(manifests.CreateOptions).WithAll(opts.All).WithAmend(opts.Amend).WithAnnotation(opts.Annotations)
imageID, err := manifests.Create(ir.ClientCtx, name, images, options)
if err != nil {
return imageID, fmt.Errorf("creating manifest: %w", err)
}
return imageID, err
}
// ManifestExists checks if a manifest list with the given name exists
func (ir *ImageEngine) ManifestExists(ctx context.Context, name string) (*entities.BoolReport, error) {
exists, err := manifests.Exists(ir.ClientCtx, name, nil)
if err != nil {
return nil, err
}
return &entities.BoolReport{Value: exists}, nil
}
// ManifestInspect returns contents of manifest list with given name
func (ir *ImageEngine) ManifestInspect(ctx context.Context, name string, opts entities.ManifestInspectOptions) (*define.ManifestListData, error) {
options := new(manifests.InspectOptions).WithAuthfile(opts.Authfile)
if s := opts.SkipTLSVerify; s != types.OptionalBoolUndefined {
if s == types.OptionalBoolTrue {
options.WithSkipTLSVerify(true)
} else {
options.WithSkipTLSVerify(false)
}
}
list, err := manifests.InspectListData(ir.ClientCtx, name, options)
if err != nil {
return nil, fmt.Errorf("getting content of manifest list or image %s: %w", name, err)
}
return list, err
}
// ManifestAdd adds images to the manifest list
func (ir *ImageEngine) ManifestAdd(_ context.Context, name string, imageNames []string, opts entities.ManifestAddOptions) (string, error) {
imageNames = slices.Clone(imageNames)
for _, image := range opts.Images {
if !slices.Contains(imageNames, image) {
imageNames = append(imageNames, image)
}
}
options := new(manifests.AddOptions).WithAll(opts.All).WithArch(opts.Arch).WithVariant(opts.Variant)
options.WithFeatures(opts.Features).WithImages(imageNames).WithOS(opts.OS).WithOSVersion(opts.OSVersion)
options.WithUsername(opts.Username).WithPassword(opts.Password).WithAuthfile(opts.Authfile)
if len(opts.Annotation) != 0 {
annotations := make(map[string]string)
for _, annotationSpec := range opts.Annotation {
key, val, hasVal := strings.Cut(annotationSpec, "=")
if !hasVal {
return "", fmt.Errorf("no value given for annotation %q", key)
}
annotations[key] = val
}
opts.Annotations = envLib.Join(opts.Annotations, annotations)
}
options.WithAnnotation(opts.Annotations)
if s := opts.SkipTLSVerify; s != types.OptionalBoolUndefined {
if s == types.OptionalBoolTrue {
options.WithSkipTLSVerify(true)
} else {
options.WithSkipTLSVerify(false)
}
}
id, err := manifests.Add(ir.ClientCtx, name, options)
if err != nil {
return id, fmt.Errorf("adding to manifest list %s: %w", name, err)
}
return id, nil
}
// ManifestAddArtifact creates artifact manifests and adds them to the manifest list
func (ir *ImageEngine) ManifestAddArtifact(_ context.Context, name string, files []string, opts entities.ManifestAddArtifactOptions) (string, error) {
files = slices.Clone(files)
for _, file := range opts.Files {
if !slices.Contains(files, file) {
files = append(files, file)
}
}
options := new(manifests.AddArtifactOptions).WithArch(opts.Arch).WithVariant(opts.Variant)
options.WithFeatures(opts.Features).WithOS(opts.OS).WithOSVersion(opts.OSVersion).WithOSFeatures(opts.OSFeatures)
if len(opts.Annotation) != 0 {
annotations := make(map[string]string)
for _, annotationSpec := range opts.Annotation {
key, val, hasVal := strings.Cut(annotationSpec, "=")
if !hasVal {
return "", fmt.Errorf("no value given for annotation %q", key)
}
annotations[key] = val
}
options.WithAnnotation(annotations)
}
options.WithType(opts.Type).WithConfigType(opts.ConfigType).WithLayerType(opts.LayerType)
options.WithConfig(opts.Config)
options.WithExcludeTitles(opts.ExcludeTitles).WithSubject(opts.Subject)
options.WithAnnotations(opts.Annotations)
options.WithFiles(files)
id, err := manifests.AddArtifact(ir.ClientCtx, name, options)
if err != nil {
return id, fmt.Errorf("adding to manifest list %s: %w", name, err)
}
return id, nil
}
// ManifestAnnotate updates an entry of the manifest list
func (ir *ImageEngine) ManifestAnnotate(ctx context.Context, name, images string, opts entities.ManifestAnnotateOptions) (string, error) {
options := new(manifests.ModifyOptions).WithArch(opts.Arch).WithVariant(opts.Variant)
options.WithFeatures(opts.Features).WithOS(opts.OS).WithOSVersion(opts.OSVersion)
annotations, err := mergeAnnotations(opts.Annotations, opts.Annotation)
if err != nil {
return "", err
}
options.WithAnnotations(annotations)
indexAnnotations, err := mergeAnnotations(opts.IndexAnnotations, opts.IndexAnnotation)
if err != nil {
return "", err
}
options.WithIndexAnnotations(indexAnnotations)
id, err := manifests.Annotate(ir.ClientCtx, name, []string{images}, options)
if err != nil {
return id, fmt.Errorf("annotating to manifest list %s: %w", name, err)
}
return id, nil
}
func mergeAnnotations(preferred map[string]string, aux []string) (map[string]string, error) {
if len(aux) != 0 {
auxAnnotations := make(map[string]string)
for _, annotationSpec := range aux {
key, val, hasVal := strings.Cut(annotationSpec, "=")
if !hasVal {
return nil, fmt.Errorf("no value given for annotation %q", key)
}
auxAnnotations[key] = val
}
if preferred == nil {
preferred = make(map[string]string)
}
preferred = envLib.Join(auxAnnotations, preferred)
}
return preferred, nil
}
// ManifestRemoveDigest removes the digest from manifest list
func (ir *ImageEngine) ManifestRemoveDigest(ctx context.Context, name string, image string) (string, error) {
updatedListID, err := manifests.Remove(ir.ClientCtx, name, image, nil)
if err != nil {
return updatedListID, fmt.Errorf("removing from manifest %s: %w", name, err)
}
return fmt.Sprintf("%s :%s\n", updatedListID, image), nil
}
// ManifestRm removes the specified manifest list from storage
func (ir *ImageEngine) ManifestRm(ctx context.Context, names []string) (*entities.ImageRemoveReport, []error) {
return ir.Remove(ctx, names, entities.ImageRemoveOptions{LookupManifest: true})
}
// ManifestPush pushes a manifest list or image index to the destination
func (ir *ImageEngine) ManifestPush(ctx context.Context, name, destination string, opts entities.ImagePushOptions) (string, error) {
if opts.Signers != nil {
return "", fmt.Errorf("forwarding Signers is not supported for remote clients")
}
options := new(images.PushOptions)
options.WithUsername(opts.Username).WithPassword(opts.Password).WithAuthfile(opts.Authfile).WithRemoveSignatures(opts.RemoveSignatures).WithAll(opts.All).WithFormat(opts.Format).WithCompressionFormat(opts.CompressionFormat).WithQuiet(opts.Quiet).WithProgressWriter(opts.Writer).WithAddCompression(opts.AddCompression).WithForceCompressionFormat(opts.ForceCompressionFormat)
if s := opts.SkipTLSVerify; s != types.OptionalBoolUndefined {
if s == types.OptionalBoolTrue {
options.WithSkipTLSVerify(true)
} else {
options.WithSkipTLSVerify(false)
}
}
digest, err := manifests.Push(ir.ClientCtx, name, destination, options)
if err != nil {
return "", fmt.Errorf("pushing manifest list %s: %w", name, err)
}
if opts.Rm {
if _, rmErrors := ir.Remove(ctx, []string{name}, entities.ImageRemoveOptions{LookupManifest: true}); len(rmErrors) > 0 {
return "", fmt.Errorf("removing manifest after push: %w", rmErrors[0])
}
}
return digest, err
}
// ManifestListClear clears out all instances from a manifest list
func (ir *ImageEngine) ManifestListClear(ctx context.Context, name string) (string, error) {
listContents, err := manifests.InspectListData(ir.ClientCtx, name, &manifests.InspectOptions{})
if err != nil {
return "", err
}
for _, instance := range listContents.Manifests {
if _, err := manifests.Remove(ir.ClientCtx, name, instance.Digest.String(), &manifests.RemoveOptions{}); err != nil {
return "", err
}
}
return name, nil
}