-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
images_push.go
222 lines (200 loc) · 6.22 KB
/
images_push.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
//go:build !remote
package compat
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
"github.com/containers/image/v5/types"
"github.com/containers/podman/v5/libpod"
"github.com/containers/podman/v5/pkg/api/handlers/utils"
api "github.com/containers/podman/v5/pkg/api/types"
"github.com/containers/podman/v5/pkg/auth"
"github.com/containers/podman/v5/pkg/domain/entities"
"github.com/containers/podman/v5/pkg/domain/infra/abi"
"github.com/containers/storage"
"github.com/docker/docker/pkg/jsonmessage"
"github.com/sirupsen/logrus"
)
// PushImage is the handler for the compat http endpoint for pushing images.
func PushImage(w http.ResponseWriter, r *http.Request) {
decoder := utils.GetDecoder(r)
runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime)
// Now use the ABI implementation to prevent us from having duplicate
// code.
imageEngine := abi.ImageEngine{Libpod: runtime}
query := struct {
All bool `schema:"all"`
Compress bool `schema:"compress"`
Destination string `schema:"destination"`
Format string `schema:"format"`
TLSVerify bool `schema:"tlsVerify"`
Tag string `schema:"tag"`
}{
// This is where you can override the golang default value for one of fields
TLSVerify: true,
}
if err := decoder.Decode(&query, r.URL.Query()); err != nil {
utils.Error(w, http.StatusBadRequest, fmt.Errorf("failed to parse parameters for %s: %w", r.URL.String(), err))
return
}
// Note that Docker's docs state "Image name or ID" to be in the path
// parameter but it really must be a name as Docker does not allow for
// pushing an image by ID.
imageName := strings.TrimSuffix(utils.GetName(r), "/push") // GetName returns the entire path
if query.Tag != "" {
imageName += ":" + query.Tag
}
if _, err := utils.ParseStorageReference(imageName); err != nil {
utils.Error(w, http.StatusBadRequest, fmt.Errorf("image source %q is not a containers-storage-transport reference: %w", imageName, err))
return
}
possiblyNormalizedName, err := utils.NormalizeToDockerHub(r, imageName)
if err != nil {
utils.Error(w, http.StatusInternalServerError, fmt.Errorf("normalizing image: %w", err))
return
}
imageName = possiblyNormalizedName
localImage, _, err := runtime.LibimageRuntime().LookupImage(possiblyNormalizedName, nil)
if err != nil {
utils.ImageNotFound(w, imageName, fmt.Errorf("failed to find image %s: %w", imageName, err))
return
}
rawManifest, _, err := localImage.Manifest(r.Context())
if err != nil {
utils.Error(w, http.StatusBadRequest, err)
return
}
authconf, authfile, err := auth.GetCredentials(r)
if err != nil {
utils.Error(w, http.StatusBadRequest, err)
return
}
defer auth.RemoveAuthfile(authfile)
var username, password string
if authconf != nil {
username = authconf.Username
password = authconf.Password
}
options := entities.ImagePushOptions{
All: query.All,
Authfile: authfile,
Compress: query.Compress,
Format: query.Format,
Password: password,
Username: username,
Quiet: true,
Progress: make(chan types.ProgressProperties),
}
if _, found := r.URL.Query()["tlsVerify"]; found {
options.SkipTLSVerify = types.NewOptionalBool(!query.TLSVerify)
}
var destination string
if _, found := r.URL.Query()["destination"]; found {
destination = query.Destination
} else {
destination = imageName
}
enc := json.NewEncoder(w)
enc.SetEscapeHTML(true)
flush := func() {}
if flusher, ok := w.(http.Flusher); ok {
flush = flusher.Flush
}
statusWritten := false
writeStatusCode := func(code int) {
if !statusWritten {
w.WriteHeader(code)
w.Header().Set("Content-Type", "application/json")
flush()
statusWritten = true
}
}
referenceWritten := false
writeReference := func() {
if !referenceWritten {
var report jsonmessage.JSONMessage
report.Status = fmt.Sprintf("The push refers to repository [%s]", imageName)
if err := enc.Encode(report); err != nil {
logrus.Warnf("Failed to json encode error %q", err.Error())
}
flush()
referenceWritten = true
}
}
pushErrChan := make(chan error)
var pushReport *entities.ImagePushReport
go func() {
var err error
pushReport, err = imageEngine.Push(r.Context(), imageName, destination, options)
pushErrChan <- err
}()
loop: // break out of for/select infinite loop
for {
var report jsonmessage.JSONMessage
select {
case e := <-options.Progress:
writeStatusCode(http.StatusOK)
writeReference()
switch e.Event {
case types.ProgressEventNewArtifact:
report.Status = "Preparing"
case types.ProgressEventRead:
report.Status = "Pushing"
report.Progress = &jsonmessage.JSONProgress{
Current: int64(e.Offset),
Total: e.Artifact.Size,
}
report.ProgressMessage = report.Progress.String()
case types.ProgressEventSkipped:
report.Status = "Layer already exists"
case types.ProgressEventDone:
report.Status = "Pushed"
}
report.ID = e.Artifact.Digest.Encoded()[0:12]
if err := enc.Encode(report); err != nil {
logrus.Warnf("Failed to json encode error %q", err.Error())
}
flush()
case err := <-pushErrChan:
if err != nil {
var msg string
if errors.Is(err, storage.ErrImageUnknown) {
// Image may have been removed in the meantime.
writeStatusCode(http.StatusNotFound)
msg = "An image does not exist locally with the tag: " + imageName
} else {
writeStatusCode(http.StatusInternalServerError)
msg = err.Error()
}
report.Error = &jsonmessage.JSONError{
Message: msg,
}
report.ErrorMessage = msg
if err := enc.Encode(report); err != nil {
logrus.Warnf("Failed to json encode error %q", err.Error())
}
flush()
break loop
} else {
writeStatusCode(http.StatusOK)
writeReference() // There may not be any progress, so make sure the reference gets written
}
tag := query.Tag
if tag == "" {
tag = "latest"
}
var digestStr string
if pushReport != nil {
digestStr = pushReport.ManifestDigest
}
report.Status = fmt.Sprintf("%s: digest: %s size: %d", tag, digestStr, len(rawManifest))
if err := enc.Encode(report); err != nil {
logrus.Warnf("Failed to json encode error %q", err.Error())
}
flush()
break loop // break out of for/select infinite loop
}
}
}