@@ -17,7 +17,6 @@ limitations under the License.
1717package main
1818
1919import (
20- "context"
2120 "math/rand"
2221 "os"
2322 "time"
@@ -29,6 +28,7 @@ import (
2928 ctrl "sigs.k8s.io/controller-runtime"
3029 api "sigs.k8s.io/controller-runtime/examples/crd/pkg"
3130 "sigs.k8s.io/controller-runtime/pkg/client"
31+ "sigs.k8s.io/controller-runtime/pkg/context"
3232 "sigs.k8s.io/controller-runtime/pkg/log/zap"
3333)
3434
@@ -42,22 +42,20 @@ type reconciler struct {
4242 scheme * runtime.Scheme
4343}
4444
45- func (r * reconciler ) Reconcile (req ctrl.Request ) (ctrl.Result , error ) {
46- log := recLog .WithValues ("chaospod" , req .NamespacedName )
47- log .V (1 ).Info ("reconciling chaos pod" )
48- ctx := context .Background ()
45+ func (r * reconciler ) Reconcile (ctx context.Context , req ctrl.Request ) (ctrl.Result , error ) {
46+ ctx .Log ().V (1 ).Info ("reconciling chaos pod" )
4947
5048 var chaospod api.ChaosPod
5149 if err := r .Get (ctx , req .NamespacedName , & chaospod ); err != nil {
52- log .Error (err , "unable to get chaosctl" )
50+ ctx . Log () .Error (err , "unable to get chaosctl" )
5351 return ctrl.Result {}, err
5452 }
5553
5654 var pod corev1.Pod
5755 podFound := true
5856 if err := r .Get (ctx , req .NamespacedName , & pod ); err != nil {
5957 if ! apierrors .IsNotFound (err ) {
60- log .Error (err , "unable to get pod" )
58+ ctx . Log () .Error (err , "unable to get pod" )
6159 return ctrl.Result {}, err
6260 }
6361 podFound = false
@@ -70,7 +68,7 @@ func (r *reconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
7068 }
7169
7270 if err := r .Delete (ctx , & pod ); err != nil {
73- log .Error (err , "unable to delete pod" )
71+ ctx . Log () .Error (err , "unable to delete pod" )
7472 return ctrl.Result {}, err
7573 }
7674
@@ -84,19 +82,19 @@ func (r *reconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
8482 pod .Spec = templ .Spec
8583
8684 if err := ctrl .SetControllerReference (& chaospod , & pod , r .scheme ); err != nil {
87- log .Error (err , "unable to set pod's owner reference" )
85+ ctx . Log () .Error (err , "unable to set pod's owner reference" )
8886 return ctrl.Result {}, err
8987 }
9088
9189 if err := r .Create (ctx , & pod ); err != nil {
92- log .Error (err , "unable to create pod" )
90+ ctx . Log () .Error (err , "unable to create pod" )
9391 return ctrl.Result {}, err
9492 }
9593
9694 chaospod .Spec .NextStop .Time = time .Now ().Add (time .Duration (10 * (rand .Int63n (2 )+ 1 )) * time .Second )
9795 chaospod .Status .LastRun = pod .CreationTimestamp
9896 if err := r .Update (ctx , & chaospod ); err != nil {
99- log .Error (err , "unable to update chaosctl status" )
97+ ctx . Log () .Error (err , "unable to update chaosctl status" )
10098 return ctrl.Result {}, err
10199 }
102100 return ctrl.Result {}, nil
0 commit comments