volcano.sh/volcano@v1.9.0/pkg/scheduler/util/assert/assert.go (about)

     1  package assert
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"runtime/debug"
     7  
     8  	"k8s.io/klog/v2"
     9  )
    10  
    11  const (
    12  	// EnvPanicOnError is the env name to determine panic on assertion failed or not
    13  	EnvPanicOnError = "PANIC_ON_ERROR"
    14  )
    15  
    16  var (
    17  	panicOnError = true
    18  )
    19  
    20  func init() {
    21  	env := os.Getenv(EnvPanicOnError)
    22  	if env == "false" {
    23  		panicOnError = false
    24  	}
    25  }
    26  
    27  // Assert check condition, if condition is false, print message by log or panic
    28  func Assert(condition bool, message string) {
    29  	if condition {
    30  		return
    31  	}
    32  	if panicOnError {
    33  		panic(message)
    34  	}
    35  	klog.Errorf("%s, %s", message, debug.Stack())
    36  }
    37  
    38  // Assertf check condition, if condition is false, print message using Assert
    39  func Assertf(condition bool, format string, args ...interface{}) {
    40  	if condition {
    41  		return
    42  	}
    43  	Assert(condition, fmt.Sprintf(format, args...))
    44  }