Skip to content

Commit 11acf67

Browse files
committed
fix status handling for RootShards/FrontProxies, fix scheme
On-behalf-of: @SAP [email protected]
1 parent 6d57bf8 commit 11acf67

File tree

8 files changed

+75
-44
lines changed

8 files changed

+75
-44
lines changed

internal/controller/cacheserver/controller_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,14 @@ import (
2020
"context"
2121
"testing"
2222

23-
certmanagerv1 "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
2423
"github.com/stretchr/testify/require"
2524

2625
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27-
"k8s.io/apimachinery/pkg/runtime"
2826
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
2927
ctrlruntimefakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake"
3028
"sigs.k8s.io/controller-runtime/pkg/reconcile"
3129

30+
"github.com/kcp-dev/kcp-operator/internal/controller/util"
3231
operatorv1alpha1 "github.com/kcp-dev/kcp-operator/sdk/apis/operator/v1alpha1"
3332
)
3433

@@ -55,15 +54,14 @@ func TestReconciling(t *testing.T) {
5554
},
5655
}
5756

58-
scheme := runtime.NewScheme()
59-
require.Nil(t, operatorv1alpha1.AddToScheme(scheme))
60-
require.Nil(t, certmanagerv1.AddToScheme(scheme))
57+
scheme := util.GetTestScheme()
6158

6259
for _, testcase := range testcases {
6360
t.Run(testcase.name, func(t *testing.T) {
6461
client := ctrlruntimefakeclient.
6562
NewClientBuilder().
6663
WithScheme(scheme).
64+
WithStatusSubresource(testcase.cacheServer).
6765
WithObjects(testcase.cacheServer).
6866
Build()
6967

@@ -77,7 +75,7 @@ func TestReconciling(t *testing.T) {
7775
_, err := controllerReconciler.Reconcile(ctx, reconcile.Request{
7876
NamespacedName: ctrlruntimeclient.ObjectKeyFromObject(testcase.cacheServer),
7977
})
80-
require.Nil(t, err)
78+
require.NoError(t, err)
8179
})
8280
}
8381
}

internal/controller/frontproxy/controller.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -191,16 +191,15 @@ func (r *FrontProxyReconciler) reconcileStatus(ctx context.Context, oldFrontProx
191191
frontProxy.Status.Conditions = util.UpdateCondition(frontProxy.Status.Conditions, condition)
192192
}
193193

194-
availableCond := apimeta.FindStatusCondition(frontProxy.Status.Conditions, string(operatorv1alpha1.ConditionTypeAvailable))
195-
switch {
196-
case availableCond.Status == metav1.ConditionTrue:
197-
frontProxy.Status.Phase = operatorv1alpha1.FrontProxyPhaseRunning
198-
199-
case frontProxy.DeletionTimestamp != nil:
194+
if frontProxy.DeletionTimestamp != nil {
200195
frontProxy.Status.Phase = operatorv1alpha1.FrontProxyPhaseDeleting
201-
202-
case frontProxy.Status.Phase == "":
203-
frontProxy.Status.Phase = operatorv1alpha1.FrontProxyPhaseProvisioning
196+
} else {
197+
availableCond := apimeta.FindStatusCondition(frontProxy.Status.Conditions, string(operatorv1alpha1.ConditionTypeAvailable))
198+
if availableCond != nil && availableCond.Status == metav1.ConditionTrue {
199+
frontProxy.Status.Phase = operatorv1alpha1.FrontProxyPhaseRunning
200+
} else {
201+
frontProxy.Status.Phase = operatorv1alpha1.FrontProxyPhaseProvisioning
202+
}
204203
}
205204

206205
// only patch the status if there are actual changes.

internal/controller/frontproxy/controller_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,15 @@ import (
2020
"context"
2121
"testing"
2222

23-
certmanagerv1 "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
2423
"github.com/stretchr/testify/require"
2524

2625
corev1 "k8s.io/api/core/v1"
2726
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28-
"k8s.io/apimachinery/pkg/runtime"
2927
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
3028
ctrlruntimefakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake"
3129
"sigs.k8s.io/controller-runtime/pkg/reconcile"
3230

31+
"github.com/kcp-dev/kcp-operator/internal/controller/util"
3332
operatorv1alpha1 "github.com/kcp-dev/kcp-operator/sdk/apis/operator/v1alpha1"
3433
)
3534

@@ -76,15 +75,14 @@ func TestReconciling(t *testing.T) {
7675
},
7776
}
7877

79-
scheme := runtime.NewScheme()
80-
require.Nil(t, operatorv1alpha1.AddToScheme(scheme))
81-
require.Nil(t, certmanagerv1.AddToScheme(scheme))
78+
scheme := util.GetTestScheme()
8279

8380
for _, testcase := range testcases {
8481
t.Run(testcase.name, func(t *testing.T) {
8582
client := ctrlruntimefakeclient.
8683
NewClientBuilder().
8784
WithScheme(scheme).
85+
WithStatusSubresource(testcase.rootShard, testcase.frontProxy).
8886
WithObjects(testcase.rootShard, testcase.frontProxy).
8987
Build()
9088

@@ -98,7 +96,7 @@ func TestReconciling(t *testing.T) {
9896
_, err := controllerReconciler.Reconcile(ctx, reconcile.Request{
9997
NamespacedName: ctrlruntimeclient.ObjectKeyFromObject(testcase.frontProxy),
10098
})
101-
require.Nil(t, err)
99+
require.NoError(t, err)
102100
})
103101
}
104102
}

internal/controller/kubeconfig/controller_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,15 @@ import (
2121
"testing"
2222
"time"
2323

24-
certmanagerv1 "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
2524
"github.com/stretchr/testify/require"
2625

2726
corev1 "k8s.io/api/core/v1"
2827
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29-
"k8s.io/apimachinery/pkg/runtime"
3028
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
3129
ctrlruntimefakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake"
3230
"sigs.k8s.io/controller-runtime/pkg/reconcile"
3331

32+
"github.com/kcp-dev/kcp-operator/internal/controller/util"
3433
operatorv1alpha1 "github.com/kcp-dev/kcp-operator/sdk/apis/operator/v1alpha1"
3534
)
3635

@@ -81,15 +80,14 @@ func TestReconciling(t *testing.T) {
8180
},
8281
}
8382

84-
scheme := runtime.NewScheme()
85-
require.Nil(t, operatorv1alpha1.AddToScheme(scheme))
86-
require.Nil(t, certmanagerv1.AddToScheme(scheme))
83+
scheme := util.GetTestScheme()
8784

8885
for _, testcase := range testcases {
8986
t.Run(testcase.name, func(t *testing.T) {
9087
client := ctrlruntimefakeclient.
9188
NewClientBuilder().
9289
WithScheme(scheme).
90+
WithStatusSubresource(testcase.rootShard).
9391
WithObjects(testcase.rootShard, testcase.kubeConfig).
9492
Build()
9593

@@ -103,7 +101,7 @@ func TestReconciling(t *testing.T) {
103101
_, err := controllerReconciler.Reconcile(ctx, reconcile.Request{
104102
NamespacedName: ctrlruntimeclient.ObjectKeyFromObject(testcase.kubeConfig),
105103
})
106-
require.Nil(t, err)
104+
require.NoError(t, err)
107105
})
108106
}
109107
}

internal/controller/rootshard/controller.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -206,16 +206,15 @@ func (r *RootShardReconciler) reconcileStatus(ctx context.Context, oldRootShard
206206
rootShard.Status.Conditions = util.UpdateCondition(rootShard.Status.Conditions, condition)
207207
}
208208

209-
availableCond := apimeta.FindStatusCondition(rootShard.Status.Conditions, string(operatorv1alpha1.ConditionTypeAvailable))
210-
switch {
211-
case availableCond.Status == metav1.ConditionTrue:
212-
rootShard.Status.Phase = operatorv1alpha1.RootShardPhaseRunning
213-
214-
case rootShard.DeletionTimestamp != nil:
209+
if rootShard.DeletionTimestamp != nil {
215210
rootShard.Status.Phase = operatorv1alpha1.RootShardPhaseDeleting
216-
217-
case rootShard.Status.Phase == "":
218-
rootShard.Status.Phase = operatorv1alpha1.RootShardPhaseProvisioning
211+
} else {
212+
availableCond := apimeta.FindStatusCondition(rootShard.Status.Conditions, string(operatorv1alpha1.ConditionTypeAvailable))
213+
if availableCond != nil && availableCond.Status == metav1.ConditionTrue {
214+
rootShard.Status.Phase = operatorv1alpha1.RootShardPhaseRunning
215+
} else {
216+
rootShard.Status.Phase = operatorv1alpha1.RootShardPhaseProvisioning
217+
}
219218
}
220219

221220
shards, err := util.GetRootShardChildren(ctx, r.Client, rootShard)

internal/controller/rootshard/controller_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,14 @@ import (
2020
"context"
2121
"testing"
2222

23-
certmanagerv1 "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
2423
"github.com/stretchr/testify/require"
2524

2625
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27-
"k8s.io/apimachinery/pkg/runtime"
2826
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
2927
ctrlruntimefakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake"
3028
"sigs.k8s.io/controller-runtime/pkg/reconcile"
3129

30+
"github.com/kcp-dev/kcp-operator/internal/controller/util"
3231
operatorv1alpha1 "github.com/kcp-dev/kcp-operator/sdk/apis/operator/v1alpha1"
3332
)
3433

@@ -61,15 +60,14 @@ func TestReconciling(t *testing.T) {
6160
},
6261
}
6362

64-
scheme := runtime.NewScheme()
65-
require.Nil(t, operatorv1alpha1.AddToScheme(scheme))
66-
require.Nil(t, certmanagerv1.AddToScheme(scheme))
63+
scheme := util.GetTestScheme()
6764

6865
for _, testcase := range testcases {
6966
t.Run(testcase.name, func(t *testing.T) {
7067
client := ctrlruntimefakeclient.
7168
NewClientBuilder().
7269
WithScheme(scheme).
70+
WithStatusSubresource(testcase.rootShard).
7371
WithObjects(testcase.rootShard).
7472
Build()
7573

@@ -83,7 +81,7 @@ func TestReconciling(t *testing.T) {
8381
_, err := controllerReconciler.Reconcile(ctx, reconcile.Request{
8482
NamespacedName: ctrlruntimeclient.ObjectKeyFromObject(testcase.rootShard),
8583
})
86-
require.Nil(t, err)
84+
require.NoError(t, err)
8785
})
8886
}
8987
}

internal/controller/shard/controller_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ func TestReconciling(t *testing.T) {
9090
client := ctrlruntimefakeclient.
9191
NewClientBuilder().
9292
WithScheme(scheme).
93-
WithObjects(testcase.rootShard).
93+
WithStatusSubresource(testcase.rootShard, testcase.shard).
94+
WithObjects(testcase.rootShard, testcase.shard).
9495
Build()
9596

9697
ctx := context.Background()
@@ -103,7 +104,7 @@ func TestReconciling(t *testing.T) {
103104
_, err := controllerReconciler.Reconcile(ctx, reconcile.Request{
104105
NamespacedName: ctrlruntimeclient.ObjectKeyFromObject(testcase.rootShard),
105106
})
106-
require.Nil(t, err)
107+
require.NoError(t, err)
107108
})
108109
}
109110
}

internal/controller/util/test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
Copyright 2024 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 util
18+
19+
import (
20+
certmanagerv1 "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
21+
22+
appsv1 "k8s.io/api/apps/v1"
23+
corev1 "k8s.io/api/core/v1"
24+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25+
"k8s.io/apimachinery/pkg/runtime"
26+
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
27+
28+
operatorv1alpha1 "github.com/kcp-dev/kcp-operator/sdk/apis/operator/v1alpha1"
29+
)
30+
31+
func GetTestScheme() *runtime.Scheme {
32+
scheme := runtime.NewScheme()
33+
utilruntime.Must(metav1.AddMetaToScheme(scheme))
34+
utilruntime.Must(corev1.AddToScheme(scheme))
35+
utilruntime.Must(operatorv1alpha1.AddToScheme(scheme))
36+
utilruntime.Must(certmanagerv1.AddToScheme(scheme))
37+
utilruntime.Must(appsv1.AddToScheme(scheme))
38+
39+
return scheme
40+
}

0 commit comments

Comments
 (0)