mirror of
				https://github.com/coredns/coredns.git
				synced 2025-11-03 18:53:13 -05:00 
			
		
		
		
	* update docs * plugins: use plugin specific logging Hooking up pkg/log also changed NewWithPlugin to just take a string instead of a plugin.Handler as that is more flexible and for instance the Root "plugin" doesn't implement it fully. Same logging from the reload plugin: .:1043 2018/04/22 08:56:37 [INFO] CoreDNS-1.1.1 2018/04/22 08:56:37 [INFO] linux/amd64, go1.10.1, CoreDNS-1.1.1 linux/amd64, go1.10.1, 2018/04/22 08:56:37 [INFO] plugin/reload: Running configuration MD5 = ec4c9c55cd19759ea1c46b8c45742b06 2018/04/22 08:56:54 [INFO] Reloading 2018/04/22 08:56:54 [INFO] plugin/reload: Running configuration MD5 = 9e2bfdd85bdc9cceb740ba9c80f34c1a 2018/04/22 08:56:54 [INFO] Reloading complete * update docs * better doc
		
			
				
	
	
		
			76 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			76 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
package kubernetes
 | 
						|
 | 
						|
import (
 | 
						|
	"fmt"
 | 
						|
	"io"
 | 
						|
	"net"
 | 
						|
	"net/http"
 | 
						|
 | 
						|
	"github.com/coredns/coredns/plugin/pkg/healthcheck"
 | 
						|
)
 | 
						|
 | 
						|
type proxyHandler struct {
 | 
						|
	healthcheck.HealthCheck
 | 
						|
}
 | 
						|
 | 
						|
type apiProxy struct {
 | 
						|
	http.Server
 | 
						|
	listener net.Listener
 | 
						|
	handler  proxyHandler
 | 
						|
}
 | 
						|
 | 
						|
func (p *proxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 | 
						|
	upstream := p.Select()
 | 
						|
	network := "tcp"
 | 
						|
	address := upstream.Name
 | 
						|
 | 
						|
	d, err := net.Dial(network, address)
 | 
						|
	if err != nil {
 | 
						|
		log.Errorf("Unable to establish connection to upstream %s://%s: %s", network, address, err)
 | 
						|
		http.Error(w, fmt.Sprintf("Unable to establish connection to upstream %s://%s: %s", network, address, err), 500)
 | 
						|
		return
 | 
						|
	}
 | 
						|
	hj, ok := w.(http.Hijacker)
 | 
						|
	if !ok {
 | 
						|
		log.Error("Unable to establish connection: no hijacker")
 | 
						|
		http.Error(w, "Unable to establish connection: no hijacker", 500)
 | 
						|
		return
 | 
						|
	}
 | 
						|
	nc, _, err := hj.Hijack()
 | 
						|
	if err != nil {
 | 
						|
		log.Errorf("Unable to hijack connection: %s", err)
 | 
						|
		http.Error(w, fmt.Sprintf("Unable to hijack connection: %s", err), 500)
 | 
						|
		return
 | 
						|
	}
 | 
						|
	defer nc.Close()
 | 
						|
	defer d.Close()
 | 
						|
 | 
						|
	err = r.Write(d)
 | 
						|
	if err != nil {
 | 
						|
		log.Errorf("Unable to copy connection to upstream %s://%s: %s", network, address, err)
 | 
						|
		http.Error(w, fmt.Sprintf("Unable to copy connection to upstream %s://%s: %s", network, address, err), 500)
 | 
						|
		return
 | 
						|
	}
 | 
						|
 | 
						|
	errChan := make(chan error, 2)
 | 
						|
	cp := func(dst io.Writer, src io.Reader) {
 | 
						|
		_, err := io.Copy(dst, src)
 | 
						|
		errChan <- err
 | 
						|
	}
 | 
						|
	go cp(d, nc)
 | 
						|
	go cp(nc, d)
 | 
						|
	<-errChan
 | 
						|
}
 | 
						|
 | 
						|
func (p *apiProxy) Run() {
 | 
						|
	p.handler.Start()
 | 
						|
	go func() {
 | 
						|
		p.Serve(p.listener)
 | 
						|
	}()
 | 
						|
}
 | 
						|
 | 
						|
func (p *apiProxy) Stop() {
 | 
						|
	p.handler.Stop()
 | 
						|
	p.listener.Close()
 | 
						|
}
 |