Skip to content
This repository has been archived by the owner on Aug 31, 2020. It is now read-only.

Commit

Permalink
rudimentary Prometheus container retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
nustiueudinastea committed Mar 24, 2017
1 parent c2e1ac1 commit cf6d1d9
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/spf13/cobra"
)

var prometheusContainerID = "b171793f9e26"
var prometheusService string

// allocateIP returns the 3rd last IP in the network range.
func allocateIP(netCIDR *net.IPNet) string {
Expand All @@ -29,13 +29,13 @@ func allocateIP(netCIDR *net.IPNet) string {
return allocIP.String()
}

func connectNetworks(networks map[string]swarm.Network) {
func connectNetworks(networks map[string]swarm.Network, containerID string) {
cli, err := client.NewEnvClient()
if err != nil {
panic(err)
}

prometheusContainer, err := cli.ContainerInspect(context.Background(), prometheusContainerID)
prometheusContainer, err := cli.ContainerInspect(context.Background(), containerID)
if err != nil {
panic(err)
}
Expand All @@ -56,14 +56,14 @@ func connectNetworks(networks map[string]swarm.Network) {
panic(err)
}
prometheusIP := allocateIP(netCIDR)
fmt.Println("Connecting network ", netwrk.Spec.Name, "(", netCIDR.IP, ") to ", prometheusContainerID, "(", prometheusIP, ")")
fmt.Println("Connecting network ", netwrk.Spec.Name, "(", netCIDR.IP, ") to ", containerID, "(", prometheusIP, ")")
netconfig := &network.EndpointSettings{
IPAMConfig: &network.EndpointIPAMConfig{
IPv4Address: prometheusIP,
},
}

err = cli.NetworkConnect(context.Background(), netwrk.ID, prometheusContainerID, netconfig)
err = cli.NetworkConnect(context.Background(), netwrk.ID, containerID, netconfig)
if err != nil {
panic(err)
}
Expand All @@ -86,12 +86,31 @@ func writeSDConfig(scrapeTargets []scrapeTarget) {
}
}

func findPrometheusContainer(serviceName string) string {
cli, err := client.NewEnvClient()
if err != nil {
panic(err)
}

taskFilters := filters.NewArgs()
taskFilters.Add("desired-state", string(swarm.TaskStateRunning))
taskFilters.Add("service", serviceName)

promTasks, err := cli.TaskList(context.Background(), types.TaskListOptions{Filters: taskFilters})
if err != nil {
panic(err)
}

return promTasks[0].Status.ContainerStatus.ContainerID
}

type scrapeTarget struct {
Targets []string
Labels map[string]string
}

func discoverSwarm(cmd *cobra.Command, args []string) {
findPrometheusContainer(prometheusService)
cli, err := client.NewEnvClient()
if err != nil {
panic(err)
Expand Down Expand Up @@ -135,7 +154,8 @@ func discoverSwarm(cmd *cobra.Command, args []string) {
}
}

connectNetworks(taskNetworks)
prometheusContainerID := findPrometheusContainer(prometheusService)
connectNetworks(taskNetworks, prometheusContainerID)
writeSDConfig(scrapeTargets)
}

Expand All @@ -147,6 +167,8 @@ func main() {
Run: discoverSwarm,
}

cmdDiscover.Flags().StringVarP(&prometheusService, "prometheus", "p", "prometheus", "Name of the Prometheus service")

var rootCmd = &cobra.Command{Use: "promswarm"}
rootCmd.AddCommand(cmdDiscover)
rootCmd.Execute()
Expand Down

0 comments on commit cf6d1d9

Please sign in to comment.