40
40
import javax .inject .Inject ;
41
41
import javax .naming .ConfigurationException ;
42
42
43
- import com .cloud .uservm .UserVm ;
44
- import com .cloud .vm .UserVmService ;
45
43
import org .apache .cloudstack .acl .ControlledEntity ;
46
44
import org .apache .cloudstack .acl .SecurityChecker ;
47
45
import org .apache .cloudstack .annotation .AnnotationService ;
48
46
import org .apache .cloudstack .annotation .dao .AnnotationDao ;
47
+ import org .apache .cloudstack .api .ApiCommandResourceType ;
49
48
import org .apache .cloudstack .api .ApiConstants ;
50
49
import org .apache .cloudstack .api .ApiConstants .VMDetails ;
51
50
import org .apache .cloudstack .api .BaseCmd ;
92
91
import com .cloud .dc .dao .DataCenterDao ;
93
92
import com .cloud .deploy .DeployDestination ;
94
93
import com .cloud .domain .Domain ;
94
+ import com .cloud .event .ActionEvent ;
95
95
import com .cloud .exception .ConcurrentOperationException ;
96
96
import com .cloud .exception .InsufficientCapacityException ;
97
97
import com .cloud .exception .InsufficientServerCapacityException ;
159
159
import com .cloud .user .UserVO ;
160
160
import com .cloud .user .dao .SSHKeyPairDao ;
161
161
import com .cloud .user .dao .UserDao ;
162
+ import com .cloud .uservm .UserVm ;
162
163
import com .cloud .utils .Pair ;
163
164
import com .cloud .utils .Ternary ;
164
165
import com .cloud .utils .component .ComponentContext ;
176
177
import com .cloud .utils .fsm .NoTransitionException ;
177
178
import com .cloud .utils .fsm .StateMachine2 ;
178
179
import com .cloud .utils .net .NetUtils ;
180
+ import com .cloud .vm .UserVmService ;
179
181
import com .cloud .vm .VMInstanceVO ;
180
182
import com .cloud .vm .VirtualMachine ;
181
183
import com .cloud .vm .dao .VMInstanceDao ;
@@ -863,13 +865,15 @@ private Network getKubernetesClusterNetworkIfMissing(final String clusterName, f
863
865
LOGGER .info (String .format ("Creating network for account ID: %s from the network offering ID: %s as part of Kubernetes cluster: %s deployment process" , owner .getUuid (), networkOffering .getUuid (), clusterName ));
864
866
}
865
867
868
+ CallContext networkContext = CallContext .register (CallContext .current (), ApiCommandResourceType .Network );
866
869
try {
867
- network = networkMgr .createGuestNetwork (networkOffering .getId (), clusterName + "-network" , owner .getAccountName () + "-network" ,
868
- null , null , null , false , null , owner , null , physicalNetwork , zone .getId (),
869
- ControlledEntity .ACLType .Account , null , null , null , null , true , null ,
870
- null , null , null , null , null , null , null , null , null );
870
+ network = networkService .createGuestNetwork (networkOffering .getId (), clusterName + "-network" ,
871
+ owner .getAccountName () + "-network" , owner , physicalNetwork , zone .getId (),
872
+ ControlledEntity .ACLType .Account );
871
873
} catch (ConcurrentOperationException | InsufficientCapacityException | ResourceAllocationException e ) {
872
874
logAndThrow (Level .ERROR , String .format ("Unable to create network for the Kubernetes cluster: %s" , clusterName ));
875
+ } finally {
876
+ CallContext .unregister ();
873
877
}
874
878
}
875
879
return network ;
@@ -1138,6 +1142,8 @@ protected boolean stateTransitTo(long kubernetesClusterId, KubernetesCluster.Eve
1138
1142
}
1139
1143
1140
1144
@ Override
1145
+ @ ActionEvent (eventType = KubernetesClusterEventTypes .EVENT_KUBERNETES_CLUSTER_CREATE ,
1146
+ eventDescription = "creating Kubernetes cluster" , create = true )
1141
1147
public KubernetesCluster createUnmanagedKubernetesCluster (CreateKubernetesClusterCmd cmd ) throws CloudRuntimeException {
1142
1148
if (!KubernetesServiceEnabled .value ()) {
1143
1149
logAndThrow (Level .ERROR , "Kubernetes Service plugin is disabled" );
@@ -1184,10 +1190,13 @@ public KubernetesClusterVO doInTransaction(TransactionStatus status) {
1184
1190
if (LOGGER .isInfoEnabled ()) {
1185
1191
LOGGER .info (String .format ("Kubernetes cluster with name: %s and ID: %s has been created" , cluster .getName (), cluster .getUuid ()));
1186
1192
}
1193
+ CallContext .current ().putContextParameter (KubernetesCluster .class , cluster .getUuid ());
1187
1194
return cluster ;
1188
1195
}
1189
1196
1190
1197
@ Override
1198
+ @ ActionEvent (eventType = KubernetesClusterEventTypes .EVENT_KUBERNETES_CLUSTER_CREATE ,
1199
+ eventDescription = "creating Kubernetes cluster" , create = true )
1191
1200
public KubernetesCluster createManagedKubernetesCluster (CreateKubernetesClusterCmd cmd ) throws CloudRuntimeException {
1192
1201
if (!KubernetesServiceEnabled .value ()) {
1193
1202
logAndThrow (Level .ERROR , "Kubernetes Service plugin is disabled" );
@@ -1244,6 +1253,7 @@ public KubernetesClusterVO doInTransaction(TransactionStatus status) {
1244
1253
if (LOGGER .isInfoEnabled ()) {
1245
1254
LOGGER .info (String .format ("Kubernetes cluster name: %s and ID: %s has been created" , cluster .getName (), cluster .getUuid ()));
1246
1255
}
1256
+ CallContext .current ().putContextParameter (KubernetesCluster .class , cluster .getUuid ());
1247
1257
return cluster ;
1248
1258
}
1249
1259
@@ -1270,29 +1280,64 @@ private SecurityGroup getOrCreateSecurityGroupForAccount(Account owner) {
1270
1280
return securityGroup ;
1271
1281
}
1272
1282
1283
+ @ Override
1284
+ @ ActionEvent (eventType = KubernetesClusterEventTypes .EVENT_KUBERNETES_CLUSTER_CREATE ,
1285
+ eventDescription = "creating Kubernetes cluster" , async = true )
1286
+ public void startKubernetesCluster (CreateKubernetesClusterCmd cmd ) throws CloudRuntimeException {
1287
+ final Long id = cmd .getEntityId ();
1288
+ if (KubernetesCluster .ClusterType .valueOf (cmd .getClusterType ()) != KubernetesCluster .ClusterType .CloudManaged ) {
1289
+ return ;
1290
+ }
1291
+ final KubernetesClusterVO kubernetesCluster = kubernetesClusterDao .findById (id );
1292
+ if (kubernetesCluster == null ) {
1293
+ throw new InvalidParameterValueException ("Failed to find Kubernetes cluster with given ID" );
1294
+ }
1295
+ if (!startKubernetesCluster (kubernetesCluster , true )) {
1296
+ throw new CloudRuntimeException (String .format ("Failed to start created Kubernetes cluster: %s" ,
1297
+ kubernetesCluster .getName ()));
1298
+ }
1299
+ }
1300
+
1301
+ @ Override
1302
+ @ ActionEvent (eventType = KubernetesClusterEventTypes .EVENT_KUBERNETES_CLUSTER_START ,
1303
+ eventDescription = "starting Kubernetes cluster" , async = true )
1304
+ public void startKubernetesCluster (StartKubernetesClusterCmd cmd ) throws CloudRuntimeException {
1305
+ final Long id = cmd .getId ();
1306
+ if (id == null || id < 1L ) {
1307
+ throw new InvalidParameterValueException ("Invalid Kubernetes cluster ID provided" );
1308
+ }
1309
+ final KubernetesClusterVO kubernetesCluster = kubernetesClusterDao .findById (id );
1310
+ if (kubernetesCluster == null ) {
1311
+ throw new InvalidParameterValueException ("Given Kubernetes cluster was not found" );
1312
+ }
1313
+ if (!isCommandSupported (kubernetesCluster , cmd .getActualCommandName ())) {
1314
+ throw new InvalidParameterValueException (String .format ("Start kubernetes cluster is not supported for " +
1315
+ "an externally managed cluster (%s)" , kubernetesCluster .getName ()));
1316
+ }
1317
+ if (!startKubernetesCluster (kubernetesCluster , false )) {
1318
+ throw new CloudRuntimeException (String .format ("Failed to start Kubernetes cluster: %s" ,
1319
+ kubernetesCluster .getName ()));
1320
+ }
1321
+ }
1322
+
1273
1323
/**
1274
1324
* Start operation can be performed at two different life stages of Kubernetes cluster. First when a freshly created cluster
1275
1325
* in which case there are no resources provisioned for the Kubernetes cluster. So during start all the resources
1276
1326
* are provisioned from scratch. Second kind of start, happens on Stopped Kubernetes cluster, in which all resources
1277
1327
* are provisioned (like volumes, nics, networks etc). It just that VM's are not in running state. So just
1278
1328
* start the VM's (which can possibly implicitly start the network also).
1279
- * @param kubernetesClusterId
1329
+ * @param kubernetesCluster
1280
1330
* @param onCreate
1281
1331
* @return
1282
1332
* @throws CloudRuntimeException
1283
1333
*/
1284
-
1285
- @ Override
1286
- public boolean startKubernetesCluster (long kubernetesClusterId , boolean onCreate ) throws CloudRuntimeException {
1334
+ public boolean startKubernetesCluster (KubernetesClusterVO kubernetesCluster , boolean onCreate ) throws CloudRuntimeException {
1287
1335
if (!KubernetesServiceEnabled .value ()) {
1288
1336
logAndThrow (Level .ERROR , "Kubernetes Service plugin is disabled" );
1289
1337
}
1290
- final KubernetesClusterVO kubernetesCluster = kubernetesClusterDao .findById (kubernetesClusterId );
1291
- if (kubernetesCluster == null ) {
1292
- throw new InvalidParameterValueException ("Failed to find Kubernetes cluster with given ID" );
1293
- }
1294
1338
if (kubernetesCluster .getRemoved () != null ) {
1295
- throw new InvalidParameterValueException (String .format ("Kubernetes cluster : %s is already deleted" , kubernetesCluster .getName ()));
1339
+ throw new InvalidParameterValueException (String .format ("Kubernetes cluster : %s is already deleted" ,
1340
+ kubernetesCluster .getName ()));
1296
1341
}
1297
1342
accountManager .checkAccess (CallContext .current ().getCallingAccount (), SecurityChecker .AccessType .OperateEntry , false , kubernetesCluster );
1298
1343
if (kubernetesCluster .getState ().equals (KubernetesCluster .State .Running )) {
@@ -1350,6 +1395,8 @@ private String[] getServiceUserKeys(KubernetesClusterVO kubernetesCluster) {
1350
1395
}
1351
1396
1352
1397
@ Override
1398
+ @ ActionEvent (eventType = KubernetesClusterEventTypes .EVENT_KUBERNETES_CLUSTER_STOP ,
1399
+ eventDescription = "stopping Kubernetes cluster" , async = true )
1353
1400
public boolean stopKubernetesCluster (StopKubernetesClusterCmd cmd ) throws CloudRuntimeException {
1354
1401
long kubernetesClusterId = cmd .getId ();
1355
1402
if (!KubernetesServiceEnabled .value ()) {
@@ -1384,6 +1431,8 @@ public boolean stopKubernetesCluster(StopKubernetesClusterCmd cmd) throws CloudR
1384
1431
}
1385
1432
1386
1433
@ Override
1434
+ @ ActionEvent (eventType = KubernetesClusterEventTypes .EVENT_KUBERNETES_CLUSTER_DELETE ,
1435
+ eventDescription = "deleting Kubernetes cluster" , async = true )
1387
1436
public boolean deleteKubernetesCluster (DeleteKubernetesClusterCmd cmd ) throws CloudRuntimeException {
1388
1437
if (!KubernetesServiceEnabled .value ()) {
1389
1438
logAndThrow (Level .ERROR , "Kubernetes Service plugin is disabled" );
@@ -1526,29 +1575,38 @@ public KubernetesClusterConfigResponse getKubernetesClusterConfig(GetKubernetesC
1526
1575
}
1527
1576
1528
1577
@ Override
1578
+ @ ActionEvent (eventType = KubernetesClusterEventTypes .EVENT_KUBERNETES_CLUSTER_SCALE ,
1579
+ eventDescription = "scaling Kubernetes cluster" , async = true )
1529
1580
public boolean scaleKubernetesCluster (ScaleKubernetesClusterCmd cmd ) throws CloudRuntimeException {
1530
1581
if (!KubernetesServiceEnabled .value ()) {
1531
1582
logAndThrow (Level .ERROR , "Kubernetes Service plugin is disabled" );
1532
1583
}
1533
1584
validateKubernetesClusterScaleParameters (cmd );
1534
1585
1535
1586
KubernetesClusterVO kubernetesCluster = kubernetesClusterDao .findById (cmd .getId ());
1587
+ final Long clusterSize = cmd .getClusterSize ();
1588
+ if (clusterSize != null ) {
1589
+ CallContext .current ().setEventDetails (String .format ("Kubernetes cluster ID: %s scaling from size: %d to %d" ,
1590
+ kubernetesCluster .getUuid (), kubernetesCluster .getNodeCount (), clusterSize ));
1591
+ }
1536
1592
String [] keys = getServiceUserKeys (kubernetesCluster );
1537
1593
KubernetesClusterScaleWorker scaleWorker =
1538
1594
new KubernetesClusterScaleWorker (kubernetesClusterDao .findById (cmd .getId ()),
1539
- serviceOfferingDao .findById (cmd .getServiceOfferingId ()),
1540
- cmd . getClusterSize () ,
1541
- cmd .getNodeIds (),
1542
- cmd .isAutoscalingEnabled (),
1543
- cmd .getMinSize (),
1544
- cmd .getMaxSize (),
1545
- this );
1595
+ serviceOfferingDao .findById (cmd .getServiceOfferingId ()),
1596
+ clusterSize ,
1597
+ cmd .getNodeIds (),
1598
+ cmd .isAutoscalingEnabled (),
1599
+ cmd .getMinSize (),
1600
+ cmd .getMaxSize (),
1601
+ this );
1546
1602
scaleWorker .setKeys (keys );
1547
1603
scaleWorker = ComponentContext .inject (scaleWorker );
1548
1604
return scaleWorker .scaleCluster ();
1549
1605
}
1550
1606
1551
1607
@ Override
1608
+ @ ActionEvent (eventType = KubernetesClusterEventTypes .EVENT_KUBERNETES_CLUSTER_UPGRADE ,
1609
+ eventDescription = "upgrading Kubernetes cluster" , async = true )
1552
1610
public boolean upgradeKubernetesCluster (UpgradeKubernetesClusterCmd cmd ) throws CloudRuntimeException {
1553
1611
if (!KubernetesServiceEnabled .value ()) {
1554
1612
logAndThrow (Level .ERROR , "Kubernetes Service plugin is disabled" );
0 commit comments