@@ -12,6 +12,7 @@ package mlops
1212import (
1313 "context"
1414 "fmt"
15+ "time"
1516
1617 "github.com/go-logr/logr"
1718 appsv1 "k8s.io/api/apps/v1"
@@ -45,10 +46,12 @@ type SeldonRuntimeReconciler struct {
4546 Recorder record.EventRecorder
4647}
4748
48- func (r * SeldonRuntimeReconciler ) getNumberOfServers (namespace string ) (int , error ) {
49+ func (r * SeldonRuntimeReconciler ) getNumberOfServers (ctx context. Context , namespace string ) (int , error ) {
4950 servers := mlopsv1alpha1.ServerList {}
51+ ctx , cancel := context .WithTimeout (ctx , constants .K8sAPISingleCallTimeout )
52+ defer cancel ()
5053 inNamespace := client .InNamespace (namespace )
51- err := r .List (context . TODO () , & servers , inNamespace )
54+ err := r .List (ctx , & servers , inNamespace )
5255 if err != nil {
5356 if errors .IsNotFound (err ) {
5457 return 0 , nil
@@ -69,7 +72,7 @@ func (r *SeldonRuntimeReconciler) handleFinalizer(ctx context.Context, logger lo
6972 }
7073 }
7174 } else { // runtime is being deleted
72- numServers , err := r .getNumberOfServers (runtime .Namespace )
75+ numServers , err := r .getNumberOfServers (ctx , runtime .Namespace )
7376 logger .Info ("Runtime being deleted" , "namespace" , runtime .Namespace , "numServers" , numServers )
7477 if err != nil {
7578 return true , err
@@ -119,10 +122,15 @@ func (r *SeldonRuntimeReconciler) handleFinalizer(ctx context.Context, logger lo
119122// For more details, check Reconcile and its Result here:
120123// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.17.4/pkg/reconcile
121124func (r * SeldonRuntimeReconciler ) Reconcile (ctx context.Context , req ctrl.Request ) (ctrl.Result , error ) {
122- logger := log .FromContext (ctx ).WithName ("Reconcile " )
125+ logger := log .FromContext (ctx ).WithName ("SeldonRuntimeReconcile " )
123126 ctx , cancel := context .WithTimeout (ctx , constants .ReconcileTimeout )
124127 defer cancel ()
125128
129+ now := time .Now ()
130+ defer func () {
131+ logger .Info ("Finished SeldonRuntime Reconcile" , "duration" , time .Since (now ))
132+ }()
133+
126134 seldonRuntime := & mlopsv1alpha1.SeldonRuntime {}
127135 if err := r .Get (ctx , req .NamespacedName , seldonRuntime ); err != nil {
128136 if errors .IsNotFound (err ) {
@@ -141,9 +149,9 @@ func (r *SeldonRuntimeReconciler) Reconcile(ctx context.Context, req ctrl.Reques
141149 }
142150
143151 sr , err := seldonreconcile .NewSeldonRuntimeReconciler (
152+ ctx ,
144153 seldonRuntime ,
145154 common.ReconcilerConfig {
146- Ctx : ctx ,
147155 Logger : logger ,
148156 Client : r .Client ,
149157 Recorder : r .Recorder ,
@@ -160,7 +168,7 @@ func (r *SeldonRuntimeReconciler) Reconcile(ctx context.Context, req ctrl.Reques
160168 return reconcile.Result {}, err
161169 }
162170
163- err = sr .Reconcile ()
171+ err = sr .Reconcile (ctx )
164172 if err != nil {
165173 return reconcile.Result {}, err
166174 }
@@ -187,42 +195,47 @@ func seldonRuntimeReady(status mlopsv1alpha1.SeldonRuntimeStatus) bool {
187195func (r * SeldonRuntimeReconciler ) updateStatus (seldonRuntime * mlopsv1alpha1.SeldonRuntime , logger logr.Logger ) error {
188196 existingRuntime := & mlopsv1alpha1.SeldonRuntime {}
189197 namespacedName := types.NamespacedName {Name : seldonRuntime .Name , Namespace : seldonRuntime .Namespace }
190- if err := r .Get (context .TODO (), namespacedName , existingRuntime ); err != nil {
198+
199+ ctx , cancel := context .WithTimeout (context .Background (), constants .K8sAPICallsTxTimeout )
200+ defer cancel ()
201+ if err := r .Get (ctx , namespacedName , existingRuntime ); err != nil {
191202 if apimachinary_errors .IsNotFound (err ) { //Ignore NotFound errors
192203 return nil
193204 }
194205 return err
195206 }
196207
197208 if equality .Semantic .DeepEqual (existingRuntime .Status , seldonRuntime .Status ) {
209+ return nil
198210 // Not updating as no difference
211+ }
212+
213+ if err := r .Status ().Update (ctx , seldonRuntime ); err != nil {
214+ logger .Info ("Failed to update status" , "name" , seldonRuntime .Name , "namespace" , seldonRuntime .Namespace )
215+ r .Recorder .Eventf (seldonRuntime , v1 .EventTypeWarning , "UpdateFailed" ,
216+ "Failed to update status for SeldonRuntime %q: %v" , seldonRuntime .Name , err )
217+ return err
199218 } else {
200- if err := r .Status ().Update (context .TODO (), seldonRuntime ); err != nil {
201- logger .Info ("Failed to update status" , "name" , seldonRuntime .Name , "namespace" , seldonRuntime .Namespace )
202- r .Recorder .Eventf (seldonRuntime , v1 .EventTypeWarning , "UpdateFailed" ,
203- "Failed to update status for SeldonRuntime %q: %v" , seldonRuntime .Name , err )
204- return err
205- } else {
206- logger .Info ("Successfully updated status" , "name" , seldonRuntime .Name , "namespace" , seldonRuntime .Namespace )
207- prevWasReady := seldonRuntimeReady (existingRuntime .Status )
208- currentIsReady := seldonRuntimeReady (seldonRuntime .Status )
209- if prevWasReady && ! currentIsReady {
210- r .Recorder .Eventf (seldonRuntime , v1 .EventTypeWarning , "SeldonRuntimeNotReady" ,
211- fmt .Sprintf ("SeldonRuntime %v is no longer Ready" , seldonRuntime .GetName ()))
212- } else if ! prevWasReady && currentIsReady {
213- r .Recorder .Eventf (seldonRuntime , v1 .EventTypeNormal , "RuntimeReady" ,
214- fmt .Sprintf ("SeldonRuntime %v is Ready" , seldonRuntime .GetName ()))
215- }
219+ logger .Info ("Successfully updated status" , "name" , seldonRuntime .Name , "namespace" , seldonRuntime .Namespace )
220+ prevWasReady := seldonRuntimeReady (existingRuntime .Status )
221+ currentIsReady := seldonRuntimeReady (seldonRuntime .Status )
222+ if prevWasReady && ! currentIsReady {
223+ r .Recorder .Eventf (seldonRuntime , v1 .EventTypeWarning , "SeldonRuntimeNotReady" ,
224+ fmt .Sprintf ("SeldonRuntime %v is no longer Ready" , seldonRuntime .GetName ()))
225+ } else if ! prevWasReady && currentIsReady {
226+ r .Recorder .Eventf (seldonRuntime , v1 .EventTypeNormal , "RuntimeReady" ,
227+ fmt .Sprintf ("SeldonRuntime %v is Ready" , seldonRuntime .GetName ()))
216228 }
217229 }
230+
218231 return nil
219232}
220233
221234// Find SeldonRuntimes that reference the changes SeldonConfig
222- // TODO: pass an actual context from the caller to be used here
223- func (r * SeldonRuntimeReconciler ) mapSeldonRuntimesFromSeldonConfig (_ context.Context , obj client.Object ) []reconcile.Request {
224- ctx , cancel := context .WithTimeout (context .Background (), constants .K8sAPICallsTxTimeout )
235+ func (r * SeldonRuntimeReconciler ) mapSeldonRuntimesFromSeldonConfig (ctx context.Context , obj client.Object ) []reconcile.Request {
236+ ctx , cancel := context .WithTimeout (ctx , constants .K8sAPISingleCallTimeout )
225237 defer cancel ()
238+
226239 logger := log .FromContext (ctx ).WithName ("mapSeldonRuntimesFromSeldonConfig" )
227240 var seldonRuntimes mlopsv1alpha1.SeldonRuntimeList
228241 if err := r .Client .List (ctx , & seldonRuntimes ); err != nil {
@@ -263,9 +276,10 @@ func (r *SeldonRuntimeReconciler) mapSeldonRuntimesByNamespace(ctx context.Conte
263276 return req
264277}
265278
266- func (r * SeldonRuntimeReconciler ) mapSeldonRuntimesFromPipeline (_ context.Context , obj client.Object ) []reconcile.Request {
267- ctx , cancel := context .WithTimeout (context . Background () , constants .K8sAPICallsTxTimeout )
279+ func (r * SeldonRuntimeReconciler ) mapSeldonRuntimesFromPipeline (ctx context.Context , obj client.Object ) []reconcile.Request {
280+ ctx , cancel := context .WithTimeout (ctx , constants .K8sAPICallsTxTimeout )
268281 defer cancel ()
282+
269283 logger := log .FromContext (ctx ).WithName ("mapSeldonRuntimesFromPipeline" )
270284 pipeline , ok := obj .(* mlopsv1alpha1.Pipeline )
271285 if ! ok {
@@ -275,9 +289,10 @@ func (r *SeldonRuntimeReconciler) mapSeldonRuntimesFromPipeline(_ context.Contex
275289 return r .mapSeldonRuntimesByNamespace (ctx , pipeline .Namespace , logger )
276290}
277291
278- func (r * SeldonRuntimeReconciler ) mapSeldonRuntimesFromModel (_ context.Context , obj client.Object ) []reconcile.Request {
279- ctx , cancel := context .WithTimeout (context . Background () , constants .K8sAPICallsTxTimeout )
292+ func (r * SeldonRuntimeReconciler ) mapSeldonRuntimesFromModel (ctx context.Context , obj client.Object ) []reconcile.Request {
293+ ctx , cancel := context .WithTimeout (ctx , constants .K8sAPICallsTxTimeout )
280294 defer cancel ()
295+
281296 logger := log .FromContext (ctx ).WithName ("mapSeldonRuntimesFromModel" )
282297 model , ok := obj .(* mlopsv1alpha1.Model )
283298 if ! ok {
0 commit comments