Skip to content

Instantly share code, notes, and snippets.

@varshavaradarajan
Created September 2, 2020 18:37
Show Gist options
  • Save varshavaradarajan/ab8112970c537e8ce805118d35d91fd8 to your computer and use it in GitHub Desktop.
Save varshavaradarajan/ab8112970c537e8ce805118d35d91fd8 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"os"
"golang.org/x/sync/errgroup"
)
func main() {
nodeNames := []string{"host-1", "host-2", "host-3"}
nodeStatuses := make(map[string]string)
type drainStatus struct {
nodeName string
status string
}
var g errgroup.Group
statuses := make(chan drainStatus)
for _, nodeName := range nodeNames {
nodeName := nodeName
g.Go(func() error {
switch nodeName {
case "host-1":
statuses <- drainStatus{
nodeName: nodeName,
status: "draining",
}
case "host-2":
statuses <- drainStatus{
nodeName: nodeName,
status: "cordoned",
}
case "host-3":
statuses <- drainStatus{
nodeName: nodeName,
status: "drained",
}
}
// in actuality, we get all pods on the node, then iterate over pods and start a go routine for each to either delete or evict the pod
// and after waiting for the errgroup on the pods, we set the status of the node to "draining"
// if len(pods) == 0, we set node status to "drained" before we begin the for loop mentioned right above
// there is another case we determine and set node status to "drained" too before beginning the loop for pods.
return nil
})
}
s := <-statuses
nodeStatuses[s.nodeName] = s.status
if err := g.Wait(); err != nil {
fmt.Println("error")
os.Exit(1)
}
fmt.Println(nodeStatuses)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment