| 
									
										
										
										
											2017-08-18 14:45:20 +01:00
										 |  |  | package federation
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"fmt"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/core/dnsserver"
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin"
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin/kubernetes"
 | 
					
						
							| 
									
										
										
										
											2018-10-15 12:43:03 -04:00
										 |  |  | 	"github.com/coredns/coredns/plugin/pkg/upstream"
 | 
					
						
							| 
									
										
										
										
											2017-08-18 14:45:20 +01:00
										 |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/mholt/caddy"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func init() {
 | 
					
						
							|  |  |  | 	caddy.RegisterPlugin("federation", caddy.Plugin{
 | 
					
						
							|  |  |  | 		ServerType: "dns",
 | 
					
						
							|  |  |  | 		Action:     setup,
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func setup(c *caddy.Controller) error {
 | 
					
						
							|  |  |  | 	fed, err := federationParse(c)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 		return plugin.Error("federation", err)
 | 
					
						
							| 
									
										
										
										
											2017-08-18 14:45:20 +01:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	// Do this in OnStartup, so all plugin has been initialized.
 | 
					
						
							| 
									
										
										
										
											2017-08-18 14:45:20 +01:00
										 |  |  | 	c.OnStartup(func() error {
 | 
					
						
							| 
									
										
										
										
											2017-08-22 14:21:42 +01:00
										 |  |  | 		m := dnsserver.GetConfig(c).Handler("kubernetes")
 | 
					
						
							| 
									
										
										
										
											2017-08-18 14:45:20 +01:00
										 |  |  | 		if m == nil {
 | 
					
						
							|  |  |  | 			return nil
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2017-09-06 23:28:41 +01:00
										 |  |  | 		if x, ok := m.(*kubernetes.Kubernetes); ok {
 | 
					
						
							| 
									
										
										
										
											2017-08-18 14:45:20 +01:00
										 |  |  | 			fed.Federations = x.Federations
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		return nil
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
 | 
					
						
							| 
									
										
										
										
											2017-08-18 14:45:20 +01:00
										 |  |  | 		fed.Next = next
 | 
					
						
							| 
									
										
										
										
											2017-09-06 23:28:41 +01:00
										 |  |  | 		return fed
 | 
					
						
							| 
									
										
										
										
											2017-08-18 14:45:20 +01:00
										 |  |  | 	})
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func federationParse(c *caddy.Controller) (*Federation, error) {
 | 
					
						
							|  |  |  | 	fed := New()
 | 
					
						
							| 
									
										
										
										
											2019-07-02 16:23:47 +01:00
										 |  |  | 	fed.Upstream = upstream.New()
 | 
					
						
							| 
									
										
										
										
											2017-08-18 14:45:20 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	for c.Next() {
 | 
					
						
							|  |  |  | 		// federation [zones..]
 | 
					
						
							| 
									
										
										
										
											2017-09-06 22:25:05 +01:00
										 |  |  | 		zones := c.RemainingArgs()
 | 
					
						
							| 
									
										
										
										
											2018-07-28 17:32:13 +08:00
										 |  |  | 		var origins []string
 | 
					
						
							| 
									
										
										
										
											2017-09-06 22:25:05 +01:00
										 |  |  | 		if len(zones) > 0 {
 | 
					
						
							|  |  |  | 			origins = make([]string, len(zones))
 | 
					
						
							|  |  |  | 			copy(origins, zones)
 | 
					
						
							|  |  |  | 		} else {
 | 
					
						
							|  |  |  | 			origins = make([]string, len(c.ServerBlockKeys))
 | 
					
						
							|  |  |  | 			copy(origins, c.ServerBlockKeys)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2017-08-18 14:45:20 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		for c.NextBlock() {
 | 
					
						
							|  |  |  | 			x := c.Val()
 | 
					
						
							| 
									
										
										
										
											2017-09-06 22:25:05 +01:00
										 |  |  | 			switch x {
 | 
					
						
							| 
									
										
										
										
											2018-10-15 12:43:03 -04:00
										 |  |  | 			case "upstream":
 | 
					
						
							| 
									
										
										
										
											2019-07-02 16:23:47 +01:00
										 |  |  | 				// remove soon
 | 
					
						
							| 
									
										
										
										
											2019-01-13 16:54:49 +00:00
										 |  |  | 				c.RemainingArgs()
 | 
					
						
							| 
									
										
										
										
											2017-08-18 14:45:20 +01:00
										 |  |  | 			default:
 | 
					
						
							|  |  |  | 				args := c.RemainingArgs()
 | 
					
						
							| 
									
										
										
										
											2017-09-06 22:25:05 +01:00
										 |  |  | 				if x := len(args); x != 1 {
 | 
					
						
							|  |  |  | 					return fed, fmt.Errorf("need two arguments for federation, got %d", x)
 | 
					
						
							| 
									
										
										
										
											2017-08-18 14:45:20 +01:00
										 |  |  | 				}
 | 
					
						
							| 
									
										
										
										
											2017-09-06 22:25:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-18 14:45:20 +01:00
										 |  |  | 				fed.f[x] = dns.Fqdn(args[0])
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		for i := range origins {
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 			origins[i] = plugin.Host(origins[i]).Normalize()
 | 
					
						
							| 
									
										
										
										
											2017-08-18 14:45:20 +01:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		fed.zones = origins
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if len(fed.f) == 0 {
 | 
					
						
							|  |  |  | 			return fed, fmt.Errorf("at least one name to zone federation expected")
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return fed, nil
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return fed, nil
 | 
					
						
							|  |  |  | }
 |