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

Commit

Permalink
implemented network removal if Prometheus is the last container conne…
Browse files Browse the repository at this point in the history
…cted to a network
  • Loading branch information
nustiueudinastea committed Mar 27, 2017
1 parent 5a8af25 commit a59950e
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"io/ioutil"
"net"
"reflect"
"time"

"strings"
Expand All @@ -24,6 +25,7 @@ var prometheusService string
var discoveryInterval int
var logLevel string
var output string
var clean bool
var logger = logrus.New()

// allocateIP returns the 3rd last IP in the network range.
Expand Down Expand Up @@ -113,6 +115,36 @@ func findPrometheusContainer(serviceName string) (string, error) {
return promTasks[0].Status.ContainerStatus.ContainerID, nil
}

func cleanNetworks(prometheusContainerID string) {
cli, err := client.NewEnvClient()

networkFilters := filters.NewArgs()
networkFilters.Add("driver", "overlay")
networks, err := cli.NetworkList(context.Background(), types.NetworkListOptions{Filters: networkFilters})
if err != nil {
panic(err)
}

for _, network := range networks {
if network.Name == "ingress" {
continue
}

if len(network.Containers) == 1 && reflect.ValueOf(network.Containers).MapKeys()[0].String() == prometheusContainerID {
logger.Info("Network ", network.Name, " contains only the Prometheus container. Disconnecting and removing network.")
cli.NetworkDisconnect(context.Background(), network.ID, prometheusContainerID, true)
if err != nil {
logger.Error(err)
}
err := cli.NetworkRemove(context.Background(), network.ID)
if err != nil {
logger.Error(err)
}
}
}

}

type scrapeService struct {
ServiceName string
scrapeTasks scrapeTask
Expand Down Expand Up @@ -220,6 +252,9 @@ func discoveryProcess(cmd *cobra.Command, args []string) {
}

discoverSwarm(prometheusContainerID)
if clean {
cleanNetworks(prometheusContainerID)
}
}
}

Expand All @@ -235,6 +270,7 @@ func main() {
cmdDiscover.Flags().IntVarP(&discoveryInterval, "interval", "i", 30, "The interval, in seconds, at which the discovery process is kicked off")
cmdDiscover.Flags().StringVarP(&logLevel, "loglevel", "l", "info", "Specify log level: debug, info, warn, error")
cmdDiscover.Flags().StringVarP(&output, "output", "o", "swarm-endpoints.json", "Output file that contains the Prometheus endpoints.")
cmdDiscover.Flags().BoolVarP(&clean, "clean", "c", true, "Disconnects unused networks from the Prometheus container, and deletes them.")

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

0 comments on commit a59950e

Please sign in to comment.