The service discovery adapter

As a high-level overview, the adapter takes care of launching and managing our custom service discovery code, consuming the groups of targets it produces, converting them into file_sd format, and ensuring that the JSON data is written to a file when required. When writing a service discovery integration using this adapter, no change is needed in its code, and so it can just be imported as a library. To give a bit more context about what the adapter is doing, we're going to explain some of the lower-level details so that its behaviors are clear when we implement our own discovery using it.

The following snippet illustrates the Run function of the adapter that we will need to invoke from our code. This function will take care of starting a discovery.Manager in its own goroutine (a.manager.Run), instructing it to run our discovery implementation (a.disc), and, finally, running the adapter itself in another goroutine (a.runCustomSD):

// Run starts a Discovery Manager and the custom service discovery implementation.
func (a *Adapter) Run() {
go a.manager.Run()
a.manager.StartCustomProvider(a.ctx, a.name, a.disc)
go a.runCustomSD(a.ctx)
}

After starting, the adapter consumes from a channel provided by the Manager that updates the target groups that our code will produce. When an update arrives, it will convert the target groups into file_sd format and verify whether there were any changes since the last update. If there are changes, it will store the new target groups for future comparisons and write them out as JSON to the output file. This implies that the full list of target groups should be sent in every update; groups that are not sent through the channel will get removed from the produced discovery file.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.227.194