/
yolo.go
209 lines (173 loc) · 5.1 KB
/
yolo.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
package images
import (
"bytes"
_ "embed"
"fmt"
"io"
"os"
"strings"
"time"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/crane"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/empty"
"github.com/google/go-containerregistry/pkg/v1/mutate"
"github.com/google/go-containerregistry/pkg/v1/stream"
"github.com/google/go-containerregistry/pkg/v1/types"
)
func Yolo(baseRef string, dest string, files []LayerFile, schema string, commit string, env []string, session authn.Authenticator) (string, error) {
fmt.Fprintln(os.Stderr, "fetching metadata for", baseRef)
base, err := crane.Pull(baseRef, crane.WithAuth(session))
if err != nil {
return "", fmt.Errorf("pulling %w", err)
}
var img v1.Image
if len(files) > 0 {
yoloLess, err := removeYolo(base)
if err != nil {
return "", fmt.Errorf("removing existing yolo layers: %w", err)
}
// try to parse the predictor if it's provided
if schema != "" {
yoloLess, err = updatePredictor(yoloLess, schema)
if err != nil {
return "", fmt.Errorf("updating predictor: %w", err)
}
}
if len(env) > 0 {
yoloLess, err = updateEnv(yoloLess, env)
if err != nil {
return "", fmt.Errorf("updating env: %w", err)
}
}
if commit != "" {
yoloLess, err = updateCommit(yoloLess, commit)
if err != nil {
return "", fmt.Errorf("updating commit: %w", err)
}
}
fmt.Fprintln(os.Stderr, "appending as new layer")
yoloLayers, err := GetSourceLayers(base, false, true)
if err != nil {
return "", fmt.Errorf("getting source layers: %w", err)
}
newLayer, err := MakeTar(files, yoloLayers)
if err != nil {
return "", fmt.Errorf("making tar: %w", err)
}
img, err = appendLayer(yoloLess, newLayer)
if err != nil {
return "", fmt.Errorf("appending %v: %w", newLayer, err)
}
} else {
if len(env) > 0 {
img, err = updateEnv(base, env)
if err != nil {
return "", fmt.Errorf("updating env: %w", err)
}
}
}
// --- pushing image
start := time.Now()
err = crane.Push(img, dest, crane.WithAuth(session))
if err != nil {
return "", fmt.Errorf("pushing %s: %w", dest, err)
}
fmt.Fprintln(os.Stderr, "pushing took", time.Since(start))
return ImageId(dest, img)
}
// All of this code is from pkg/v1/mutate - so we can add history and use a tarball
func appendLayer(base v1.Image, tarball *bytes.Buffer) (v1.Image, error) {
baseMediaType, err := base.MediaType()
if err != nil {
return nil, fmt.Errorf("getting base image media type: %w", err)
}
layerType := types.DockerLayer
if baseMediaType == types.OCIManifestSchema1 {
layerType = types.OCILayer
}
layer := stream.NewLayer(io.NopCloser(tarball), stream.WithMediaType(layerType))
history := v1.History{
CreatedBy: "cp . /src # yolo",
Created: v1.Time{Time: time.Now()},
Author: "yolo",
Comment: "",
}
return mutate.Append(base, mutate.Addendum{Layer: layer, History: history})
}
func updateCommit(img v1.Image, commit string) (v1.Image, error) {
cfg, err := img.ConfigFile()
if err != nil {
return nil, err
}
cfg.Config.Labels["org.opencontainers.image.revision"] = commit
return mutate.Config(img, cfg.Config)
}
func updateEnv(base v1.Image, env []string) (v1.Image, error) {
cfg, err := base.ConfigFile()
if err != nil {
return nil, err
}
for _, e := range env {
fmt.Fprintf(os.Stderr, "updating env: %s\n", e)
key := e[:strings.Index(e, "=")]
found := false
for i, v := range cfg.Config.Env {
if strings.HasPrefix(v, key+"=") {
cfg.Config.Env[i] = e
found = true
}
}
if !found {
cfg.Config.Env = append(cfg.Config.Env, e)
}
}
return mutate.Config(base, cfg.Config)
}
func updatePredictor(img v1.Image, schema string) (v1.Image, error) {
cfg, err := img.ConfigFile()
if err != nil {
return nil, err
}
fmt.Fprintln(os.Stderr, "updating predictor to schema with length", len(schema))
cfg.Config.Labels["org.cogmodel.openapi_schema"] = schema
cfg.Config.Labels["run.cog.openapi_schema"] = schema
return mutate.Config(img, cfg.Config)
}
// we need to remove any existing yolo layers before adding more... otherwise
// we'll end up with a bunch of yolo layers
func removeYolo(orig v1.Image) (v1.Image, error) {
layers, err := orig.Layers()
if err != nil {
return nil, fmt.Errorf("failed to get layers for original: %w", err)
}
config, err := orig.ConfigFile()
if err != nil {
return nil, fmt.Errorf("failed to get config for original: %w", err)
}
yololessImage, err := mutate.Config(empty.Image, *config.Config.DeepCopy())
if err != nil {
return nil, fmt.Errorf("failed to create empty image with original config: %w", err)
}
idx := 0
for _, h := range config.History {
if h.CreatedBy != "cp . /src # yolo" {
add := mutate.Addendum{
Layer: nil,
History: h,
}
if !h.EmptyLayer {
add.Layer = layers[idx]
}
fmt.Println("adding layer", add.Layer, "with history", h)
yololessImage, err = mutate.Append(yololessImage, add)
if err != nil {
return nil, fmt.Errorf("failed to add layer: %w", err)
}
}
if !h.EmptyLayer {
idx++
}
}
return yololessImage, nil
}