| 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 cri |
| 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 |
runtime "k8s.io/cri-api/pkg/apis/runtime/v1" |
| 31 |
) |
| 32 |
|
| 33 |
// NewCRIKeychain provides creds passed through CRI PullImage API. |
| 34 |
// This also returns a CRI image service server that works as a proxy backed by the specified CRI service. |
| 35 |
// This server reads all PullImageRequest and uses PullImageRequest.AuthConfig for authenticating snapshots. |
| 36 |
func NewCRIKeychain(ctx context.Context, connectCRI func() (runtime.ImageServiceClient, error)) (resolver.Credential, runtime.ImageServiceServer) { |
| 37 |
server := &instrumentedService{config: make(map[string]*runtime.AuthConfig)} |
| 38 |
go func() { |
| 39 |
log.G(ctx).Debugf("Waiting for CRI service is started...") |
| 40 |
for i := 0; i < 100; i++ { |
| 41 |
client, err := connectCRI() |
| 42 |
if err == nil { |
| 43 |
server.criMu.Lock() |
| 44 |
server.cri = client |
| 45 |
server.criMu.Unlock() |
| 46 |
log.G(ctx).Info("connected to backend CRI service") |
| 47 |
return |
| 48 |
} |
| 49 |
log.G(ctx).WithError(err).Warnf("failed to connect to CRI") |
| 50 |
time.Sleep(10 * time.Second) |
| 51 |
} |
| 52 |
log.G(ctx).Warnf("no connection is available to CRI") |
| 53 |
}() |
| 54 |
return server.credentials, server |
| 55 |
} |
| 56 |
|
| 57 |
type instrumentedService struct { |
| 58 |
cri runtime.ImageServiceClient |
| 59 |
criMu sync.Mutex |
| 60 |
|
| 61 |
config map[string]*runtime.AuthConfig |
| 62 |
configMu sync.Mutex |
| 63 |
} |
| 64 |
|
| 65 |
func (in *instrumentedService) credentials(host string, refspec reference.Spec) (string, string, error) { |
| 66 |
if host == "docker.io" || host == "registry-1.docker.io" { |
| 67 |
// Creds of "docker.io" is stored keyed by "https://index.docker.io/v1/". |
| 68 |
host = "index.docker.io" |
| 69 |
} |
| 70 |
in.configMu.Lock() |
| 71 |
defer in.configMu.Unlock() |
| 72 |
if cfg, ok := in.config[refspec.String()]; ok { |
| 73 |
return resolver.ParseAuth(cfg, host) |
| 74 |
} |
| 75 |
return "", "", nil |
| 76 |
} |
| 77 |
|
| 78 |
func (in *instrumentedService) getCRI() (c runtime.ImageServiceClient) { |
| 79 |
in.criMu.Lock() |
| 80 |
c = in.cri |
| 81 |
in.criMu.Unlock() |
| 82 |
return |
| 83 |
} |
| 84 |
|
| 85 |
func (in *instrumentedService) ListImages(ctx context.Context, r *runtime.ListImagesRequest) (res *runtime.ListImagesResponse, err error) { |
| 86 |
cri := in.getCRI() |
| 87 |
if cri == nil { |
| 88 |
return nil, errors.New("server is not initialized yet") |
| 89 |
} |
| 90 |
return cri.ListImages(ctx, r) |
| 91 |
} |
| 92 |
|
| 93 |
func (in *instrumentedService) ImageStatus(ctx context.Context, r *runtime.ImageStatusRequest) (res *runtime.ImageStatusResponse, err error) { |
| 94 |
cri := in.getCRI() |
| 95 |
if cri == nil { |
| 96 |
return nil, errors.New("server is not initialized yet") |
| 97 |
} |
| 98 |
return cri.ImageStatus(ctx, r) |
| 99 |
} |
| 100 |
|
| 101 |
func (in *instrumentedService) PullImage(ctx context.Context, r *runtime.PullImageRequest) (res *runtime.PullImageResponse, err error) { |
| 102 |
cri := in.getCRI() |
| 103 |
if cri == nil { |
| 104 |
return nil, errors.New("server is not initialized yet") |
| 105 |
} |
| 106 |
refspec, err := parseReference(r.GetImage().GetImage()) |
| 107 |
if err != nil { |
| 108 |
return nil, err |
| 109 |
} |
| 110 |
in.configMu.Lock() |
| 111 |
in.config[refspec.String()] = r.GetAuth() |
| 112 |
in.configMu.Unlock() |
| 113 |
return cri.PullImage(ctx, r) |
| 114 |
} |
| 115 |
|
| 116 |
func (in *instrumentedService) RemoveImage(ctx context.Context, r *runtime.RemoveImageRequest) (_ *runtime.RemoveImageResponse, err error) { |
| 117 |
cri := in.getCRI() |
| 118 |
if cri == nil { |
| 119 |
return nil, errors.New("server is not initialized yet") |
| 120 |
} |
| 121 |
refspec, err := parseReference(r.GetImage().GetImage()) |
| 122 |
if err != nil { |
| 123 |
return nil, err |
| 124 |
} |
| 125 |
in.configMu.Lock() |
| 126 |
delete(in.config, refspec.String()) |
| 127 |
in.configMu.Unlock() |
| 128 |
return cri.RemoveImage(ctx, r) |
| 129 |
} |
| 130 |
|
| 131 |
func (in *instrumentedService) ImageFsInfo(ctx context.Context, r *runtime.ImageFsInfoRequest) (res *runtime.ImageFsInfoResponse, err error) { |
| 132 |
cri := in.getCRI() |
| 133 |
if cri == nil { |
| 134 |
return nil, errors.New("server is not initialized yet") |
| 135 |
} |
| 136 |
return cri.ImageFsInfo(ctx, r) |
| 137 |
} |
| 138 |
|
| 139 |
func parseReference(ref string) (reference.Spec, error) { |
| 140 |
namedRef, err := distribution.ParseDockerRef(ref) |
| 141 |
if err != nil { |
| 142 |
return reference.Spec{}, fmt.Errorf("failed to parse image reference %q: %w", ref, err) |
| 143 |
} |
| 144 |
return reference.Parse(namedRef.String()) |
| 145 |
} |