generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathadvancedview.cpp
306 lines (272 loc) · 11.1 KB
/
advancedview.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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
Copyright 2022 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "advancedview.h"
#include <QComboBox>
#include <QDialog>
#include <QDialogButtonBox>
#include <QFormLayout>
#include <QHeaderView>
#include <QLineEdit>
AdvancedView::AdvancedView(QIcon icon)
{
m_icon = icon;
advancedView = new QWidget();
advancedView->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
ClusterList clusters;
m_clusterModel = new ClusterModel(clusters);
clusterListView = new QTableView();
clusterListView->setModel(m_clusterModel);
clusterListView->setSelectionMode(QAbstractItemView::SingleSelection);
clusterListView->setSelectionBehavior(QAbstractItemView::SelectRows);
clusterListView->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
clusterListView->horizontalHeader()->setSectionResizeMode(1, QHeaderView::ResizeToContents);
clusterListView->horizontalHeader()->setSectionResizeMode(2, QHeaderView::ResizeToContents);
clusterListView->horizontalHeader()->setSectionResizeMode(3, QHeaderView::ResizeToContents);
clusterListView->horizontalHeader()->setSectionResizeMode(4, QHeaderView::ResizeToContents);
clusterListView->horizontalHeader()->setSectionResizeMode(5, QHeaderView::ResizeToContents);
clusterListView->horizontalHeader()->setSectionResizeMode(6, QHeaderView::ResizeToContents);
setSelectedClusterName("default");
startButton = new QPushButton(tr("Start"));
stopButton = new QPushButton(tr("Stop"));
pauseButton = new QPushButton(tr("Pause"));
deleteButton = new QPushButton(tr("Delete"));
refreshButton = new QPushButton(tr("Refresh"));
createButton = new QPushButton(tr("Create"));
dockerEnvButton = new QPushButton("docker-env");
sshButton = new QPushButton("SSH");
dashboardButton = new QPushButton(tr("Dashboard"));
basicButton = new QPushButton(tr("Basic View"));
disableButtons();
QHBoxLayout *topButtonLayout = new QHBoxLayout;
topButtonLayout->addWidget(createButton);
topButtonLayout->addWidget(refreshButton);
topButtonLayout->addWidget(basicButton);
topButtonLayout->addSpacing(340);
QHBoxLayout *bottomButtonLayout = new QHBoxLayout;
bottomButtonLayout->addWidget(startButton);
bottomButtonLayout->addWidget(stopButton);
bottomButtonLayout->addWidget(pauseButton);
bottomButtonLayout->addWidget(deleteButton);
bottomButtonLayout->addWidget(dockerEnvButton);
bottomButtonLayout->addWidget(sshButton);
bottomButtonLayout->addWidget(dashboardButton);
QVBoxLayout *clusterLayout = new QVBoxLayout;
clusterLayout->addLayout(topButtonLayout);
clusterLayout->addWidget(clusterListView);
clusterLayout->addLayout(bottomButtonLayout);
advancedView->setLayout(clusterLayout);
QFont *loadingFont = new QFont();
loadingFont->setPointSize(30);
loading = new QLabel("Loading...");
loading->setFont(*loadingFont);
loading->setParent(clusterListView);
loading->setHidden(true);
connect(startButton, &QAbstractButton::clicked, this, &AdvancedView::start);
connect(stopButton, &QAbstractButton::clicked, this, &AdvancedView::stop);
connect(pauseButton, &QAbstractButton::clicked, this, &AdvancedView::pause);
connect(deleteButton, &QAbstractButton::clicked, this, &AdvancedView::delete_);
connect(refreshButton, &QAbstractButton::clicked, this, &AdvancedView::refresh);
connect(createButton, &QAbstractButton::clicked, this, &AdvancedView::askName);
connect(dockerEnvButton, &QAbstractButton::clicked, this, &AdvancedView::dockerEnv);
connect(sshButton, &QAbstractButton::clicked, this, &AdvancedView::ssh);
connect(dashboardButton, &QAbstractButton::clicked, this, &AdvancedView::dashboard);
connect(basicButton, &QAbstractButton::clicked, this, &AdvancedView::basic);
}
static QString getPauseLabel(bool isPaused)
{
if (isPaused) {
return "Unpause";
}
return "Pause";
}
static QString getStartLabel(bool isRunning, bool isPaused)
{
if (isRunning || isPaused) {
return "Restart";
}
return "Start";
}
void AdvancedView::update(Cluster cluster)
{
basicButton->setEnabled(true);
createButton->setEnabled(true);
refreshButton->setEnabled(true);
bool exists = !cluster.isEmpty();
bool isRunning = cluster.status() == "Running";
bool isPaused = cluster.status() == "Paused";
startButton->setEnabled(exists);
stopButton->setEnabled(isRunning || isPaused);
pauseButton->setEnabled(isRunning || isPaused);
deleteButton->setEnabled(exists);
dashboardButton->setEnabled(isRunning);
#if __linux__ || __APPLE__
dockerEnvButton->setEnabled(isRunning);
sshButton->setEnabled(exists);
#else
dockerEnvButton->setEnabled(false);
sshButton->setEnabled(false);
#endif
pauseButton->setText(getPauseLabel(isPaused));
startButton->setText(getStartLabel(isRunning, isPaused));
QString startToolTip = "";
if (isRunning) {
startToolTip = "Restart an already running minikube instance to pickup "
"config changes.";
}
startButton->setToolTip(startToolTip);
}
void AdvancedView::setSelectedClusterName(QString cluster)
{
QAbstractItemModel *model = clusterListView->model();
QModelIndex start = model->index(0, 0);
QModelIndexList index = model->match(start, Qt::DisplayRole, cluster);
if (index.size() == 0) {
return;
}
clusterListView->setCurrentIndex(index[0]);
}
QString AdvancedView::selectedClusterName()
{
QModelIndex index = clusterListView->currentIndex();
QVariant variant = index.siblingAtColumn(0).data(Qt::DisplayRole);
if (variant.isNull()) {
return QString();
}
return variant.toString();
}
void AdvancedView::updateClustersTable(ClusterList clusterList)
{
QString cluster = selectedClusterName();
m_clusterModel->setClusters(clusterList);
setSelectedClusterName(cluster);
}
static int getCenter(int widgetSize, int parentSize)
{
return parentSize / 2 - widgetSize / 2;
}
void AdvancedView::showLoading()
{
clusterListView->setEnabled(false);
loading->setHidden(false);
loading->raise();
int width = getCenter(loading->width(), clusterListView->width());
int height = getCenter(loading->height(), clusterListView->height());
loading->move(width, height);
}
void AdvancedView::hideLoading()
{
loading->setHidden(true);
clusterListView->setEnabled(true);
}
static QString profile = "minikube";
static int cpus = 2;
static int memory = 2400;
static QString driver = "";
static QString containerRuntime = "";
static QString k8sVersion = "";
void AdvancedView::askName()
{
QDialog dialog;
dialog.setWindowTitle(tr("Create minikube Cluster"));
dialog.setWindowIcon(m_icon);
dialog.setModal(true);
QFormLayout form(&dialog);
QDialogButtonBox buttonBox(Qt::Horizontal, &dialog);
QLineEdit profileField(profile, &dialog);
form.addRow(new QLabel(tr("Profile")), &profileField);
buttonBox.addButton(QString(tr("Use Default Values")), QDialogButtonBox::AcceptRole);
connect(&buttonBox, &QDialogButtonBox::accepted, &dialog, &QDialog::accept);
buttonBox.addButton(QString(tr("Set Custom Values")), QDialogButtonBox::RejectRole);
connect(&buttonBox, &QDialogButtonBox::rejected, &dialog, &QDialog::reject);
form.addRow(&buttonBox);
int code = dialog.exec();
profile = profileField.text();
if (code == QDialog::Accepted) {
QStringList args = { "-p", profile };
emit createCluster(args);
} else if (code == QDialog::Rejected) {
askCustom();
}
}
void AdvancedView::askCustom()
{
QDialog dialog;
dialog.setWindowTitle(tr("Set Cluster Values"));
dialog.setWindowIcon(m_icon);
dialog.setModal(true);
QFormLayout form(&dialog);
QComboBox *driverComboBox = new QComboBox;
driverComboBox->addItems({ "docker", "virtualbox", "vmware", "podman" });
#if __linux__
driverComboBox->addItems({ "kvm2", "qemu" });
#elif __APPLE__
driverComboBox->addItems({ "hyperkit", "qemu", "parallels" });
#else
driverComboBox->addItem("hyperv");
#endif
form.addRow(new QLabel(tr("Driver")), driverComboBox);
QComboBox *containerRuntimeComboBox = new QComboBox;
containerRuntimeComboBox->addItems({ "docker", "containerd", "crio" });
form.addRow(new QLabel(tr("Container Runtime")), containerRuntimeComboBox);
QComboBox *k8sVersionComboBox = new QComboBox;
k8sVersionComboBox->addItems({ "stable", "latest", "none" });
form.addRow(new QLabel(tr("Kubernetes Version")), k8sVersionComboBox);
QLineEdit cpuField(QString::number(cpus), &dialog);
form.addRow(new QLabel(tr("CPUs")), &cpuField);
QLineEdit memoryField(QString::number(memory), &dialog);
form.addRow(new QLabel(tr("Memory")), &memoryField);
QDialogButtonBox buttonBox(Qt::Horizontal, &dialog);
buttonBox.addButton(QString(tr("Create")), QDialogButtonBox::AcceptRole);
connect(&buttonBox, &QDialogButtonBox::accepted, &dialog, &QDialog::accept);
buttonBox.addButton(QString(tr("Cancel")), QDialogButtonBox::RejectRole);
connect(&buttonBox, &QDialogButtonBox::rejected, &dialog, &QDialog::reject);
form.addRow(&buttonBox);
int code = dialog.exec();
if (code == QDialog::Accepted) {
driver = driverComboBox->itemText(driverComboBox->currentIndex());
containerRuntime =
containerRuntimeComboBox->itemText(containerRuntimeComboBox->currentIndex());
k8sVersion = k8sVersionComboBox->itemText(k8sVersionComboBox->currentIndex());
if (k8sVersion == "none") {
k8sVersion = "v0.0.0";
}
cpus = cpuField.text().toInt();
memory = memoryField.text().toInt();
QStringList args = { "-p",
profile,
"--driver",
driver,
"--container-runtime",
containerRuntime,
"--kubernetes-version",
k8sVersion,
"--cpus",
QString::number(cpus),
"--memory",
QString::number(memory) };
emit createCluster(args);
}
}
void AdvancedView::disableButtons()
{
startButton->setEnabled(false);
stopButton->setEnabled(false);
pauseButton->setEnabled(false);
deleteButton->setEnabled(false);
dockerEnvButton->setEnabled(false);
sshButton->setEnabled(false);
dashboardButton->setEnabled(false);
basicButton->setEnabled(false);
createButton->setEnabled(false);
refreshButton->setEnabled(false);
}