| 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 plugin |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"time" |
| 22 |
|
| 23 |
"github.com/containerd/containerd/defaults" |
| 24 |
"github.com/containerd/containerd/pkg/dialer" |
| 25 |
grpc "google.golang.org/grpc" |
| 26 |
"google.golang.org/grpc/backoff" |
| 27 |
"google.golang.org/grpc/credentials/insecure" |
| 28 |
|
| 29 |
runtime_alpha "github.com/containerd/containerd/third_party/k8s.io/cri-api/pkg/apis/runtime/v1alpha2" |
| 30 |
"github.com/containerd/stargz-snapshotter/service/keychain/crialpha" |
| 31 |
"github.com/containerd/stargz-snapshotter/service/plugincore" |
| 32 |
"github.com/containerd/stargz-snapshotter/service/resolver" |
| 33 |
) |
| 34 |
|
| 35 |
func init() { |
| 36 |
plugincore.RegisterPlugin(registerCRIAlphaServer) |
| 37 |
} |
| 38 |
|
| 39 |
func registerCRIAlphaServer(ctx context.Context, criAddr string, rpc *grpc.Server) resolver.Credential { |
| 40 |
connectAlphaCRI := func() (runtime_alpha.ImageServiceClient, error) { |
| 41 |
conn, err := newCRIConn(criAddr) |
| 42 |
if err != nil { |
| 43 |
return nil, err |
| 44 |
} |
| 45 |
return runtime_alpha.NewImageServiceClient(conn), nil |
| 46 |
} |
| 47 |
criAlphaCreds, criAlphaServer := crialpha.NewCRIAlphaKeychain(ctx, connectAlphaCRI) |
| 48 |
runtime_alpha.RegisterImageServiceServer(rpc, criAlphaServer) |
| 49 |
return criAlphaCreds |
| 50 |
} |
| 51 |
|
| 52 |
func newCRIConn(criAddr string) (*grpc.ClientConn, error) { |
| 53 |
// TODO: make gRPC options configurable from config.toml |
| 54 |
backoffConfig := backoff.DefaultConfig |
| 55 |
backoffConfig.MaxDelay = 3 * time.Second |
| 56 |
connParams := grpc.ConnectParams{ |
| 57 |
Backoff: backoffConfig, |
| 58 |
} |
| 59 |
gopts := []grpc.DialOption{ |
| 60 |
grpc.WithTransportCredentials(insecure.NewCredentials()), |
| 61 |
grpc.WithConnectParams(connParams), |
| 62 |
grpc.WithContextDialer(dialer.ContextDialer), |
| 63 |
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(defaults.DefaultMaxRecvMsgSize)), |
| 64 |
grpc.WithDefaultCallOptions(grpc.MaxCallSendMsgSize(defaults.DefaultMaxSendMsgSize)), |
| 65 |
} |
| 66 |
return grpc.Dial(dialer.DialAddress(criAddr), gopts...) |
| 67 |
} |