Skip to content

Commit 3567819

Browse files
committed
fix automatic dns record registration based on vm and nic lifecycle
1 parent 8d10ae1 commit 3567819

File tree

9 files changed

+535
-208
lines changed

9 files changed

+535
-208
lines changed

api/src/main/java/org/apache/cloudstack/dns/DnsProviderManager.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ public interface DnsProviderManager extends Manager, PluggableService {
7575

7676
boolean disassociateZoneFromNetwork(DisassociateDnsZoneFromNetworkCmd cmd);
7777

78-
void addDnsRecordForVM(VirtualMachine instance, Network network, Nic nic);
7978
void deleteDnsRecordForVM(VirtualMachine instance, Network network, Nic nic);
8079

8180
void checkDnsServerPermission(Account caller, DnsServer dnsServer);

engine/schema/src/main/resources/META-INF/cloudstack/core/spring-engine-schema-core-daos-context.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,5 +317,6 @@
317317
<bean id="dnsZoneNetworkMapDao" class="org.apache.cloudstack.dns.dao.DnsZoneNetworkMapDaoImpl" />
318318
<bean id="dnsServerJoinDao" class="org.apache.cloudstack.dns.dao.DnsServerJoinDaoImpl" />
319319
<bean id="dnsZoneJoinDao" class="org.apache.cloudstack.dns.dao.DnsZoneJoinDaoImpl" />
320+
<bean id="dnsNicJoinDao" class="org.apache.cloudstack.dns.dao.DnsNicJoinDaoImpl" />
320321

321322
</beans>

engine/schema/src/main/resources/META-INF/db/schema-42210to42300.sql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ CALL `cloud`.`IDEMPOTENT_ADD_COLUMN`('cloud.nics','enabled', 'TINYINT(1) NOT NUL
124124
-- ======================================================================
125125

126126
-- DNS Server Table (Stores DNS Server Configurations)
127-
CREATE TABLE `cloud`.`dns_server` (
127+
CREATE TABLE IF NOT EXISTS `cloud`.`dns_server` (
128128
`id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT 'id of the dns server',
129129
`uuid` varchar(40) COMMENT 'uuid of the dns server',
130130
`name` varchar(255) NOT NULL COMMENT 'display name of the dns server',
@@ -148,7 +148,7 @@ CREATE TABLE `cloud`.`dns_server` (
148148
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
149149

150150
-- DNS Zone Table (Stores DNS Zone Metadata)
151-
CREATE TABLE `cloud`.`dns_zone` (
151+
CREATE TABLE IF NOT EXISTS `cloud`.`dns_zone` (
152152
`id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT 'id of the dns zone',
153153
`uuid` varchar(40) COMMENT 'uuid of the dns zone',
154154
`name` varchar(255) NOT NULL COMMENT 'dns zone name (e.g. example.com)',
@@ -171,7 +171,7 @@ CREATE TABLE `cloud`.`dns_zone` (
171171
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
172172

173173
-- DNS Zone Network Map (One-to-Many Link)
174-
CREATE TABLE `cloud`.`dns_zone_network_map` (
174+
CREATE TABLE IF NOT EXISTS `cloud`.`dns_zone_network_map` (
175175
`id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT 'id of the dns zone to network mapping',
176176
`uuid` varchar(40),
177177
`dns_zone_id` bigint(20) unsigned NOT NULL,
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
-- Licensed to the Apache Software Foundation (ASF) under one
2+
-- or more contributor license agreements. See the NOTICE file
3+
-- distributed with this work for additional information
4+
-- regarding copyright ownership. The ASF licenses this file
5+
-- to you under the Apache License, Version 2.0 (the
6+
-- "License"); you may not use this file except in compliance
7+
-- with the License. You may obtain a copy of the License at
8+
--
9+
-- http://www.apache.org/licenses/LICENSE-2.0
10+
--
11+
-- Unless required by applicable law or agreed to in writing,
12+
-- software distributed under the License is distributed on an
13+
-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
-- KIND, either express or implied. See the License for the
15+
-- specific language governing permissions and limitations
16+
-- under the License.
17+
18+
-- VIEW `cloud`.`dns_nic_view`;
19+
20+
DROP VIEW IF EXISTS `cloud`.`dns_nic_view`;
21+
CREATE VIEW `cloud`.`dns_nic_view` AS
22+
SELECT
23+
n.id AS id,
24+
n.uuid AS uuid,
25+
n.instance_id AS instance_id,
26+
n.network_id AS network_id,
27+
n.ip4_address AS ip4_address,
28+
n.ip6_address AS ip6_address,
29+
n.removed AS removed,
30+
nd.value AS nic_dns_url,
31+
map.dns_zone_id AS dns_zone_id,
32+
map.sub_domain AS sub_domain
33+
FROM
34+
`cloud`.`nics` n
35+
INNER JOIN
36+
`cloud`.`dns_zone_network_map` map ON n.network_id = map.network_id
37+
LEFT JOIN
38+
`cloud`.`nic_details` nd ON n.id = nd.nic_id AND nd.name = 'nicdnsrecord'
39+
WHERE
40+
map.removed IS NULL;

server/src/main/java/org/apache/cloudstack/dns/DnsProviderManagerImpl.java

Lines changed: 218 additions & 97 deletions
Large diffs are not rendered by default.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.apache.cloudstack.dns.dao;
19+
20+
import java.util.List;
21+
22+
import org.apache.cloudstack.dns.vo.DnsNicJoinVO;
23+
24+
import com.cloud.utils.db.GenericDao;
25+
26+
public interface DnsNicJoinDao extends GenericDao<DnsNicJoinVO, Long> {
27+
28+
/**
29+
* Used for Collision Checks.
30+
* @param dnsRecordUrl
31+
* @param dnsZoneId
32+
* @return active records to see who currently owns the dnsRecordUrl.
33+
*/
34+
DnsNicJoinVO findActiveByDnsRecordAndZone(String dnsRecordUrl, long dnsZoneId);
35+
36+
/**
37+
* Used to sync DNS record url based on available ips for vmId in the dnsZone
38+
* @param vmId
39+
* @param dnsZoneId
40+
* @param dnsRecordUrl
41+
* @return list of active nics using the dnsRecordUrl, supports null vmId for dnsZone wide query
42+
*/
43+
List<DnsNicJoinVO> listActiveByVmIdZoneAndDnsRecord(Long vmId, long dnsZoneId, String dnsRecordUrl);
44+
45+
/**
46+
* Used for VM Start/Running
47+
* @param vmId
48+
* @return records associated to vmId
49+
*/
50+
List<DnsNicJoinVO> listActiveByVmId(long vmId);
51+
52+
/**
53+
* Used by Instance Destroy/Stop or NIC delete
54+
* @param vmId
55+
* @return records with soft-delete
56+
*/
57+
List<DnsNicJoinVO> listIncludingRemovedByVmId(long vmId);
58+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.apache.cloudstack.dns.dao;
19+
20+
import java.util.List;
21+
22+
import org.apache.cloudstack.api.ApiConstants;
23+
import org.apache.cloudstack.dns.vo.DnsNicJoinVO;
24+
25+
import com.cloud.utils.db.GenericDaoBase;
26+
import com.cloud.utils.db.SearchBuilder;
27+
import com.cloud.utils.db.SearchCriteria;
28+
29+
public class DnsNicJoinDaoImpl extends GenericDaoBase<DnsNicJoinVO, Long> implements DnsNicJoinDao {
30+
private final SearchBuilder<DnsNicJoinVO> activeDnsRecordZoneSearch;
31+
private final SearchBuilder<DnsNicJoinVO> activeVmZoneDnsRecordSearch; // Route for null vmId
32+
private final SearchBuilder<DnsNicJoinVO> activeVmSearch;
33+
34+
public DnsNicJoinDaoImpl() {
35+
36+
activeDnsRecordZoneSearch = createSearchBuilder();
37+
activeDnsRecordZoneSearch.and(ApiConstants.NIC_DNS_RECORD, activeDnsRecordZoneSearch.entity().getNicDnsUrl(), SearchCriteria.Op.EQ);
38+
activeDnsRecordZoneSearch.and(ApiConstants.DNS_ZONE_ID, activeDnsRecordZoneSearch.entity().getDnsZoneId(), SearchCriteria.Op.EQ);
39+
activeDnsRecordZoneSearch.and(ApiConstants.REMOVED, activeDnsRecordZoneSearch.entity().getRemoved(), SearchCriteria.Op.NULL);
40+
activeDnsRecordZoneSearch.done();
41+
42+
activeVmZoneDnsRecordSearch = createSearchBuilder();
43+
activeVmZoneDnsRecordSearch.and(ApiConstants.INSTANCE_ID, activeVmZoneDnsRecordSearch.entity().getInstanceId(), SearchCriteria.Op.EQ);
44+
activeVmZoneDnsRecordSearch.and(ApiConstants.NIC_DNS_RECORD, activeVmZoneDnsRecordSearch.entity().getNicDnsUrl(), SearchCriteria.Op.EQ);
45+
activeVmZoneDnsRecordSearch.and(ApiConstants.DNS_ZONE_ID, activeVmZoneDnsRecordSearch.entity().getDnsZoneId(), SearchCriteria.Op.EQ);
46+
activeVmZoneDnsRecordSearch.and(ApiConstants.REMOVED, activeVmZoneDnsRecordSearch.entity().getRemoved(), SearchCriteria.Op.NULL);
47+
activeVmZoneDnsRecordSearch.done();
48+
49+
activeVmSearch = createSearchBuilder();
50+
activeVmSearch.and(ApiConstants.INSTANCE_ID, activeVmSearch.entity().getInstanceId(), SearchCriteria.Op.EQ);
51+
activeVmSearch.done();
52+
}
53+
54+
@Override
55+
public DnsNicJoinVO findActiveByDnsRecordAndZone(String dnsRecordUrl, long dnsZoneId) {
56+
SearchCriteria<DnsNicJoinVO> sc = activeDnsRecordZoneSearch.create();
57+
sc.setParameters(ApiConstants.NIC_DNS_RECORD, dnsRecordUrl);
58+
sc.setParameters(ApiConstants.DNS_ZONE_ID, dnsZoneId);
59+
return findOneBy(sc);
60+
}
61+
62+
@Override
63+
public List<DnsNicJoinVO> listActiveByVmIdZoneAndDnsRecord(Long vmId, long dnsZoneId, String dnsRecordUrl) {
64+
if (vmId != null) {
65+
SearchCriteria<DnsNicJoinVO> sc = activeDnsRecordZoneSearch.create();
66+
sc.setParameters(ApiConstants.INSTANCE_ID, vmId);
67+
sc.setParameters(ApiConstants.DNS_ZONE_ID, dnsZoneId);
68+
sc.setParameters(ApiConstants.NIC_DNS_RECORD, dnsRecordUrl);
69+
return listBy(sc);
70+
} else {
71+
SearchCriteria<DnsNicJoinVO> sc = activeDnsRecordZoneSearch.create();
72+
sc.setParameters(ApiConstants.NIC_DNS_RECORD, dnsRecordUrl);
73+
sc.setParameters(ApiConstants.DNS_ZONE_ID, dnsZoneId);
74+
return listBy(sc);
75+
}
76+
}
77+
78+
@Override
79+
public List<DnsNicJoinVO> listActiveByVmId(long vmId) {
80+
SearchCriteria<DnsNicJoinVO> sc = activeVmSearch.create();
81+
sc.setParameters(ApiConstants.INSTANCE_ID, vmId);
82+
return listBy(sc);
83+
}
84+
85+
@Override
86+
public List<DnsNicJoinVO> listIncludingRemovedByVmId(long vmId) {
87+
SearchCriteria<DnsNicJoinVO> sc = activeVmSearch.create();
88+
sc.setParameters(ApiConstants.INSTANCE_ID, vmId);
89+
return listIncludingRemovedBy(sc);
90+
}
91+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.cloudstack.dns.vo;
21+
22+
import java.util.Date;
23+
24+
import javax.persistence.Column;
25+
import javax.persistence.Entity;
26+
import javax.persistence.GeneratedValue;
27+
import javax.persistence.GenerationType;
28+
import javax.persistence.Id;
29+
import javax.persistence.Table;
30+
31+
import org.apache.cloudstack.api.Identity;
32+
import org.apache.cloudstack.api.InternalIdentity;
33+
34+
import com.cloud.api.query.vo.BaseViewVO;
35+
36+
@Entity
37+
@Table(name = "dns_nic_view")
38+
public class DnsNicJoinVO extends BaseViewVO implements InternalIdentity, Identity {
39+
@Id
40+
@GeneratedValue(strategy = GenerationType.IDENTITY)
41+
@Column(name = "id")
42+
private long id;
43+
44+
@Column(name = "uuid")
45+
private String uuid;
46+
47+
@Column(name = "instance_id")
48+
private long instanceId;
49+
50+
@Column(name = "network_id")
51+
private long networkId;
52+
53+
@Column(name = "ip4_address")
54+
private String ip4Address;
55+
56+
@Column(name = "ip6_address")
57+
private String ip6Address;
58+
59+
@Column(name = "nic_dns_url")
60+
private String nicDnsUrl;
61+
62+
@Column(name = "dns_zone_id")
63+
private long dnsZoneId;
64+
65+
@Column(name = "sub_domain")
66+
private String subDomain;
67+
68+
@Column(name = "removed")
69+
private Date removed;
70+
71+
public DnsNicJoinVO() {
72+
}
73+
74+
@Override
75+
public long getId() {
76+
return id;
77+
}
78+
79+
@Override
80+
public String getUuid() {
81+
return uuid;
82+
}
83+
84+
public long getInstanceId() {
85+
return instanceId;
86+
}
87+
88+
public long getNetworkId() {
89+
return networkId;
90+
}
91+
92+
public long getDnsZoneId() {
93+
return dnsZoneId;
94+
}
95+
96+
public String getSubDomain() {
97+
return subDomain;
98+
}
99+
100+
public String getNicDnsUrl() {
101+
return nicDnsUrl;
102+
}
103+
104+
public String getIp4Address() {
105+
return ip4Address;
106+
}
107+
108+
public String getIp6Address() {
109+
return ip6Address;
110+
}
111+
112+
public Date getRemoved() {
113+
return removed;
114+
}
115+
}

0 commit comments

Comments
 (0)