Refactor KubernetesClusterResourceModifierActionWorker (#8801)

Co-authored-by: dahn <daan.hoogland@gmail.com>
This commit is contained in:
Felipe 2024-07-14 11:02:04 -03:00 committed by GitHub
parent 77cc75ab02
commit a87778be9a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 158 additions and 112 deletions

View File

@ -94,11 +94,35 @@ public class CreateFirewallRuleCmd extends BaseAsyncCreateCmd implements Firewal
return ipAddressId; return ipAddressId;
} }
public void setIpAddressId(Long ipAddressId) {
this.ipAddressId = ipAddressId;
}
@Override @Override
public String getProtocol() { public String getProtocol() {
return protocol.trim(); return protocol.trim();
} }
public void setProtocol(String protocol) {
this.protocol = protocol;
}
public Integer getPublicStartPort() {
return publicStartPort;
}
public void setPublicStartPort(Integer publicStartPort) {
this.publicStartPort = publicStartPort;
}
public Integer getPublicEndPort() {
return publicEndPort;
}
public void setPublicEndPort(Integer publicEndPort) {
this.publicEndPort = publicEndPort;
}
@Override @Override
public List<String> getSourceCidrList() { public List<String> getSourceCidrList() {
if (cidrlist != null) { if (cidrlist != null) {

View File

@ -113,6 +113,10 @@ public class CreateNetworkACLCmd extends BaseAsyncCreateCmd {
return p; return p;
} }
public void setProtocol(String protocol) {
this.protocol = protocol;
}
public List<String> getSourceCidrList() { public List<String> getSourceCidrList() {
if (cidrlist != null) { if (cidrlist != null) {
return cidrlist; return cidrlist;
@ -136,6 +140,9 @@ public class CreateNetworkACLCmd extends BaseAsyncCreateCmd {
throw new InvalidParameterValueException("Invalid traffic type " + trafficType); throw new InvalidParameterValueException("Invalid traffic type " + trafficType);
} }
public void setTrafficType(String trafficType) {
this.trafficType = trafficType;
}
// /////////////////////////////////////////////////// // ///////////////////////////////////////////////////
// ///////////// API Implementation/////////////////// // ///////////// API Implementation///////////////////
// /////////////////////////////////////////////////// // ///////////////////////////////////////////////////
@ -144,15 +151,23 @@ public class CreateNetworkACLCmd extends BaseAsyncCreateCmd {
return action; return action;
} }
public void setAction(String action) {
this.action = action;
}
public Integer getNumber() { public Integer getNumber() {
return number; return number;
} }
public Integer getSourcePortStart() { public Integer getPublicStartPort() {
return publicStartPort; return publicStartPort;
} }
public Integer getSourcePortEnd() { public void setPublicStartPort(Integer publicStartPort) {
this.publicStartPort = publicStartPort;
}
public Integer getPublicEndPort() {
if (publicEndPort == null) { if (publicEndPort == null) {
if (publicStartPort != null) { if (publicStartPort != null) {
return publicStartPort; return publicStartPort;
@ -164,10 +179,18 @@ public class CreateNetworkACLCmd extends BaseAsyncCreateCmd {
return null; return null;
} }
public void setPublicEndPort(Integer publicEndPort) {
this.publicEndPort = publicEndPort;
}
public Long getNetworkId() { public Long getNetworkId() {
return networkId; return networkId;
} }
public void setNetworkId(Long networkId) {
this.networkId = networkId;
}
@Override @Override
public long getEntityOwnerId() { public long getEntityOwnerId() {
Account caller = CallContext.current().getCallingAccount(); Account caller = CallContext.current().getCallingAccount();
@ -207,6 +230,10 @@ public class CreateNetworkACLCmd extends BaseAsyncCreateCmd {
return aclId; return aclId;
} }
public void setAclId(Long aclId) {
this.aclId = aclId;
}
public String getReason() { public String getReason() {
return reason; return reason;
} }

View File

@ -102,6 +102,10 @@ public class StartVMCmd extends BaseAsyncCmd implements UserCmd {
return id; return id;
} }
public void setId(Long id) {
this.id = id;
}
public Long getHostId() { public Long getHostId() {
return hostId; return hostId;
} }

View File

@ -101,6 +101,10 @@ public class ResizeVolumeCmd extends BaseAsyncCmd implements UserCmd {
return getEntityId(); return getEntityId();
} }
public void setId(Long id) {
this.id = id;
}
public Long getMinIops() { public Long getMinIops() {
return minIops; return minIops;
} }
@ -113,6 +117,10 @@ public class ResizeVolumeCmd extends BaseAsyncCmd implements UserCmd {
return size; return size;
} }
public void setSize(Long size) {
this.size = size;
}
public boolean isShrinkOk() { public boolean isShrinkOk() {
return shrinkOk; return shrinkOk;
} }

View File

@ -17,31 +17,6 @@
package com.cloud.kubernetes.cluster.actionworkers; package com.cloud.kubernetes.cluster.actionworkers;
import static com.cloud.utils.NumbersUtil.toHumanReadableSize;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import javax.inject.Inject;
import org.apache.cloudstack.api.ApiCommandResourceType;
import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.api.BaseCmd;
import org.apache.cloudstack.api.command.user.firewall.CreateFirewallRuleCmd;
import org.apache.cloudstack.api.command.user.network.CreateNetworkACLCmd;
import org.apache.cloudstack.api.command.user.volume.ResizeVolumeCmd;
import org.apache.cloudstack.context.CallContext;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import com.cloud.capacity.CapacityManager; import com.cloud.capacity.CapacityManager;
import com.cloud.dc.ClusterDetailsDao; import com.cloud.dc.ClusterDetailsDao;
import com.cloud.dc.ClusterDetailsVO; import com.cloud.dc.ClusterDetailsVO;
@ -57,6 +32,7 @@ import com.cloud.exception.ManagementServerException;
import com.cloud.exception.NetworkRuleConflictException; import com.cloud.exception.NetworkRuleConflictException;
import com.cloud.exception.OperationTimedoutException; import com.cloud.exception.OperationTimedoutException;
import com.cloud.exception.PermissionDeniedException; import com.cloud.exception.PermissionDeniedException;
import com.cloud.exception.ResourceAllocationException;
import com.cloud.exception.ResourceUnavailableException; import com.cloud.exception.ResourceUnavailableException;
import com.cloud.host.Host; import com.cloud.host.Host;
import com.cloud.host.HostVO; import com.cloud.host.HostVO;
@ -102,7 +78,6 @@ import com.cloud.utils.component.ComponentContext;
import com.cloud.utils.db.Transaction; import com.cloud.utils.db.Transaction;
import com.cloud.utils.db.TransactionCallback; import com.cloud.utils.db.TransactionCallback;
import com.cloud.utils.db.TransactionCallbackWithException; import com.cloud.utils.db.TransactionCallbackWithException;
import com.cloud.utils.db.TransactionStatus;
import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.exception.CloudRuntimeException;
import com.cloud.utils.net.Ip; import com.cloud.utils.net.Ip;
import com.cloud.utils.net.NetUtils; import com.cloud.utils.net.NetUtils;
@ -112,8 +87,30 @@ import com.cloud.vm.UserVmManager;
import com.cloud.vm.VirtualMachine; import com.cloud.vm.VirtualMachine;
import com.cloud.vm.VmDetailConstants; import com.cloud.vm.VmDetailConstants;
import com.cloud.vm.dao.VMInstanceDao; import com.cloud.vm.dao.VMInstanceDao;
import org.apache.cloudstack.api.ApiCommandResourceType;
import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.api.BaseCmd;
import org.apache.cloudstack.api.command.user.firewall.CreateFirewallRuleCmd;
import org.apache.cloudstack.api.command.user.network.CreateNetworkACLCmd;
import org.apache.cloudstack.api.command.user.volume.ResizeVolumeCmd;
import org.apache.cloudstack.context.CallContext;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.Level; import org.apache.logging.log4j.Level;
import javax.inject.Inject;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import static com.cloud.utils.NumbersUtil.toHumanReadableSize;
public class KubernetesClusterResourceModifierActionWorker extends KubernetesClusterActionWorker { public class KubernetesClusterResourceModifierActionWorker extends KubernetesClusterActionWorker {
@Inject @Inject
@ -308,17 +305,14 @@ public class KubernetesClusterResourceModifierActionWorker extends KubernetesClu
if (volumeVO.getVolumeType() == Volume.Type.ROOT) { if (volumeVO.getVolumeType() == Volume.Type.ROOT) {
ResizeVolumeCmd resizeVolumeCmd = new ResizeVolumeCmd(); ResizeVolumeCmd resizeVolumeCmd = new ResizeVolumeCmd();
resizeVolumeCmd = ComponentContext.inject(resizeVolumeCmd); resizeVolumeCmd = ComponentContext.inject(resizeVolumeCmd);
Field f = resizeVolumeCmd.getClass().getDeclaredField("size"); resizeVolumeCmd.setSize(kubernetesCluster.getNodeRootDiskSize());
Field f1 = resizeVolumeCmd.getClass().getDeclaredField("id"); resizeVolumeCmd.setId(volumeVO.getId());
f.setAccessible(true);
f1.setAccessible(true);
f1.set(resizeVolumeCmd, volumeVO.getId());
f.set(resizeVolumeCmd, kubernetesCluster.getNodeRootDiskSize());
volumeService.resizeVolume(resizeVolumeCmd); volumeService.resizeVolume(resizeVolumeCmd);
} }
} }
} }
} catch (IllegalAccessException | NoSuchFieldException e) { } catch (ResourceAllocationException e) {
throw new ManagementServerException(String.format("Failed to resize volume of VM in the Kubernetes cluster : %s", kubernetesCluster.getName()), e); throw new ManagementServerException(String.format("Failed to resize volume of VM in the Kubernetes cluster : %s", kubernetesCluster.getName()), e);
} }
} }
@ -431,30 +425,20 @@ public class KubernetesClusterResourceModifierActionWorker extends KubernetesClu
List<String> sourceCidrList = new ArrayList<String>(); List<String> sourceCidrList = new ArrayList<String>();
sourceCidrList.add("0.0.0.0/0"); sourceCidrList.add("0.0.0.0/0");
CreateFirewallRuleCmd rule = new CreateFirewallRuleCmd(); CreateFirewallRuleCmd firewallRule = new CreateFirewallRuleCmd();
rule = ComponentContext.inject(rule); firewallRule = ComponentContext.inject(firewallRule);
Field addressField = rule.getClass().getDeclaredField("ipAddressId"); firewallRule.setIpAddressId(publicIp.getId());
addressField.setAccessible(true);
addressField.set(rule, publicIp.getId());
Field protocolField = rule.getClass().getDeclaredField("protocol"); firewallRule.setProtocol("TCP");
protocolField.setAccessible(true);
protocolField.set(rule, "TCP");
Field startPortField = rule.getClass().getDeclaredField("publicStartPort"); firewallRule.setPublicStartPort(startPort);
startPortField.setAccessible(true);
startPortField.set(rule, startPort);
Field endPortField = rule.getClass().getDeclaredField("publicEndPort"); firewallRule.setPublicEndPort(endPort);
endPortField.setAccessible(true);
endPortField.set(rule, endPort);
Field cidrField = rule.getClass().getDeclaredField("cidrlist"); firewallRule.setSourceCidrList(sourceCidrList);
cidrField.setAccessible(true);
cidrField.set(rule, sourceCidrList);
firewallService.createIngressFirewallRule(rule); firewallService.createIngressFirewallRule(firewallRule);
firewallService.applyIngressFwRules(publicIp.getId(), account); firewallService.applyIngressFwRules(publicIp.getId(), account);
} }
@ -515,6 +499,7 @@ public class KubernetesClusterResourceModifierActionWorker extends KubernetesClu
firewallRule.getSourcePortEnd() == CLUSTER_API_PORT) { firewallRule.getSourcePortEnd() == CLUSTER_API_PORT) {
rule = firewallRule; rule = firewallRule;
firewallService.revokeIngressFwRule(firewallRule.getId(), true); firewallService.revokeIngressFwRule(firewallRule.getId(), true);
logger.debug("The API firewall rule [%s] with the id [%s] was revoked",firewallRule.getName(),firewallRule.getId());
break; break;
} }
} }
@ -528,6 +513,7 @@ public class KubernetesClusterResourceModifierActionWorker extends KubernetesClu
if (firewallRule.getSourcePortStart() == CLUSTER_NODES_DEFAULT_START_SSH_PORT) { if (firewallRule.getSourcePortStart() == CLUSTER_NODES_DEFAULT_START_SSH_PORT) {
rule = firewallRule; rule = firewallRule;
firewallService.revokeIngressFwRule(firewallRule.getId(), true); firewallService.revokeIngressFwRule(firewallRule.getId(), true);
logger.debug("The SSH firewall rule [%s] with the id [%s] was revoked",firewallRule.getName(),firewallRule.getId());
break; break;
} }
} }
@ -541,6 +527,7 @@ public class KubernetesClusterResourceModifierActionWorker extends KubernetesClu
for (PortForwardingRuleVO pfRule : pfRules) { for (PortForwardingRuleVO pfRule : pfRules) {
if (pfRule.getVirtualMachineId() == vmId) { if (pfRule.getVirtualMachineId() == vmId) {
portForwardingRulesDao.remove(pfRule.getId()); portForwardingRulesDao.remove(pfRule.getId());
logger.debug("The Port forwarding rule [%s] with the id [%s] was removed.", pfRule.getName(), pfRule.getId());
break; break;
} }
} }
@ -555,6 +542,7 @@ public class KubernetesClusterResourceModifierActionWorker extends KubernetesClu
for (PortForwardingRuleVO pfRule : pfRules) { for (PortForwardingRuleVO pfRule : pfRules) {
if (startPort <= pfRule.getSourcePortStart() && pfRule.getSourcePortStart() <= endPort) { if (startPort <= pfRule.getSourcePortStart() && pfRule.getSourcePortStart() <= endPort) {
portForwardingRulesDao.remove(pfRule.getId()); portForwardingRulesDao.remove(pfRule.getId());
logger.debug("The Port forwarding rule [{}] with the id [{}] was removed.", pfRule.getName(), pfRule.getId());
} }
} }
rulesService.applyPortForwardingRules(publicIp.getId(), account); rulesService.applyPortForwardingRules(publicIp.getId(), account);
@ -562,39 +550,36 @@ public class KubernetesClusterResourceModifierActionWorker extends KubernetesClu
protected void removeLoadBalancingRule(final IpAddress publicIp, final Network network, protected void removeLoadBalancingRule(final IpAddress publicIp, final Network network,
final Account account) throws ResourceUnavailableException { final Account account) throws ResourceUnavailableException {
List<LoadBalancerVO> rules = loadBalancerDao.listByIpAddress(publicIp.getId()); List<LoadBalancerVO> loadBalancerRules = loadBalancerDao.listByIpAddress(publicIp.getId());
for (LoadBalancerVO rule : rules) { loadBalancerRules.stream().filter(lbRules -> lbRules.getNetworkId() == network.getId() && lbRules.getAccountId() == account.getId() && lbRules.getSourcePortStart() == CLUSTER_API_PORT
if (rule.getNetworkId() == network.getId() && && lbRules.getSourcePortEnd() == CLUSTER_API_PORT).forEach(lbRule -> {
rule.getAccountId() == account.getId() && lbService.deleteLoadBalancerRule(lbRule.getId(), true);
rule.getSourcePortStart() == CLUSTER_API_PORT && logger.debug("The load balancing rule with the Id: {} was removed",lbRule.getId());
rule.getSourcePortEnd() == CLUSTER_API_PORT) { });
lbService.deleteLoadBalancerRule(rule.getId(), true);
break;
}
}
} }
protected void provisionVpcTierAllowPortACLRule(final Network network, int startPort, int endPorts) throws NoSuchFieldException, protected void provisionVpcTierAllowPortACLRule(final Network network, int startPort, int endPorts) throws NoSuchFieldException,
IllegalAccessException, ResourceUnavailableException { IllegalAccessException, ResourceUnavailableException {
List<NetworkACLItemVO> aclItems = networkACLItemDao.listByACL(network.getNetworkACLId()); List<NetworkACLItemVO> aclItems = networkACLItemDao.listByACL(network.getNetworkACLId());
aclItems = aclItems.stream().filter(x -> !NetworkACLItem.State.Revoke.equals(x.getState())).collect(Collectors.toList()); aclItems = aclItems.stream().filter(networkACLItem -> !NetworkACLItem.State.Revoke.equals(networkACLItem.getState())).collect(Collectors.toList());
CreateNetworkACLCmd rule = new CreateNetworkACLCmd(); CreateNetworkACLCmd networkACLRule = new CreateNetworkACLCmd();
rule = ComponentContext.inject(rule); networkACLRule = ComponentContext.inject(networkACLRule);
Map<String, Object> fieldValues = Map.of(
"protocol", "TCP", networkACLRule.setProtocol("TCP");
"publicStartPort", startPort,
"publicEndPort", endPorts, networkACLRule.setPublicStartPort(startPort);
"trafficType", NetworkACLItem.TrafficType.Ingress.toString(),
"networkId", network.getId(), networkACLRule.setPublicEndPort(endPorts);
"aclId", network.getNetworkACLId(),
"action", NetworkACLItem.Action.Allow.toString() networkACLRule.setTrafficType(NetworkACLItem.TrafficType.Ingress.toString());
);
for (Map.Entry<String, Object> entry : fieldValues.entrySet()) { networkACLRule.setNetworkId(network.getId());
Field field = rule.getClass().getDeclaredField(entry.getKey());
field.setAccessible(true); networkACLRule.setAclId(network.getNetworkACLId());
field.set(rule, entry.getValue());
} networkACLRule.setAction(NetworkACLItem.Action.Allow.toString());
NetworkACLItem aclRule = networkACLService.createNetworkACLItem(rule);
NetworkACLItem aclRule = networkACLService.createNetworkACLItem(networkACLRule);
networkACLService.moveRuleToTheTopInACLList(aclRule); networkACLService.moveRuleToTheTopInACLList(aclRule);
networkACLService.applyNetworkACL(aclRule.getAclId()); networkACLService.applyNetworkACL(aclRule.getAclId());
} }
@ -602,13 +587,13 @@ public class KubernetesClusterResourceModifierActionWorker extends KubernetesClu
protected void removeVpcTierAllowPortACLRule(final Network network, int startPort, int endPort) throws NoSuchFieldException, protected void removeVpcTierAllowPortACLRule(final Network network, int startPort, int endPort) throws NoSuchFieldException,
IllegalAccessException, ResourceUnavailableException { IllegalAccessException, ResourceUnavailableException {
List<NetworkACLItemVO> aclItems = networkACLItemDao.listByACL(network.getNetworkACLId()); List<NetworkACLItemVO> aclItems = networkACLItemDao.listByACL(network.getNetworkACLId());
aclItems = aclItems.stream().filter(x -> (x.getProtocol() != null && aclItems = aclItems.stream().filter(networkACLItem -> (networkACLItem.getProtocol() != null &&
x.getProtocol().equals("TCP") && networkACLItem.getProtocol().equals("TCP") &&
x.getSourcePortStart() != null && networkACLItem.getSourcePortStart() != null &&
x.getSourcePortStart().equals(startPort) && networkACLItem.getSourcePortStart().equals(startPort) &&
x.getSourcePortEnd() != null && networkACLItem.getSourcePortEnd() != null &&
x.getSourcePortEnd().equals(endPort) && networkACLItem.getSourcePortEnd().equals(endPort) &&
x.getAction().equals(NetworkACLItem.Action.Allow))) networkACLItem.getAction().equals(NetworkACLItem.Action.Allow)))
.collect(Collectors.toList()); .collect(Collectors.toList());
for (NetworkACLItemVO aclItem : aclItems) { for (NetworkACLItemVO aclItem : aclItems) {
@ -801,29 +786,27 @@ public class KubernetesClusterResourceModifierActionWorker extends KubernetesClu
protected KubernetesClusterVO updateKubernetesClusterEntry(final Long cores, final Long memory, final Long size, protected KubernetesClusterVO updateKubernetesClusterEntry(final Long cores, final Long memory, final Long size,
final Long serviceOfferingId, final Boolean autoscaleEnabled, final Long minSize, final Long maxSize) { final Long serviceOfferingId, final Boolean autoscaleEnabled, final Long minSize, final Long maxSize) {
return Transaction.execute(new TransactionCallback<KubernetesClusterVO>() { return Transaction.execute((TransactionCallback<KubernetesClusterVO>) status -> {
@Override KubernetesClusterVO updatedCluster = kubernetesClusterDao.createForUpdate(kubernetesCluster.getId());
public KubernetesClusterVO doInTransaction(TransactionStatus status) {
KubernetesClusterVO updatedCluster = kubernetesClusterDao.findById(kubernetesCluster.getId()); if (cores != null) {
if (cores != null) { updatedCluster.setCores(cores);
updatedCluster.setCores(cores);
}
if (memory != null) {
updatedCluster.setMemory(memory);
}
if (size != null) {
updatedCluster.setNodeCount(size);
}
if (serviceOfferingId != null) {
updatedCluster.setServiceOfferingId(serviceOfferingId);
}
if (autoscaleEnabled != null) {
updatedCluster.setAutoscalingEnabled(autoscaleEnabled.booleanValue());
}
updatedCluster.setMinSize(minSize);
updatedCluster.setMaxSize(maxSize);
return kubernetesClusterDao.persist(updatedCluster);
} }
if (memory != null) {
updatedCluster.setMemory(memory);
}
if (size != null) {
updatedCluster.setNodeCount(size);
}
if (serviceOfferingId != null) {
updatedCluster.setServiceOfferingId(serviceOfferingId);
}
if (autoscaleEnabled != null) {
updatedCluster.setAutoscalingEnabled(autoscaleEnabled.booleanValue());
}
updatedCluster.setMinSize(minSize);
updatedCluster.setMaxSize(maxSize);
return kubernetesClusterDao.persist(updatedCluster);
}); });
} }

View File

@ -325,8 +325,8 @@ public class NetworkACLServiceImpl extends ManagerBase implements NetworkACLServ
public NetworkACLItem createNetworkACLItem(CreateNetworkACLCmd createNetworkACLCmd) { public NetworkACLItem createNetworkACLItem(CreateNetworkACLCmd createNetworkACLCmd) {
Long aclId = createAclListIfNeeded(createNetworkACLCmd); Long aclId = createAclListIfNeeded(createNetworkACLCmd);
Integer sourcePortStart = createNetworkACLCmd.getSourcePortStart(); Integer sourcePortStart = createNetworkACLCmd.getPublicStartPort();
Integer sourcePortEnd = createNetworkACLCmd.getSourcePortEnd(); Integer sourcePortEnd = createNetworkACLCmd.getPublicEndPort();
String protocol = createNetworkACLCmd.getProtocol(); String protocol = createNetworkACLCmd.getProtocol();
List<String> sourceCidrList = createNetworkACLCmd.getSourceCidrList(); List<String> sourceCidrList = createNetworkACLCmd.getSourceCidrList();
Integer icmpCode = createNetworkACLCmd.getIcmpCode(); Integer icmpCode = createNetworkACLCmd.getIcmpCode();