/
image.go
127 lines (106 loc) · 3.69 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
/*
Copyright 2023 The Katanomi Authors.
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 registry
import (
"context"
"fmt"
"net/http"
"github.com/go-resty/resty/v2"
artifactv1 "github.com/katanomi/pkg/apis/artifacts/v1alpha1"
"github.com/katanomi/pkg/client"
pclient "github.com/katanomi/pkg/plugin/client"
"github.com/katanomi/pkg/restclient"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"knative.dev/pkg/logging"
"oras.land/oras-go/v2"
"oras.land/oras-go/v2/content/memory"
"oras.land/oras-go/v2/registry/remote"
"oras.land/oras-go/v2/registry/remote/auth"
)
// PushEmptyImage an empty image will be created and pushed.
// The function will read the credentials from the context. If the credentials
// are empty, only the credentials will not be used for push.
func PushEmptyImage(ctx context.Context, uri artifactv1.URI) error {
log := logging.FromContext(ctx).Named("PushImage").With("ref", uri.String())
if uri.Host == "" || uri.Path == "" || uri.Tag == "" {
err := fmt.Errorf("repository host, path and tag must be set")
return err
}
// create empty layers.
memStore := memory.New()
opts := oras.PackManifestOptions{
Layers: []ocispec.Descriptor{},
}
manifestDescriptor, err := oras.PackManifest(ctx, memStore, oras.PackManifestVersion1_1_RC4, ocispec.MediaTypeImageManifest, opts)
if err != nil {
log.Errorw("failed to pack manifest with memory", "err", err)
return err
}
log.Debugw("pack manifest descriptor", "descriptor", manifestDescriptor)
if err = memStore.Tag(ctx, manifestDescriptor, uri.Tag); err != nil {
log.Errorw("failed to create tag with memory", "err", err)
return err
}
var httpCli *http.Client
restyClient := restclient.RESTClient(ctx)
if restyClient != nil {
httpCli = restyClient.GetClient()
} else {
httpCli = client.NewHTTPClient()
}
repo, err := remote.NewRepository(uri.Repository())
if err != nil {
log.Errorw("failed to new repository", "err", err)
return err
}
schemeDeter := NewDefaultRegistrySchemeDetection(resty.NewWithClient(httpCli), true, true)
scheme, _ := schemeDeter.DetectScheme(ctx, uri.Host)
if scheme == HTTP {
repo.PlainHTTP = true
}
credential, err := extraAuthFromContext(ctx)
if err != nil {
log.Warnw("failed to extra auth, will try to push without credentials", "err", err)
}
// need to make sure ignore authentication is set.
// Warn: may pollute the global client.
client.InsecureSkipVerifyOption(httpCli)
repo.Client = &auth.Client{
Client: httpCli,
Cache: auth.DefaultCache,
Credential: auth.StaticCredential(uri.Host, credential),
}
// Copy from the memory store to the remote repository
_, err = oras.Copy(ctx, memStore, uri.Tag, repo, uri.Tag, oras.DefaultCopyOptions)
if err != nil {
return err
}
return nil
}
func extraAuthFromContext(ctx context.Context) (auth.Credential, error) {
authInfo := pclient.ExtractAuth(ctx)
if authInfo == nil {
return auth.EmptyCredential, nil
}
if !authInfo.IsBasic() {
return auth.EmptyCredential, fmt.Errorf("only support basic auth, current auth type: %s", authInfo.Type)
}
username, password, err := authInfo.GetBasicInfo()
if err != nil {
return auth.EmptyCredential, err
}
return auth.Credential{
Username: username,
Password: password,
}, nil
}