Skip to content

Instantly share code, notes, and snippets.

@albertoleal
Created June 8, 2016 13:45
Show Gist options
  • Save albertoleal/ea9f223c37656e386ca5ad5b7a9f6047 to your computer and use it in GitHub Desktop.
Save albertoleal/ea9f223c37656e386ca5ad5b7a9f6047 to your computer and use it in GitHub Desktop.
ipcheck
package main
import (
"fmt"
"sync"
"github.com/cloudfoundry-incubator/garden"
"github.com/cloudfoundry-incubator/garden/client"
"github.com/cloudfoundry-incubator/garden/client/connection"
)
func existingIPs(gdn client.Client) map[string]string {
containers, err := gdn.Containers(garden.Properties{})
if err != nil {
fmt.Printf("[ERROR] Unable to get containers\n")
}
ips := make(map[string]string)
for _, container := range containers {
info, err := container.Info()
if err != nil {
fmt.Printf("[ERROR] Unable to get container Info for %s\n", container.Handle())
}
ips[info.ContainerIP] = container.Handle()
}
return ips
}
func main() {
containerAmt := 20
gardenClient := client.New(connection.New("unix", "/var/vcap/data/garden/garden.sock"))
for {
containersCh := make(chan string, containerAmt)
wg := sync.WaitGroup{}
existingIps := existingIPs(gardenClient)
for i := 0; i < containerAmt; i++ {
wg.Add(1)
go func(i int) {
fmt.Printf("[INFO] Creating container %d\n", i)
container, err := gardenClient.Create(garden.ContainerSpec{})
if err != nil {
wg.Done()
return
}
info, err := container.Info()
if err != nil {
return
}
if val, ok := existingIps[info.ContainerIP]; ok {
fmt.Printf("Collision happened between handle: %s, %s", val, container.Handle())
panic("Boom")
}
existingIps[info.ContainerIP] = container.Handle()
containersCh <- container.Handle()
}(i)
}
for i := 0; i < containerAmt; i++ {
go func() {
defer wg.Done()
handle := <-containersCh
fmt.Printf("[INFO] Deleting container %s\n", handle)
err := gardenClient.Destroy(handle)
if err != nil {
fmt.Printf("[ERROR} Failed to destroy container %s\n", handle)
return
}
}()
}
wg.Wait()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment