4040import javax .inject .Inject ;
4141import javax .naming .ConfigurationException ;
4242
43- import com .cloud .uservm .UserVm ;
44- import com .cloud .vm .UserVmService ;
4543import org .apache .cloudstack .acl .ControlledEntity ;
4644import org .apache .cloudstack .acl .SecurityChecker ;
4745import org .apache .cloudstack .annotation .AnnotationService ;
4846import org .apache .cloudstack .annotation .dao .AnnotationDao ;
47+ import org .apache .cloudstack .api .ApiCommandResourceType ;
4948import org .apache .cloudstack .api .ApiConstants ;
5049import org .apache .cloudstack .api .ApiConstants .VMDetails ;
5150import org .apache .cloudstack .api .BaseCmd ;
9291import com .cloud .dc .dao .DataCenterDao ;
9392import com .cloud .deploy .DeployDestination ;
9493import com .cloud .domain .Domain ;
94+ import com .cloud .event .ActionEvent ;
9595import com .cloud .exception .ConcurrentOperationException ;
9696import com .cloud .exception .InsufficientCapacityException ;
9797import com .cloud .exception .InsufficientServerCapacityException ;
159159import com .cloud .user .UserVO ;
160160import com .cloud .user .dao .SSHKeyPairDao ;
161161import com .cloud .user .dao .UserDao ;
162+ import com .cloud .uservm .UserVm ;
162163import com .cloud .utils .Pair ;
163164import com .cloud .utils .Ternary ;
164165import com .cloud .utils .component .ComponentContext ;
176177import com .cloud .utils .fsm .NoTransitionException ;
177178import com .cloud .utils .fsm .StateMachine2 ;
178179import com .cloud .utils .net .NetUtils ;
180+ import com .cloud .vm .UserVmService ;
179181import com .cloud .vm .VMInstanceVO ;
180182import com .cloud .vm .VirtualMachine ;
181183import com .cloud .vm .dao .VMInstanceDao ;
@@ -863,13 +865,15 @@ private Network getKubernetesClusterNetworkIfMissing(final String clusterName, f
863865 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 ));
864866 }
865867
868+ CallContext networkContext = CallContext .register (CallContext .current (), ApiCommandResourceType .Network );
866869 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 );
871873 } catch (ConcurrentOperationException | InsufficientCapacityException | ResourceAllocationException e ) {
872874 logAndThrow (Level .ERROR , String .format ("Unable to create network for the Kubernetes cluster: %s" , clusterName ));
875+ } finally {
876+ CallContext .unregister ();
873877 }
874878 }
875879 return network ;
@@ -1138,6 +1142,8 @@ protected boolean stateTransitTo(long kubernetesClusterId, KubernetesCluster.Eve
11381142 }
11391143
11401144 @ Override
1145+ @ ActionEvent (eventType = KubernetesClusterEventTypes .EVENT_KUBERNETES_CLUSTER_CREATE ,
1146+ eventDescription = "creating Kubernetes cluster" , create = true )
11411147 public KubernetesCluster createUnmanagedKubernetesCluster (CreateKubernetesClusterCmd cmd ) throws CloudRuntimeException {
11421148 if (!KubernetesServiceEnabled .value ()) {
11431149 logAndThrow (Level .ERROR , "Kubernetes Service plugin is disabled" );
@@ -1184,10 +1190,13 @@ public KubernetesClusterVO doInTransaction(TransactionStatus status) {
11841190 if (LOGGER .isInfoEnabled ()) {
11851191 LOGGER .info (String .format ("Kubernetes cluster with name: %s and ID: %s has been created" , cluster .getName (), cluster .getUuid ()));
11861192 }
1193+ CallContext .current ().putContextParameter (KubernetesCluster .class , cluster .getUuid ());
11871194 return cluster ;
11881195 }
11891196
11901197 @ Override
1198+ @ ActionEvent (eventType = KubernetesClusterEventTypes .EVENT_KUBERNETES_CLUSTER_CREATE ,
1199+ eventDescription = "creating Kubernetes cluster" , create = true )
11911200 public KubernetesCluster createManagedKubernetesCluster (CreateKubernetesClusterCmd cmd ) throws CloudRuntimeException {
11921201 if (!KubernetesServiceEnabled .value ()) {
11931202 logAndThrow (Level .ERROR , "Kubernetes Service plugin is disabled" );
@@ -1244,6 +1253,7 @@ public KubernetesClusterVO doInTransaction(TransactionStatus status) {
12441253 if (LOGGER .isInfoEnabled ()) {
12451254 LOGGER .info (String .format ("Kubernetes cluster name: %s and ID: %s has been created" , cluster .getName (), cluster .getUuid ()));
12461255 }
1256+ CallContext .current ().putContextParameter (KubernetesCluster .class , cluster .getUuid ());
12471257 return cluster ;
12481258 }
12491259
@@ -1270,29 +1280,64 @@ private SecurityGroup getOrCreateSecurityGroupForAccount(Account owner) {
12701280 return securityGroup ;
12711281 }
12721282
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+
12731323 /**
12741324 * Start operation can be performed at two different life stages of Kubernetes cluster. First when a freshly created cluster
12751325 * in which case there are no resources provisioned for the Kubernetes cluster. So during start all the resources
12761326 * are provisioned from scratch. Second kind of start, happens on Stopped Kubernetes cluster, in which all resources
12771327 * are provisioned (like volumes, nics, networks etc). It just that VM's are not in running state. So just
12781328 * start the VM's (which can possibly implicitly start the network also).
1279- * @param kubernetesClusterId
1329+ * @param kubernetesCluster
12801330 * @param onCreate
12811331 * @return
12821332 * @throws CloudRuntimeException
12831333 */
1284-
1285- @ Override
1286- public boolean startKubernetesCluster (long kubernetesClusterId , boolean onCreate ) throws CloudRuntimeException {
1334+ public boolean startKubernetesCluster (KubernetesClusterVO kubernetesCluster , boolean onCreate ) throws CloudRuntimeException {
12871335 if (!KubernetesServiceEnabled .value ()) {
12881336 logAndThrow (Level .ERROR , "Kubernetes Service plugin is disabled" );
12891337 }
1290- final KubernetesClusterVO kubernetesCluster = kubernetesClusterDao .findById (kubernetesClusterId );
1291- if (kubernetesCluster == null ) {
1292- throw new InvalidParameterValueException ("Failed to find Kubernetes cluster with given ID" );
1293- }
12941338 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 ()));
12961341 }
12971342 accountManager .checkAccess (CallContext .current ().getCallingAccount (), SecurityChecker .AccessType .OperateEntry , false , kubernetesCluster );
12981343 if (kubernetesCluster .getState ().equals (KubernetesCluster .State .Running )) {
@@ -1350,6 +1395,8 @@ private String[] getServiceUserKeys(KubernetesClusterVO kubernetesCluster) {
13501395 }
13511396
13521397 @ Override
1398+ @ ActionEvent (eventType = KubernetesClusterEventTypes .EVENT_KUBERNETES_CLUSTER_STOP ,
1399+ eventDescription = "stopping Kubernetes cluster" , async = true )
13531400 public boolean stopKubernetesCluster (StopKubernetesClusterCmd cmd ) throws CloudRuntimeException {
13541401 long kubernetesClusterId = cmd .getId ();
13551402 if (!KubernetesServiceEnabled .value ()) {
@@ -1384,6 +1431,8 @@ public boolean stopKubernetesCluster(StopKubernetesClusterCmd cmd) throws CloudR
13841431 }
13851432
13861433 @ Override
1434+ @ ActionEvent (eventType = KubernetesClusterEventTypes .EVENT_KUBERNETES_CLUSTER_DELETE ,
1435+ eventDescription = "deleting Kubernetes cluster" , async = true )
13871436 public boolean deleteKubernetesCluster (DeleteKubernetesClusterCmd cmd ) throws CloudRuntimeException {
13881437 if (!KubernetesServiceEnabled .value ()) {
13891438 logAndThrow (Level .ERROR , "Kubernetes Service plugin is disabled" );
@@ -1526,29 +1575,38 @@ public KubernetesClusterConfigResponse getKubernetesClusterConfig(GetKubernetesC
15261575 }
15271576
15281577 @ Override
1578+ @ ActionEvent (eventType = KubernetesClusterEventTypes .EVENT_KUBERNETES_CLUSTER_SCALE ,
1579+ eventDescription = "scaling Kubernetes cluster" , async = true )
15291580 public boolean scaleKubernetesCluster (ScaleKubernetesClusterCmd cmd ) throws CloudRuntimeException {
15301581 if (!KubernetesServiceEnabled .value ()) {
15311582 logAndThrow (Level .ERROR , "Kubernetes Service plugin is disabled" );
15321583 }
15331584 validateKubernetesClusterScaleParameters (cmd );
15341585
15351586 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+ }
15361592 String [] keys = getServiceUserKeys (kubernetesCluster );
15371593 KubernetesClusterScaleWorker scaleWorker =
15381594 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 );
15461602 scaleWorker .setKeys (keys );
15471603 scaleWorker = ComponentContext .inject (scaleWorker );
15481604 return scaleWorker .scaleCluster ();
15491605 }
15501606
15511607 @ Override
1608+ @ ActionEvent (eventType = KubernetesClusterEventTypes .EVENT_KUBERNETES_CLUSTER_UPGRADE ,
1609+ eventDescription = "upgrading Kubernetes cluster" , async = true )
15521610 public boolean upgradeKubernetesCluster (UpgradeKubernetesClusterCmd cmd ) throws CloudRuntimeException {
15531611 if (!KubernetesServiceEnabled .value ()) {
15541612 logAndThrow (Level .ERROR , "Kubernetes Service plugin is disabled" );
0 commit comments