@@ -2,6 +2,7 @@ package oci |
2 | 2 |
|
3 | 3 | import ( |
4 | 4 | "context" |
| 5 | + "errors" |
5 | 6 | "fmt" |
6 | 7 | "net/http" |
7 | 8 | "os" |
@@ -12,8 +13,10 @@ import ( |
12 | 13 | "github.com/google/go-containerregistry/pkg/authn" |
13 | 14 | "github.com/google/go-containerregistry/pkg/name" |
14 | 15 | v1 "github.com/google/go-containerregistry/pkg/v1" |
| 16 | + "github.com/google/go-containerregistry/pkg/v1/empty" |
15 | 17 | "github.com/google/go-containerregistry/pkg/v1/layout" |
16 | 18 | "github.com/google/go-containerregistry/pkg/v1/remote" |
| 19 | + "github.com/google/go-containerregistry/pkg/v1/remote/transport" |
17 | 20 | progress "github.com/schollz/progressbar/v3" |
18 | 21 |
|
19 | 22 | fn "knative.dev/func/pkg/functions" |
@@ -181,13 +184,30 @@ func getBuildDir(f fn.Function) (string, error) { |
181 | 184 | func (p *Pusher) writeIndex(ctx context.Context, ref name.Reference, ii v1.ImageIndex, creds Credentials) error { |
182 | 185 | oo := []remote.Option{ |
183 | 186 | remote.WithContext(ctx), |
184 | | - remote.WithProgress(p.updates), |
185 | 187 | remote.WithTransport(p.transport), |
186 | 188 | } |
187 | 189 |
|
188 | 190 | if !p.Anonymous { |
189 | 191 | oo = append(oo, remote.WithAuth(creds)) |
190 | 192 | } |
191 | 193 |
|
192 | | - return remote.WriteIndex(ref, ii, oo...) |
| 194 | + // Ensure the repository exists. Some registries (notably |
| 195 | + // OpenShift's internal registry) return HTTP 500 for manifest |
| 196 | + // HEAD requests by digest when the repository doesn't exist yet. |
| 197 | + // WriteIndex checks child manifests by digest, so the repository |
| 198 | + // must exist beforehand. A HEAD by tag returns a proper 404, so |
| 199 | + // use that to detect the missing repository and create it by |
| 200 | + // pushing an empty image. The tag is overwritten by the index. |
| 201 | + if _, err := remote.Head(ref, oo...); err != nil { |
| 202 | + var terr *transport.Error |
| 203 | + if errors.As(err, &terr) && terr.StatusCode == http.StatusNotFound { |
| 204 | + if err := remote.Write(ref, empty.Image, oo...); err != nil { |
| 205 | + return fmt.Errorf("cannot create repo: %w", err) |
| 206 | + } |
| 207 | + } else { |
| 208 | + return fmt.Errorf("cannot check repo existence: %w", err) |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + return remote.WriteIndex(ref, ii, append(oo, remote.WithProgress(p.updates))...) |
193 | 213 | } |