-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.go
169 lines (140 loc) · 4.78 KB
/
start.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
// Copyright (c) 2022 Fujitsu Limited
package volumectl
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
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 startOptions struct {
PodNamespace string
PodName string
DataContainerName string
PersistentVolumeClaimNamespace string
PersistentVolumeClaimName string
AllowedExecutablesFilePath string
MessageQueueFilePath string
}
func newStartOptions() *startOptions {
return &startOptions{
PodNamespace: metav1.NamespaceDefault,
}
}
func addStartFlags(cmd *cobra.Command, so *startOptions) {
cmd.Flags().StringVar(
&so.PodNamespace, "pod-namespace", so.PodNamespace,
"Namespace of the volume-controlled pod.")
cmd.Flags().StringVar(
&so.PodName, "pod-name", so.PodName,
"Names of the volume-controlled pod.")
cmd.MarkFlagRequired("pod-name")
cmd.Flags().StringVar(
&so.DataContainerName, "data-container-name", so.DataContainerName,
"Name of the data container.")
cmd.MarkFlagRequired("data-container-name")
cmd.Flags().StringVar(
&so.PersistentVolumeClaimNamespace, "pvc-namespace",
so.PersistentVolumeClaimNamespace,
"Namespace of the volume-controlled persistent volume claim. "+
"It can be ommitted if the same pod one")
cmd.Flags().StringVar(
&so.PersistentVolumeClaimName, "pvc-name",
so.PersistentVolumeClaimName,
"Name of the volume-controlled persistent volume claim.")
cmd.MarkFlagRequired("pvc-name")
cmd.Flags().StringVar(
&so.AllowedExecutablesFilePath, "allowed-executables-file-path",
so.AllowedExecutablesFilePath,
"Path to the file including executable checksums per 'datalifetime'.")
cmd.Flags().StringVar(
&so.MessageQueueFilePath, "message-queue-file-path",
so.MessageQueueFilePath,
"Path to the file including the configuration of a message queue "+
"publisher.")
}
func getAllowedExecutablesConfigurationFromJsonFile(
executablesFile string) (map[string]*api.Executable, error) {
executablesBytes, err := ioutil.ReadFile(executablesFile)
if err != nil {
return nil, fmt.Errorf(
"Could not read %s: %v", executablesFile, err.Error())
}
executables := map[string]*api.Executable{}
err = json.Unmarshal(executablesBytes, &executables)
if err != nil {
return nil, fmt.Errorf(
"Could not convert %q (the JSON format) into "+
"'LifetimeExecutable' struct",
executablesBytes)
}
return executables, nil
}
func getMessageQueueUpdatePublisherConfigurationFromJsonFile(
messageQueueFile string) (*api.MessageQueue, error) {
messageQueueBytes, err := ioutil.ReadFile(messageQueueFile)
if err != nil {
return nil, fmt.Errorf(
"Could not read %s: %v", messageQueueFile, err.Error())
}
messageQueue := api.MessageQueue{}
err = json.Unmarshal(messageQueueBytes, &messageQueue)
if err != nil {
return nil, fmt.Errorf(
"Could not convert %q (the JSON format) into "+
"'MessageQueue' struct",
messageQueueBytes)
}
return &messageQueue, nil
}
func newStartCommand() *cobra.Command {
opts := newStartOptions()
startCmd := &cobra.Command{
Use: "start",
Long: `the shadowy volumectl sends the 'start' request to ` +
`the shadowy volume controller in order to set up information ` +
`on target containers and binaries`,
Run: func(cmd *cobra.Command, args []string) {
util.PrintFlags(cmd.Flags())
volumeControllerEndpoint := getVolumeControllerEndpoint(cmd, args)
pvcNamespace := opts.PersistentVolumeClaimNamespace
if pvcNamespace == "" {
pvcNamespace = opts.PodNamespace
}
pvcKey := util.ConcatenateNamespaceAndName(
pvcNamespace, opts.PersistentVolumeClaimName)
executables, err := getAllowedExecutablesConfigurationFromJsonFile(
opts.AllowedExecutablesFilePath)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err.Error())
os.Exit(1)
}
messageQueue, err := getMessageQueueUpdatePublisherConfigurationFromJsonFile(
opts.AllowedExecutablesFilePath)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err.Error())
os.Exit(1)
}
if messageQueue.MaxBatchBytes == "" {
messageQueue.MaxBatchBytes = "0"
}
ucClient := volumecontrol.NewClient(volumeControllerEndpoint)
mountPoints, err := ucClient.Start(
opts.PodNamespace, opts.PodName, pvcKey, executables,
opts.DataContainerName, messageQueue)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err.Error())
os.Exit(1)
}
fmt.Fprintf(
os.Stdout,
"Target fuse mount points: %+v", mountPoints)
},
}
addStartFlags(startCmd, opts)
return startCmd
}