@@ -19,6 +19,7 @@ package controller
19
19
import (
20
20
"context"
21
21
"fmt"
22
+ "sort"
22
23
23
24
certmanagerv1 "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
24
25
k8creconciling "k8c.io/reconciler/pkg/reconciling"
@@ -31,9 +32,12 @@ import (
31
32
"k8s.io/apimachinery/pkg/runtime"
32
33
"k8s.io/apimachinery/pkg/types"
33
34
kerrors "k8s.io/apimachinery/pkg/util/errors"
35
+ utilruntime "k8s.io/apimachinery/pkg/util/runtime"
34
36
ctrl "sigs.k8s.io/controller-runtime"
35
37
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
38
+ "sigs.k8s.io/controller-runtime/pkg/handler"
36
39
"sigs.k8s.io/controller-runtime/pkg/log"
40
+ "sigs.k8s.io/controller-runtime/pkg/reconcile"
37
41
38
42
"github.com/kcp-dev/kcp-operator/internal/reconciling"
39
43
"github.com/kcp-dev/kcp-operator/internal/resources"
@@ -49,13 +53,29 @@ type RootShardReconciler struct {
49
53
50
54
// SetupWithManager sets up the controller with the Manager.
51
55
func (r * RootShardReconciler ) SetupWithManager (mgr ctrl.Manager ) error {
56
+ shardHandler := handler .TypedEnqueueRequestsFromMapFunc (func (ctx context.Context , obj ctrlruntimeclient.Object ) []reconcile.Request {
57
+ shard := obj .(* operatorv1alpha1.Shard )
58
+
59
+ var rootShard operatorv1alpha1.RootShard
60
+ if err := mgr .GetClient ().Get (ctx , ctrlruntimeclient.ObjectKey {Namespace : shard .Namespace , Name : shard .Spec .RootShard .Reference .Name }, & rootShard ); err != nil {
61
+ utilruntime .HandleError (err )
62
+ return nil
63
+ }
64
+
65
+ var requests []reconcile.Request
66
+ requests = append (requests , reconcile.Request {NamespacedName : ctrlruntimeclient .ObjectKeyFromObject (& rootShard )})
67
+
68
+ return requests
69
+ })
70
+
52
71
return ctrl .NewControllerManagedBy (mgr ).
53
72
For (& operatorv1alpha1.RootShard {}).
54
73
Owns (& appsv1.Deployment {}).
55
74
Owns (& corev1.ConfigMap {}).
56
75
Owns (& corev1.Service {}).
57
76
Owns (& corev1.Secret {}).
58
77
Owns (& certmanagerv1.Certificate {}).
78
+ Watches (& operatorv1alpha1.Shard {}, shardHandler ).
59
79
Complete (r )
60
80
}
61
81
@@ -197,6 +217,20 @@ func (r *RootShardReconciler) reconcileStatus(ctx context.Context, oldRootShard
197
217
rootShard .Status .Phase = operatorv1alpha1 .RootShardPhaseProvisioning
198
218
}
199
219
220
+ shards , err := getRootShardChildren (ctx , r .Client , rootShard )
221
+ if err != nil {
222
+ errs = append (errs , err )
223
+ } else {
224
+ rootShard .Status .Shards = make ([]operatorv1alpha1.ShardReference , len (shards ))
225
+ for i , shard := range shards {
226
+ rootShard .Status .Shards [i ] = operatorv1alpha1.ShardReference {Name : shard .Name }
227
+ }
228
+ }
229
+ // sort the shards by name for equality comparison
230
+ sort .Slice (rootShard .Status .Shards , func (i , j int ) bool {
231
+ return rootShard .Status .Shards [i ].Name < rootShard .Status .Shards [j ].Name
232
+ })
233
+
200
234
// only patch the status if there are actual changes.
201
235
if ! equality .Semantic .DeepEqual (oldRootShard .Status , rootShard .Status ) {
202
236
if err := r .Status ().Patch (ctx , rootShard , ctrlruntimeclient .MergeFrom (oldRootShard )); err != nil {
0 commit comments