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
17package diff
18
19import (
20	"context"
21
22	diffapi "github.com/containerd/containerd/api/services/diff/v1"
23	"github.com/containerd/containerd/plugin"
24	"github.com/containerd/containerd/services"
25	"github.com/pkg/errors"
26	"google.golang.org/grpc"
27)
28
29func init() {
30	plugin.Register(&plugin.Registration{
31		Type: plugin.GRPCPlugin,
32		ID:   "diff",
33		Requires: []plugin.Type{
34			plugin.ServicePlugin,
35		},
36		InitFn: func(ic *plugin.InitContext) (interface{}, error) {
37			plugins, err := ic.GetByType(plugin.ServicePlugin)
38			if err != nil {
39				return nil, err
40			}
41			p, ok := plugins[services.DiffService]
42			if !ok {
43				return nil, errors.New("diff service not found")
44			}
45			i, err := p.Instance()
46			if err != nil {
47				return nil, err
48			}
49			return &service{local: i.(diffapi.DiffClient)}, nil
50		},
51	})
52}
53
54type service struct {
55	local diffapi.DiffClient
56}
57
58var _ diffapi.DiffServer = &service{}
59
60func (s *service) Register(gs *grpc.Server) error {
61	diffapi.RegisterDiffServer(gs, s)
62	return nil
63}
64
65func (s *service) Apply(ctx context.Context, er *diffapi.ApplyRequest) (*diffapi.ApplyResponse, error) {
66	return s.local.Apply(ctx, er)
67}
68
69func (s *service) Diff(ctx context.Context, dr *diffapi.DiffRequest) (*diffapi.DiffResponse, error) {
70	return s.local.Diff(ctx, dr)
71}
72