-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.go
112 lines (91 loc) · 3.31 KB
/
init.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
// Copyright (c) 2022 Fujitsu Limited
package volumectl
import (
"encoding/json"
"fmt"
"os"
"github.com/spf13/cobra"
api "github.com/FujitsuResearch/data-sharing-controllers-on-k8s/pkg/apis/grpc/volumecontrol/v1alpha1"
"github.com/FujitsuResearch/data-sharing-controllers-on-k8s/pkg/client/grpc/volumecontrol"
"github.com/FujitsuResearch/data-sharing-controllers-on-k8s/pkg/util"
)
type initOptions struct {
volumeSourcePath string
externalAllowedExecutables string
localFuseMountsHostRootDirectory string
csiVolumeHandle string
disableUsageControl bool
}
func newInitOptions() *initOptions {
return &initOptions{}
}
func getExternalAllowedExecutables(
allowedExecutablesString string) (
[]*api.ExternalAllowedExecutable, error) {
allowedExecutablesBytes := []byte(allowedExecutablesString)
allowedExecutables := []*api.ExternalAllowedExecutable{}
err := json.Unmarshal(allowedExecutablesBytes, &allowedExecutables)
if err != nil {
return nil, fmt.Errorf(
"Could not convert %q (the JSON format) into "+
"'AllowedExecutable' struct",
allowedExecutablesBytes)
}
return allowedExecutables, nil
}
func addInitFlags(cmd *cobra.Command, io *initOptions) {
cmd.Flags().StringVar(
&io.volumeSourcePath, "volume-source-path", io.volumeSourcePath,
"Path to the controlled volume. "+
"This path is used as a fuse-mount source path.")
cmd.MarkFlagRequired("volume-source-path")
cmd.Flags().StringVar(
&io.externalAllowedExecutables, "external-allowed-executables",
io.externalAllowedExecutables,
"External executables allowed to access for the fuse-mount point "+
"in the JSON format.")
cmd.Flags().StringVar(
&io.localFuseMountsHostRootDirectory,
"fuse-mounts-host-root-directory", io.localFuseMountsHostRootDirectory,
"Name of the fuse-mount points root directory on the host. "+
"This flag is required when the local persistent volume is used.")
cmd.Flags().StringVar(
&io.csiVolumeHandle, "volume-handle", io.csiVolumeHandle,
"Name of the CSI volume handle. "+
"This flag is required when the CSI volume is used.")
cmd.Flags().BoolVar(
&io.disableUsageControl, "disable-usage-control", io.disableUsageControl,
"Disable the usage control for file system data.")
}
func newInitCommand() *cobra.Command {
opts := newInitOptions()
initCmd := &cobra.Command{
Use: "init",
Long: `the shadowy volumectl sends the 'init' request to ` +
`the shadowy volume controller in order to fuse-mount a volume`,
Run: func(cmd *cobra.Command, args []string) {
util.PrintFlags(cmd.Flags())
volumeControllerEndpoint := getVolumeControllerEndpoint(cmd, args)
allowedExecutables, err := getExternalAllowedExecutables(
opts.externalAllowedExecutables)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err.Error())
os.Exit(1)
}
ucClient := volumecontrol.NewClient(volumeControllerEndpoint)
fuseMountPointPath, err := ucClient.Initialize(
opts.volumeSourcePath, allowedExecutables,
opts.csiVolumeHandle, opts.localFuseMountsHostRootDirectory,
opts.disableUsageControl)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err.Error())
os.Exit(1)
}
fmt.Fprintf(
os.Stdout,
"Path to the fuse mount point: %s\n", fuseMountPointPath)
},
}
addInitFlags(initCmd, opts)
return initCmd
}