Last active
September 28, 2022 13:57
-
-
Save clementnuss/78298c31f57f27ac0e3ee82e9ef2466a to your computer and use it in GitHub Desktop.
script to rollout restart all deployments on a kubernetes cluster by packs of 20, waiting 1min between packs
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
package main | |
import ( | |
"fmt" | |
"os" | |
"strings" | |
"time" | |
"github.com/bitfield/script" | |
) | |
func main() { | |
const rsrcType string = "statefulset" | |
ressources, err := script.Exec(fmt.Sprintf( | |
`kubectl get %s --all-namespaces --no-headers | |
-o custom-columns='ns:.metadata.namespace,name:.metadata.name, | |
lastRestartAt:.spec.template.metadata.annotations.kubectl\.kubernetes\.io/restartedAt' | |
`, rsrcType)).Slice() | |
if err != nil { | |
fmt.Println(fmt.Sprintf("couldn't retrieve %s list. err: %v", rsrcType, err)) | |
os.Exit(-1) | |
} | |
count := 0 | |
for idx, rsrcLine := range ressources { | |
splitted := strings.Fields(rsrcLine) | |
ns, name, lastRestartedAt := splitted[0], splitted[1], splitted[2] | |
identifier := fmt.Sprintf("%s %s/%s", rsrcType, ns, name) | |
lastRestart, err := time.Parse(time.RFC3339, lastRestartedAt) | |
if err != nil { | |
lastRestart = time.Time{} | |
} | |
fmt.Printf("lastRestartTime: %v ## ", lastRestart) | |
if time.Now().Sub(lastRestart) < 5*time.Hour { | |
fmt.Printf("skipping restart for %s. last restart < 5 hours ago\n", identifier) | |
continue | |
} | |
fmt.Printf("restarting %s\n", identifier) | |
rolloutPipe := script.Exec(fmt.Sprintf("kubectl rollout restart -n %s %s %s", ns, rsrcType, name)) | |
rolloutPipe.Wait() | |
if rolloutPipe.Error() != nil { | |
fmt.Printf("failed to restart %s: error: %v\n", | |
identifier, rolloutPipe.Error()) | |
} | |
count++ | |
if count%20 == 0 { | |
fmt.Printf("sleeping for a minute. current progress: %2.2f%% \n", | |
100.0*float64(idx)/float64(len(ressources))) | |
time.Sleep(time.Minute) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment