Skip to content

Commit

Permalink
merge controllers + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mjudeikis committed Oct 19, 2024
1 parent 73bc028 commit 08b37fe
Show file tree
Hide file tree
Showing 9 changed files with 261 additions and 340 deletions.
9 changes: 9 additions & 0 deletions pkg/reconciler/tenancy/workspace/README.md
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.
22 changes: 22 additions & 0 deletions pkg/reconciler/tenancy/workspace/workspace_reconcile_phase.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/kcp-dev/logicalcluster/v3"

v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/klog/v2"

Expand Down Expand Up @@ -74,7 +75,13 @@ func (r *phaseReconciler) reconcile(ctx context.Context, workspace *tenancyv1alp
workspace.Status.Phase = corev1alpha1.LogicalClusterPhaseReady
conditions.MarkTrue(workspace, tenancyv1alpha1.WorkspaceInitialized)

case corev1alpha1.LogicalClusterPhaseNotReady:
checkConditionAndSetPhase(workspace)
return reconcileStatusContinue, nil

case corev1alpha1.LogicalClusterPhaseReady:
// On delete we need to wait for the logical cluster to be deleted
// before we can mark the workspace as deleted.
if !workspace.DeletionTimestamp.IsZero() {
logger = logger.WithValues("cluster", workspace.Spec.Cluster)

Expand Down Expand Up @@ -106,7 +113,22 @@ func (r *phaseReconciler) reconcile(ctx context.Context, workspace *tenancyv1alp
logger.Info("workspace content is deleted")
return reconcileStatusContinue, nil
}
// if workspace is ready, we check if it suppose to be ready by checking conditions.
checkConditionAndSetPhase(workspace)
return reconcileStatusContinue, nil
}

return reconcileStatusContinue, nil
}

func checkConditionAndSetPhase(workspace *tenancyv1alpha1.Workspace) {
// if workspace is ready, we check if it suppose to be ready by checking conditions.
// Conditions can be added by other reconcilers, so we need to check them.
for _, c := range workspace.Status.Conditions {
if c.Status != v1.ConditionTrue {
workspace.Status.Phase = corev1alpha1.LogicalClusterPhaseNotReady
return
}
}
workspace.Status.Phase = corev1alpha1.LogicalClusterPhaseReady
}
230 changes: 230 additions & 0 deletions pkg/reconciler/tenancy/workspace/workspace_reconcile_phase_test.go
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)
}
}
})
}
}
6 changes: 0 additions & 6 deletions pkg/reconciler/tenancy/workspacestatuses/README.md

This file was deleted.

Loading

0 comments on commit 08b37fe

Please sign in to comment.