github.com/verrazzano/verrazzano@v1.7.1/tests/e2e/pkg/ha/signal.go (about)

     1  // Copyright (c) 2022, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  
     4  package ha
     5  
     6  import (
     7  	"context"
     8  	"time"
     9  
    10  	"github.com/onsi/gomega"
    11  	"github.com/verrazzano/verrazzano/tests/e2e/pkg/test/framework"
    12  	"go.uber.org/zap"
    13  	corev1 "k8s.io/api/core/v1"
    14  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    15  	"k8s.io/client-go/kubernetes"
    16  )
    17  
    18  const (
    19  	shutdownSignalName      = "ha-shutdown-signal"
    20  	shutdownSignalNamespace = "default"
    21  
    22  	shortWaitTimeout     = time.Minute
    23  	shortPollingInterval = 5 * time.Second
    24  )
    25  
    26  func EventuallyCreateShutdownSignal(cs *kubernetes.Clientset, log *zap.SugaredLogger) {
    27  	gomega.Eventually(func() bool {
    28  		shutdownSignal := &corev1.Secret{
    29  			ObjectMeta: metav1.ObjectMeta{
    30  				Namespace: shutdownSignalNamespace,
    31  				Name:      shutdownSignalName,
    32  			},
    33  		}
    34  		if _, err := cs.CoreV1().Secrets(shutdownSignalNamespace).Create(context.TODO(), shutdownSignal, metav1.CreateOptions{}); err != nil {
    35  			log.Errorf("Failed to create shutdown signal: %v", err)
    36  			return false
    37  		}
    38  		return true
    39  	}).WithTimeout(shortWaitTimeout).WithPolling(shortPollingInterval).Should(gomega.BeTrue())
    40  	log.Infof("Created shutdown signal %s", shutdownSignalName)
    41  }
    42  
    43  func IsShutdownSignalSet(cs *kubernetes.Clientset) bool {
    44  	_, err := cs.CoreV1().Secrets(shutdownSignalNamespace).Get(context.TODO(), shutdownSignalName, metav1.GetOptions{})
    45  	return err == nil
    46  }
    47  
    48  // RunningUntilShutdownIt runs the test function repeatedly until the shutdown signal is set. If the "runContinues" flag is false, the
    49  // test is only run one time.
    50  func RunningUntilShutdownIt(t *framework.TestFramework, description string, clientset *kubernetes.Clientset, runContinuous bool, test func()) {
    51  	t.It(description, func() {
    52  		gomega.Expect(clientset).ToNot(gomega.BeNil())
    53  		for {
    54  			test()
    55  			// break out of the loop if we are not running the suite continuously,
    56  			// or the shutdown signal is set
    57  			if !runContinuous || IsShutdownSignalSet(clientset) {
    58  				t.Logs.Info("Shutting down...")
    59  				break
    60  			}
    61  		}
    62  	})
    63  }