knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/test/ha/ha.go (about)

     1  /*
     2  Copyright 2020 The Knative Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package ha
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"strings"
    23  	"testing"
    24  	"time"
    25  
    26  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    27  	"k8s.io/apimachinery/pkg/util/sets"
    28  	"k8s.io/apimachinery/pkg/util/wait"
    29  	"k8s.io/client-go/kubernetes"
    30  
    31  	"knative.dev/pkg/test/logging"
    32  )
    33  
    34  func countingRFind(wr rune, wc int) func(rune) bool {
    35  	cnt := 0
    36  	return func(r rune) bool {
    37  		if r == wr {
    38  			cnt++
    39  		}
    40  		return cnt == wc
    41  	}
    42  }
    43  
    44  func extractDeployment(pod string) string {
    45  	if x := strings.LastIndexFunc(pod, countingRFind('-', 2)); x != -1 {
    46  		return pod[:x]
    47  	}
    48  	return ""
    49  }
    50  
    51  // GetLeaders collects all of the leader pods from the specified deployment.
    52  // GetLeaders will return duplicate pods by design.
    53  func GetLeaders(ctx context.Context, t *testing.T, client kubernetes.Interface, deploymentName, namespace string) ([]string, error) {
    54  	leases, err := client.CoordinationV1().Leases(namespace).List(ctx, metav1.ListOptions{})
    55  	if err != nil {
    56  		return nil, fmt.Errorf("error getting leases for deployment %q: %w", deploymentName, err)
    57  	}
    58  	ret := make([]string, 0, len(leases.Items))
    59  	for _, lease := range leases.Items {
    60  		if lease.Spec.HolderIdentity == nil || *lease.Spec.HolderIdentity == "" {
    61  			t.Logf("GetLeaders[%s] skipping lease %s as it has no holder", deploymentName, lease.Name)
    62  			continue
    63  		}
    64  		pod := strings.SplitN(*lease.Spec.HolderIdentity, "_", 2)[0]
    65  
    66  		// Deconstruct the pod name and look for the deployment.  This won't work for very long deployment names.
    67  		if extractDeployment(pod) != deploymentName {
    68  			continue
    69  		}
    70  		t.Logf("GetLeaders[%s] adding lease %s for pod %s", deploymentName, lease.Name, pod)
    71  		ret = append(ret, pod)
    72  	}
    73  	return ret, nil
    74  }
    75  
    76  // WaitForNewLeaders waits until the collection of current leaders consists of "n" leaders
    77  // which do not include the specified prior leaders.
    78  func WaitForNewLeaders(ctx context.Context, t *testing.T, client kubernetes.Interface, deploymentName, namespace string, previousLeaders sets.Set[string], n int) (sets.Set[string], error) {
    79  	span := logging.GetEmitableSpan(ctx, "WaitForNewLeaders/"+deploymentName)
    80  	defer span.End()
    81  
    82  	var leaders sets.Set[string]
    83  	err := wait.PollUntilContextTimeout(ctx, time.Second, 2*time.Minute, true, func(ctx context.Context) (bool, error) {
    84  		currLeaders, err := GetLeaders(ctx, t, client, deploymentName, namespace)
    85  		if err != nil {
    86  			return false, err
    87  		}
    88  		if len(currLeaders) < n {
    89  			t.Logf("WaitForNewLeaders[%s] not enough leaders, got: %d, want: %d", deploymentName, len(currLeaders), n)
    90  			return false, nil
    91  		}
    92  		l := sets.New[string](currLeaders...)
    93  		if previousLeaders.HasAny(currLeaders...) {
    94  			t.Logf("WaitForNewLeaders[%s] still see intersection: %v", deploymentName, previousLeaders.Intersection(l))
    95  			return false, nil
    96  		}
    97  		leaders = l
    98  		return true, nil
    99  	})
   100  	return leaders, err
   101  }
   102  
   103  // WaitForNewLeader waits until the holder of the given lease is different from the previousLeader.
   104  //
   105  // Deprecated: Use WaitForNewLeaders.
   106  func WaitForNewLeader(ctx context.Context, client kubernetes.Interface, lease, namespace, previousLeader string) (string, error) {
   107  	span := logging.GetEmitableSpan(ctx, "WaitForNewLeader/"+lease)
   108  	defer span.End()
   109  	var leader string
   110  	err := wait.PollUntilContextTimeout(ctx, time.Second, time.Minute, true, func(ctx context.Context) (bool, error) {
   111  		lease, err := client.CoordinationV1().Leases(namespace).Get(ctx, lease, metav1.GetOptions{})
   112  		if err != nil {
   113  			return false, fmt.Errorf("error getting lease %s: %w", lease, err)
   114  		}
   115  		leader = strings.Split(*lease.Spec.HolderIdentity, "_")[0]
   116  		return leader != previousLeader, nil
   117  	})
   118  	return leader, err
   119  }