Skip to content

Commit ce9b2c5

Browse files
authored
cks: fix events (apache#9070)
Fixes apache#8043 Signed-off-by: Abhishek Kumar <[email protected]>
1 parent df5c546 commit ce9b2c5

File tree

40 files changed

+430
-129
lines changed

40 files changed

+430
-129
lines changed

api/src/main/java/com/cloud/event/EventTypes.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
import org.apache.cloudstack.api.response.ZoneResponse;
3030
import org.apache.cloudstack.config.Configuration;
3131
import org.apache.cloudstack.ha.HAConfig;
32+
import org.apache.cloudstack.quota.QuotaTariff;
3233
import org.apache.cloudstack.storage.object.Bucket;
3334
import org.apache.cloudstack.storage.object.ObjectStore;
34-
import org.apache.cloudstack.quota.QuotaTariff;
3535
import org.apache.cloudstack.usage.Usage;
3636
import org.apache.cloudstack.vm.schedule.VMSchedule;
3737

@@ -1229,4 +1229,8 @@ public static Class getEntityClassForEvent(String eventName) {
12291229
public static boolean isVpcEvent(String eventType) {
12301230
return EventTypes.EVENT_VPC_CREATE.equals(eventType) || EventTypes.EVENT_VPC_DELETE.equals(eventType);
12311231
}
1232+
1233+
public static void addEntityEventDetail(String event, Class<?> clazz) {
1234+
entityEventDetails.put(event, clazz);
1235+
}
12321236
}

api/src/main/java/com/cloud/kubernetes/cluster/KubernetesClusterHelper.java renamed to api/src/main/java/com/cloud/kubernetes/cluster/KubernetesServiceHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import com.cloud.uservm.UserVm;
2222
import com.cloud.utils.component.Adapter;
2323

24-
public interface KubernetesClusterHelper extends Adapter {
24+
public interface KubernetesServiceHelper extends Adapter {
2525

2626
ControlledEntity findByUuid(String uuid);
2727
void checkVmCanBeDestroyed(UserVm userVm);

api/src/main/java/com/cloud/network/NetworkService.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.List;
2020
import java.util.Map;
2121

22+
import org.apache.cloudstack.acl.ControlledEntity;
2223
import org.apache.cloudstack.api.command.admin.address.ReleasePodIpCmdByAdmin;
2324
import org.apache.cloudstack.api.command.admin.network.DedicateGuestVlanRangeCmd;
2425
import org.apache.cloudstack.api.command.admin.network.ListDedicatedGuestVlanRangesCmd;
@@ -98,6 +99,10 @@ IpAddress allocatePortableIP(Account ipOwner, int regionId, Long zoneId, Long ne
9899

99100
Network createGuestNetwork(CreateNetworkCmd cmd) throws InsufficientCapacityException, ConcurrentOperationException, ResourceAllocationException;
100101

102+
Network createGuestNetwork(long networkOfferingId, String name, String displayText, Account owner,
103+
PhysicalNetwork physicalNetwork, long zoneId, ControlledEntity.ACLType aclType) throws
104+
InsufficientCapacityException, ConcurrentOperationException, ResourceAllocationException;
105+
101106
Pair<List<? extends Network>, Integer> searchForNetworks(ListNetworksCmd cmd);
102107

103108
boolean deleteNetwork(long networkId, boolean forced);

api/src/main/java/com/cloud/vm/UserVmService.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import com.cloud.exception.ConcurrentOperationException;
4646
import com.cloud.exception.InsufficientCapacityException;
4747
import com.cloud.exception.ManagementServerException;
48+
import com.cloud.exception.OperationTimedoutException;
4849
import com.cloud.exception.ResourceAllocationException;
4950
import com.cloud.exception.ResourceUnavailableException;
5051
import com.cloud.exception.StorageUnavailableException;
@@ -110,6 +111,8 @@ public interface UserVmService {
110111
UserVm startVirtualMachine(StartVMCmd cmd) throws StorageUnavailableException, ExecutionException, ConcurrentOperationException, ResourceUnavailableException,
111112
InsufficientCapacityException, ResourceAllocationException;
112113

114+
void startVirtualMachine(UserVm vm) throws OperationTimedoutException, ResourceUnavailableException, InsufficientCapacityException;
115+
113116
UserVm rebootVirtualMachine(RebootVMCmd cmd) throws InsufficientCapacityException, ResourceUnavailableException;
114117

115118
UserVm updateVirtualMachine(UpdateVMCmd cmd) throws ResourceUnavailableException, InsufficientCapacityException;

api/src/main/java/org/apache/cloudstack/api/ApiCommandResourceType.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
package org.apache.cloudstack.api;
1818

1919
import java.util.ArrayList;
20+
import java.util.HashMap;
2021
import java.util.List;
22+
import java.util.Map;
2123

2224
import org.apache.cloudstack.region.PortableIp;
2325
import org.apache.commons.collections.CollectionUtils;
@@ -81,15 +83,22 @@ public enum ApiCommandResourceType {
8183
ManagementServer(org.apache.cloudstack.management.ManagementServerHost.class),
8284
ObjectStore(org.apache.cloudstack.storage.object.ObjectStore.class),
8385
Bucket(org.apache.cloudstack.storage.object.Bucket.class),
84-
QuotaTariff(org.apache.cloudstack.quota.QuotaTariff.class);
86+
QuotaTariff(org.apache.cloudstack.quota.QuotaTariff.class),
87+
KubernetesCluster(null),
88+
KubernetesSupportedVersion(null);
8589

8690
private final Class<?> clazz;
8791

92+
static final Map<ApiCommandResourceType, Class<?>> additionalClassMappings = new HashMap<>();
93+
8894
private ApiCommandResourceType(Class<?> clazz) {
8995
this.clazz = clazz;
9096
}
9197

9298
public Class<?> getAssociatedClass() {
99+
if (this.clazz == null && additionalClassMappings.containsKey(this)) {
100+
return additionalClassMappings.get(this);
101+
}
93102
return this.clazz;
94103
}
95104

@@ -119,4 +128,8 @@ public static ApiCommandResourceType fromString(String value) {
119128
}
120129
return null;
121130
}
131+
132+
public static void setClassMapping(ApiCommandResourceType type, Class<?> clazz) {
133+
additionalClassMappings.put(type, clazz);
134+
}
122135
}

core/src/main/resources/META-INF/cloudstack/core/spring-core-registry-core-context.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@
339339
class="org.apache.cloudstack.spring.lifecycle.registry.ExtensionRegistry">
340340
</bean>
341341

342-
<bean id="kubernetesClusterHelperRegistry"
342+
<bean id="kubernetesServiceHelperRegistry"
343343
class="org.apache.cloudstack.spring.lifecycle.registry.ExtensionRegistry">
344344
</bean>
345345

core/src/main/resources/META-INF/cloudstack/kubernetes/spring-core-lifecycle-kubernetes-context-inheritable.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
>
2626

2727
<bean class="org.apache.cloudstack.spring.lifecycle.registry.RegistryLifecycle">
28-
<property name="registry" ref="kubernetesClusterHelperRegistry" />
29-
<property name="typeClass" value="com.cloud.kubernetes.cluster.KubernetesClusterHelper" />
28+
<property name="registry" ref="kubernetesServiceHelperRegistry" />
29+
<property name="typeClass" value="com.cloud.kubernetes.cluster.KubernetesServiceHelper" />
3030
</bean>
3131

3232
</beans>

plugins/integrations/kubernetes-service/src/main/java/com/cloud/kubernetes/cluster/KubernetesClusterManagerImpl.java

Lines changed: 80 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,11 @@
4040
import javax.inject.Inject;
4141
import javax.naming.ConfigurationException;
4242

43-
import com.cloud.uservm.UserVm;
44-
import com.cloud.vm.UserVmService;
4543
import org.apache.cloudstack.acl.ControlledEntity;
4644
import org.apache.cloudstack.acl.SecurityChecker;
4745
import org.apache.cloudstack.annotation.AnnotationService;
4846
import org.apache.cloudstack.annotation.dao.AnnotationDao;
47+
import org.apache.cloudstack.api.ApiCommandResourceType;
4948
import org.apache.cloudstack.api.ApiConstants;
5049
import org.apache.cloudstack.api.ApiConstants.VMDetails;
5150
import org.apache.cloudstack.api.BaseCmd;
@@ -92,6 +91,7 @@
9291
import com.cloud.dc.dao.DataCenterDao;
9392
import com.cloud.deploy.DeployDestination;
9493
import com.cloud.domain.Domain;
94+
import com.cloud.event.ActionEvent;
9595
import com.cloud.exception.ConcurrentOperationException;
9696
import com.cloud.exception.InsufficientCapacityException;
9797
import com.cloud.exception.InsufficientServerCapacityException;
@@ -159,6 +159,7 @@
159159
import com.cloud.user.UserVO;
160160
import com.cloud.user.dao.SSHKeyPairDao;
161161
import com.cloud.user.dao.UserDao;
162+
import com.cloud.uservm.UserVm;
162163
import com.cloud.utils.Pair;
163164
import com.cloud.utils.Ternary;
164165
import com.cloud.utils.component.ComponentContext;
@@ -176,6 +177,7 @@
176177
import com.cloud.utils.fsm.NoTransitionException;
177178
import com.cloud.utils.fsm.StateMachine2;
178179
import com.cloud.utils.net.NetUtils;
180+
import com.cloud.vm.UserVmService;
179181
import com.cloud.vm.VMInstanceVO;
180182
import com.cloud.vm.VirtualMachine;
181183
import 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");

plugins/integrations/kubernetes-service/src/main/java/com/cloud/kubernetes/cluster/KubernetesClusterService.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.apache.cloudstack.api.command.user.kubernetes.cluster.ListKubernetesClustersCmd;
2424
import org.apache.cloudstack.api.command.user.kubernetes.cluster.RemoveVirtualMachinesFromKubernetesClusterCmd;
2525
import org.apache.cloudstack.api.command.user.kubernetes.cluster.ScaleKubernetesClusterCmd;
26+
import org.apache.cloudstack.api.command.user.kubernetes.cluster.StartKubernetesClusterCmd;
2627
import org.apache.cloudstack.api.command.user.kubernetes.cluster.StopKubernetesClusterCmd;
2728
import org.apache.cloudstack.api.command.user.kubernetes.cluster.UpgradeKubernetesClusterCmd;
2829
import org.apache.cloudstack.api.response.KubernetesClusterConfigResponse;
@@ -98,7 +99,9 @@ public interface KubernetesClusterService extends PluggableService, Configurable
9899

99100
KubernetesCluster createManagedKubernetesCluster(CreateKubernetesClusterCmd cmd) throws CloudRuntimeException;
100101

101-
boolean startKubernetesCluster(long kubernetesClusterId, boolean onCreate) throws CloudRuntimeException;
102+
void startKubernetesCluster(CreateKubernetesClusterCmd cmd) throws CloudRuntimeException;
103+
104+
void startKubernetesCluster(StartKubernetesClusterCmd cmd) throws CloudRuntimeException;
102105

103106
boolean stopKubernetesCluster(StopKubernetesClusterCmd cmd) throws CloudRuntimeException;
104107

0 commit comments

Comments
 (0)