-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathservice.go
178 lines (134 loc) · 4.93 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright Confidential Containers Contributors
// SPDX-License-Identifier: Apache-2.0
package proxy
import (
"context"
b64 "encoding/base64"
"net"
"os"
"path/filepath"
"strings"
"github.com/confidential-containers/cloud-api-adaptor/src/cloud-api-adaptor/pkg/util/agentproto"
pb "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/agent/protocols/grpc"
"google.golang.org/protobuf/types/known/emptypb"
)
type proxyService struct {
agentproto.Redirector
pauseImage string
}
const (
defaultPauseImage = "registry.k8s.io/pause:3.7"
kataDirectVolumesDir = "/run/kata-containers/shared/direct-volumes"
volumeTargetPathKey = "io.confidentialcontainers.org.peerpodvolumes.target_path"
csiPluginEscapeQualifiedName = "kubernetes.io~csi"
imageGuestPull = "image_guest_pull"
)
func newProxyService(dialer func(context.Context) (net.Conn, error), pauseImage string) *proxyService {
redirector := agentproto.NewRedirector(dialer)
return &proxyService{
Redirector: redirector,
pauseImage: pauseImage,
}
}
// AgentServiceService methods
func (s *proxyService) CreateContainer(ctx context.Context, req *pb.CreateContainerRequest) (*emptypb.Empty, error) {
var pullImageInGuest bool
logger.Printf("CreateContainer: containerID:%s", req.ContainerId)
if len(req.OCI.Mounts) > 0 {
logger.Print(" mounts:")
for i, m := range req.OCI.Mounts {
logger.Printf(" destination:%s source:%s type:%s", m.Destination, m.Source, m.Type)
if isNodePublishVolumeTargetPath(m.Source, kataDirectVolumesDir) {
if i > 0 {
req.OCI.Annotations[volumeTargetPathKey] += ","
}
req.OCI.Annotations[volumeTargetPathKey] += m.Source
}
}
}
if len(req.OCI.Annotations) > 0 {
logger.Print(" annotations:")
for k, v := range req.OCI.Annotations {
logger.Printf(" %s: %s", k, v)
}
}
if len(req.Storages) > 0 {
logger.Print(" storages:")
for _, s := range req.Storages {
logger.Printf(" mount_point:%s source:%s fstype:%s driver:%s", s.MountPoint, s.Source, s.Fstype, s.Driver)
// remote-snapshotter in contanerd appends image_guest_pull drivers for image layer will be pulled in guest.
// Image will be pull in guest via image-rs according to the driver info.
if s.Driver == imageGuestPull {
pullImageInGuest = true
}
}
}
if len(req.Devices) > 0 {
logger.Print(" devices:")
for _, d := range req.Devices {
logger.Printf(" container_path:%s vm_path:%s type:%s", d.ContainerPath, d.VmPath, d.Type)
}
}
if !pullImageInGuest {
logger.Printf("Pulling image separately not support on main. It is required to use the nydus-snapshotter, which isn't configured properly here.")
}
res, err := s.Redirector.CreateContainer(ctx, req)
if err != nil {
logger.Printf("CreateContainer fails: %v", err)
}
return res, err
}
func isNodePublishVolumeTargetPath(volumePath, directVolumesDir string) bool {
if !strings.Contains(filepath.Clean(volumePath), "/volumes/"+csiPluginEscapeQualifiedName+"/") {
return false
}
volumeDir := filepath.Join(directVolumesDir, b64.URLEncoding.EncodeToString([]byte(volumePath)))
_, err := os.Stat(volumeDir)
return err == nil
}
func (s *proxyService) SetPolicy(ctx context.Context, req *pb.SetPolicyRequest) (*emptypb.Empty, error) {
logger.Printf("SetPolicy: policy:%s", req.Policy)
res, err := s.Redirector.SetPolicy(ctx, req)
if err != nil {
logger.Printf("SetPolicy fails: %v", err)
}
return res, err
}
func (s *proxyService) StartContainer(ctx context.Context, req *pb.StartContainerRequest) (*emptypb.Empty, error) {
logger.Printf("StartContainer: containerID:%s", req.ContainerId)
res, err := s.Redirector.StartContainer(ctx, req)
if err != nil {
logger.Printf("StartContainer fails: %v", err)
}
return res, err
}
func (s *proxyService) RemoveContainer(ctx context.Context, req *pb.RemoveContainerRequest) (*emptypb.Empty, error) {
logger.Printf("RemoveContainer: containerID:%s", req.ContainerId)
res, err := s.Redirector.RemoveContainer(ctx, req)
if err != nil {
logger.Printf("RemoveContainer fails: %v", err)
}
return res, err
}
func (s *proxyService) CreateSandbox(ctx context.Context, req *pb.CreateSandboxRequest) (*emptypb.Empty, error) {
logger.Printf("CreateSandbox: hostname:%s sandboxId:%s", req.Hostname, req.SandboxId)
if len(req.Storages) > 0 {
logger.Print(" storages:")
for _, s := range req.Storages {
logger.Printf(" mountpoint:%s source:%s fstype:%s driver:%s", s.MountPoint, s.Source, s.Fstype, s.Driver)
}
}
res, err := s.Redirector.CreateSandbox(ctx, req)
if err != nil {
logger.Printf("CreateSandbox fails: %v", err)
}
return res, err
}
func (s *proxyService) DestroySandbox(ctx context.Context, req *pb.DestroySandboxRequest) (*emptypb.Empty, error) {
logger.Printf("DestroySandbox")
res, err := s.Redirector.DestroySandbox(ctx, req)
if err != nil {
logger.Printf("DestroySandbox fails: %v", err)
}
return res, err
}