@@ -22,7 +22,11 @@ import (
2222
2323 k8creconciling "k8c.io/reconciler/pkg/reconciling"
2424
25+ appsv1 "k8s.io/api/apps/v1"
26+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2527 "k8s.io/apimachinery/pkg/runtime"
28+ "k8s.io/apimachinery/pkg/types"
29+ "k8s.io/utils/ptr"
2630 ctrl "sigs.k8s.io/controller-runtime"
2731 "sigs.k8s.io/controller-runtime/pkg/client"
2832 "sigs.k8s.io/controller-runtime/pkg/log"
@@ -65,6 +69,22 @@ func (r *RootShardReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
6569 return ctrl.Result {}, fmt .Errorf ("failed to find %s/%s: %w" , req .Namespace , req .Name , err )
6670 }
6771
72+ if rootShard .DeletionTimestamp != nil {
73+ rootShard .Status .Phase = operatorkcpiov1alpha1 .RootShardPhaseDeleting
74+ if err := r .Client .Status ().Update (ctx , & rootShard ); err != nil {
75+ return ctrl.Result {}, err
76+ }
77+ }
78+
79+ if rootShard .Status .Phase == "" {
80+ rootShard .Status .Phase = operatorkcpiov1alpha1 .RootShardPhaseProvisioning
81+ if err := r .Client .Status ().Update (ctx , & rootShard ); err != nil {
82+ return ctrl.Result {}, err
83+ }
84+ }
85+
86+ ownerRefWrapper := k8creconciling .OwnerRefWrapper (* metav1 .NewControllerRef (& rootShard , operatorkcpiov1alpha1 .GroupVersion .WithKind (rootShard .Kind )))
87+
6888 // Intermediate CAs that we need to generate a certificate and an issuer for.
6989 intermediateCAs := []v1alpha1.CA {
7090 v1alpha1 .ServerCA ,
@@ -91,32 +111,48 @@ func (r *RootShardReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
91111 certReconcilers = append (certReconcilers , rootshard .RootCACertificateReconciler (& rootShard ))
92112 }
93113
94- if err := reconciling .ReconcileCertificates (ctx , certReconcilers , req .Namespace , r .Client ); err != nil {
114+ if err := reconciling .ReconcileCertificates (ctx , certReconcilers , req .Namespace , r .Client , ownerRefWrapper ); err != nil {
95115 return ctrl.Result {}, err
96116 }
97117
98- if err := reconciling .ReconcileIssuers (ctx , issuerReconcilers , req .Namespace , r .Client ); err != nil {
118+ if err := reconciling .ReconcileIssuers (ctx , issuerReconcilers , req .Namespace , r .Client , ownerRefWrapper ); err != nil {
99119 return ctrl.Result {}, err
100120 }
101121
102122 if err := k8creconciling .ReconcileDeployments (ctx , []k8creconciling.NamedDeploymentReconcilerFactory {
103123 rootshard .DeploymentReconciler (& rootShard ),
104- }, req .Namespace , r .Client ); err != nil {
124+ }, req .Namespace , r .Client , ownerRefWrapper ); err != nil {
105125 return ctrl.Result {}, err
106126 }
107127
108128 if err := k8creconciling .ReconcileServices (ctx , []k8creconciling.NamedServiceReconcilerFactory {
109129 rootshard .ServiceReconciler (& rootShard ),
110- }, req .Namespace , r .Client ); err != nil {
130+ }, req .Namespace , r .Client , ownerRefWrapper ); err != nil {
111131 return ctrl.Result {}, err
112132 }
113133
134+ // check for Deployment health and update the rootShard phase if necessary.
135+ var dep appsv1.Deployment
136+ err := r .Client .Get (ctx , types.NamespacedName {Namespace : req .Namespace , Name : fmt .Sprintf ("%s-kcp" , rootShard .Name )}, & dep )
137+ if client .IgnoreNotFound (err ) != nil {
138+ return ctrl.Result {}, err
139+ }
140+ if err == nil {
141+ if rootShard .Status .Phase == operatorkcpiov1alpha1 .RootShardPhaseProvisioning && dep .Status .ReadyReplicas == ptr .Deref (dep .Spec .Replicas , 0 ) {
142+ rootShard .Status .Phase = operatorkcpiov1alpha1 .RootShardPhaseRunning
143+ if err := r .Client .Status ().Update (ctx , & rootShard ); err != nil {
144+ return ctrl.Result {}, err
145+ }
146+ }
147+ }
148+
114149 return ctrl.Result {}, nil
115150}
116151
117152// SetupWithManager sets up the controller with the Manager.
118153func (r * RootShardReconciler ) SetupWithManager (mgr ctrl.Manager ) error {
119154 return ctrl .NewControllerManagedBy (mgr ).
120155 For (& operatorkcpiov1alpha1.RootShard {}).
156+ Owns (& appsv1.Deployment {}).
121157 Complete (r )
122158}
0 commit comments