This repository has been archived by the owner on May 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
242 lines (210 loc) · 7.33 KB
/
main.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
package main
import (
"fmt"
"os"
"os/signal"
"path/filepath"
"syscall"
"github.com/jawher/mow.cli"
zfs "github.com/mistifyio/go-zfs"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
clientset "k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"sigs.k8s.io/sig-storage-lib-external-provisioner/controller"
)
var (
Version = "0.0.1"
DefaultProvisionerName = "asteven/local-zfs"
DefaultNamespace = "kube-system"
DefaultDatasetMountDir = "/var/lib/local-zfs-provisioner"
DefaultContainerImage = "asteven/local-zfs-provisioner"
)
func RegisterShutdownChannel(done chan struct{}) {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
sig := <-sigs
logrus.Infof("Receive %v to exit", sig)
close(done)
}()
}
func startController(configFile string, datasetMountDir string, provisionerName string, namespace string) error {
stopCh := make(chan struct{})
RegisterShutdownChannel(stopCh)
config, err := rest.InClusterConfig()
if err != nil {
return errors.Wrap(err, "unable to get client config")
}
kubeClient, err := clientset.NewForConfig(config)
if err != nil {
return errors.Wrap(err, "unable to get k8s client")
}
if kubeClient != nil {
logrus.Debug("kubeClient FTW")
}
serverVersion, err := kubeClient.Discovery().ServerVersion()
if err != nil {
return errors.Wrap(err, "Cannot start Provisioner: failed to get Kubernetes server version")
}
nodeName := os.Getenv("NODE_NAME")
if nodeName == "" {
return fmt.Errorf("NODE_NAME environment variable not set")
}
containerImage := os.Getenv("CONTAINER_IMAGE")
if containerImage == "" {
containerImage = "DefaultContainerImage"
}
fmt.Println("configFile: ", configFile)
fmt.Println("datasetMountDir: ", datasetMountDir)
fmt.Println("provisionerName: ", provisionerName)
fmt.Println("namespace: ", namespace)
fmt.Println("containerImage: ", containerImage)
provisioner, err := NewProvisioner(stopCh, kubeClient, configFile, datasetMountDir, provisionerName, namespace, nodeName, containerImage)
if err != nil {
return err
}
pc := controller.NewProvisionController(
kubeClient,
provisionerName,
provisioner,
serverVersion.GitVersion,
)
logrus.Debug("Provisioner started")
pc.Run(stopCh)
logrus.Debug("Provisioner stopped")
return nil
}
func main() {
logrus.SetFormatter(&logrus.TextFormatter{FullTimestamp: true})
app := cli.App("local-zfs-provisioner", "Local ZFS Provisioner")
app.Version("version", Version)
app.Spec = "[-d]"
var (
debug = app.BoolOpt("d debug", false, "enable debug logging level")
)
app.Before = func() {
if *debug {
logrus.SetLevel(logrus.DebugLevel)
}
}
app.Command("controller", "start controller", func(cmd *cli.Cmd) {
var (
configFile = cmd.StringOpt("config", "", "Provisioner configuration file.")
datasetMountDir = cmd.StringOpt("dataset-mount-dir", DefaultDatasetMountDir, "Directory under which to mount the created persistent volumes.")
provisionerName = cmd.String(cli.StringOpt{
Name: "provisioner",
Value: DefaultProvisionerName,
Desc: "Specify Provisioner name.",
EnvVar: "PROVISIONER_NAME",
})
namespace = cmd.String(cli.StringOpt{
Name: "namespace",
Value: DefaultNamespace,
Desc: "The namespace that Provisioner is running in.",
EnvVar: "NAMESPACE",
})
)
cmd.Spec = "--config [--dataset-mount-dir] [--provisioner] [--namespace]"
cmd.Action = func() {
if *configFile == "" {
logrus.Fatalf("invalid empty flag %v", "config")
}
if *datasetMountDir == "" {
logrus.Fatalf("invalid empty flag %v", "datasetMountDir")
}
if *provisionerName == "" {
logrus.Fatalf("invalid empty flag %v", "provisioner")
}
if *namespace == "" {
logrus.Fatalf("invalid empty flag %v", "namespace")
}
if err := startController(*configFile, *datasetMountDir, *provisionerName, *namespace); err != nil {
logrus.Fatalf("Error starting provisioner: %v", err)
}
}
})
app.Command("dataset", "manage datasets", func(datasetCmd *cli.Cmd) {
datasetCmd.Command("create", "create dataset", func(cmd *cli.Cmd) {
var (
datasetName = cmd.StringArg("DATASET", "", "Name of the dataset")
mountPoint = cmd.StringArg("MOUNTPOINT", "", "Mountpoint of the dataset")
quota = cmd.StringOpt("quota", "", "Quota of the dataset")
parent = cmd.StringOpt("parent", "", "Parent dataset under which to create datasets")
)
cmd.Spec = "--parent [--quota] DATASET MOUNTPOINT"
cmd.Action = func() {
fmt.Println("dataset: ", *datasetName)
fmt.Println("mountpoint: ", *mountPoint)
fmt.Println("parent: ", *parent)
fmt.Println("quota: ", *quota)
// Ensure parent dataset exists.
_, err := zfs.GetDataset(*parent)
if err != nil {
_, err = zfs.CreateFilesystem(*parent, map[string]string{
"mountpoint": "legacy",
})
if err != nil {
logrus.Fatalf("Failed to create parent dataset %v: %v", *parent, err)
}
}
zfsCreateProperties := map[string]string{
"quota": *quota,
"mountpoint": *mountPoint,
}
// TODO: could the dataset already exist? Would that be a valid use case?
// e.g. depending on the PersistentVolumeReclaimPolicy?
//dataset, err = zfs.CreateFilesystem(datasetName, zfsCreateProperties)
_, err = zfs.CreateFilesystem(*datasetName, zfsCreateProperties)
if err != nil {
logrus.Fatalf("Failed to create dataset %v: %v", *datasetName, err)
}
// It seems the dataset is mounted at creation time.
// We do not have to explicitly mount it.
//dataset, err = dataset.Mount(false, nil)
//if err != nil {
// logrus.Fatalf("Failed to mount dataset %v at %v: %v", *datasetName, *mountPoint, err)
// return nil, err
//}
// Ensure safe permissions on the directory containing the mount point.
// This allows us to create the actual mountpoint with the otherwise
// insecure mode 0777 which again ensures that the pod owning the volume
// can write there in any case.
parentMountPoint := filepath.Dir(*mountPoint)
if err := os.Chmod(parentMountPoint, 0700); err != nil {
logrus.Fatalf("Failed to set permissions on base mountpoint %v: %v", parentMountPoint, err)
}
// Ensure the dataset is writable by the pod.
if err := os.Chmod(*mountPoint, 0777); err != nil {
logrus.Fatalf("Failed to set permissions on mountpoint %v: %v", *mountPoint, err)
}
logrus.Infof("Created and mounted dataset %v at %v", *datasetName, *mountPoint)
}
})
datasetCmd.Command("destroy", "destroy dataset", func(cmd *cli.Cmd) {
var (
datasetName = cmd.StringArg("DATASET", "", "Name of the dataset")
mountPoint = cmd.StringArg("MOUNTPOINT", "", "Mountpoint of the dataset")
)
cmd.Spec = "DATASET MOUNTPOINT"
cmd.Action = func() {
fmt.Println("dataset: ", *datasetName)
fmt.Println("mountpoint: ", *mountPoint)
// Destroy the dataset.
dataset, err := zfs.GetDataset(*datasetName)
if err != nil {
// TODO: should this error be ingored?
logrus.Fatalf("Failed to get dataset %v: %v", *datasetName, err)
}
err = dataset.Destroy(zfs.DestroyDefault)
if err != nil {
logrus.Fatalf("Failed to delete dataset %v: %v", *datasetName, err)
}
// Also delete the mountpoint.
os.Remove(*mountPoint)
logrus.Infof("Deleted dataset %v", *datasetName)
}
})
})
app.Run(os.Args)
}