Debian Codegraph @

service/keychain/crialpha/crialpha.go

raw
1 /*
2 Copyright The containerd Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package crialpha
18
19 import (
20 "context"
21 "errors"
22 "fmt"
23 "sync"
24 "time"
25
26 "github.com/containerd/containerd/log"
27 "github.com/containerd/containerd/reference"
28 distribution "github.com/containerd/containerd/reference/docker"
29 "github.com/containerd/stargz-snapshotter/service/resolver"
30
31 runtime_alpha "github.com/containerd/containerd/third_party/k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
32 runtime "k8s.io/cri-api/pkg/apis/runtime/v1"
33
34 "github.com/containerd/stargz-snapshotter/util/criutil"
35 )
36
37 // NewAlphaCRIKeychain provides creds passed through CRI PullImage API.
38 // Same as NewCRIKeychain but for CRI v1alpha API.
39 // Containerd doesn't drop v1alpha API support so our proxy also exposes this API as well.
40 func NewCRIAlphaKeychain(ctx context.Context, connectCRI func() (runtime_alpha.ImageServiceClient, error)) (resolver.Credential, runtime_alpha.ImageServiceServer) {
41 server := &instrumentedAlphaService{config: make(map[string]*runtime_alpha.AuthConfig)}
42 go func() {
43 log.G(ctx).Debugf("Waiting for CRI service is started...")
44 for i := 0; i < 100; i++ {
45 client, err := connectCRI()
46 if err == nil {
47 server.criMu.Lock()
48 server.cri = client
49 server.criMu.Unlock()
50 log.G(ctx).Info("connected to backend CRI service")
51 return
52 }
53 log.G(ctx).WithError(err).Warnf("failed to connect to CRI")
54 time.Sleep(10 * time.Second)
55 }
56 log.G(ctx).Warnf("no connection is available to CRI")
57 }()
58 return server.credentials, server
59 }
60
61 type instrumentedAlphaService struct {
62 runtime_alpha.UnimplementedImageServiceServer
63
64 cri runtime_alpha.ImageServiceClient
65 criMu sync.Mutex
66
67 config map[string]*runtime_alpha.AuthConfig
68 configMu sync.Mutex
69 }
70
71 func (in *instrumentedAlphaService) credentials(host string, refspec reference.Spec) (string, string, error) {
72 if host == "docker.io" || host == "registry-1.docker.io" {
73 // Creds of "docker.io" is stored keyed by "https://index.docker.io/v1/".
74 host = "index.docker.io"
75 }
76 in.configMu.Lock()
77 defer in.configMu.Unlock()
78 if cfg, ok := in.config[refspec.String()]; ok {
79 var v1cfg runtime.AuthConfig
80 if err := criutil.AlphaReqToV1Req(cfg, &v1cfg); err != nil {
81 return "", "", err
82 }
83 return resolver.ParseAuth(&v1cfg, host)
84 }
85 return "", "", nil
86 }
87
88 func (in *instrumentedAlphaService) getCRI() (c runtime_alpha.ImageServiceClient) {
89 in.criMu.Lock()
90 c = in.cri
91 in.criMu.Unlock()
92 return
93 }
94
95 func (in *instrumentedAlphaService) ListImages(ctx context.Context, r *runtime_alpha.ListImagesRequest) (res *runtime_alpha.ListImagesResponse, err error) {
96 cri := in.getCRI()
97 if cri == nil {
98 return nil, errors.New("server is not initialized yet")
99 }
100 return cri.ListImages(ctx, r)
101 }
102
103 func (in *instrumentedAlphaService) ImageStatus(ctx context.Context, r *runtime_alpha.ImageStatusRequest) (res *runtime_alpha.ImageStatusResponse, err error) {
104 cri := in.getCRI()
105 if cri == nil {
106 return nil, errors.New("server is not initialized yet")
107 }
108 return cri.ImageStatus(ctx, r)
109 }
110
111 func (in *instrumentedAlphaService) PullImage(ctx context.Context, r *runtime_alpha.PullImageRequest) (res *runtime_alpha.PullImageResponse, err error) {
112 cri := in.getCRI()
113 if cri == nil {
114 return nil, errors.New("server is not initialized yet")
115 }
116 refspec, err := parseReference(r.GetImage().GetImage())
117 if err != nil {
118 return nil, err
119 }
120 in.configMu.Lock()
121 in.config[refspec.String()] = r.GetAuth()
122 in.configMu.Unlock()
123 return cri.PullImage(ctx, r)
124 }
125
126 func (in *instrumentedAlphaService) RemoveImage(ctx context.Context, r *runtime_alpha.RemoveImageRequest) (_ *runtime_alpha.RemoveImageResponse, err error) {
127 cri := in.getCRI()
128 if cri == nil {
129 return nil, errors.New("server is not initialized yet")
130 }
131 refspec, err := parseReference(r.GetImage().GetImage())
132 if err != nil {
133 return nil, err
134 }
135 in.configMu.Lock()
136 delete(in.config, refspec.String())
137 in.configMu.Unlock()
138 return cri.RemoveImage(ctx, r)
139 }
140
141 func (in *instrumentedAlphaService) ImageFsInfo(ctx context.Context, r *runtime_alpha.ImageFsInfoRequest) (res *runtime_alpha.ImageFsInfoResponse, err error) {
142 cri := in.getCRI()
143 if cri == nil {
144 return nil, errors.New("server is not initialized yet")
145 }
146 return cri.ImageFsInfo(ctx, r)
147 }
148
149 func parseReference(ref string) (reference.Spec, error) {
150 namedRef, err := distribution.ParseDockerRef(ref)
151 if err != nil {
152 return reference.Spec{}, fmt.Errorf("failed to parse image reference %q: %w", ref, err)
153 }
154 return reference.Parse(namedRef.String())
155 }