Skip to content

Commit bbe564b

Browse files
committed
wait for cluster readiness
On-behalf-of: @SAP christoph.mewes@sap.com
1 parent ede7fbc commit bbe564b

File tree

3 files changed

+39
-37
lines changed

3 files changed

+39
-37
lines changed

test/e2e/clusterinit/basic_test.go

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,21 @@ limitations under the License.
1919
package clusterinit
2020

2121
import (
22-
"context"
23-
"slices"
2422
"strings"
2523
"testing"
26-
"time"
2724

2825
"github.com/go-logr/logr"
2926

3027
initializationv1alpha1 "github.com/kcp-dev/init-agent/sdk/apis/initialization/v1alpha1"
3128
"github.com/kcp-dev/init-agent/test/utils"
3229

3330
"github.com/kcp-dev/logicalcluster/v3"
34-
kcptenancyinitialization "github.com/kcp-dev/sdk/apis/tenancy/initialization"
3531
kcptenancyv1alpha1 "github.com/kcp-dev/sdk/apis/tenancy/v1alpha1"
3632

3733
corev1 "k8s.io/api/core/v1"
3834
rbacv1 "k8s.io/api/rbac/v1"
3935
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
4036
"k8s.io/apimachinery/pkg/types"
41-
"k8s.io/apimachinery/pkg/util/wait"
4237
ctrlruntime "sigs.k8s.io/controller-runtime"
4338
)
4439

@@ -77,8 +72,6 @@ func TestInitializeNewCluster(t *testing.T) {
7772
t.Fatalf("Failed to create WorkspaceType: %v", err)
7873
}
7974

80-
initializer := kcptenancyinitialization.InitializerForType(wst)
81-
8275
utils.GrantWorkspaceAccess(t, ctx, wstClient, utils.Subject(), rbacv1.PolicyRule{
8376
APIGroups: []string{"tenancy.kcp.io"},
8477
Resources: []string{"workspacetypes"},
@@ -185,17 +178,7 @@ metadata:
185178
}
186179

187180
// wait for the agent to do its work and initialize the cluster and ultimately remove the initializer
188-
err := wait.PollUntilContextTimeout(ctx, 500*time.Millisecond, 30*time.Second, false, func(ctx context.Context) (done bool, err error) {
189-
err = rootClient.Get(ctx, types.NamespacedName{Name: targetWorkspace}, targetWs)
190-
if err != nil {
191-
return false, err
192-
}
193-
194-
return !slices.Contains(targetWs.Status.Initializers, initializer), nil
195-
})
196-
if err != nil {
197-
t.Fatalf("Failed to wait for workspace to be initialized: %v", err)
198-
}
181+
targetWs = utils.WaitForWorkspaceInitialization(t, ctx, kcpClusterClient, rootCluster, targetWorkspace)
199182

200183
// connect into the new workspace and verify the generated content
201184
targetClient := kcpClusterClient.Cluster(rootCluster.Join(targetWorkspace))

test/e2e/clusterinit/wait_for_ready_test.go

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,20 @@ limitations under the License.
1919
package clusterinit
2020

2121
import (
22-
"context"
23-
"slices"
2422
"strings"
2523
"testing"
26-
"time"
2724

2825
"github.com/go-logr/logr"
2926

3027
initializationv1alpha1 "github.com/kcp-dev/init-agent/sdk/apis/initialization/v1alpha1"
3128
initagenttypes "github.com/kcp-dev/init-agent/sdk/types"
3229
"github.com/kcp-dev/init-agent/test/utils"
3330

34-
kcptenancyinitialization "github.com/kcp-dev/sdk/apis/tenancy/initialization"
3531
kcptenancyv1alpha1 "github.com/kcp-dev/sdk/apis/tenancy/v1alpha1"
3632

3733
rbacv1 "k8s.io/api/rbac/v1"
3834
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3935
"k8s.io/apimachinery/pkg/types"
40-
"k8s.io/apimachinery/pkg/util/wait"
4136
ctrlruntime "sigs.k8s.io/controller-runtime"
4237
)
4338

@@ -75,8 +70,6 @@ func TestWaitForReadyAnnotation(t *testing.T) {
7570
t.Fatalf("Failed to create WorkspaceType: %v", err)
7671
}
7772

78-
initializer := kcptenancyinitialization.InitializerForType(wst)
79-
8073
utils.GrantWorkspaceAccess(t, ctx, wstClient, utils.Subject(), rbacv1.PolicyRule{
8174
APIGroups: []string{"tenancy.kcp.io"},
8275
Resources: []string{"workspacetypes"},
@@ -195,18 +188,7 @@ spec:
195188
}
196189

197190
// Wait for the agent to do its work and initialize the cluster.
198-
// The initializer should only be removed AFTER the CRD's Established condition is True.
199-
err := wait.PollUntilContextTimeout(ctx, 500*time.Millisecond, 60*time.Second, false, func(ctx context.Context) (done bool, err error) {
200-
err = rootClient.Get(ctx, types.NamespacedName{Name: targetWorkspace}, targetWs)
201-
if err != nil {
202-
return false, err
203-
}
204-
205-
return !slices.Contains(targetWs.Status.Initializers, initializer), nil
206-
})
207-
if err != nil {
208-
t.Fatalf("Failed to wait for workspace to be initialized: %v", err)
209-
}
191+
targetWs = utils.WaitForWorkspaceInitialization(t, ctx, kcpClusterClient, rootCluster, targetWorkspace)
210192

211193
// Verify the CRD exists in the target workspace and is Established
212194
targetClient := kcpClusterClient.Cluster(rootCluster.Join(targetWorkspace))

test/utils/fixtures.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,16 @@ import (
2626
"time"
2727

2828
"github.com/kcp-dev/logicalcluster/v3"
29+
mcclient "github.com/kcp-dev/multicluster-provider/client"
2930
kcpcorev1alpha1 "github.com/kcp-dev/sdk/apis/core/v1alpha1"
3031
kcptenancyv1alpha1 "github.com/kcp-dev/sdk/apis/tenancy/v1alpha1"
3132

33+
corev1 "k8s.io/api/core/v1"
3234
rbacv1 "k8s.io/api/rbac/v1"
3335
"k8s.io/apiextensions-apiserver/pkg/apihelpers"
3436
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
3537
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
38+
"k8s.io/apimachinery/pkg/types"
3639
"k8s.io/apimachinery/pkg/util/wait"
3740
"k8s.io/apimachinery/pkg/util/yaml"
3841
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
@@ -76,6 +79,40 @@ func CreateAndWaitForWorkspace(t *testing.T, ctx context.Context, client ctrlrun
7679
return logicalcluster.Name(testWs.Spec.Cluster)
7780
}
7881

82+
func WaitForWorkspaceInitialization(t *testing.T, ctx context.Context, clusterClient mcclient.ClusterClient, parentWorkspace logicalcluster.Path, workspaceName string) *kcptenancyv1alpha1.Workspace {
83+
t.Helper()
84+
85+
parentClient := clusterClient.Cluster(parentWorkspace)
86+
87+
// wait for the agent to do its work and initialize the cluster and ultimately remove the initializer
88+
ws := &kcptenancyv1alpha1.Workspace{}
89+
90+
err := wait.PollUntilContextTimeout(ctx, 500*time.Millisecond, 30*time.Second, false, func(ctx context.Context) (done bool, err error) {
91+
err = parentClient.Get(ctx, types.NamespacedName{Name: workspaceName}, ws)
92+
if err != nil {
93+
return false, err
94+
}
95+
96+
return len(ws.Status.Initializers) == 0, nil
97+
})
98+
if err != nil {
99+
t.Fatalf("Failed to wait for workspace to be initialized: %v", err)
100+
}
101+
102+
// connect into the new workspace and verify it's truly ready (there is a brief delay between
103+
// all initializes being gone and the cluster actually being usable)
104+
targetClient := clusterClient.Cluster(parentWorkspace.Join(workspaceName))
105+
106+
err = wait.PollUntilContextTimeout(ctx, 500*time.Millisecond, 30*time.Second, false, func(ctx context.Context) (done bool, err error) {
107+
return targetClient.List(ctx, &corev1.NamespaceList{}) == nil, nil
108+
})
109+
if err != nil {
110+
t.Fatalf("Failed to wait for workspace to be usable: %v", err)
111+
}
112+
113+
return ws
114+
}
115+
79116
func GrantWorkspaceAccess(t *testing.T, ctx context.Context, client ctrlruntimeclient.Client, rbacSubject rbacv1.Subject, extraRules ...rbacv1.PolicyRule) {
80117
t.Helper()
81118

0 commit comments

Comments
 (0)