|
| 1 | +package utils |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "sync" |
| 7 | + "time" |
| 8 | + |
| 9 | + corev1 "k8s.io/api/core/v1" |
| 10 | + "k8s.io/apimachinery/pkg/api/errors" |
| 11 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 12 | + "k8s.io/apimachinery/pkg/types" |
| 13 | + "k8s.io/client-go/util/retry" |
| 14 | + |
| 15 | + placementv1beta1 "github.com/kubefleet-dev/kubefleet/apis/placement/v1beta1" |
| 16 | +) |
| 17 | + |
| 18 | +func (r *Runner) CleanUp(ctx context.Context) { |
| 19 | + wg := sync.WaitGroup{} |
| 20 | + |
| 21 | + // Run the producer. |
| 22 | + wg.Add(1) |
| 23 | + go func() { |
| 24 | + defer wg.Done() |
| 25 | + |
| 26 | + for i := 0; i < r.maxCRPToCreateCount; i++ { |
| 27 | + select { |
| 28 | + case r.toDeleteChan <- i: |
| 29 | + case <-ctx.Done(): |
| 30 | + close(r.toDeleteChan) |
| 31 | + return |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + close(r.toDeleteChan) |
| 36 | + }() |
| 37 | + |
| 38 | + // Run the workers. |
| 39 | + for i := 0; i < r.resourceSetupWorkerCount; i++ { |
| 40 | + wg.Add(1) |
| 41 | + go func(workerIdx int) { |
| 42 | + defer wg.Done() |
| 43 | + |
| 44 | + for { |
| 45 | + // Read from the channel. |
| 46 | + var resIdx int |
| 47 | + var readOk bool |
| 48 | + select { |
| 49 | + case resIdx, readOk = <-r.toDeleteChan: |
| 50 | + if !readOk { |
| 51 | + fmt.Printf("worker %d exits\n", workerIdx) |
| 52 | + return |
| 53 | + } |
| 54 | + case <-ctx.Done(): |
| 55 | + return |
| 56 | + } |
| 57 | + |
| 58 | + // Delete the CRPs. |
| 59 | + crp := placementv1beta1.ClusterResourcePlacement{ |
| 60 | + ObjectMeta: metav1.ObjectMeta{ |
| 61 | + Name: fmt.Sprintf(placementNameFmt, resIdx), |
| 62 | + }, |
| 63 | + } |
| 64 | + errAfterRetries := retry.OnError(r.retryOpsBackoff, func(err error) bool { |
| 65 | + return err != nil && !errors.IsNotFound(err) |
| 66 | + }, func() error { |
| 67 | + return r.hubClient.Delete(ctx, &crp) |
| 68 | + }) |
| 69 | + if errAfterRetries != nil && !errors.IsNotFound(errAfterRetries) { |
| 70 | + fmt.Printf("worker %d: failed to delete CRP %s after retries: %v\n", workerIdx, fmt.Sprintf(placementNameFmt, resIdx), errAfterRetries) |
| 71 | + continue |
| 72 | + } |
| 73 | + |
| 74 | + // Wait until the CRP is deleted. |
| 75 | + errAfterRetries = retry.OnError(r.retryOpsBackoff, func(err error) bool { |
| 76 | + return err != nil && !errors.IsNotFound(err) |
| 77 | + }, func() error { |
| 78 | + crp := placementv1beta1.ClusterResourcePlacement{} |
| 79 | + err := r.hubClient.Get(ctx, types.NamespacedName{Name: fmt.Sprintf(placementNameFmt, resIdx)}, &crp) |
| 80 | + if err == nil { |
| 81 | + return fmt.Errorf("CRP %s still exists", fmt.Sprintf(placementNameFmt, resIdx)) |
| 82 | + } |
| 83 | + return err |
| 84 | + }) |
| 85 | + if !errors.IsNotFound(errAfterRetries) { |
| 86 | + fmt.Printf("worker %d: failed to wait for CRP %s to be deleted after retries: %v\n", workerIdx, fmt.Sprintf(placementNameFmt, resIdx), errAfterRetries) |
| 87 | + } else { |
| 88 | + fmt.Printf("worker %d: deleted CRP %s\n", workerIdx, fmt.Sprintf(placementNameFmt, resIdx)) |
| 89 | + } |
| 90 | + |
| 91 | + // Delete the namespace if it exists. |
| 92 | + namespace := corev1.Namespace{ |
| 93 | + ObjectMeta: metav1.ObjectMeta{ |
| 94 | + Name: fmt.Sprintf(nsNameFmt, resIdx), |
| 95 | + }, |
| 96 | + } |
| 97 | + errAfterRetries = retry.OnError(r.retryOpsBackoff, func(err error) bool { |
| 98 | + return err != nil && !errors.IsNotFound(err) |
| 99 | + }, func() error { |
| 100 | + return r.hubClient.Delete(ctx, &namespace) |
| 101 | + }) |
| 102 | + if errAfterRetries != nil && !errors.IsNotFound(errAfterRetries) { |
| 103 | + fmt.Printf("worker %d: failed to delete namespace %s after retries: %v\n", workerIdx, fmt.Sprintf(nsNameFmt, resIdx), errAfterRetries) |
| 104 | + continue |
| 105 | + } |
| 106 | + |
| 107 | + // Wait until the namespace is deleted. |
| 108 | + errAfterRetries = retry.OnError(r.retryOpsBackoff, func(err error) bool { |
| 109 | + return err != nil && !errors.IsNotFound(err) |
| 110 | + }, func() error { |
| 111 | + namespace := corev1.Namespace{} |
| 112 | + err := r.hubClient.Get(ctx, types.NamespacedName{Name: fmt.Sprintf(nsNameFmt, resIdx)}, &namespace) |
| 113 | + if err == nil { |
| 114 | + return fmt.Errorf("namespace %s still exists", fmt.Sprintf(nsNameFmt, resIdx)) |
| 115 | + } |
| 116 | + return err |
| 117 | + }) |
| 118 | + if errAfterRetries == nil || !errors.IsNotFound(errAfterRetries) { |
| 119 | + fmt.Printf("worker %d: failed to wait for namespace %s to be deleted after retries: %v\n", workerIdx, fmt.Sprintf(nsNameFmt, resIdx), errAfterRetries) |
| 120 | + } else { |
| 121 | + fmt.Printf("worker %d: deleted namespace %s\n", workerIdx, fmt.Sprintf(nsNameFmt, resIdx)) |
| 122 | + } |
| 123 | + } |
| 124 | + }(i) |
| 125 | + } |
| 126 | + wg.Wait() |
| 127 | + |
| 128 | + // Cool down. |
| 129 | + time.Sleep(r.betweenStageCoolDownPeriod) |
| 130 | +} |
0 commit comments