Skip to content

Commit 05b7609

Browse files
authored
feat: support service type externalName (#464)
1 parent 2e79a33 commit 05b7609

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

pkg/dns/dns.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ type Config struct {
3939
Hosts []Entry
4040
Lock *sync.Mutex
4141

42+
HowToGetExternalName func(name string) (string, error)
43+
4244
// only set it on linux
4345
OSConfigurator dns.OSConfigurator
4446
}
@@ -278,10 +280,19 @@ func (c *Config) generateAppendHosts(serviceList []v12.Service, hosts []Entry) [
278280
if strings.EqualFold(service.Name, ServiceKubernetes) {
279281
continue
280282
}
281-
if net.ParseIP(service.Spec.ClusterIP) == nil {
283+
var ip net.IP
284+
if service.Spec.ClusterIP != "" {
285+
ip = net.ParseIP(service.Spec.ClusterIP)
286+
}
287+
if service.Spec.ExternalName != "" {
288+
name, _ := c.HowToGetExternalName(service.Spec.ExternalName)
289+
ip = net.ParseIP(name)
290+
}
291+
if ip == nil {
282292
continue
283293
}
284-
var e = Entry{IP: service.Spec.ClusterIP, Domain: service.Name}
294+
295+
var e = Entry{IP: ip.String(), Domain: service.Name}
285296
if !sets.New[Entry]().Insert(entryList...).Has(e) {
286297
entryList = append([]Entry{e}, entryList...)
287298
}

pkg/handler/connect.go

+16
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,22 @@ func (c *ConnectOptions) setupDNS(ctx context.Context) error {
622622
TunName: c.tunName,
623623
Hosts: c.extraHost,
624624
Lock: c.Lock,
625+
HowToGetExternalName: func(domain string) (string, error) {
626+
podList, err := c.GetRunningPodList(ctx)
627+
if err != nil {
628+
return "", err
629+
}
630+
pod := podList[0]
631+
return util.Shell(
632+
ctx,
633+
c.clientset,
634+
c.config,
635+
pod.GetName(),
636+
config.ContainerSidecarVPN,
637+
c.Namespace,
638+
[]string{"dig", "+short", domain},
639+
)
640+
},
625641
}
626642
log.Debugf("Setup DNS...")
627643
if err = c.dnsConfig.SetupDNS(ctx); err != nil {

0 commit comments

Comments
 (0)