-
-
Notifications
You must be signed in to change notification settings - Fork 964
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement docker service discovery via labels
- Loading branch information
Showing
8 changed files
with
145 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,26 @@ | ||
import { promises as fs } from "fs"; | ||
import path from "path"; | ||
|
||
import yaml from "js-yaml"; | ||
|
||
import checkAndCopyConfig from "utils/config"; | ||
import { servicesFromConfig, servicesFromDocker, cleanServiceGroups } from "utils/service-helpers"; | ||
|
||
export default async function handler(req, res) { | ||
checkAndCopyConfig("services.yaml"); | ||
const discoveredServices = cleanServiceGroups(await servicesFromDocker()); | ||
const configuredServices = cleanServiceGroups(await servicesFromConfig()); | ||
|
||
const servicesYaml = path.join(process.cwd(), "config", "services.yaml"); | ||
const fileContents = await fs.readFile(servicesYaml, "utf8"); | ||
const services = yaml.load(fileContents); | ||
const mergedGroupsNames = [ | ||
...new Set([discoveredServices.map((group) => group.name), configuredServices.map((group) => group.name)].flat()), | ||
]; | ||
|
||
// map easy to write YAML objects into easy to consume JS arrays | ||
const servicesArray = services.map((group) => ({ | ||
name: Object.keys(group)[0], | ||
services: group[Object.keys(group)[0]].map((entries) => { | ||
const { widget, ...service } = entries[Object.keys(entries)[0]]; | ||
const result = { | ||
name: Object.keys(entries)[0], | ||
...service, | ||
}; | ||
const mergedGroups = []; | ||
|
||
if (widget) { | ||
const { type } = widget; | ||
mergedGroupsNames.forEach((groupName) => { | ||
const discoveredGroup = discoveredServices.find((group) => group.name === groupName) || { services: [] }; | ||
const configuredGroup = configuredServices.find((group) => group.name === groupName) || { services: [] }; | ||
|
||
result.widget = { | ||
type, | ||
service_group: Object.keys(group)[0], | ||
service_name: Object.keys(entries)[0], | ||
}; | ||
} | ||
const mergedGroup = { | ||
name: groupName, | ||
services: [...discoveredGroup.services, ...configuredGroup.services].filter((service) => service), | ||
}; | ||
|
||
return result; | ||
}), | ||
})); | ||
mergedGroups.push(mergedGroup); | ||
}); | ||
|
||
res.send(servicesArray); | ||
res.send(mergedGroups); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters