Skip to content

Commit 2253a33

Browse files
committed
Merge remote-tracking branch 'apache/4.18'
2 parents 33e2a4d + ab70108 commit 2253a33

3 files changed

Lines changed: 30 additions & 22 deletions

File tree

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
@@ -131,9 +131,9 @@
131131
import com.cloud.network.router.NetworkHelper;
132132
import com.cloud.network.rules.FirewallRule;
133133
import com.cloud.network.rules.FirewallRuleVO;
134+
import com.cloud.network.security.SecurityGroup;
134135
import com.cloud.network.security.SecurityGroupManager;
135136
import com.cloud.network.security.SecurityGroupService;
136-
import com.cloud.network.security.SecurityGroupVO;
137137
import com.cloud.network.security.SecurityRule;
138138
import com.cloud.network.vpc.NetworkACL;
139139
import com.cloud.offering.NetworkOffering;
@@ -1213,30 +1213,17 @@ public KubernetesCluster createManagedKubernetesCluster(CreateKubernetesClusterC
12131213
logAndThrow(Level.ERROR, String.format("Creating Kubernetes cluster failed due to error while finding suitable deployment plan for cluster in zone : %s", zone.getName()));
12141214
}
12151215

1216-
SecurityGroupVO securityGroupVO = null;
1216+
SecurityGroup securityGroup = null;
12171217
if (zone.isSecurityGroupEnabled()) {
1218-
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());
1219-
if (securityGroupVO == null) {
1220-
throw new CloudRuntimeException(String.format("Failed to create security group: %s", KubernetesClusterActionWorker.CKS_CLUSTER_SECURITY_GROUP_NAME));
1221-
}
1222-
List<String> cidrList = new ArrayList<>();
1223-
cidrList.add(NetUtils.ALL_IP4_CIDRS);
1224-
securityGroupService.authorizeSecurityGroupRule(securityGroupVO.getId(), NetUtils.TCP_PROTO,
1225-
KubernetesClusterActionWorker.CLUSTER_NODES_DEFAULT_SSH_PORT_SG, KubernetesClusterActionWorker.CLUSTER_NODES_DEFAULT_SSH_PORT_SG,
1226-
null, null, cidrList, null, SecurityRule.SecurityRuleType.IngressRule);
1227-
securityGroupService.authorizeSecurityGroupRule(securityGroupVO.getId(), NetUtils.TCP_PROTO,
1228-
KubernetesClusterActionWorker.CLUSTER_API_PORT, KubernetesClusterActionWorker.CLUSTER_API_PORT,
1229-
null, null, cidrList, null, SecurityRule.SecurityRuleType.IngressRule);
1230-
securityGroupService.authorizeSecurityGroupRule(securityGroupVO.getId(), NetUtils.ALL_PROTO,
1231-
null, null, null, null, cidrList, null, SecurityRule.SecurityRuleType.EgressRule);
1218+
securityGroup = getOrCreateSecurityGroupForAccount(owner);
12321219
}
12331220

12341221
final Network defaultNetwork = getKubernetesClusterNetworkIfMissing(cmd.getName(), zone, owner, (int)controlNodeCount, (int)clusterSize, cmd.getExternalLoadBalancerIpAddress(), cmd.getNetworkId());
12351222
final VMTemplateVO finalTemplate = getKubernetesServiceTemplate(zone, deployDestination.getCluster().getHypervisorType());
12361223
final long cores = serviceOffering.getCpu() * (controlNodeCount + clusterSize);
12371224
final long memory = serviceOffering.getRamSize() * (controlNodeCount + clusterSize);
12381225

1239-
SecurityGroupVO finalSecurityGroupVO = securityGroupVO;
1226+
final SecurityGroup finalSecurityGroup = securityGroup;
12401227
final KubernetesClusterVO cluster = Transaction.execute(new TransactionCallback<KubernetesClusterVO>() {
12411228
@Override
12421229
public KubernetesClusterVO doInTransaction(TransactionStatus status) {
@@ -1245,7 +1232,7 @@ public KubernetesClusterVO doInTransaction(TransactionStatus status) {
12451232
owner.getAccountId(), controlNodeCount, clusterSize, KubernetesCluster.State.Created, cmd.getSSHKeyPairName(), cores, memory,
12461233
cmd.getNodeRootDiskSize(), "", KubernetesCluster.ClusterType.CloudManaged);
12471234
if (zone.isSecurityGroupEnabled()) {
1248-
newCluster.setSecurityGroupId(finalSecurityGroupVO.getId());
1235+
newCluster.setSecurityGroupId(finalSecurityGroup.getId());
12491236
}
12501237
kubernetesClusterDao.persist(newCluster);
12511238
return newCluster;
@@ -1260,6 +1247,29 @@ public KubernetesClusterVO doInTransaction(TransactionStatus status) {
12601247
return cluster;
12611248
}
12621249

1250+
private SecurityGroup getOrCreateSecurityGroupForAccount(Account owner) {
1251+
String securityGroupName = String.format("%s-%s", KubernetesClusterActionWorker.CKS_CLUSTER_SECURITY_GROUP_NAME, owner.getUuid());
1252+
String securityGroupDesc = String.format("%s and account %s", KubernetesClusterActionWorker.CKS_SECURITY_GROUP_DESCRIPTION, owner.getName());
1253+
SecurityGroup securityGroup = securityGroupManager.getSecurityGroup(securityGroupName, owner.getId());
1254+
if (securityGroup == null) {
1255+
securityGroup = securityGroupManager.createSecurityGroup(securityGroupName, securityGroupDesc, owner.getDomainId(), owner.getId(), owner.getAccountName());
1256+
if (securityGroup == null) {
1257+
throw new CloudRuntimeException(String.format("Failed to create security group: %s", KubernetesClusterActionWorker.CKS_CLUSTER_SECURITY_GROUP_NAME));
1258+
}
1259+
List<String> cidrList = new ArrayList<>();
1260+
cidrList.add(NetUtils.ALL_IP4_CIDRS);
1261+
securityGroupService.authorizeSecurityGroupRule(securityGroup.getId(), NetUtils.TCP_PROTO,
1262+
KubernetesClusterActionWorker.CLUSTER_NODES_DEFAULT_SSH_PORT_SG, KubernetesClusterActionWorker.CLUSTER_NODES_DEFAULT_SSH_PORT_SG,
1263+
null, null, cidrList, null, SecurityRule.SecurityRuleType.IngressRule);
1264+
securityGroupService.authorizeSecurityGroupRule(securityGroup.getId(), NetUtils.TCP_PROTO,
1265+
KubernetesClusterActionWorker.CLUSTER_API_PORT, KubernetesClusterActionWorker.CLUSTER_API_PORT,
1266+
null, null, cidrList, null, SecurityRule.SecurityRuleType.IngressRule);
1267+
securityGroupService.authorizeSecurityGroupRule(securityGroup.getId(), NetUtils.ALL_PROTO,
1268+
null, null, null, null, cidrList, null, SecurityRule.SecurityRuleType.EgressRule);
1269+
}
1270+
return securityGroup;
1271+
}
1272+
12631273
/**
12641274
* Start operation can be performed at two different life stages of Kubernetes cluster. First when a freshly created cluster
12651275
* 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)