/
main.go
83 lines (70 loc) · 2.65 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
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package main
import (
"fmt"
"os"
"strings"
"github.com/elastic/k8s-operators/local-volume/pkg/driver/daemon"
"github.com/elastic/k8s-operators/local-volume/pkg/driver/daemon/cmdutil"
"github.com/elastic/k8s-operators/local-volume/pkg/driver/daemon/drivers"
"github.com/elastic/k8s-operators/local-volume/pkg/driver/daemon/drivers/bindmount"
"github.com/elastic/k8s-operators/local-volume/pkg/driver/daemon/drivers/lvm"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
const (
driverKindFlag = "driver-kind"
lvmVolumeGroupFlag = "lvm-volume-group"
lvmUseThinVolumesFlag = "lvm-use-thin-volumes"
lvmThinPoolFlag = "lvm-thin-pool"
nodeNameFlag = "node-name"
)
var rootCmd = &cobra.Command{
Short: "Run the local volume driver daemon",
Run: func(cmd *cobra.Command, args []string) {
nodeName := viper.GetString(nodeNameFlag)
if nodeName == "" {
log.Fatal("$NODE_NAME should be set by referencing spec.nodeName")
}
driverKind := viper.GetString(driverKindFlag)
driverOpts := drivers.Options{
BindMount: bindmount.Options{
Factory: cmdutil.NewExecutableFactory(),
MountPath: bindmount.DefaultContainerMountPath,
},
LVM: lvm.Options{
ExecutableFactory: cmdutil.NewExecutableFactory(),
VolumeGroupName: viper.GetString(lvmVolumeGroupFlag),
UseThinVolumes: viper.GetBool(lvmUseThinVolumesFlag),
ThinPoolName: viper.GetString(lvmThinPoolFlag),
},
}
server, err := daemon.NewServer(nodeName, driverKind, driverOpts)
if err != nil {
log.WithError(err).Fatal("Cannot create driver daemon")
}
log.Fatal(server.Start())
},
}
func main() {
flags := rootCmd.Flags()
// Driver kind
flags.String(driverKindFlag, lvm.DriverKind, "Driver kind (eg. LVM or BINDMOUNT)")
// LVM flags
flags.String(lvmVolumeGroupFlag, lvm.DefaultVolumeGroup, "LVM Volume Group to be used for provisioning logical volumes")
flags.Bool(lvmUseThinVolumesFlag, lvm.DefaultUseThinVolumes, "Use LVM thin volumes")
flags.String(lvmThinPoolFlag, lvm.DefaultThinPoolName, "LVM thin pool name")
// node name should be passed to the environment through the yaml spec
flags.String(nodeNameFlag, "", "Name of the node this pod is running on")
// Bind flags to environment variables
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
viper.AutomaticEnv()
viper.BindPFlags(flags)
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}