From ccbc3737c8c7f234026a61b1ae83deed4a16dee2 Mon Sep 17 00:00:00 2001 From: Francesco Romani Date: Tue, 24 Sep 2024 17:33:13 +0200 Subject: [PATCH] gh actions: catkubeletconfmap: add and use wait add and use wait loop to avoid false negatives in CI Signed-off-by: Francesco Romani --- .github/workflows/e2e.yaml | 2 +- tools/catkubeletconfmap/catkubeletconfmap.go | 46 +++++++++++++++----- 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/.github/workflows/e2e.yaml b/.github/workflows/e2e.yaml index a4fe58f30..11417173c 100644 --- a/.github/workflows/e2e.yaml +++ b/.github/workflows/e2e.yaml @@ -60,7 +60,7 @@ jobs: - name: E2E Tests run: | export KUBECONFIG=${HOME}/.kube/config - bin/catkubeletconfmap --namespace ${E2E_NAMESPACE_NAME} --prefix 'E2E_' >> $GITHUB_ENV + bin/catkubeletconfmap --wait 2m --namespace ${E2E_NAMESPACE_NAME} --prefix 'E2E_' >> $GITHUB_ENV make test-e2e - name: Export E2E Tests logs diff --git a/tools/catkubeletconfmap/catkubeletconfmap.go b/tools/catkubeletconfmap/catkubeletconfmap.go index f0bedd708..19462e89f 100644 --- a/tools/catkubeletconfmap/catkubeletconfmap.go +++ b/tools/catkubeletconfmap/catkubeletconfmap.go @@ -21,8 +21,11 @@ import ( "flag" "fmt" "log" + "time" corev1 "k8s.io/api/core/v1" + apierrors "k8s.io/apimachinery/pkg/api/errors" + k8swait "k8s.io/apimachinery/pkg/util/wait" "sigs.k8s.io/controller-runtime/pkg/client" @@ -32,12 +35,17 @@ import ( ) func main() { - var prefix string - var cmNamespace string - var cmName string - flag.StringVar(&cmNamespace, "namespace", "numaresources", "namespace to look the configmap into") - flag.StringVar(&cmName, "name", "numaresourcesoperator-worker", "name of the configmap to look for") - flag.StringVar(&prefix, "prefix", "", "prefix for the output") + prefix := "" + cmNamespace := "numaresources" + cmName := "numaresourcesoperator-worker" + waitTimeout := 0 * time.Second // no wait + waitInterval := 2 * time.Second // if we wait at all, this is the intervall between polls + waitImmediate := true // guarantee to call at least once, necessary with default timout + + flag.StringVar(&cmNamespace, "namespace", cmNamespace, "namespace to look the configmap into") + flag.StringVar(&cmName, "name", cmName, "name of the configmap to look for") + flag.StringVar(&prefix, "prefix", prefix, "prefix for the output") + flag.DurationVar(&waitTimeout, "wait", waitTimeout, "retry till this time limit") flag.Parse() cli, err := clientutil.New() @@ -45,13 +53,29 @@ func main() { log.Fatalf("error creating a client: %v", err) } + log.Printf("trying to fetch %s/%s...", cmNamespace, cmName) + ctx := context.Background() - key := client.ObjectKey{ - Namespace: cmNamespace, - Name: cmName, - } cm := corev1.ConfigMap{} - err = cli.Get(ctx, key, &cm) + + err = k8swait.PollUntilContextTimeout(ctx, waitInterval, waitTimeout, waitImmediate, func(fctx context.Context) (bool, error) { + key := client.ObjectKey{ + Namespace: cmNamespace, + Name: cmName, + } + ferr := cli.Get(fctx, key, &cm) + if ferr != nil { + if apierrors.IsNotFound(ferr) { + log.Printf("failed to get %s/%s - not found, retrying...", cmNamespace, cmName) + return false, nil + } + log.Printf("failed to get %s/%s: %v, aborting", cmNamespace, cmName, ferr) + return false, ferr + } + log.Printf("got %s/%s!", cmNamespace, cmName) + return true, nil + }) + if err != nil { log.Fatalf("error getting the ConfigMap %s/%s: %v", cmNamespace, cmName, err) }