| 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 plugincore |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"errors" |
| 22 |
"fmt" |
| 23 |
"net" |
| 24 |
"os" |
| 25 |
"path/filepath" |
| 26 |
"time" |
| 27 |
|
| 28 |
"github.com/containerd/containerd/defaults" |
| 29 |
"github.com/containerd/containerd/log" |
| 30 |
"github.com/containerd/containerd/pkg/dialer" |
| 31 |
"github.com/containerd/containerd/platforms" |
| 32 |
ctdplugin "github.com/containerd/containerd/plugin" |
| 33 |
"github.com/containerd/stargz-snapshotter/service" |
| 34 |
"github.com/containerd/stargz-snapshotter/service/keychain/cri" |
| 35 |
"github.com/containerd/stargz-snapshotter/service/keychain/dockerconfig" |
| 36 |
"github.com/containerd/stargz-snapshotter/service/keychain/kubeconfig" |
| 37 |
"github.com/containerd/stargz-snapshotter/service/resolver" |
| 38 |
grpc "google.golang.org/grpc" |
| 39 |
"google.golang.org/grpc/backoff" |
| 40 |
"google.golang.org/grpc/credentials/insecure" |
| 41 |
runtime "k8s.io/cri-api/pkg/apis/runtime/v1" |
| 42 |
) |
| 43 |
|
| 44 |
// Config represents configuration for the stargz snapshotter plugin. |
| 45 |
type Config struct { |
| 46 |
service.Config |
| 47 |
|
| 48 |
// RootPath is the directory for the plugin |
| 49 |
RootPath string `toml:"root_path"` |
| 50 |
|
| 51 |
// CRIKeychainImageServicePath is the path to expose CRI service wrapped by CRI keychain |
| 52 |
CRIKeychainImageServicePath string `toml:"cri_keychain_image_service_path"` |
| 53 |
|
| 54 |
// Registry is CRI-plugin-compatible registry configuration |
| 55 |
Registry resolver.Registry `toml:"registry"` |
| 56 |
} |
| 57 |
|
| 58 |
func RegisterPlugin(registerCRIAlphaServer func(ctx context.Context, criAddr string, rpc *grpc.Server) resolver.Credential) { |
| 59 |
ctdplugin.Register(&ctdplugin.Registration{ |
| 60 |
Type: ctdplugin.SnapshotPlugin, |
| 61 |
ID: "stargz", |
| 62 |
Config: &Config{}, |
| 63 |
InitFn: func(ic *ctdplugin.InitContext) (interface{}, error) { |
| 64 |
ic.Meta.Platforms = append(ic.Meta.Platforms, platforms.DefaultSpec()) |
| 65 |
ctx := ic.Context |
| 66 |
|
| 67 |
config, ok := ic.Config.(*Config) |
| 68 |
if !ok { |
| 69 |
return nil, errors.New("invalid stargz snapshotter configuration") |
| 70 |
} |
| 71 |
|
| 72 |
root := ic.Root |
| 73 |
if config.RootPath != "" { |
| 74 |
root = config.RootPath |
| 75 |
} |
| 76 |
ic.Meta.Exports["root"] = root |
| 77 |
|
| 78 |
// Configure keychain |
| 79 |
credsFuncs := []resolver.Credential{dockerconfig.NewDockerconfigKeychain(ctx)} |
| 80 |
if config.Config.KubeconfigKeychainConfig.EnableKeychain { |
| 81 |
var opts []kubeconfig.Option |
| 82 |
if kcp := config.Config.KubeconfigKeychainConfig.KubeconfigPath; kcp != "" { |
| 83 |
opts = append(opts, kubeconfig.WithKubeconfigPath(kcp)) |
| 84 |
} |
| 85 |
credsFuncs = append(credsFuncs, kubeconfig.NewKubeconfigKeychain(ctx, opts...)) |
| 86 |
} |
| 87 |
if addr := config.CRIKeychainImageServicePath; config.Config.CRIKeychainConfig.EnableKeychain && addr != "" { |
| 88 |
// connects to the backend CRI service (defaults to containerd socket) |
| 89 |
criAddr := ic.Address |
| 90 |
if cp := config.Config.CRIKeychainConfig.ImageServicePath; cp != "" { |
| 91 |
criAddr = cp |
| 92 |
} |
| 93 |
if criAddr == "" { |
| 94 |
return nil, errors.New("backend CRI service address is not specified") |
| 95 |
} |
| 96 |
connectCRI := func() (runtime.ImageServiceClient, error) { |
| 97 |
conn, err := newCRIConn(criAddr) |
| 98 |
if err != nil { |
| 99 |
return nil, err |
| 100 |
} |
| 101 |
return runtime.NewImageServiceClient(conn), nil |
| 102 |
} |
| 103 |
criCreds, criServer := cri.NewCRIKeychain(ctx, connectCRI) |
| 104 |
// Create a gRPC server |
| 105 |
rpc := grpc.NewServer() |
| 106 |
runtime.RegisterImageServiceServer(rpc, criServer) |
| 107 |
criAlphaCreds := registerCRIAlphaServer(ctx, criAddr, rpc) |
| 108 |
// Prepare the directory for the socket |
| 109 |
if err := os.MkdirAll(filepath.Dir(addr), 0700); err != nil { |
| 110 |
return nil, fmt.Errorf("failed to create directory %q: %w", filepath.Dir(addr), err) |
| 111 |
} |
| 112 |
// Try to remove the socket file to avoid EADDRINUSE |
| 113 |
if err := os.RemoveAll(addr); err != nil { |
| 114 |
return nil, fmt.Errorf("failed to remove %q: %w", addr, err) |
| 115 |
} |
| 116 |
// Listen and serve |
| 117 |
l, err := net.Listen("unix", addr) |
| 118 |
if err != nil { |
| 119 |
return nil, fmt.Errorf("error on listen socket %q: %w", addr, err) |
| 120 |
} |
| 121 |
go func() { |
| 122 |
if err := rpc.Serve(l); err != nil { |
| 123 |
log.G(ctx).WithError(err).Warnf("error on serving via socket %q", addr) |
| 124 |
} |
| 125 |
}() |
| 126 |
credsFuncs = append(credsFuncs, criAlphaCreds, criCreds) |
| 127 |
} |
| 128 |
|
| 129 |
// TODO(ktock): print warn if old configuration is specified. |
| 130 |
// TODO(ktock): should we respect old configuration? |
| 131 |
return service.NewStargzSnapshotterService(ctx, root, &config.Config, |
| 132 |
service.WithCustomRegistryHosts(resolver.RegistryHostsFromCRIConfig(ctx, config.Registry, credsFuncs...))) |
| 133 |
}, |
| 134 |
}) |
| 135 |
} |
| 136 |
|
| 137 |
func newCRIConn(criAddr string) (*grpc.ClientConn, error) { |
| 138 |
// TODO: make gRPC options configurable from config.toml |
| 139 |
backoffConfig := backoff.DefaultConfig |
| 140 |
backoffConfig.MaxDelay = 3 * time.Second |
| 141 |
connParams := grpc.ConnectParams{ |
| 142 |
Backoff: backoffConfig, |
| 143 |
} |
| 144 |
gopts := []grpc.DialOption{ |
| 145 |
grpc.WithTransportCredentials(insecure.NewCredentials()), |
| 146 |
grpc.WithConnectParams(connParams), |
| 147 |
grpc.WithContextDialer(dialer.ContextDialer), |
| 148 |
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(defaults.DefaultMaxRecvMsgSize)), |
| 149 |
grpc.WithDefaultCallOptions(grpc.MaxCallSendMsgSize(defaults.DefaultMaxSendMsgSize)), |
| 150 |
} |
| 151 |
return grpc.Dial(dialer.DialAddress(criAddr), gopts...) |
| 152 |
} |