-
Notifications
You must be signed in to change notification settings - Fork 383
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
261 additions
and
340 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Workspace Controller | ||
|
||
The workspace controller is root controller for the workspace. | ||
It is responsible for (in calling order): | ||
|
||
- `Metadata` - updates workspace metadata in annotations. | ||
- `Delete` the workspace if it is in the `Deleting` phase and LogicalCluster finalizer is removed. | ||
- `Scheduling` - picks the shard and schedules the workspace on the shard. Creates logical cluster for it. | ||
- `Phase` - updates workspace phase based on the conditions of the workspace and LogicalCluster. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
230 changes: 230 additions & 0 deletions
230
pkg/reconciler/tenancy/workspace/workspace_reconcile_phase_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,230 @@ | ||
/* | ||
Copyright 2024 The KCP Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package workspace | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
corev1 "k8s.io/api/core/v1" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
|
||
corev1alpha1 "github.com/kcp-dev/kcp/sdk/apis/core/v1alpha1" | ||
tenancyv1alpha1 "github.com/kcp-dev/kcp/sdk/apis/tenancy/v1alpha1" | ||
conditionsv1alpha1 "github.com/kcp-dev/kcp/sdk/apis/third_party/conditions/apis/conditions/v1alpha1" | ||
"github.com/kcp-dev/logicalcluster/v3" | ||
) | ||
|
||
func TestReconcilePhase(t *testing.T) { | ||
//date, err := time.Parse(time.RFC3339, "2006-01-02T15:04:05Z") | ||
//require.NoError(t, err) | ||
|
||
for _, testCase := range []struct { | ||
name string | ||
input *tenancyv1alpha1.Workspace | ||
getLogicalCluster func(ctx context.Context, cluster logicalcluster.Path) (*corev1alpha1.LogicalCluster, error) | ||
|
||
wantPhase corev1alpha1.LogicalClusterPhaseType | ||
wantStatus reconcileStatus | ||
wantCondition conditionsv1alpha1.Condition | ||
}{ | ||
{ | ||
name: "workspace is scheduling but not yet initialized", | ||
input: &tenancyv1alpha1.Workspace{ | ||
Status: tenancyv1alpha1.WorkspaceStatus{ | ||
Phase: corev1alpha1.LogicalClusterPhaseScheduling, | ||
}, | ||
}, | ||
wantPhase: corev1alpha1.LogicalClusterPhaseScheduling, | ||
wantStatus: reconcileStatusContinue, | ||
wantCondition: conditionsv1alpha1.Condition{ | ||
Type: tenancyv1alpha1.WorkspaceInitialized, | ||
Status: corev1.ConditionFalse, | ||
}, | ||
}, | ||
{ | ||
name: "workspace is scheduled and moved to initializing", | ||
input: &tenancyv1alpha1.Workspace{ | ||
Spec: tenancyv1alpha1.WorkspaceSpec{ | ||
URL: "http://example.com", | ||
Cluster: "cluster-1", | ||
}, | ||
Status: tenancyv1alpha1.WorkspaceStatus{ | ||
Phase: corev1alpha1.LogicalClusterPhaseScheduling, | ||
}, | ||
}, | ||
wantPhase: corev1alpha1.LogicalClusterPhaseInitializing, | ||
wantStatus: reconcileStatusContinue, | ||
}, | ||
{ | ||
name: "workspace is initializing and logical cluster is not found", | ||
input: &tenancyv1alpha1.Workspace{ | ||
Spec: tenancyv1alpha1.WorkspaceSpec{ | ||
URL: "http://example.com", | ||
Cluster: "cluster-1", | ||
}, | ||
Status: tenancyv1alpha1.WorkspaceStatus{ | ||
Phase: corev1alpha1.LogicalClusterPhaseInitializing, | ||
}, | ||
}, | ||
getLogicalCluster: func(ctx context.Context, cluster logicalcluster.Path) (*corev1alpha1.LogicalCluster, error) { | ||
return nil, apierrors.NewNotFound(corev1alpha1.Resource("logicalcluster"), "cluster-1") | ||
}, | ||
wantPhase: corev1alpha1.LogicalClusterPhaseInitializing, | ||
wantStatus: reconcileStatusContinue, | ||
}, | ||
{ | ||
name: "workspace is initializing and logicalCluster not yet initialized", | ||
input: &tenancyv1alpha1.Workspace{ | ||
Spec: tenancyv1alpha1.WorkspaceSpec{ | ||
URL: "http://example.com", | ||
Cluster: "cluster-1", | ||
}, | ||
Status: tenancyv1alpha1.WorkspaceStatus{ | ||
Phase: corev1alpha1.LogicalClusterPhaseInitializing, | ||
}, | ||
}, | ||
getLogicalCluster: func(ctx context.Context, cluster logicalcluster.Path) (*corev1alpha1.LogicalCluster, error) { | ||
return &corev1alpha1.LogicalCluster{ | ||
Status: corev1alpha1.LogicalClusterStatus{ | ||
Initializers: []corev1alpha1.LogicalClusterInitializer{ | ||
"initializer-1", | ||
}, | ||
}, | ||
}, nil | ||
}, | ||
wantPhase: corev1alpha1.LogicalClusterPhaseInitializing, | ||
wantStatus: reconcileStatusContinue, | ||
}, | ||
{ | ||
name: "workspace is initializing and logicalCluster initialized", | ||
input: &tenancyv1alpha1.Workspace{ | ||
Spec: tenancyv1alpha1.WorkspaceSpec{ | ||
URL: "http://example.com", | ||
Cluster: "cluster-1", | ||
}, | ||
Status: tenancyv1alpha1.WorkspaceStatus{ | ||
Phase: corev1alpha1.LogicalClusterPhaseInitializing, | ||
}, | ||
}, | ||
getLogicalCluster: func(ctx context.Context, cluster logicalcluster.Path) (*corev1alpha1.LogicalCluster, error) { | ||
return &corev1alpha1.LogicalCluster{ | ||
Status: corev1alpha1.LogicalClusterStatus{ | ||
Initializers: []corev1alpha1.LogicalClusterInitializer{}, | ||
}, | ||
}, nil | ||
}, | ||
wantPhase: corev1alpha1.LogicalClusterPhaseReady, | ||
wantStatus: reconcileStatusContinue, | ||
}, | ||
{ | ||
name: "workspace is ready - no-op", | ||
input: &tenancyv1alpha1.Workspace{ | ||
Spec: tenancyv1alpha1.WorkspaceSpec{ | ||
URL: "http://example.com", | ||
Cluster: "cluster-1", | ||
}, | ||
Status: tenancyv1alpha1.WorkspaceStatus{ | ||
Phase: corev1alpha1.LogicalClusterPhaseReady, | ||
}, | ||
}, | ||
getLogicalCluster: func(ctx context.Context, cluster logicalcluster.Path) (*corev1alpha1.LogicalCluster, error) { | ||
return &corev1alpha1.LogicalCluster{ | ||
Status: corev1alpha1.LogicalClusterStatus{ | ||
Initializers: []corev1alpha1.LogicalClusterInitializer{}, | ||
}, | ||
}, nil | ||
}, | ||
wantPhase: corev1alpha1.LogicalClusterPhaseReady, | ||
wantStatus: reconcileStatusContinue, | ||
}, | ||
{ | ||
name: "workspace is ready - but condition is not ready", | ||
input: &tenancyv1alpha1.Workspace{ | ||
Spec: tenancyv1alpha1.WorkspaceSpec{ | ||
URL: "http://example.com", | ||
Cluster: "cluster-1", | ||
}, | ||
Status: tenancyv1alpha1.WorkspaceStatus{ | ||
Phase: corev1alpha1.LogicalClusterPhaseReady, | ||
Conditions: []conditionsv1alpha1.Condition{ | ||
{ | ||
Type: tenancyv1alpha1.MountConditionReady, | ||
Status: corev1.ConditionFalse, | ||
}, | ||
}, | ||
}, | ||
}, | ||
getLogicalCluster: func(ctx context.Context, cluster logicalcluster.Path) (*corev1alpha1.LogicalCluster, error) { | ||
return &corev1alpha1.LogicalCluster{ | ||
Status: corev1alpha1.LogicalClusterStatus{ | ||
Initializers: []corev1alpha1.LogicalClusterInitializer{}, | ||
}, | ||
}, nil | ||
}, | ||
wantPhase: corev1alpha1.LogicalClusterPhaseNotReady, | ||
wantStatus: reconcileStatusContinue, | ||
}, | ||
{ | ||
name: "workspace is no ready - but condition is ready", | ||
input: &tenancyv1alpha1.Workspace{ | ||
Spec: tenancyv1alpha1.WorkspaceSpec{ | ||
URL: "http://example.com", | ||
Cluster: "cluster-1", | ||
}, | ||
Status: tenancyv1alpha1.WorkspaceStatus{ | ||
Phase: corev1alpha1.LogicalClusterPhaseNotReady, | ||
Conditions: []conditionsv1alpha1.Condition{ | ||
{ | ||
Type: tenancyv1alpha1.MountConditionReady, | ||
Status: corev1.ConditionTrue, | ||
}, | ||
}, | ||
}, | ||
}, | ||
getLogicalCluster: func(ctx context.Context, cluster logicalcluster.Path) (*corev1alpha1.LogicalCluster, error) { | ||
return &corev1alpha1.LogicalCluster{ | ||
Status: corev1alpha1.LogicalClusterStatus{ | ||
Initializers: []corev1alpha1.LogicalClusterInitializer{}, | ||
}, | ||
}, nil | ||
}, | ||
wantPhase: corev1alpha1.LogicalClusterPhaseReady, | ||
wantStatus: reconcileStatusContinue, | ||
}, | ||
} { | ||
t.Run(testCase.name, func(t *testing.T) { | ||
reconciler := phaseReconciler{ | ||
getLogicalCluster: testCase.getLogicalCluster, | ||
requeueAfter: func(workspace *tenancyv1alpha1.Workspace, after time.Duration) {}, | ||
} | ||
status, err := reconciler.reconcile(context.Background(), testCase.input) | ||
|
||
require.NoError(t, err) | ||
require.Equal(t, testCase.wantStatus, status) | ||
require.Equal(t, testCase.wantPhase, testCase.input.Status.Phase) | ||
|
||
for _, condition := range testCase.input.Status.Conditions { | ||
if condition.Type == testCase.wantCondition.Type { | ||
require.Equal(t, testCase.wantCondition, condition) | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.