mirror of
https://github.com/apache/cloudstack.git
synced 2025-10-26 08:42:29 +01:00
CKS: create Security Groups for CKS clusters of each account (#8316)
This PR fixes #7684 The security groups contain the same rules for port 22 and 6443, no need to recreate for each CKS cluster.
This commit is contained in:
parent
3bb318bab9
commit
ab70108f15
@ -121,9 +121,9 @@ import com.cloud.network.dao.PhysicalNetworkDao;
|
|||||||
import com.cloud.network.router.NetworkHelper;
|
import com.cloud.network.router.NetworkHelper;
|
||||||
import com.cloud.network.rules.FirewallRule;
|
import com.cloud.network.rules.FirewallRule;
|
||||||
import com.cloud.network.rules.FirewallRuleVO;
|
import com.cloud.network.rules.FirewallRuleVO;
|
||||||
|
import com.cloud.network.security.SecurityGroup;
|
||||||
import com.cloud.network.security.SecurityGroupManager;
|
import com.cloud.network.security.SecurityGroupManager;
|
||||||
import com.cloud.network.security.SecurityGroupService;
|
import com.cloud.network.security.SecurityGroupService;
|
||||||
import com.cloud.network.security.SecurityGroupVO;
|
|
||||||
import com.cloud.network.security.SecurityRule;
|
import com.cloud.network.security.SecurityRule;
|
||||||
import com.cloud.network.vpc.NetworkACL;
|
import com.cloud.network.vpc.NetworkACL;
|
||||||
import com.cloud.offering.NetworkOffering;
|
import com.cloud.offering.NetworkOffering;
|
||||||
@ -1068,22 +1068,9 @@ public class KubernetesClusterManagerImpl extends ManagerBase implements Kuberne
|
|||||||
logAndThrow(Level.ERROR, String.format("Creating Kubernetes cluster failed due to error while finding suitable deployment plan for cluster in zone : %s", zone.getName()));
|
logAndThrow(Level.ERROR, String.format("Creating Kubernetes cluster failed due to error while finding suitable deployment plan for cluster in zone : %s", zone.getName()));
|
||||||
}
|
}
|
||||||
|
|
||||||
SecurityGroupVO securityGroupVO = null;
|
SecurityGroup securityGroup = null;
|
||||||
if (zone.isSecurityGroupEnabled()) {
|
if (zone.isSecurityGroupEnabled()) {
|
||||||
securityGroupVO = securityGroupManager.createSecurityGroup(KubernetesClusterActionWorker.CKS_CLUSTER_SECURITY_GROUP_NAME.concat(Long.toHexString(System.currentTimeMillis())), "Security group for CKS nodes", owner.getDomainId(), owner.getId(), owner.getAccountName());
|
securityGroup = getOrCreateSecurityGroupForAccount(owner);
|
||||||
if (securityGroupVO == null) {
|
|
||||||
throw new CloudRuntimeException(String.format("Failed to create security group: %s", KubernetesClusterActionWorker.CKS_CLUSTER_SECURITY_GROUP_NAME));
|
|
||||||
}
|
|
||||||
List<String> cidrList = new ArrayList<>();
|
|
||||||
cidrList.add(NetUtils.ALL_IP4_CIDRS);
|
|
||||||
securityGroupService.authorizeSecurityGroupRule(securityGroupVO.getId(), NetUtils.TCP_PROTO,
|
|
||||||
KubernetesClusterActionWorker.CLUSTER_NODES_DEFAULT_SSH_PORT_SG, KubernetesClusterActionWorker.CLUSTER_NODES_DEFAULT_SSH_PORT_SG,
|
|
||||||
null, null, cidrList, null, SecurityRule.SecurityRuleType.IngressRule);
|
|
||||||
securityGroupService.authorizeSecurityGroupRule(securityGroupVO.getId(), NetUtils.TCP_PROTO,
|
|
||||||
KubernetesClusterActionWorker.CLUSTER_API_PORT, KubernetesClusterActionWorker.CLUSTER_API_PORT,
|
|
||||||
null, null, cidrList, null, SecurityRule.SecurityRuleType.IngressRule);
|
|
||||||
securityGroupService.authorizeSecurityGroupRule(securityGroupVO.getId(), NetUtils.ALL_PROTO,
|
|
||||||
null, null, null, null, cidrList, null, SecurityRule.SecurityRuleType.EgressRule);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
final Network defaultNetwork = getKubernetesClusterNetworkIfMissing(cmd.getName(), zone, owner, (int)controlNodeCount, (int)clusterSize, cmd.getExternalLoadBalancerIpAddress(), cmd.getNetworkId());
|
final Network defaultNetwork = getKubernetesClusterNetworkIfMissing(cmd.getName(), zone, owner, (int)controlNodeCount, (int)clusterSize, cmd.getExternalLoadBalancerIpAddress(), cmd.getNetworkId());
|
||||||
@ -1091,7 +1078,7 @@ public class KubernetesClusterManagerImpl extends ManagerBase implements Kuberne
|
|||||||
final long cores = serviceOffering.getCpu() * (controlNodeCount + clusterSize);
|
final long cores = serviceOffering.getCpu() * (controlNodeCount + clusterSize);
|
||||||
final long memory = serviceOffering.getRamSize() * (controlNodeCount + clusterSize);
|
final long memory = serviceOffering.getRamSize() * (controlNodeCount + clusterSize);
|
||||||
|
|
||||||
SecurityGroupVO finalSecurityGroupVO = securityGroupVO;
|
final SecurityGroup finalSecurityGroup = securityGroup;
|
||||||
final KubernetesClusterVO cluster = Transaction.execute(new TransactionCallback<KubernetesClusterVO>() {
|
final KubernetesClusterVO cluster = Transaction.execute(new TransactionCallback<KubernetesClusterVO>() {
|
||||||
@Override
|
@Override
|
||||||
public KubernetesClusterVO doInTransaction(TransactionStatus status) {
|
public KubernetesClusterVO doInTransaction(TransactionStatus status) {
|
||||||
@ -1099,7 +1086,7 @@ public class KubernetesClusterManagerImpl extends ManagerBase implements Kuberne
|
|||||||
serviceOffering.getId(), finalTemplate.getId(), defaultNetwork.getId(), owner.getDomainId(),
|
serviceOffering.getId(), finalTemplate.getId(), defaultNetwork.getId(), owner.getDomainId(),
|
||||||
owner.getAccountId(), controlNodeCount, clusterSize, KubernetesCluster.State.Created, cmd.getSSHKeyPairName(), cores, memory, cmd.getNodeRootDiskSize(), "");
|
owner.getAccountId(), controlNodeCount, clusterSize, KubernetesCluster.State.Created, cmd.getSSHKeyPairName(), cores, memory, cmd.getNodeRootDiskSize(), "");
|
||||||
if (zone.isSecurityGroupEnabled()) {
|
if (zone.isSecurityGroupEnabled()) {
|
||||||
newCluster.setSecurityGroupId(finalSecurityGroupVO.getId());
|
newCluster.setSecurityGroupId(finalSecurityGroup.getId());
|
||||||
}
|
}
|
||||||
kubernetesClusterDao.persist(newCluster);
|
kubernetesClusterDao.persist(newCluster);
|
||||||
return newCluster;
|
return newCluster;
|
||||||
@ -1114,6 +1101,29 @@ public class KubernetesClusterManagerImpl extends ManagerBase implements Kuberne
|
|||||||
return cluster;
|
return cluster;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private SecurityGroup getOrCreateSecurityGroupForAccount(Account owner) {
|
||||||
|
String securityGroupName = String.format("%s-%s", KubernetesClusterActionWorker.CKS_CLUSTER_SECURITY_GROUP_NAME, owner.getUuid());
|
||||||
|
String securityGroupDesc = String.format("%s and account %s", KubernetesClusterActionWorker.CKS_SECURITY_GROUP_DESCRIPTION, owner.getName());
|
||||||
|
SecurityGroup securityGroup = securityGroupManager.getSecurityGroup(securityGroupName, owner.getId());
|
||||||
|
if (securityGroup == null) {
|
||||||
|
securityGroup = securityGroupManager.createSecurityGroup(securityGroupName, securityGroupDesc, owner.getDomainId(), owner.getId(), owner.getAccountName());
|
||||||
|
if (securityGroup == null) {
|
||||||
|
throw new CloudRuntimeException(String.format("Failed to create security group: %s", KubernetesClusterActionWorker.CKS_CLUSTER_SECURITY_GROUP_NAME));
|
||||||
|
}
|
||||||
|
List<String> cidrList = new ArrayList<>();
|
||||||
|
cidrList.add(NetUtils.ALL_IP4_CIDRS);
|
||||||
|
securityGroupService.authorizeSecurityGroupRule(securityGroup.getId(), NetUtils.TCP_PROTO,
|
||||||
|
KubernetesClusterActionWorker.CLUSTER_NODES_DEFAULT_SSH_PORT_SG, KubernetesClusterActionWorker.CLUSTER_NODES_DEFAULT_SSH_PORT_SG,
|
||||||
|
null, null, cidrList, null, SecurityRule.SecurityRuleType.IngressRule);
|
||||||
|
securityGroupService.authorizeSecurityGroupRule(securityGroup.getId(), NetUtils.TCP_PROTO,
|
||||||
|
KubernetesClusterActionWorker.CLUSTER_API_PORT, KubernetesClusterActionWorker.CLUSTER_API_PORT,
|
||||||
|
null, null, cidrList, null, SecurityRule.SecurityRuleType.IngressRule);
|
||||||
|
securityGroupService.authorizeSecurityGroupRule(securityGroup.getId(), NetUtils.ALL_PROTO,
|
||||||
|
null, null, null, null, cidrList, null, SecurityRule.SecurityRuleType.EgressRule);
|
||||||
|
}
|
||||||
|
return securityGroup;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Start operation can be performed at two different life stages of Kubernetes cluster. First when a freshly created cluster
|
* Start operation can be performed at two different life stages of Kubernetes cluster. First when a freshly created cluster
|
||||||
* in which case there are no resources provisioned for the Kubernetes cluster. So during start all the resources
|
* in which case there are no resources provisioned for the Kubernetes cluster. So during start all the resources
|
||||||
|
|||||||
@ -106,6 +106,7 @@ public class KubernetesClusterActionWorker {
|
|||||||
public static final int CLUSTER_NODES_DEFAULT_SSH_PORT_SG = DEFAULT_SSH_PORT;
|
public static final int CLUSTER_NODES_DEFAULT_SSH_PORT_SG = DEFAULT_SSH_PORT;
|
||||||
|
|
||||||
public static final String CKS_CLUSTER_SECURITY_GROUP_NAME = "CKSSecurityGroup";
|
public static final String CKS_CLUSTER_SECURITY_GROUP_NAME = "CKSSecurityGroup";
|
||||||
|
public static final String CKS_SECURITY_GROUP_DESCRIPTION = "Security group for CKS nodes";
|
||||||
|
|
||||||
protected static final Logger LOGGER = Logger.getLogger(KubernetesClusterActionWorker.class);
|
protected static final Logger LOGGER = Logger.getLogger(KubernetesClusterActionWorker.class);
|
||||||
|
|
||||||
|
|||||||
@ -63,7 +63,6 @@ import com.cloud.agent.api.SecurityGroupRulesCmd;
|
|||||||
import com.cloud.agent.api.SecurityGroupRulesCmd.IpPortAndProto;
|
import com.cloud.agent.api.SecurityGroupRulesCmd.IpPortAndProto;
|
||||||
import com.cloud.agent.api.to.VirtualMachineTO;
|
import com.cloud.agent.api.to.VirtualMachineTO;
|
||||||
import com.cloud.agent.manager.Commands;
|
import com.cloud.agent.manager.Commands;
|
||||||
import com.cloud.api.query.dao.SecurityGroupJoinDao;
|
|
||||||
import com.cloud.configuration.Config;
|
import com.cloud.configuration.Config;
|
||||||
import com.cloud.domain.dao.DomainDao;
|
import com.cloud.domain.dao.DomainDao;
|
||||||
import com.cloud.event.ActionEvent;
|
import com.cloud.event.ActionEvent;
|
||||||
@ -131,8 +130,6 @@ public class SecurityGroupManagerImpl extends ManagerBase implements SecurityGro
|
|||||||
@Inject
|
@Inject
|
||||||
SecurityGroupDao _securityGroupDao;
|
SecurityGroupDao _securityGroupDao;
|
||||||
@Inject
|
@Inject
|
||||||
SecurityGroupJoinDao _securityGroupJoinDao;
|
|
||||||
@Inject
|
|
||||||
SecurityGroupRuleDao _securityGroupRuleDao;
|
SecurityGroupRuleDao _securityGroupRuleDao;
|
||||||
@Inject
|
@Inject
|
||||||
SecurityGroupVMMapDao _securityGroupVMMapDao;
|
SecurityGroupVMMapDao _securityGroupVMMapDao;
|
||||||
@ -1405,7 +1402,7 @@ public class SecurityGroupManagerImpl extends ManagerBase implements SecurityGro
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SecurityGroupVO getDefaultSecurityGroup(long accountId) {
|
public SecurityGroup getDefaultSecurityGroup(long accountId) {
|
||||||
return _securityGroupDao.findByAccountAndName(accountId, DEFAULT_GROUP_NAME);
|
return _securityGroupDao.findByAccountAndName(accountId, DEFAULT_GROUP_NAME);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user