in projects/aws/bottlerocket-bootstrap/pkg/utils/waiters.go [127:175]
func KillCmdAfterFilesGeneration(cmd *exec.Cmd, checkFiles []string) error {
// Check for files written and send ok signal back on channel
// ctx is used here to cancel the goroutine if the timeout has occurred
okChan := make(chan bool)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func(ctx context.Context, okChan chan bool) {
for {
time.Sleep(2 * time.Second)
select {
case <-ctx.Done():
return
default:
allFilesExist := true
for _, file := range checkFiles {
if fileInfo, err := os.Stat(file); err == nil {
fmt.Printf("File %s exists with size: %d\n", file, fileInfo.Size())
if fileInfo.Size() == 0 {
fmt.Printf("File %s doesnt not have any size yet\n", file)
}
} else if os.IsNotExist(err) {
fmt.Printf("File %s doest not exist yet\n", file)
allFilesExist = false
}
}
// Send ok on the channel
if allFilesExist {
okChan <- true
return
}
}
}
}(ctx, okChan)
timeout := time.After(40 * time.Second)
select {
case <-okChan:
fmt.Println("All files were created, exiting kubeadm command")
cmd.Process.Kill()
return nil
case <-timeout:
cmd.Process.Kill()
cancel()
return errors.New(fmt.Sprintf("command: %s killed after timeout", strings.Join(cmd.Args, " ")))
}
}