-
Notifications
You must be signed in to change notification settings - Fork 109
/
image.go
200 lines (173 loc) · 5.84 KB
/
image.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
// Copyright 2022, 2023 Chainguard, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package oci
import (
"errors"
"fmt"
"sort"
"strings"
"time"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/empty"
"github.com/google/go-containerregistry/pkg/v1/mutate"
v1tar "github.com/google/go-containerregistry/pkg/v1/tarball"
ggcrtypes "github.com/google/go-containerregistry/pkg/v1/types"
"github.com/google/shlex"
"github.com/sigstore/cosign/v2/pkg/oci"
"github.com/sigstore/cosign/v2/pkg/oci/signed"
"golang.org/x/exp/maps"
"chainguard.dev/apko/pkg/build/types"
"chainguard.dev/apko/pkg/log"
"chainguard.dev/apko/pkg/options"
)
func BuildImageFromLayer(layer v1.Layer, ic types.ImageConfiguration, created time.Time, arch types.Architecture, logger log.Logger) (oci.SignedImage, error) {
mediaType, err := layer.MediaType()
if err != nil {
return nil, fmt.Errorf("accessing layer MediaType: %w", err)
}
imageType := humanReadableImageType(mediaType)
logger.Printf("building image from layer")
digest, err := layer.Digest()
if err != nil {
return nil, fmt.Errorf("could not calculate layer digest: %w", err)
}
diffid, err := layer.DiffID()
if err != nil {
return nil, fmt.Errorf("could not calculate layer diff id: %w", err)
}
logger.Printf("%s layer digest: %v", imageType, digest)
logger.Printf("%s layer diffID: %v", imageType, diffid)
adds := make([]mutate.Addendum, 0, 1)
adds = append(adds, mutate.Addendum{
Layer: layer,
History: v1.History{
Author: "apko",
Comment: "This is an apko single-layer image",
CreatedBy: "apko",
Created: v1.Time{Time: created},
},
})
emptyImage := empty.Image
if mediaType == ggcrtypes.OCILayer {
// If building an OCI layer, then we should assume OCI manifest and config too
emptyImage = mutate.MediaType(emptyImage, ggcrtypes.OCIManifestSchema1)
emptyImage = mutate.ConfigMediaType(emptyImage, ggcrtypes.OCIConfigJSON)
}
v1Image, err := mutate.Append(emptyImage, adds...)
if err != nil {
return nil, fmt.Errorf("unable to append %s layer to empty image: %w", imageType, err)
}
annotations := ic.Annotations
if annotations == nil {
annotations = map[string]string{}
}
if ic.VCSUrl != "" {
if url, hash, ok := strings.Cut(ic.VCSUrl, "@"); ok {
annotations["org.opencontainers.image.source"] = url
annotations["org.opencontainers.image.revision"] = hash
}
}
if mediaType != ggcrtypes.DockerLayer && len(annotations) > 0 {
v1Image = mutate.Annotations(v1Image, annotations).(v1.Image)
}
cfg, err := v1Image.ConfigFile()
if err != nil {
return nil, fmt.Errorf("unable to get %s config file: %w", imageType, err)
}
cfg = cfg.DeepCopy()
cfg.Author = "github.com/chainguard-dev/apko"
platform := arch.ToOCIPlatform()
cfg.Architecture = platform.Architecture
cfg.Variant = platform.Variant
cfg.Created = v1.Time{Time: created}
cfg.Config.Labels = make(map[string]string)
cfg.OS = "linux"
// NOTE: Need to allow empty Entrypoints. The runtime will override to `/bin/sh -c` and handle quoting
switch {
case ic.Entrypoint.ShellFragment != "":
cfg.Config.Entrypoint = []string{"/bin/sh", "-c", ic.Entrypoint.ShellFragment}
case ic.Entrypoint.Command != "":
splitcmd, err := shlex.Split(ic.Entrypoint.Command)
if err != nil {
return nil, fmt.Errorf("unable to parse entrypoint command: %w", err)
}
cfg.Config.Entrypoint = splitcmd
}
if ic.Cmd != "" {
splitcmd, err := shlex.Split(ic.Cmd)
if err != nil {
return nil, fmt.Errorf("unable to parse cmd: %w", err)
}
cfg.Config.Cmd = splitcmd
}
if ic.WorkDir != "" {
cfg.Config.WorkingDir = ic.WorkDir
}
if ic.Volumes != nil {
cfg.Config.Volumes = make(map[string]struct{})
for _, v := range ic.Volumes {
cfg.Config.Volumes[v] = struct{}{}
}
}
env := maps.Clone(ic.Environment)
// Set these environment variables if they are not already set.
if env == nil {
env = map[string]string{}
}
for k, v := range map[string]string{
"PATH": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"SSL_CERT_FILE": "/etc/ssl/certs/ca-certificates.crt",
} {
if _, found := env[k]; !found {
env[k] = v
}
}
envs := []string{}
for k, v := range env {
envs = append(envs, fmt.Sprintf("%s=%s", k, v))
}
sort.Strings(envs)
cfg.Config.Env = envs
if ic.Accounts.RunAs != "" {
cfg.Config.User = ic.Accounts.RunAs
}
if ic.StopSignal != "" {
cfg.Config.StopSignal = ic.StopSignal
}
v1Image, err = mutate.ConfigFile(v1Image, cfg)
if err != nil {
return nil, fmt.Errorf("unable to update %s config file: %w", imageType, err)
}
si := signed.Image(v1Image)
return si, nil
}
func BuildImageTarballFromLayer(imageRef string, layer v1.Layer, outputTarGZ string, ic types.ImageConfiguration, logger log.Logger, opts options.Options) error {
v1Image, err := BuildImageFromLayer(layer, ic, opts.SourceDateEpoch, opts.Arch, logger)
if err != nil {
return err
}
if v1Image == nil {
return errors.New("image build from layer returned nil")
}
imgRefTag, err := name.NewTag(imageRef)
if err != nil {
return fmt.Errorf("unable to validate image reference tag: %w", err)
}
if err := v1tar.WriteToFile(outputTarGZ, imgRefTag, v1Image); err != nil {
return fmt.Errorf("unable to write image to disk: %w", err)
}
logger.Printf("output image file to %s", outputTarGZ)
return nil
}