/
output.go
324 lines (265 loc) · 9.35 KB
/
output.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// Copyright (c) 2022 Fujitsu Limited
package filesystem
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/klog/v2"
lifetimesapi "github.com/FujitsuResearch/data-sharing-controllers-on-k8s/pkg/apis/cr/lifetimes/v1alpha1"
volumecontrolapi "github.com/FujitsuResearch/data-sharing-controllers-on-k8s/pkg/apis/grpc/volumecontrol/v1alpha1"
coreclientset "github.com/FujitsuResearch/data-sharing-controllers-on-k8s/pkg/client/cr/core/clientset/versioned"
"github.com/FujitsuResearch/data-sharing-controllers-on-k8s/pkg/client/grpc/volumecontrol"
"github.com/FujitsuResearch/data-sharing-controllers-on-k8s/pkg/lifetimes/storage"
lifetimesvolumectl "github.com/FujitsuResearch/data-sharing-controllers-on-k8s/pkg/lifetimes/volumecontrol"
"github.com/FujitsuResearch/data-sharing-controllers-on-k8s/pkg/messagequeue/publish"
"github.com/FujitsuResearch/data-sharing-controllers-on-k8s/pkg/util"
volumepub "github.com/FujitsuResearch/data-sharing-controllers-on-k8s/pkg/volume/messagequeue/publish"
)
var (
_ = (storage.LifetimeOutputOperations)((*FileSystemOutputLifetimer)(nil))
)
type outputFileSpec struct {
path string
}
type mountPointInfo struct {
mountPoint string
usageControlEnabled bool
messageQueuePublisherEnabled bool
}
type FileSystemOutputLifetimer struct {
fileSpec *outputFileSpec
allowedExecutables []*lifetimesapi.AllowedExecutable
messageQueueInitPublisher *volumepub.VolumeMessageQueueInitPublisher
messageQueueUpdatePublisherConfig *volumecontrolapi.MessageQueue
volumeController *volumeController
volumeControlPvcKeyToMountPoint map[string]*volumecontrolapi.StartResponse
lifetimeStatus *lifetimesapi.ConsumerStatus
}
func createOutputFileSpec(
fileSystemSpec *lifetimesapi.OutputFileSystemSpec,
pod *corev1.Pod, dataContainerName string) (*outputFileSpec, error) {
err := hasPersistentVolumeClaim(
fileSystemSpec.PersistentVolumeClaimRef.Name, pod)
if err != nil {
return nil, err
}
container, found := findDataCntainer(pod, dataContainerName)
if !found {
return nil, fmt.Errorf(
"The pod does not have the data contaniner %q", dataContainerName)
}
volumeMountToIndex := createPersistentVolumeClaimToIndex(
pod.Spec.Volumes, container)
outputPvcName := fileSystemSpec.PersistentVolumeClaimRef.Name
index, ok := volumeMountToIndex[outputPvcName]
if !ok {
return nil, fmt.Errorf(
"The data container %q does not have the output persistent "+
"volume claim %q",
dataContainerName, outputPvcName)
}
return &outputFileSpec{
path: container.VolumeMounts[index].MountPath,
}, nil
}
func NewFileSystemLifetimerForOutputData(
fileSystemSpec *lifetimesapi.OutputFileSystemSpec, pod *corev1.Pod,
dataClient coreclientset.Interface, kubeClient kubernetes.Interface,
messageQueueSpec *lifetimesapi.PublishMessageQueueSpec,
messageQueuePublisherOptions *publish.MessageQueuePublisherOptions,
disableUsageControl bool, volumeControllerEndpoint string,
podNamespace string, podName string, dataContainerName string) (
*FileSystemOutputLifetimer, error) {
outputFilespec, err := createOutputFileSpec(
fileSystemSpec, pod, dataContainerName)
if err != nil {
return nil, err
}
pvcKey := util.ConcatenateNamespaceAndName(
fileSystemSpec.PersistentVolumeClaimRef.Namespace,
fileSystemSpec.PersistentVolumeClaimRef.Name)
lifetimer := &FileSystemOutputLifetimer{
fileSpec: outputFilespec,
allowedExecutables: fileSystemSpec.AllowedExecutables,
volumeController: &volumeController{
disableUsageControl: disableUsageControl,
endpoint: volumeControllerEndpoint,
podNamespace: podNamespace,
podName: podName,
persistentVolumeClaimKey: pvcKey,
dataContainerName: dataContainerName,
},
volumeControlPvcKeyToMountPoint: map[string]*volumecontrolapi.StartResponse{},
}
if messageQueueSpec != nil {
initPublisher, err := volumepub.NewVolumeMessageQueueInitPublisher(
fileSystemSpec, messageQueuePublisherOptions, dataClient, kubeClient,
messageQueueSpec.MessageQueueSpec, outputFilespec.path)
if err != nil {
return nil, err
}
updatePublisherConfig, err := volumepub.
CreateVolumeMessageQueueUpdatePublisherConfiguration(
fileSystemSpec, dataClient, kubeClient, messageQueueSpec,
messageQueuePublisherOptions)
if err != nil {
return nil, err
}
lifetimer.messageQueueInitPublisher = initPublisher
lifetimer.
messageQueueUpdatePublisherConfig = updatePublisherConfig
}
return lifetimer, nil
}
func (fsol *FileSystemOutputLifetimer) Start(ctx context.Context) error {
executables := map[string]*volumecontrolapi.Executable(nil)
if !fsol.volumeController.disableUsageControl {
var err error
executables, err = lifetimesvolumectl.CreateGrpcAllowedExecutables(
fsol.allowedExecutables)
if err != nil {
return err
}
} else if fsol.messageQueueUpdatePublisherConfig == nil {
return nil
}
ucClient := volumecontrol.NewClient(fsol.volumeController.endpoint)
klog.Infof(
"Send 'start' request to the volume controller: %+v", executables)
mountPointInfo, err := ucClient.Start(
fsol.volumeController.podNamespace, fsol.volumeController.podName,
fsol.volumeController.persistentVolumeClaimKey,
executables, fsol.volumeController.dataContainerName,
fsol.messageQueueUpdatePublisherConfig)
if err != nil {
return fmt.Errorf(
"Failed the 'start' of the volume cntroller client: %v\n",
err.Error())
}
if mountPointInfo.UsageControlEnabled !=
!fsol.volumeController.disableUsageControl {
return fmt.Errorf(
"The configuration of the Usage control (%t) does not match "+
"the return value of the volume controller 'Start()' (%t)",
!fsol.volumeController.disableUsageControl,
mountPointInfo.UsageControlEnabled)
}
pvcKey := fsol.volumeController.persistentVolumeClaimKey
fsol.volumeControlPvcKeyToMountPoint[pvcKey] = mountPointInfo
if fsol.messageQueueInitPublisher != nil {
go func(c context.Context) {
klog.Info(
"Start handling initial data through the message " +
"queue publisher")
err := fsol.messageQueueInitPublisher.HandleInitQueueMessages(c)
if err != nil {
klog.Errorf(
"Failed handling about the init message queue: %v",
err.Error())
}
err = fsol.messageQueueInitPublisher.CloseMessageQueues()
if err != nil {
klog.Errorf(
"Failed to close the init message queue: %v", err.Error())
}
}(ctx)
}
klog.Infof("Start the volume controller: %+v", mountPointInfo)
return nil
}
func (fsol *FileSystemOutputLifetimer) createMountPointToExecutablePaths() map[string]*volumecontrolapi.ExecutablePaths {
pvcKey := fsol.volumeController.persistentVolumeClaimKey
mountPoint := fsol.volumeControlPvcKeyToMountPoint[pvcKey]
executablePaths := make([]string, 0, len(fsol.allowedExecutables))
for _, executable := range fsol.allowedExecutables {
executablePaths = append(executablePaths, executable.CmdAbsolutePath)
}
return map[string]*volumecontrolapi.ExecutablePaths{
mountPoint.MountPoint: &volumecontrolapi.ExecutablePaths{
Paths: executablePaths,
},
}
}
func (fsol *FileSystemOutputLifetimer) Stop() error {
pvcKey := fsol.volumeController.persistentVolumeClaimKey
mountPoint, found := fsol.volumeControlPvcKeyToMountPoint[pvcKey]
if !found {
return nil
}
mountPointToExecutablePaths := (map[string]*volumecontrolapi.
ExecutablePaths)(nil)
if mountPoint.UsageControlEnabled {
mountPointToExecutablePaths = fsol.createMountPointToExecutablePaths()
}
stopMountPoint := ""
if mountPoint.MessageQueuePublisherEnabled {
stopMountPoint = mountPoint.MountPoint
}
ucClient := volumecontrol.NewClient(fsol.volumeController.endpoint)
klog.Infof(
"Send 'stop' request to the volume controller: %+v",
mountPointToExecutablePaths)
return ucClient.Stop(
fsol.volumeController.podNamespace, fsol.volumeController.podName,
mountPointToExecutablePaths, stopMountPoint)
}
func (fsol *FileSystemOutputLifetimer) chmodFiles() error {
errs := []string{}
targetPath := fsol.fileSpec.path
dcd := &deletedChmodData{
directories: []string{},
root: targetPath,
}
err := filepath.Walk(targetPath, dcd.chmodWalkFunc)
if err != nil {
errs = append(errs, err.Error())
}
for i := len(dcd.directories) - 1; i >= 0; i-- {
err := os.Chmod(dcd.directories[i], 0000)
if err != nil {
errs = append(errs, err.Error())
}
}
if len(errs) > 0 {
return fmt.Errorf(
"[Failed to change the mode] %s", strings.Join(errs, ", "))
}
return nil
}
func (fsol *FileSystemOutputLifetimer) removeFiles() error {
errs := []string{}
targetPath := fsol.fileSpec.path
drd := &deletedRemoveData{
root: targetPath,
}
err := filepath.Walk(targetPath, drd.removeFilesWalkFunc)
if err != nil {
errs = append(errs, err.Error())
}
if len(errs) > 0 {
return fmt.Errorf(
"[Failed to remove data] %s", strings.Join(errs, ", "))
}
return nil
}
func (fsol *FileSystemOutputLifetimer) Delete(
deletePolicy lifetimesapi.DeletePolicy) error {
switch deletePolicy {
case lifetimesapi.DefaultPolicy:
fallthrough
case lifetimesapi.ChmodPolicy:
return fsol.chmodFiles()
case lifetimesapi.RemovePolicy:
return fsol.removeFiles()
case lifetimesapi.NotifyPolicy:
return fmt.Errorf(
"Not implement the delete method for the '%v' policy",
deletePolicy)
default:
return fmt.Errorf("Not support sutch a delte policy: %v", deletePolicy)
}
}