| 
									
										
										
										
											2017-09-16 14:13:28 +01:00
										 |  |  | # Plugins
 | 
					
						
							| 
									
										
										
										
											2016-10-17 07:51:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-16 14:13:28 +01:00
										 |  |  | ## Writing Plugins
 | 
					
						
							| 
									
										
										
										
											2016-03-19 19:56:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-27 20:35:52 +00:00
										 |  |  | The main method that gets called is `ServeDNS`. It has three parameters: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | * a `context.Context`; | 
					
						
							|  |  |  | * `dns.ResponseWriter` that is, basically, the client's connection; | 
					
						
							|  |  |  | * `*dns.Msg` the request from the client. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-25 12:08:27 +08:00
										 |  |  | `ServeDNS` returns two values, a response code and an error. If the error is not nil, CoreDNS | 
					
						
							| 
									
										
										
										
											2018-02-26 14:51:40 +00:00
										 |  |  | will return a SERVFAIL to the client. The response code tells CoreDNS if a *reply has been | 
					
						
							|  |  |  | written by the plugin chain or not*. In the latter case CoreDNS will take care of that. | 
					
						
							| 
									
										
										
										
											2018-01-27 20:35:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | CoreDNS treats: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | * SERVFAIL (dns.RcodeServerFailure) | 
					
						
							|  |  |  | * REFUSED (dns.RcodeRefused) | 
					
						
							|  |  |  | * FORMERR (dns.RcodeFormatError) | 
					
						
							|  |  |  | * NOTIMP (dns.RcodeNotImplemented) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | as special and will then assume *nothing* has been written to the client. In all other cases it | 
					
						
							|  |  |  | assumes something has been written to the client (by the plugin). | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The [*example*](https://github.com/coredns/example) plugin shows a bare-bones implementation that | 
					
						
							| 
									
										
										
										
											2018-02-26 14:51:40 +00:00
										 |  |  | can be used as a starting point for your plugin. This plugin has tests and extensive comments in the | 
					
						
							|  |  |  | code. | 
					
						
							| 
									
										
										
										
											2018-01-27 20:35:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | ## Hooking It Up
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | See a couple of blog posts on how to write and add plugin to CoreDNS: | 
					
						
							| 
									
										
										
										
											2017-02-20 21:00:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-29 15:04:07 +01:00
										 |  |  | * <https://blog.coredns.io/2017/03/01/how-to-add-plugins-to-coredns/> | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | * <https://blog.coredns.io/2016/12/19/writing-plugin-for-coredns/>, slightly older, but useful. | 
					
						
							| 
									
										
										
										
											2016-10-17 07:51:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-27 20:35:52 +00:00
										 |  |  | ## Logging
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-18 21:02:01 +01:00
										 |  |  | If your plugin needs to output a log line you should use the `plugin/pkg/log` package. This package | 
					
						
							|  |  |  | implements log levels. The standard way of outputting is: `log.Info` for info level messages. The | 
					
						
							|  |  |  | levels available are `log.Info`, `log.Warning`, `log.Error`, `log.Debug`. Each of these also has | 
					
						
							| 
									
										
										
										
											2018-04-22 21:40:33 +01:00
										 |  |  | a `f` variant. The plugin's name should be included, by using the log package like so: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ~~~ go | 
					
						
							|  |  |  | import clog "github.com/coredns/coredns/plugin/pkg/log" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var log = clog.NewWithPlugin("whoami") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | log.Info("message") // outputs: [INFO] plugin/whoami: message | 
					
						
							|  |  |  | ~~~ | 
					
						
							| 
									
										
										
										
											2018-04-18 21:02:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-27 20:35:52 +00:00
										 |  |  | In general, logging should be left to the higher layers by returning an error. However, if there is | 
					
						
							| 
									
										
										
										
											2018-04-18 21:02:01 +01:00
										 |  |  | a reason to consume the error and notify the user, then logging in the plugin itself can be | 
					
						
							|  |  |  | acceptable. The `Debug*` functions only output something when the *debug* plugin is loaded in the | 
					
						
							|  |  |  | server. | 
					
						
							| 
									
										
										
										
											2018-01-27 20:35:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-18 07:47:18 +01:00
										 |  |  | ## Metrics
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | When exporting metrics the *Namespace* should be `plugin.Namespace` (="coredns"), and the | 
					
						
							| 
									
										
										
										
											2017-10-31 13:33:41 +00:00
										 |  |  | *Subsystem* should be the name of the plugin. The README.md for the plugin should then also contain | 
					
						
							| 
									
										
										
										
											2019-03-07 20:35:16 +00:00
										 |  |  | a *Metrics* section detailing the metrics. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-05 17:51:51 +01:00
										 |  |  | ## Readiness
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-07 22:13:47 +00:00
										 |  |  | If the plugin supports signalling readiness it should have a *Ready* section detailing how it | 
					
						
							| 
									
										
										
										
											2019-05-05 17:51:51 +01:00
										 |  |  | works, and implement the `ready.Readiness` interface. | 
					
						
							| 
									
										
										
										
											2016-10-18 07:47:18 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-17 02:02:46 +00:00
										 |  |  | ## Opening Sockets
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | See the plugin/pkg/reuseport for `Listen` and `ListenPacket` functions. Using these functions makes | 
					
						
							| 
									
										
										
										
											2019-11-18 11:10:25 -05:00
										 |  |  | your plugin handle reload events better. | 
					
						
							| 
									
										
										
										
											2019-11-17 02:02:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-18 07:47:18 +01:00
										 |  |  | ## Documentation
 | 
					
						
							| 
									
										
										
										
											2016-10-17 07:51:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-31 13:33:41 +00:00
										 |  |  | Each plugin should have a README.md explaining what the plugin does and how it is configured. The | 
					
						
							|  |  |  | file should have the following layout: | 
					
						
							| 
									
										
										
										
											2016-10-17 07:51:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | * Title: use the plugin's name | 
					
						
							| 
									
										
										
										
											2018-01-04 12:53:07 +00:00
										 |  |  | * Subsection titled: "Named" | 
					
						
							|  |  |  |     with *PLUGIN* - one line description. | 
					
						
							|  |  |  | * Subsection titled: "Description" has a longer description. | 
					
						
							|  |  |  | * Subsection titled: "Syntax", syntax and supported directives. | 
					
						
							| 
									
										
										
										
											2016-10-17 07:51:44 +01:00
										 |  |  | * Subsection titled: "Examples" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | More sections are of course possible. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-18 07:47:18 +01:00
										 |  |  | ### Style
 | 
					
						
							| 
									
										
										
										
											2016-10-17 07:51:44 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | We use the Unix manual page style: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | * The name of plugin in the running text should be italic: *plugin*. | 
					
						
							| 
									
										
										
										
											2016-10-17 07:51:44 +01:00
										 |  |  | * all CAPITAL: user supplied argument, in the running text references this use strong text: `**`: | 
					
						
							|  |  |  |   **EXAMPLE**. | 
					
						
							|  |  |  | * Optional text: in block quotes: `[optional]`. | 
					
						
							|  |  |  | * Use three dots to indicate multiple options are allowed: `arg...`. | 
					
						
							|  |  |  | * Item used literal: `literal`. | 
					
						
							| 
									
										
										
										
											2017-02-20 21:00:00 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | ### Example Domain Names
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-07 16:32:59 +00:00
										 |  |  | Please be sure to use `example.org` or `example.net` in any examples and tests you provide. These | 
					
						
							|  |  |  | are the standard domain names created for this purpose. | 
					
						
							| 
									
										
										
										
											2017-02-20 21:00:00 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | ## Fallthrough
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-31 13:33:41 +00:00
										 |  |  | In a perfect world the following would be true for plugin: "Either you are responsible for a zone or | 
					
						
							|  |  |  | not". If the answer is "not", the plugin should call the next plugin in the chain. If "yes" it | 
					
						
							|  |  |  | should handle *all* names that fall in this zone and the names below - i.e. it should handle the | 
					
						
							| 
									
										
										
										
											2018-01-07 16:32:59 +00:00
										 |  |  | entire domain and all sub domains. | 
					
						
							| 
									
										
										
										
											2017-02-20 21:00:00 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | ~~~ txt | 
					
						
							|  |  |  | . { | 
					
						
							|  |  |  |     file example.org db.example | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | ~~~ | 
					
						
							| 
									
										
										
										
											2017-10-31 13:33:41 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | In this example the *file* plugin is handling all names below (and including) `example.org`. If | 
					
						
							|  |  |  | a query comes in that is not a subdomain (or equal to) `example.org` the next plugin is called. | 
					
						
							| 
									
										
										
										
											2017-02-20 21:00:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-05 17:51:51 +01:00
										 |  |  | Now, the world isn't perfect, and there are may be reasons to "fallthrough" to the next plugin, | 
					
						
							|  |  |  | meaning a plugin is only responsible for a *subset* of names within the zone. | 
					
						
							| 
									
										
										
										
											2017-02-20 21:00:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-06 14:52:09 -05:00
										 |  |  | The `fallthrough` directive should optionally accept a list of zones. Only queries for records | 
					
						
							| 
									
										
										
										
											2019-05-05 17:51:51 +01:00
										 |  |  | in one of those zones should be allowed to fallthrough. See `plugin/pkg/fallthrough` for the | 
					
						
							|  |  |  | implementation. | 
					
						
							| 
									
										
										
										
											2018-01-06 14:52:09 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:24:57 +00:00
										 |  |  | ## General Guidelines
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Some general guidelines: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | * logging time duration should be done in seconds (call the `Seconds()` method on any duration). | 
					
						
							|  |  |  | * keep logging to a minimum. | 
					
						
							|  |  |  | * call the main config parse function just `parse`. | 
					
						
							|  |  |  | * try to minimize the number of knobs in the configuration. | 
					
						
							|  |  |  | * use `plugin.Error()` to wrap errors returned from the `setup` function. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-05 17:51:51 +01:00
										 |  |  | ## Qualifying for Main Repo
 | 
					
						
							| 
									
										
										
										
											2017-06-03 08:26:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-16 14:13:28 +01:00
										 |  |  | Plugins for CoreDNS can live out-of-tree, `plugin.cfg` defaults to CoreDNS' repo but other | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | repos work just as well. So when do we consider the inclusion of a new plugin in the main repo? | 
					
						
							| 
									
										
										
										
											2017-06-03 08:26:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | * First, the plugin should be useful for other people. "Useful" is a subjective term. We will | 
					
						
							| 
									
										
										
										
											2017-06-03 08:26:51 +01:00
										 |  |  |   probably need to further refine this. | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | * It should be sufficiently different from other plugin to warrant inclusion. | 
					
						
							| 
									
										
										
										
											2017-06-03 08:26:51 +01:00
										 |  |  | * Current internet standards need be supported: IPv4 and IPv6, so A and AAAA records should be | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  |   handled (if your plugin is in the business of dealing with address records that is). | 
					
						
							| 
									
										
										
										
											2017-06-03 08:26:51 +01:00
										 |  |  | * It must have tests. | 
					
						
							|  |  |  | * It must have a README.md for documentation. |