-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.go
More file actions
135 lines (107 loc) · 3.77 KB
/
main.go
File metadata and controls
135 lines (107 loc) · 3.77 KB
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
package main
import (
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"k8s.io/component-base/cli"
"k8s.io/klog/v2"
"github.com/stackitcloud/cloud-provider-stackit/pkg/csi"
"github.com/stackitcloud/cloud-provider-stackit/pkg/csi/blockstorage"
"github.com/stackitcloud/cloud-provider-stackit/pkg/csi/util/mount"
"github.com/stackitcloud/cloud-provider-stackit/pkg/stackit"
"github.com/stackitcloud/cloud-provider-stackit/pkg/stackit/metadata"
"github.com/stackitcloud/cloud-provider-stackit/pkg/version"
)
var (
endpoint string
cloudConfig string
cluster string
httpEndpoint string
provideControllerService bool
provideNodeService bool
legacyStorageMode bool
)
func main() {
cmd := &cobra.Command{
Use: "stackit-csi-plugin",
Short: "STACKIT block-storage CSI plugin",
Run: func(_ *cobra.Command, _ []string) {
handle()
},
PersistentPreRunE: func(cmd *cobra.Command, _ []string) error {
f := cmd.Flags()
if !provideControllerService {
return nil
}
configs, err := f.GetString("cloud-config")
if err != nil {
return err
}
if configs == "" {
return fmt.Errorf("unable to mark flag cloud-config to be required")
}
return nil
},
Version: version.Version,
}
csi.AddPVCFlags(cmd)
cmd.PersistentFlags().StringVar(&endpoint, "endpoint", "", "CSI endpoint")
if err := cmd.MarkPersistentFlagRequired("endpoint"); err != nil {
klog.Fatalf("Unable to mark flag endpoint to be required: %v", err)
}
cmd.Flags().StringVar(&cloudConfig, "cloud-config", "", "CSI driver cloud config. This option can be given multiple times")
cmd.PersistentFlags().StringVar(&cluster, "cluster", "", "The identifier of the cluster that the plugin is running in.")
cmd.PersistentFlags().StringVar(&httpEndpoint, "http-endpoint", "",
"The TCP network address where the HTTP server for providing metrics for diagnostics, will listen (example: `:8080`)."+
"The default is empty string, which means the server is disabled.")
cmd.PersistentFlags().BoolVar(&provideControllerService, "provide-controller-service", true,
"If set to true then the CSI driver does provide the controller service (default: true)")
cmd.PersistentFlags().BoolVar(&provideNodeService, "provide-node-service", true,
"If set to true then the CSI driver does provide the node service (default: true)")
cmd.PersistentFlags().BoolVar(&legacyStorageMode, "legacy-storage-mode", false,
"Configures the CSI to listen to the legacy storage driverName cinder.csi.openstack.org instead")
stackit.AddExtraFlags(pflag.CommandLine)
code := cli.Run(cmd)
os.Exit(code)
}
func handle() {
// Initialize cloud
driverOpts := &blockstorage.DriverOpts{
Endpoint: endpoint,
ClusterID: cluster,
PVCLister: csi.GetPVCLister(),
}
if legacyStorageMode {
driverOpts.LegacyDriverName = true
}
d := blockstorage.NewDriver(driverOpts)
if provideControllerService {
var err error
cfg, err := stackit.GetConfigFromFile(cloudConfig)
if err != nil {
klog.Fatal(err)
}
iaasClient, err := stackit.CreateIaaSClient(&cfg)
if err != nil {
klog.Fatalf("Failed to create IaaS client: %v", err)
}
stackitProvider, err := stackit.CreateSTACKITProvider(iaasClient, &cfg)
if err != nil {
klog.Fatalf("Failed to create STACKIT provider: %v", err)
}
d.SetupControllerService(stackitProvider)
}
if provideNodeService {
// Initialize mount
mountProvider := mount.GetMountProvider()
cfg, err := stackit.GetConfigFromFile(cloudConfig)
if err != nil {
klog.Fatal(err)
}
// Initialize Metadata
metadataProvider := metadata.GetMetadataProvider(fmt.Sprintf("%s,%s", metadata.MetadataID, metadata.ConfigDriveID))
d.SetupNodeService(mountProvider, metadataProvider, cfg.BlockStorage)
}
d.Run()
}