|
| 1 | +/* |
| 2 | +Copyright 2025 The KCP 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 workspace |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "slices" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/go-logr/logr" |
| 25 | + "github.com/google/go-cmp/cmp" |
| 26 | + |
| 27 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 28 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 29 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 30 | + "k8s.io/utils/ptr" |
| 31 | + |
| 32 | + "github.com/kcp-dev/logicalcluster/v3" |
| 33 | + |
| 34 | + corev1alpha1 "github.com/kcp-dev/kcp/sdk/apis/core/v1alpha1" |
| 35 | + tenancyv1alpha1 "github.com/kcp-dev/kcp/sdk/apis/tenancy/v1alpha1" |
| 36 | + kcpclientset "github.com/kcp-dev/kcp/sdk/client/clientset/versioned/cluster" |
| 37 | +) |
| 38 | + |
| 39 | +type fakeDeletionReconciler struct { |
| 40 | + clusters map[string]*corev1alpha1.LogicalCluster |
| 41 | + deletionReconciler |
| 42 | +} |
| 43 | + |
| 44 | +func (f *fakeDeletionReconciler) getLogicalCluster() func(ctx context.Context, cluster logicalcluster.Path) (*corev1alpha1.LogicalCluster, error) { |
| 45 | + return func(ctx context.Context, cluster logicalcluster.Path) (*corev1alpha1.LogicalCluster, error) { |
| 46 | + c, ok := f.clusters[cluster.String()] |
| 47 | + if !ok { |
| 48 | + return nil, apierrors.NewNotFound(schema.GroupResource{}, cluster.String()) |
| 49 | + } |
| 50 | + return c, nil |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func (f *fakeDeletionReconciler) deleteLogicalCluster() func(ctx context.Context, cluster logicalcluster.Path) error { |
| 55 | + return func(ctx context.Context, cluster logicalcluster.Path) error { |
| 56 | + delete(f.clusters, cluster.String()) |
| 57 | + return nil |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func (f *fakeDeletionReconciler) getShardByHash() func(hash string) (*corev1alpha1.Shard, error) { |
| 62 | + return func(hash string) (*corev1alpha1.Shard, error) { |
| 63 | + // for now we can work with a fixed shard |
| 64 | + s := &corev1alpha1.Shard{} |
| 65 | + return s, nil |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func (f *fakeDeletionReconciler) kcpLogicalClusterAdminClientFor() func(shard *corev1alpha1.Shard) (kcpclientset.ClusterInterface, error) { |
| 70 | + return func(shard *corev1alpha1.Shard) (kcpclientset.ClusterInterface, error) { |
| 71 | + return nil, nil |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func TestReconcile(t *testing.T) { |
| 76 | + tt := []struct { |
| 77 | + name string |
| 78 | + expStatus reconcileStatus |
| 79 | + workspace *tenancyv1alpha1.Workspace |
| 80 | + logicalclusters map[string]*corev1alpha1.LogicalCluster |
| 81 | + expFinalizers []string |
| 82 | + expLogicalClusters map[string]*corev1alpha1.LogicalCluster |
| 83 | + }{ |
| 84 | + { |
| 85 | + name: "no deletion timestamp", |
| 86 | + expStatus: reconcileStatusContinue, |
| 87 | + workspace: &tenancyv1alpha1.Workspace{}, |
| 88 | + }, |
| 89 | + { |
| 90 | + name: "another finalizer is still set", |
| 91 | + expStatus: reconcileStatusContinue, |
| 92 | + workspace: &tenancyv1alpha1.Workspace{ |
| 93 | + ObjectMeta: metav1.ObjectMeta{ |
| 94 | + DeletionTimestamp: ptr.To(metav1.Now()), |
| 95 | + Finalizers: []string{ |
| 96 | + corev1alpha1.LogicalClusterFinalizer, |
| 97 | + "other-finalizer", |
| 98 | + }, |
| 99 | + }, |
| 100 | + }, |
| 101 | + expFinalizers: []string{ |
| 102 | + corev1alpha1.LogicalClusterFinalizer, |
| 103 | + "other-finalizer", |
| 104 | + }, |
| 105 | + }, |
| 106 | + { |
| 107 | + name: "workspace is still initializing, no logicalcluster exists", |
| 108 | + expStatus: reconcileStatusContinue, |
| 109 | + workspace: &tenancyv1alpha1.Workspace{ |
| 110 | + ObjectMeta: metav1.ObjectMeta{ |
| 111 | + DeletionTimestamp: ptr.To(metav1.Now()), |
| 112 | + Finalizers: []string{ |
| 113 | + corev1alpha1.LogicalClusterFinalizer, |
| 114 | + }, |
| 115 | + }, |
| 116 | + Status: tenancyv1alpha1.WorkspaceStatus{ |
| 117 | + Phase: corev1alpha1.LogicalClusterPhaseScheduling, |
| 118 | + }, |
| 119 | + }, |
| 120 | + // we expect no finalizers here, as we can take a shortcut and directly |
| 121 | + // delete the workspace without needing to delete the logicalcluster first |
| 122 | + expFinalizers: []string{}, |
| 123 | + }, |
| 124 | + { |
| 125 | + name: "workspace is still initializing, logicalcluster not marked for deletion yet", |
| 126 | + expStatus: reconcileStatusContinue, |
| 127 | + workspace: &tenancyv1alpha1.Workspace{ |
| 128 | + ObjectMeta: metav1.ObjectMeta{ |
| 129 | + DeletionTimestamp: ptr.To(metav1.Now()), |
| 130 | + Finalizers: []string{ |
| 131 | + corev1alpha1.LogicalClusterFinalizer, |
| 132 | + }, |
| 133 | + Annotations: map[string]string{ |
| 134 | + workspaceClusterAnnotationKey: "test", |
| 135 | + }, |
| 136 | + }, |
| 137 | + Status: tenancyv1alpha1.WorkspaceStatus{ |
| 138 | + Phase: corev1alpha1.LogicalClusterPhaseScheduling, |
| 139 | + }, |
| 140 | + }, |
| 141 | + logicalclusters: map[string]*corev1alpha1.LogicalCluster{ |
| 142 | + "test": {}, |
| 143 | + }, |
| 144 | + // we do not remove our finalizers in this case, as we wait |
| 145 | + // for another reconciliation loop |
| 146 | + expFinalizers: []string{ |
| 147 | + corev1alpha1.LogicalClusterFinalizer, |
| 148 | + }, |
| 149 | + // we do expect our logicalCluster to be removed |
| 150 | + expLogicalClusters: map[string]*corev1alpha1.LogicalCluster{}, |
| 151 | + }, |
| 152 | + { |
| 153 | + name: "workspace is marked for deletion, logicalcluster is already deleted", |
| 154 | + expStatus: reconcileStatusStopAndRequeue, |
| 155 | + workspace: &tenancyv1alpha1.Workspace{ |
| 156 | + ObjectMeta: metav1.ObjectMeta{ |
| 157 | + DeletionTimestamp: ptr.To(metav1.Now()), |
| 158 | + Finalizers: []string{ |
| 159 | + corev1alpha1.LogicalClusterFinalizer, |
| 160 | + }, |
| 161 | + }, |
| 162 | + }, |
| 163 | + // finalizer should be removed |
| 164 | + expFinalizers: []string{}, |
| 165 | + }, |
| 166 | + { |
| 167 | + name: "workspace is marked for deletion, finalizer is already removed (edge case)", |
| 168 | + expStatus: reconcileStatusContinue, |
| 169 | + workspace: &tenancyv1alpha1.Workspace{ |
| 170 | + ObjectMeta: metav1.ObjectMeta{ |
| 171 | + DeletionTimestamp: ptr.To(metav1.Now()), |
| 172 | + }, |
| 173 | + }, |
| 174 | + }, |
| 175 | + } |
| 176 | + |
| 177 | + // swallow any log output, so we don't pollute test results |
| 178 | + ctx := logr.NewContext(context.Background(), logr.Discard()) |
| 179 | + |
| 180 | + for _, tc := range tt { |
| 181 | + t.Run(tc.name, func(t *testing.T) { |
| 182 | + fdr := fakeDeletionReconciler{ |
| 183 | + clusters: tc.logicalclusters, |
| 184 | + } |
| 185 | + fdr.deletionReconciler.getLogicalCluster = fdr.getLogicalCluster() |
| 186 | + fdr.deletionReconciler.deleteLogicalCluster = fdr.deleteLogicalCluster() |
| 187 | + fdr.deletionReconciler.getShardByHash = fdr.getShardByHash() |
| 188 | + fdr.deletionReconciler.kcpLogicalClusterAdminClientFor = fdr.kcpLogicalClusterAdminClientFor() |
| 189 | + |
| 190 | + status, err := fdr.deletionReconciler.reconcile(ctx, tc.workspace) |
| 191 | + |
| 192 | + if status != tc.expStatus { |
| 193 | + t.Errorf("Exp status %v, got %v", tc.expStatus, status) |
| 194 | + } |
| 195 | + |
| 196 | + if !slices.Equal(tc.expFinalizers, tc.workspace.Finalizers) { |
| 197 | + t.Errorf("Exp finalizers %v, got %v", tc.expFinalizers, tc.workspace.Finalizers) |
| 198 | + } |
| 199 | + |
| 200 | + if diff := cmp.Diff(tc.expLogicalClusters, tc.logicalclusters); diff != "" { |
| 201 | + t.Errorf("logicalcluster mismatch: %s", diff) |
| 202 | + } |
| 203 | + |
| 204 | + if err != nil { |
| 205 | + t.Errorf("did not expect an error, but got %b", err) |
| 206 | + } |
| 207 | + }) |
| 208 | + } |
| 209 | +} |
0 commit comments