mirror of
				https://github.com/TeaOSLab/EdgeAPI.git
				synced 2025-11-04 16:00:24 +08:00 
			
		
		
		
	可以设置集群的DNS记录TTL
This commit is contained in:
		@@ -141,6 +141,8 @@ func (this *NodeClusterDAO) CreateCluster(tx *dbs.Tx, adminId int64, name string
 | 
			
		||||
	dnsConfig := &dnsconfigs.ClusterDNSConfig{
 | 
			
		||||
		NodesAutoSync:   true,
 | 
			
		||||
		ServersAutoSync: true,
 | 
			
		||||
		CNameRecords:    []string{},
 | 
			
		||||
		TTL:             0,
 | 
			
		||||
	}
 | 
			
		||||
	dnsJSON, err := json.Marshal(dnsConfig)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
@@ -436,7 +438,7 @@ func (this *NodeClusterDAO) ExistClusterDNSName(tx *dbs.Tx, dnsName string, excl
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// UpdateClusterDNS 修改集群DNS相关信息
 | 
			
		||||
func (this *NodeClusterDAO) UpdateClusterDNS(tx *dbs.Tx, clusterId int64, dnsName string, dnsDomainId int64, nodesAutoSync bool, serversAutoSync bool, cnameRecords []string) error {
 | 
			
		||||
func (this *NodeClusterDAO) UpdateClusterDNS(tx *dbs.Tx, clusterId int64, dnsName string, dnsDomainId int64, nodesAutoSync bool, serversAutoSync bool, cnameRecords []string, ttl int32) error {
 | 
			
		||||
	if clusterId <= 0 {
 | 
			
		||||
		return errors.New("invalid clusterId")
 | 
			
		||||
	}
 | 
			
		||||
@@ -453,6 +455,7 @@ func (this *NodeClusterDAO) UpdateClusterDNS(tx *dbs.Tx, clusterId int64, dnsNam
 | 
			
		||||
		NodesAutoSync:   nodesAutoSync,
 | 
			
		||||
		ServersAutoSync: serversAutoSync,
 | 
			
		||||
		CNameRecords:    cnameRecords,
 | 
			
		||||
		TTL:             ttl,
 | 
			
		||||
	}
 | 
			
		||||
	dnsJSON, err := json.Marshal(dnsConfig)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
 
 | 
			
		||||
@@ -15,4 +15,5 @@ type Record struct {
 | 
			
		||||
	Type  RecordType `json:"type"`
 | 
			
		||||
	Value string     `json:"value"`
 | 
			
		||||
	Route string     `json:"route"`
 | 
			
		||||
	TTL   int32      `json:"ttl"`
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -7,6 +7,7 @@ import (
 | 
			
		||||
	"github.com/aliyun/alibaba-cloud-sdk-go/sdk/responses"
 | 
			
		||||
	"github.com/aliyun/alibaba-cloud-sdk-go/services/alidns"
 | 
			
		||||
	"github.com/iwind/TeaGo/maps"
 | 
			
		||||
	"github.com/iwind/TeaGo/types"
 | 
			
		||||
	"strings"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
@@ -87,6 +88,7 @@ func (this *AliDNSProvider) GetRecords(domain string) (records []*dnstypes.Recor
 | 
			
		||||
				Type:  record.Type,
 | 
			
		||||
				Value: record.Value,
 | 
			
		||||
				Route: record.Line,
 | 
			
		||||
				TTL:   types.Int32(record.TTL),
 | 
			
		||||
			})
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
@@ -141,6 +143,10 @@ func (this *AliDNSProvider) AddRecord(domain string, newRecord *dnstypes.Record)
 | 
			
		||||
	req.DomainName = domain
 | 
			
		||||
	req.Line = newRecord.Route
 | 
			
		||||
 | 
			
		||||
	if newRecord.TTL > 0 {
 | 
			
		||||
		req.TTL = requests.NewInteger(types.Int(newRecord.TTL))
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	resp := alidns.CreateAddDomainRecordResponse()
 | 
			
		||||
	err := this.doAPI(req, resp)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
@@ -162,6 +168,10 @@ func (this *AliDNSProvider) UpdateRecord(domain string, record *dnstypes.Record,
 | 
			
		||||
	req.Value = newRecord.Value
 | 
			
		||||
	req.Line = newRecord.Route
 | 
			
		||||
 | 
			
		||||
	if newRecord.TTL > 0 {
 | 
			
		||||
		req.TTL = requests.NewInteger(types.Int(newRecord.TTL))
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	resp := alidns.CreateUpdateDomainRecordResponse()
 | 
			
		||||
	err := this.doAPI(req, resp)
 | 
			
		||||
	return err
 | 
			
		||||
 
 | 
			
		||||
@@ -10,6 +10,7 @@ import (
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/dnstypes"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/errors"
 | 
			
		||||
	"github.com/iwind/TeaGo/maps"
 | 
			
		||||
	"github.com/iwind/TeaGo/types"
 | 
			
		||||
	"io"
 | 
			
		||||
	"io/ioutil"
 | 
			
		||||
	"net/http"
 | 
			
		||||
@@ -158,6 +159,7 @@ func (this *CloudFlareProvider) QueryRecord(domain string, name string, recordTy
 | 
			
		||||
		Name:  record.Name,
 | 
			
		||||
		Type:  record.Type,
 | 
			
		||||
		Value: record.Content,
 | 
			
		||||
		TTL:   types.Int32(record.Ttl),
 | 
			
		||||
		Route: CloudFlareDefaultRoute,
 | 
			
		||||
	}, nil
 | 
			
		||||
}
 | 
			
		||||
@@ -170,11 +172,17 @@ func (this *CloudFlareProvider) AddRecord(domain string, newRecord *dnstypes.Rec
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	resp := new(cloudflare.CreateDNSRecordResponse)
 | 
			
		||||
 | 
			
		||||
	var ttl = newRecord.TTL
 | 
			
		||||
	if ttl <= 0 {
 | 
			
		||||
		ttl = 1 // 自动默认
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	err = this.doAPI(http.MethodPost, "zones/"+zoneId+"/dns_records", nil, maps.Map{
 | 
			
		||||
		"type":    newRecord.Type,
 | 
			
		||||
		"name":    newRecord.Name + "." + domain,
 | 
			
		||||
		"content": newRecord.Value,
 | 
			
		||||
		"ttl":     1,
 | 
			
		||||
		"ttl":     ttl,
 | 
			
		||||
	}, resp)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
@@ -189,12 +197,17 @@ func (this *CloudFlareProvider) UpdateRecord(domain string, record *dnstypes.Rec
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var ttl = newRecord.TTL
 | 
			
		||||
	if ttl <= 0 {
 | 
			
		||||
		ttl = 1 // 自动默认
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	resp := new(cloudflare.UpdateDNSRecordResponse)
 | 
			
		||||
	return this.doAPI(http.MethodPut, "zones/"+zoneId+"/dns_records/"+record.Id, nil, maps.Map{
 | 
			
		||||
		"type":    newRecord.Type,
 | 
			
		||||
		"name":    newRecord.Name + "." + domain,
 | 
			
		||||
		"content": newRecord.Value,
 | 
			
		||||
		"ttl":     1,
 | 
			
		||||
		"ttl":     ttl,
 | 
			
		||||
	}, resp)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -98,6 +98,7 @@ func TestCloudFlareProvider_AddRecord(t *testing.T) {
 | 
			
		||||
			Type:  dnstypes.RecordTypeA,
 | 
			
		||||
			Value: "182.92.212.46",
 | 
			
		||||
			Route: "",
 | 
			
		||||
			TTL:   300,
 | 
			
		||||
		})
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			t.Fatal(err)
 | 
			
		||||
 
 | 
			
		||||
@@ -13,6 +13,10 @@ import (
 | 
			
		||||
	"strings"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
	DNSPodMaxTTL int32 = 604800
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// DNSPodProvider DNSPod服务商
 | 
			
		||||
type DNSPodProvider struct {
 | 
			
		||||
	BaseProvider
 | 
			
		||||
@@ -94,6 +98,7 @@ func (this *DNSPodProvider) GetRecords(domain string) (records []*dnstypes.Recor
 | 
			
		||||
				Type:  recordMap.GetString("type"),
 | 
			
		||||
				Value: recordMap.GetString("value"),
 | 
			
		||||
				Route: recordMap.GetString("line"),
 | 
			
		||||
				TTL:   recordMap.GetInt32("ttl"),
 | 
			
		||||
			})
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
@@ -165,13 +170,18 @@ func (this *DNSPodProvider) AddRecord(domain string, newRecord *dnstypes.Record)
 | 
			
		||||
	if newRecord.Type == dnstypes.RecordTypeCNAME && !strings.HasSuffix(newRecord.Value, ".") {
 | 
			
		||||
		newRecord.Value += "."
 | 
			
		||||
	}
 | 
			
		||||
	_, err := this.post("/Record.Create", map[string]string{
 | 
			
		||||
 | 
			
		||||
	var args = map[string]string{
 | 
			
		||||
		"domain":      domain,
 | 
			
		||||
		"sub_domain":  newRecord.Name,
 | 
			
		||||
		"record_type": newRecord.Type,
 | 
			
		||||
		"value":       newRecord.Value,
 | 
			
		||||
		"record_line": newRecord.Route,
 | 
			
		||||
	})
 | 
			
		||||
	}
 | 
			
		||||
	if newRecord.TTL > 0 && newRecord.TTL <= DNSPodMaxTTL {
 | 
			
		||||
		args["ttl"] = types.String(newRecord.TTL)
 | 
			
		||||
	}
 | 
			
		||||
	_, err := this.post("/Record.Create", args)
 | 
			
		||||
	return err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -188,14 +198,19 @@ func (this *DNSPodProvider) UpdateRecord(domain string, record *dnstypes.Record,
 | 
			
		||||
	if newRecord.Type == dnstypes.RecordTypeCNAME && !strings.HasSuffix(newRecord.Value, ".") {
 | 
			
		||||
		newRecord.Value += "."
 | 
			
		||||
	}
 | 
			
		||||
	_, err := this.post("/Record.Modify", map[string]string{
 | 
			
		||||
 | 
			
		||||
	var args = map[string]string{
 | 
			
		||||
		"domain":      domain,
 | 
			
		||||
		"record_id":   record.Id,
 | 
			
		||||
		"sub_domain":  newRecord.Name,
 | 
			
		||||
		"record_type": newRecord.Type,
 | 
			
		||||
		"value":       newRecord.Value,
 | 
			
		||||
		"record_line": newRecord.Route,
 | 
			
		||||
	})
 | 
			
		||||
	}
 | 
			
		||||
	if newRecord.TTL > 0 && newRecord.TTL <= DNSPodMaxTTL {
 | 
			
		||||
		args["ttl"] = types.String(newRecord.TTL)
 | 
			
		||||
	}
 | 
			
		||||
	_, err := this.post("/Record.Modify", args)
 | 
			
		||||
	return err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -58,6 +58,7 @@ func TestDNSPodProvider_AddRecord(t *testing.T) {
 | 
			
		||||
		Name:  "hello-forward",
 | 
			
		||||
		Value: "hello.yun4s.cn",
 | 
			
		||||
		Route: "联通",
 | 
			
		||||
		TTL:   300,
 | 
			
		||||
	})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Fatal(err)
 | 
			
		||||
 
 | 
			
		||||
@@ -35,6 +35,8 @@ var huaweiDNSHTTPClient = &http.Client{
 | 
			
		||||
	},
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// HuaweiDNSProvider 华为云DNS
 | 
			
		||||
// 相关文档链接:https://support.huaweicloud.com/api-dns/dns_api_62001.html
 | 
			
		||||
type HuaweiDNSProvider struct {
 | 
			
		||||
	BaseProvider
 | 
			
		||||
 | 
			
		||||
@@ -100,6 +102,7 @@ func (this *HuaweiDNSProvider) GetRecords(domain string) (records []*dnstypes.Re
 | 
			
		||||
					Type:  recordSet.Type,
 | 
			
		||||
					Value: value,
 | 
			
		||||
					Route: recordSet.Line,
 | 
			
		||||
					TTL:   types.Int32(recordSet.Ttl),
 | 
			
		||||
				})
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
@@ -1320,6 +1323,7 @@ func (this *HuaweiDNSProvider) QueryRecord(domain string, name string, recordTyp
 | 
			
		||||
		Type:  recordType,
 | 
			
		||||
		Value: recordSet.Records[0],
 | 
			
		||||
		Route: recordSet.Line,
 | 
			
		||||
		TTL:   types.Int32(recordSet.Ttl),
 | 
			
		||||
	}, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -1331,12 +1335,17 @@ func (this *HuaweiDNSProvider) AddRecord(domain string, newRecord *dnstypes.Reco
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var resp = new(huaweidns.ZonesCreateRecordSetResponse)
 | 
			
		||||
	var ttl = newRecord.TTL
 | 
			
		||||
	if ttl <= 0 {
 | 
			
		||||
		ttl = 300
 | 
			
		||||
	}
 | 
			
		||||
	err = this.doAPI(http.MethodPost, "/v2.1/zones/"+zoneId+"/recordsets", map[string]string{}, maps.Map{
 | 
			
		||||
		"name":        newRecord.Name + "." + domain + ".",
 | 
			
		||||
		"description": "CDN系统自动创建",
 | 
			
		||||
		"type":        newRecord.Type,
 | 
			
		||||
		"records":     []string{newRecord.Value},
 | 
			
		||||
		"line":        newRecord.Route,
 | 
			
		||||
		"ttl":         ttl,
 | 
			
		||||
	}, resp)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
@@ -1362,6 +1371,11 @@ func (this *HuaweiDNSProvider) UpdateRecord(domain string, record *dnstypes.Reco
 | 
			
		||||
		recordId = record.Id
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var ttl = newRecord.TTL
 | 
			
		||||
	if ttl <= 0 {
 | 
			
		||||
		ttl = 300
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var resp = new(huaweidns.ZonesUpdateRecordSetResponse)
 | 
			
		||||
	err = this.doAPI(http.MethodPut, "/v2.1/zones/"+zoneId+"/recordsets/"+recordId, map[string]string{}, maps.Map{
 | 
			
		||||
		"name":        newRecord.Name + "." + domain + ".",
 | 
			
		||||
@@ -1369,6 +1383,7 @@ func (this *HuaweiDNSProvider) UpdateRecord(domain string, record *dnstypes.Reco
 | 
			
		||||
		"type":        newRecord.Type,
 | 
			
		||||
		"records":     []string{newRecord.Value},
 | 
			
		||||
		"line":        newRecord.Route, // TODO 华为云此API无法修改线路,API地址:https://support.huaweicloud.com/api-dns/dns_api_65006.html
 | 
			
		||||
		"ttl":         ttl,
 | 
			
		||||
	}, resp)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
 
 | 
			
		||||
@@ -71,6 +71,7 @@ func TestHuaweiDNSProvider_AddRecord(t *testing.T) {
 | 
			
		||||
		Type:  "A",
 | 
			
		||||
		Value: "192.168.2.40",
 | 
			
		||||
		Route: "Beijing",
 | 
			
		||||
		TTL: 120,
 | 
			
		||||
	}
 | 
			
		||||
	err = provider.AddRecord("yun4s.cn", record)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
 
 | 
			
		||||
@@ -83,6 +83,7 @@ func (this *LocalEdgeDNSProvider) GetRecords(domain string) (records []*dnstypes
 | 
			
		||||
				Type:  record.Type,
 | 
			
		||||
				Value: record.Value,
 | 
			
		||||
				Route: routeIds[0],
 | 
			
		||||
				TTL:   types.Int32(record.Ttl),
 | 
			
		||||
			})
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
@@ -183,6 +184,7 @@ func (this *LocalEdgeDNSProvider) QueryRecord(domain string, name string, record
 | 
			
		||||
		Type:  record.Type,
 | 
			
		||||
		Value: record.Value,
 | 
			
		||||
		Route: routeIdString,
 | 
			
		||||
		TTL:   types.Int32(record.Ttl),
 | 
			
		||||
	}, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -202,7 +204,10 @@ func (this *LocalEdgeDNSProvider) AddRecord(domain string, newRecord *dnstypes.R
 | 
			
		||||
		routeIds = append(routeIds, newRecord.Route)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	_, err = nameservers.SharedNSRecordDAO.CreateRecord(tx, domainId, "", newRecord.Name, newRecord.Type, newRecord.Value, this.ttl, routeIds)
 | 
			
		||||
	if newRecord.TTL <= 0 {
 | 
			
		||||
		newRecord.TTL = this.ttl
 | 
			
		||||
	}
 | 
			
		||||
	_, err = nameservers.SharedNSRecordDAO.CreateRecord(tx, domainId, "", newRecord.Name, newRecord.Type, newRecord.Value, newRecord.TTL, routeIds)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
@@ -226,8 +231,12 @@ func (this *LocalEdgeDNSProvider) UpdateRecord(domain string, record *dnstypes.R
 | 
			
		||||
		routeIds = append(routeIds, newRecord.Route)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if newRecord.TTL <= 0 {
 | 
			
		||||
		newRecord.TTL = this.ttl
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if len(record.Id) > 0 {
 | 
			
		||||
		err = nameservers.SharedNSRecordDAO.UpdateRecord(tx, types.Int64(record.Id), "", newRecord.Name, newRecord.Type, newRecord.Value, this.ttl, routeIds, true)
 | 
			
		||||
		err = nameservers.SharedNSRecordDAO.UpdateRecord(tx, types.Int64(record.Id), "", newRecord.Name, newRecord.Type, newRecord.Value, newRecord.TTL, routeIds, true)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
@@ -237,7 +246,7 @@ func (this *LocalEdgeDNSProvider) UpdateRecord(domain string, record *dnstypes.R
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
		if realRecord != nil {
 | 
			
		||||
			err = nameservers.SharedNSRecordDAO.UpdateRecord(tx, types.Int64(realRecord.Id), "", newRecord.Name, newRecord.Type, newRecord.Value, this.ttl, routeIds, true)
 | 
			
		||||
			err = nameservers.SharedNSRecordDAO.UpdateRecord(tx, types.Int64(realRecord.Id), "", newRecord.Name, newRecord.Type, newRecord.Value, newRecord.TTL, routeIds, true)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return err
 | 
			
		||||
			}
 | 
			
		||||
 
 | 
			
		||||
@@ -101,6 +101,7 @@ func TestLocalEdgeDNSProvider_AddRecord(t *testing.T) {
 | 
			
		||||
		Type:  dnstypes.RecordTypeA,
 | 
			
		||||
		Value: "10.0.0.1",
 | 
			
		||||
		Route: "id:7",
 | 
			
		||||
		TTL:   300,
 | 
			
		||||
	})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Fatal(err)
 | 
			
		||||
 
 | 
			
		||||
@@ -404,10 +404,14 @@ func (this *DNSDomainService) findClusterDNSChanges(cluster *models.NodeCluster,
 | 
			
		||||
 | 
			
		||||
	// 自动设置的cname记录
 | 
			
		||||
	var cnameRecords = []string{}
 | 
			
		||||
	var ttl int32
 | 
			
		||||
	if len(cluster.Dns) > 0 {
 | 
			
		||||
		dnsConfig, _ := cluster.DecodeDNSConfig()
 | 
			
		||||
		if dnsConfig != nil {
 | 
			
		||||
			cnameRecords = dnsConfig.CNameRecords
 | 
			
		||||
			if dnsConfig.TTL > 0 {
 | 
			
		||||
				ttl = dnsConfig.TTL
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@@ -473,6 +477,7 @@ func (this *DNSDomainService) findClusterDNSChanges(cluster *models.NodeCluster,
 | 
			
		||||
							Type:  recordType,
 | 
			
		||||
							Value: ip,
 | 
			
		||||
							Route: route,
 | 
			
		||||
							TTL:   ttl,
 | 
			
		||||
						},
 | 
			
		||||
					})
 | 
			
		||||
					nodesChanged = true
 | 
			
		||||
@@ -529,6 +534,7 @@ func (this *DNSDomainService) findClusterDNSChanges(cluster *models.NodeCluster,
 | 
			
		||||
					Type:  dnstypes.RecordTypeCNAME,
 | 
			
		||||
					Value: clusterDomain + ".",
 | 
			
		||||
					Route: "", // 注意这里为空,需要在执行过程中获取默认值
 | 
			
		||||
					TTL:   ttl,
 | 
			
		||||
				},
 | 
			
		||||
			})
 | 
			
		||||
		} else {
 | 
			
		||||
@@ -550,6 +556,7 @@ func (this *DNSDomainService) findClusterDNSChanges(cluster *models.NodeCluster,
 | 
			
		||||
					Type:  dnstypes.RecordTypeCNAME,
 | 
			
		||||
					Value: clusterDomain + ".",
 | 
			
		||||
					Route: "", // 注意这里为空,需要在执行过程中获取默认值
 | 
			
		||||
					TTL:   ttl,
 | 
			
		||||
				},
 | 
			
		||||
			})
 | 
			
		||||
		} else {
 | 
			
		||||
 
 | 
			
		||||
@@ -447,6 +447,7 @@ func (this *NodeClusterService) FindEnabledNodeClusterDNS(ctx context.Context, r
 | 
			
		||||
			NodesAutoSync:   dnsConfig.NodesAutoSync,
 | 
			
		||||
			ServersAutoSync: dnsConfig.ServersAutoSync,
 | 
			
		||||
			CnameRecords:    dnsConfig.CNameRecords,
 | 
			
		||||
			Ttl:             dnsConfig.TTL,
 | 
			
		||||
		}, nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@@ -504,6 +505,7 @@ func (this *NodeClusterService) FindEnabledNodeClusterDNS(ctx context.Context, r
 | 
			
		||||
		NodesAutoSync:   dnsConfig.NodesAutoSync,
 | 
			
		||||
		ServersAutoSync: dnsConfig.ServersAutoSync,
 | 
			
		||||
		CnameRecords:    dnsConfig.CNameRecords,
 | 
			
		||||
		Ttl:             dnsConfig.TTL,
 | 
			
		||||
		DefaultRoute:    defaultRoute,
 | 
			
		||||
	}, nil
 | 
			
		||||
}
 | 
			
		||||
@@ -597,7 +599,7 @@ func (this *NodeClusterService) UpdateNodeClusterDNS(ctx context.Context, req *p
 | 
			
		||||
 | 
			
		||||
	tx := this.NullTx()
 | 
			
		||||
 | 
			
		||||
	err = models.SharedNodeClusterDAO.UpdateClusterDNS(tx, req.NodeClusterId, req.DnsName, req.DnsDomainId, req.NodesAutoSync, req.ServersAutoSync, req.CnameRecords)
 | 
			
		||||
	err = models.SharedNodeClusterDAO.UpdateClusterDNS(tx, req.NodeClusterId, req.DnsName, req.DnsDomainId, req.NodesAutoSync, req.ServersAutoSync, req.CnameRecords, req.Ttl)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
@@ -8,6 +8,7 @@ import (
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/dnstypes"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/utils"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
 | 
			
		||||
	"github.com/iwind/TeaGo/dbs"
 | 
			
		||||
	"github.com/iwind/TeaGo/lists"
 | 
			
		||||
@@ -125,7 +126,7 @@ func (this *DNSTaskExecutor) doServer(taskId int64, serverId int64) error {
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	manager, domainId, domain, clusterDNSName, _, err := this.findDNSManager(tx, int64(serverDNS.ClusterId))
 | 
			
		||||
	manager, domainId, domain, clusterDNSName, dnsConfig, err := this.findDNSManager(tx, int64(serverDNS.ClusterId))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
@@ -133,6 +134,10 @@ func (this *DNSTaskExecutor) doServer(taskId int64, serverId int64) error {
 | 
			
		||||
		isOk = true
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
	var ttl int32 = 0
 | 
			
		||||
	if dnsConfig != nil {
 | 
			
		||||
		ttl = dnsConfig.TTL
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	recordName := serverDNS.DnsName
 | 
			
		||||
	recordValue := clusterDNSName + "." + domain + "."
 | 
			
		||||
@@ -196,6 +201,7 @@ func (this *DNSTaskExecutor) doServer(taskId int64, serverId int64) error {
 | 
			
		||||
			Type:  recordType,
 | 
			
		||||
			Value: recordValue,
 | 
			
		||||
			Route: recordRoute,
 | 
			
		||||
			TTL:   ttl,
 | 
			
		||||
		})
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
@@ -264,7 +270,7 @@ func (this *DNSTaskExecutor) doCluster(taskId int64, clusterId int64) error {
 | 
			
		||||
	}()
 | 
			
		||||
 | 
			
		||||
	var tx *dbs.Tx
 | 
			
		||||
	manager, domainId, domain, clusterDNSName, cnameRecords, err := this.findDNSManager(tx, clusterId)
 | 
			
		||||
	manager, domainId, domain, clusterDNSName, dnsConfig, err := this.findDNSManager(tx, clusterId)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
@@ -275,6 +281,11 @@ func (this *DNSTaskExecutor) doCluster(taskId int64, clusterId int64) error {
 | 
			
		||||
 | 
			
		||||
	var clusterDomain = clusterDNSName + "." + domain
 | 
			
		||||
 | 
			
		||||
	var ttl int32 = 0
 | 
			
		||||
	if dnsConfig != nil {
 | 
			
		||||
		ttl = dnsConfig.TTL
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 以前的节点记录
 | 
			
		||||
	records, err := manager.GetRecords(domain)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
@@ -343,6 +354,7 @@ func (this *DNSTaskExecutor) doCluster(taskId int64, clusterId int64) error {
 | 
			
		||||
					Type:  recordType,
 | 
			
		||||
					Value: ip,
 | 
			
		||||
					Route: route,
 | 
			
		||||
					TTL:   ttl,
 | 
			
		||||
				})
 | 
			
		||||
				if err != nil {
 | 
			
		||||
					return err
 | 
			
		||||
@@ -395,6 +407,7 @@ func (this *DNSTaskExecutor) doCluster(taskId int64, clusterId int64) error {
 | 
			
		||||
				Type:  dnstypes.RecordTypeCNAME,
 | 
			
		||||
				Value: clusterDomain + ".",
 | 
			
		||||
				Route: "", // 注意这里为空,需要在执行过程中获取默认值
 | 
			
		||||
				TTL:   ttl,
 | 
			
		||||
			})
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return err
 | 
			
		||||
@@ -403,6 +416,10 @@ func (this *DNSTaskExecutor) doCluster(taskId int64, clusterId int64) error {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 自动设置的CNAME
 | 
			
		||||
	var cnameRecords = []string{}
 | 
			
		||||
	if dnsConfig != nil {
 | 
			
		||||
		cnameRecords = dnsConfig.CNameRecords
 | 
			
		||||
	}
 | 
			
		||||
	for _, cnameRecord := range cnameRecords {
 | 
			
		||||
		serverDNSNames = append(serverDNSNames, cnameRecord)
 | 
			
		||||
		_, ok := serverRecordsMap[cnameRecord]
 | 
			
		||||
@@ -414,6 +431,7 @@ func (this *DNSTaskExecutor) doCluster(taskId int64, clusterId int64) error {
 | 
			
		||||
				Type:  dnstypes.RecordTypeCNAME,
 | 
			
		||||
				Value: clusterDomain + ".",
 | 
			
		||||
				Route: "", // 注意这里为空,需要在执行过程中获取默认值
 | 
			
		||||
				TTL:   ttl,
 | 
			
		||||
			})
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return err
 | 
			
		||||
@@ -511,7 +529,7 @@ func (this *DNSTaskExecutor) doDomain(taskId int64, domainId int64) error {
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *DNSTaskExecutor) findDNSManager(tx *dbs.Tx, clusterId int64) (manager dnsclients.ProviderInterface, domainId int64, domain string, clusterDNSName string, cnameRecords []string, err error) {
 | 
			
		||||
func (this *DNSTaskExecutor) findDNSManager(tx *dbs.Tx, clusterId int64) (manager dnsclients.ProviderInterface, domainId int64, domain string, clusterDNSName string, dnsConfig *dnsconfigs.ClusterDNSConfig, err error) {
 | 
			
		||||
	clusterDNS, err := models.SharedNodeClusterDAO.FindClusterDNSInfo(tx, clusterId, nil)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, 0, "", "", nil, err
 | 
			
		||||
@@ -520,7 +538,7 @@ func (this *DNSTaskExecutor) findDNSManager(tx *dbs.Tx, clusterId int64) (manage
 | 
			
		||||
		return nil, 0, "", "", nil, nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	dnsConfig, err := clusterDNS.DecodeDNSConfig()
 | 
			
		||||
	dnsConfig, err = clusterDNS.DecodeDNSConfig()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, 0, "", "", nil, err
 | 
			
		||||
	}
 | 
			
		||||
@@ -559,5 +577,5 @@ func (this *DNSTaskExecutor) findDNSManager(tx *dbs.Tx, clusterId int64) (manage
 | 
			
		||||
		return nil, 0, "", "", nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return manager, int64(dnsDomain.Id), dnsDomain.Name, clusterDNS.DnsName, dnsConfig.CNameRecords, nil
 | 
			
		||||
	return manager, int64(dnsDomain.Id), dnsDomain.Name, clusterDNS.DnsName, dnsConfig, nil
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user