feat: support service type externalName (#464)

This commit is contained in:
naison
2025-03-12 00:26:00 +08:00
committed by GitHub
parent 2e79a331b4
commit 05b76094f0
2 changed files with 29 additions and 2 deletions

View File

@@ -39,6 +39,8 @@ type Config struct {
Hosts []Entry
Lock *sync.Mutex
HowToGetExternalName func(name string) (string, error)
// only set it on linux
OSConfigurator dns.OSConfigurator
}
@@ -278,10 +280,19 @@ func (c *Config) generateAppendHosts(serviceList []v12.Service, hosts []Entry) [
if strings.EqualFold(service.Name, ServiceKubernetes) {
continue
}
if net.ParseIP(service.Spec.ClusterIP) == nil {
var ip net.IP
if service.Spec.ClusterIP != "" {
ip = net.ParseIP(service.Spec.ClusterIP)
}
if service.Spec.ExternalName != "" {
name, _ := c.HowToGetExternalName(service.Spec.ExternalName)
ip = net.ParseIP(name)
}
if ip == nil {
continue
}
var e = Entry{IP: service.Spec.ClusterIP, Domain: service.Name}
var e = Entry{IP: ip.String(), Domain: service.Name}
if !sets.New[Entry]().Insert(entryList...).Has(e) {
entryList = append([]Entry{e}, entryList...)
}

View File

@@ -622,6 +622,22 @@ func (c *ConnectOptions) setupDNS(ctx context.Context) error {
TunName: c.tunName,
Hosts: c.extraHost,
Lock: c.Lock,
HowToGetExternalName: func(domain string) (string, error) {
podList, err := c.GetRunningPodList(ctx)
if err != nil {
return "", err
}
pod := podList[0]
return util.Shell(
ctx,
c.clientset,
c.config,
pod.GetName(),
config.ContainerSidecarVPN,
c.Namespace,
[]string{"dig", "+short", domain},
)
},
}
log.Debugf("Setup DNS...")
if err = c.dnsConfig.SetupDNS(ctx); err != nil {