| 
									
										
										
										
											2019-03-07 20:35:16 +00:00
										 |  |  | package ready
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"net"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-24 18:14:41 +02:00
										 |  |  | 	"github.com/coredns/caddy"
 | 
					
						
							| 
									
										
										
										
											2019-03-07 20:35:16 +00:00
										 |  |  | 	"github.com/coredns/coredns/core/dnsserver"
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-20 08:02:30 +01:00
										 |  |  | func init() { plugin.Register("ready", setup) }
 | 
					
						
							| 
									
										
										
										
											2019-03-07 20:35:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | func setup(c *caddy.Controller) error {
 | 
					
						
							| 
									
										
										
										
											2025-04-08 16:46:30 +03:00
										 |  |  | 	addr, monType, err := parse(c)
 | 
					
						
							| 
									
										
										
										
											2019-03-07 20:35:16 +00:00
										 |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		return plugin.Error("ready", err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2025-04-08 16:46:30 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if monType == monitorTypeContinuously {
 | 
					
						
							|  |  |  | 		plugins.keepReadiness = true
 | 
					
						
							|  |  |  | 	} else {
 | 
					
						
							|  |  |  | 		plugins.keepReadiness = false
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-07 20:35:16 +00:00
										 |  |  | 	rd := &ready{Addr: addr}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-18 18:34:46 +01:00
										 |  |  | 	uniqAddr.Set(addr, rd.onStartup)
 | 
					
						
							| 
									
										
										
										
											2019-06-09 08:10:15 +01:00
										 |  |  | 	c.OnStartup(func() error { uniqAddr.Set(addr, rd.onStartup); return nil })
 | 
					
						
							|  |  |  | 	c.OnRestartFailed(func() error { uniqAddr.Set(addr, rd.onStartup); return nil })
 | 
					
						
							| 
									
										
										
										
											2019-03-07 20:35:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-09 08:10:15 +01:00
										 |  |  | 	c.OnStartup(func() error { return uniqAddr.ForEach() })
 | 
					
						
							|  |  |  | 	c.OnRestartFailed(func() error { return uniqAddr.ForEach() })
 | 
					
						
							| 
									
										
										
										
											2019-03-07 20:35:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	c.OnStartup(func() error {
 | 
					
						
							| 
									
										
										
										
											2022-07-18 09:50:15 -04:00
										 |  |  | 		plugins.Reset()
 | 
					
						
							| 
									
										
										
										
											2019-06-09 08:10:15 +01:00
										 |  |  | 		for _, p := range dnsserver.GetConfig(c).Handlers() {
 | 
					
						
							|  |  |  | 			if r, ok := p.(Readiness); ok {
 | 
					
						
							|  |  |  | 				plugins.Append(r, p.Name())
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		return nil
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | 	c.OnRestartFailed(func() error {
 | 
					
						
							|  |  |  | 		for _, p := range dnsserver.GetConfig(c).Handlers() {
 | 
					
						
							| 
									
										
										
										
											2019-03-07 20:35:16 +00:00
										 |  |  | 			if r, ok := p.(Readiness); ok {
 | 
					
						
							|  |  |  | 				plugins.Append(r, p.Name())
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		return nil
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-09 08:10:15 +01:00
										 |  |  | 	c.OnRestart(rd.onFinalShutdown)
 | 
					
						
							| 
									
										
										
										
											2019-03-07 20:35:16 +00:00
										 |  |  | 	c.OnFinalShutdown(rd.onFinalShutdown)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-08 16:46:30 +03:00
										 |  |  | // monitorType represents the type of monitoring behavior for the readiness plugin.
 | 
					
						
							|  |  |  | type monitorType string
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const (
 | 
					
						
							|  |  |  | 	// monitorTypeUntilReady indicates the monitoring should continue until the system is ready.
 | 
					
						
							|  |  |  | 	monitorTypeUntilReady monitorType = "until-ready"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// monitorTypeContinuously indicates the monitoring should continue indefinitely.
 | 
					
						
							|  |  |  | 	monitorTypeContinuously monitorType = "continuously"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func parse(c *caddy.Controller) (string, monitorType, error) {
 | 
					
						
							| 
									
										
										
										
											2019-06-09 08:10:15 +01:00
										 |  |  | 	addr := ":8181"
 | 
					
						
							| 
									
										
										
										
											2025-04-08 16:46:30 +03:00
										 |  |  | 	monType := monitorTypeUntilReady
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-07 20:35:16 +00:00
										 |  |  | 	i := 0
 | 
					
						
							|  |  |  | 	for c.Next() {
 | 
					
						
							|  |  |  | 		if i > 0 {
 | 
					
						
							| 
									
										
										
										
											2025-04-08 16:46:30 +03:00
										 |  |  | 			return "", "", plugin.ErrOnce
 | 
					
						
							| 
									
										
										
										
											2019-03-07 20:35:16 +00:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 		i++
 | 
					
						
							|  |  |  | 		args := c.RemainingArgs()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		switch len(args) {
 | 
					
						
							|  |  |  | 		case 0:
 | 
					
						
							|  |  |  | 		case 1:
 | 
					
						
							|  |  |  | 			addr = args[0]
 | 
					
						
							|  |  |  | 			if _, _, e := net.SplitHostPort(addr); e != nil {
 | 
					
						
							| 
									
										
										
										
											2025-04-08 16:46:30 +03:00
										 |  |  | 				return "", "", e
 | 
					
						
							| 
									
										
										
										
											2019-03-07 20:35:16 +00:00
										 |  |  | 			}
 | 
					
						
							|  |  |  | 		default:
 | 
					
						
							| 
									
										
										
										
											2025-04-08 16:46:30 +03:00
										 |  |  | 			return "", "", c.ArgErr()
 | 
					
						
							| 
									
										
										
										
											2019-03-07 20:35:16 +00:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2025-04-08 16:46:30 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		for c.NextBlock() {
 | 
					
						
							|  |  |  | 			switch c.Val() {
 | 
					
						
							|  |  |  | 			case "monitor":
 | 
					
						
							|  |  |  | 				args := c.RemainingArgs()
 | 
					
						
							|  |  |  | 				if len(args) != 1 {
 | 
					
						
							|  |  |  | 					return "", "", c.ArgErr()
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				var err error
 | 
					
						
							|  |  |  | 				monType, err = parseMonitorType(c, args[0])
 | 
					
						
							|  |  |  | 				if err != nil {
 | 
					
						
							|  |  |  | 					return "", "", err
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return addr, monType, nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func parseMonitorType(c *caddy.Controller, arg string) (monitorType, error) {
 | 
					
						
							|  |  |  | 	switch arg {
 | 
					
						
							|  |  |  | 	case "until-ready":
 | 
					
						
							|  |  |  | 		return monitorTypeUntilReady, nil
 | 
					
						
							|  |  |  | 	case "continuously":
 | 
					
						
							|  |  |  | 		return monitorTypeContinuously, nil
 | 
					
						
							|  |  |  | 	default:
 | 
					
						
							|  |  |  | 		return "", c.Errf("monitor type '%s' not supported", arg)
 | 
					
						
							| 
									
										
										
										
											2019-03-07 20:35:16 +00:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | }
 |