Skip to content

Commit ab70108

Browse files
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.
1 parent 3bb318b commit ab70108

File tree

3 files changed

+30
-22
lines changed

3 files changed

+30
-22
lines changed

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

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@
121121
import com.cloud.network.router.NetworkHelper;
122122
import com.cloud.network.rules.FirewallRule;
123123
import com.cloud.network.rules.FirewallRuleVO;
124+
import com.cloud.network.security.SecurityGroup;
124125
import com.cloud.network.security.SecurityGroupManager;
125126
import com.cloud.network.security.SecurityGroupService;
126-
import com.cloud.network.security.SecurityGroupVO;
127127
import com.cloud.network.security.SecurityRule;
128128
import com.cloud.network.vpc.NetworkACL;
129129
import com.cloud.offering.NetworkOffering;
@@ -1068,38 +1068,25 @@ public KubernetesCluster createKubernetesCluster(CreateKubernetesClusterCmd cmd)
10681068
logAndThrow(Level.ERROR, String.format("Creating Kubernetes cluster failed due to error while finding suitable deployment plan for cluster in zone : %s", zone.getName()));
10691069
}
10701070

1071-
SecurityGroupVO securityGroupVO = null;
1071+
SecurityGroup securityGroup = null;
10721072
if (zone.isSecurityGroupEnabled()) {
1073-
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());
1074-
if (securityGroupVO == null) {
1075-
throw new CloudRuntimeException(String.format("Failed to create security group: %s", KubernetesClusterActionWorker.CKS_CLUSTER_SECURITY_GROUP_NAME));
1076-
}
1077-
List<String> cidrList = new ArrayList<>();
1078-
cidrList.add(NetUtils.ALL_IP4_CIDRS);
1079-
securityGroupService.authorizeSecurityGroupRule(securityGroupVO.getId(), NetUtils.TCP_PROTO,
1080-
KubernetesClusterActionWorker.CLUSTER_NODES_DEFAULT_SSH_PORT_SG, KubernetesClusterActionWorker.CLUSTER_NODES_DEFAULT_SSH_PORT_SG,
1081-
null, null, cidrList, null, SecurityRule.SecurityRuleType.IngressRule);
1082-
securityGroupService.authorizeSecurityGroupRule(securityGroupVO.getId(), NetUtils.TCP_PROTO,
1083-
KubernetesClusterActionWorker.CLUSTER_API_PORT, KubernetesClusterActionWorker.CLUSTER_API_PORT,
1084-
null, null, cidrList, null, SecurityRule.SecurityRuleType.IngressRule);
1085-
securityGroupService.authorizeSecurityGroupRule(securityGroupVO.getId(), NetUtils.ALL_PROTO,
1086-
null, null, null, null, cidrList, null, SecurityRule.SecurityRuleType.EgressRule);
1073+
securityGroup = getOrCreateSecurityGroupForAccount(owner);
10871074
}
10881075

10891076
final Network defaultNetwork = getKubernetesClusterNetworkIfMissing(cmd.getName(), zone, owner, (int)controlNodeCount, (int)clusterSize, cmd.getExternalLoadBalancerIpAddress(), cmd.getNetworkId());
10901077
final VMTemplateVO finalTemplate = getKubernetesServiceTemplate(zone, deployDestination.getCluster().getHypervisorType());
10911078
final long cores = serviceOffering.getCpu() * (controlNodeCount + clusterSize);
10921079
final long memory = serviceOffering.getRamSize() * (controlNodeCount + clusterSize);
10931080

1094-
SecurityGroupVO finalSecurityGroupVO = securityGroupVO;
1081+
final SecurityGroup finalSecurityGroup = securityGroup;
10951082
final KubernetesClusterVO cluster = Transaction.execute(new TransactionCallback<KubernetesClusterVO>() {
10961083
@Override
10971084
public KubernetesClusterVO doInTransaction(TransactionStatus status) {
10981085
KubernetesClusterVO newCluster = new KubernetesClusterVO(cmd.getName(), cmd.getDisplayName(), zone.getId(), clusterKubernetesVersion.getId(),
10991086
serviceOffering.getId(), finalTemplate.getId(), defaultNetwork.getId(), owner.getDomainId(),
11001087
owner.getAccountId(), controlNodeCount, clusterSize, KubernetesCluster.State.Created, cmd.getSSHKeyPairName(), cores, memory, cmd.getNodeRootDiskSize(), "");
11011088
if (zone.isSecurityGroupEnabled()) {
1102-
newCluster.setSecurityGroupId(finalSecurityGroupVO.getId());
1089+
newCluster.setSecurityGroupId(finalSecurityGroup.getId());
11031090
}
11041091
kubernetesClusterDao.persist(newCluster);
11051092
return newCluster;
@@ -1114,6 +1101,29 @@ public KubernetesClusterVO doInTransaction(TransactionStatus status) {
11141101
return cluster;
11151102
}
11161103

1104+
private SecurityGroup getOrCreateSecurityGroupForAccount(Account owner) {
1105+
String securityGroupName = String.format("%s-%s", KubernetesClusterActionWorker.CKS_CLUSTER_SECURITY_GROUP_NAME, owner.getUuid());
1106+
String securityGroupDesc = String.format("%s and account %s", KubernetesClusterActionWorker.CKS_SECURITY_GROUP_DESCRIPTION, owner.getName());
1107+
SecurityGroup securityGroup = securityGroupManager.getSecurityGroup(securityGroupName, owner.getId());
1108+
if (securityGroup == null) {
1109+
securityGroup = securityGroupManager.createSecurityGroup(securityGroupName, securityGroupDesc, owner.getDomainId(), owner.getId(), owner.getAccountName());
1110+
if (securityGroup == null) {
1111+
throw new CloudRuntimeException(String.format("Failed to create security group: %s", KubernetesClusterActionWorker.CKS_CLUSTER_SECURITY_GROUP_NAME));
1112+
}
1113+
List<String> cidrList = new ArrayList<>();
1114+
cidrList.add(NetUtils.ALL_IP4_CIDRS);
1115+
securityGroupService.authorizeSecurityGroupRule(securityGroup.getId(), NetUtils.TCP_PROTO,
1116+
KubernetesClusterActionWorker.CLUSTER_NODES_DEFAULT_SSH_PORT_SG, KubernetesClusterActionWorker.CLUSTER_NODES_DEFAULT_SSH_PORT_SG,
1117+
null, null, cidrList, null, SecurityRule.SecurityRuleType.IngressRule);
1118+
securityGroupService.authorizeSecurityGroupRule(securityGroup.getId(), NetUtils.TCP_PROTO,
1119+
KubernetesClusterActionWorker.CLUSTER_API_PORT, KubernetesClusterActionWorker.CLUSTER_API_PORT,
1120+
null, null, cidrList, null, SecurityRule.SecurityRuleType.IngressRule);
1121+
securityGroupService.authorizeSecurityGroupRule(securityGroup.getId(), NetUtils.ALL_PROTO,
1122+
null, null, null, null, cidrList, null, SecurityRule.SecurityRuleType.EgressRule);
1123+
}
1124+
return securityGroup;
1125+
}
1126+
11171127
/**
11181128
* Start operation can be performed at two different life stages of Kubernetes cluster. First when a freshly created cluster
11191129
* in which case there are no resources provisioned for the Kubernetes cluster. So during start all the resources

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ public class KubernetesClusterActionWorker {
106106
public static final int CLUSTER_NODES_DEFAULT_SSH_PORT_SG = DEFAULT_SSH_PORT;
107107

108108
public static final String CKS_CLUSTER_SECURITY_GROUP_NAME = "CKSSecurityGroup";
109+
public static final String CKS_SECURITY_GROUP_DESCRIPTION = "Security group for CKS nodes";
109110

110111
protected static final Logger LOGGER = Logger.getLogger(KubernetesClusterActionWorker.class);
111112

server/src/main/java/com/cloud/network/security/SecurityGroupManagerImpl.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363
import com.cloud.agent.api.SecurityGroupRulesCmd.IpPortAndProto;
6464
import com.cloud.agent.api.to.VirtualMachineTO;
6565
import com.cloud.agent.manager.Commands;
66-
import com.cloud.api.query.dao.SecurityGroupJoinDao;
6766
import com.cloud.configuration.Config;
6867
import com.cloud.domain.dao.DomainDao;
6968
import com.cloud.event.ActionEvent;
@@ -131,8 +130,6 @@ public class SecurityGroupManagerImpl extends ManagerBase implements SecurityGro
131130
@Inject
132131
SecurityGroupDao _securityGroupDao;
133132
@Inject
134-
SecurityGroupJoinDao _securityGroupJoinDao;
135-
@Inject
136133
SecurityGroupRuleDao _securityGroupRuleDao;
137134
@Inject
138135
SecurityGroupVMMapDao _securityGroupVMMapDao;
@@ -1405,7 +1402,7 @@ public boolean isVmSecurityGroupEnabled(Long vmId) {
14051402
}
14061403

14071404
@Override
1408-
public SecurityGroupVO getDefaultSecurityGroup(long accountId) {
1405+
public SecurityGroup getDefaultSecurityGroup(long accountId) {
14091406
return _securityGroupDao.findByAccountAndName(accountId, DEFAULT_GROUP_NAME);
14101407
}
14111408

0 commit comments

Comments
 (0)