-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
154 lines (130 loc) · 4.04 KB
/
main.cpp
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
#include <iostream>
#include <getopt.h>
#include <loguru/loguru.hpp>
#include <HostPathCPPConfig.h>
#include <utils.h>
#include <csi_services.h>
using namespace std;
using namespace utils;
using namespace hostpath;
using namespace csi::services;
void Usage(int retVal)
{
cout
<< "Usage: ./hostpath [OPTIONS]\n"
<< "\n"
<< "\t--help Print this help information and exit\n"
<< "\t--version Print CSI HostPath version and exit\n"
<< "\t--endpoint=ENDPOINT CSI endpoint (default='unix://tmp/csi.sock')\n"
<< "\t--nodeid=NODENAME node id\n"
<< "\t--state-dir=DIRECTORY directory for storing state information across driver restarts, volumes and snapshots (default=/csi-data-dir)"
<< endl;
exit(retVal);
}
Config ParseCommandLine(int argc, char *argv[])
{
struct option opts[] = {
{"help", no_argument, 0, 0},
{"version", no_argument, 0, 1},
{"endpoint", required_argument, 0, 'e'},
{"nodeid", required_argument, 0, 'n'},
{"driver-name", required_argument, 0, 'd'},
{"state-dir", required_argument, 0, 's'},
{0, 0, 0, 0}};
Config config;
while (true)
{
int optionIndex = 0;
int c = getopt_long(argc, argv, "e:n:", opts, &optionIndex);
if (c == -1)
break;
switch (c)
{
case 0:
Usage(0);
case 1:
cout
<< HostPathCPP_VERSION_MAJOR << "."
<< HostPathCPP_VERSION_MINOR << "."
<< HostPathCPP_VERSION_PATCH
<< endl;
exit(0);
case 'e':
config.set_endpoint(string(optarg));
break;
case 'n':
config.set_node_name(string(optarg));
break;
case 'd':
config.set_driver_name(string(optarg));
break;
case 's':
config.set_state_directory(string(optarg));
break;
case '?':
cout << "Invalid option provided" << endl;
Usage(1);
}
}
return config;
}
bool ValidateConfig(Config &config)
{
if (config.node_name().empty())
{
LOG_F(ERROR, "Node name cannot be empty. Please provide node name with '--nodeid' input");
return false;
}
else if (!IsNameValid(config.node_name()))
{
LOG_F(ERROR, "Invalid node name '%s'", config.node_name().c_str());
return false;
}
if (config.endpoint().empty())
{
LOG_F(INFO, "Endpoint not provided. Using endpoint 'unix://tmp/csi.sock'");
config.set_endpoint("unix://tmp/csi.sock");
}
else
{
if (!IsUnixSocket(config.endpoint()) && !IsValidIPaddress(config.endpoint()))
{
LOG_F(ERROR, "Invalid endpoint address'%s'", config.endpoint().c_str());
return false;
}
}
if (config.driver_name().empty())
{
LOG_F(INFO, "CSI driver name not provided. Using default drivername 'hostpath-cpp.csi.k8s.io'");
config.set_driver_name("hostpath-cpp.csi.k8s.io");
}
if (config.state_directory().empty())
{
config.set_state_directory("/csi-data-dir");
}
if (!DirectoryExists(config.state_directory()) && !CreateDirectory(config.state_directory()))
{
LOG_F(ERROR, "Failed to create state directory '%s'", config.state_directory().c_str());
return false;
}
auto [capacity, available] = GetDirectorySpace(config.state_directory());
config.set_max_capacity(capacity);
config.set_vendor_version(to_string(HostPathCPP_VERSION_MAJOR) + "." + to_string(HostPathCPP_VERSION_MINOR) + "." + to_string(HostPathCPP_VERSION_PATCH));
return true;
}
void InitializeLogger(int argc, char *argv[])
{
loguru::init(argc, argv);
}
int main(int argc, char *argv[])
{
auto config = ParseCommandLine(argc, argv);
InitializeLogger(argc, argv);
if (!ValidateConfig(config))
{
exit(1);
}
CSIServices csiService(config);
csiService.Run();
return 0;
}