Skip to content

Commit

Permalink
fix(reconcile): fixing the reconcile logs and events (#428)
Browse files Browse the repository at this point in the history
Signed-off-by: Shubham Chaudhary <shubham.chaudhary@harness.io>

Signed-off-by: Shubham Chaudhary <shubham.chaudhary@harness.io>
  • Loading branch information
ispeakc0de committed Oct 10, 2022
1 parent 9efec74 commit e91b010
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
14 changes: 12 additions & 2 deletions controllers/chaosengine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,10 @@ func engineRunnerPod(runnerPod *podEngineRunner) error {
if err := runnerPod.r.Client.Get(context.TODO(), types.NamespacedName{Name: runnerPod.engineRunner.Name, Namespace: runnerPod.engineRunner.Namespace}, runnerPod.pod); err != nil && k8serrors.IsNotFound(err) {
runnerPod.reqLogger.Info("Creating a new engineRunner Pod", "Pod.Namespace", runnerPod.engineRunner.Namespace, "Pod.Name", runnerPod.engineRunner.Name)
if err = runnerPod.r.Client.Create(context.TODO(), runnerPod.engineRunner); err != nil {
if k8serrors.IsAlreadyExists(err) {
runnerPod.reqLogger.Info("Skip reconcile: engineRunner Pod already exists", "Pod.Namespace", runnerPod.pod.Namespace, "Pod.Name", runnerPod.pod.Name)
return nil
}
return err
}

Expand Down Expand Up @@ -595,7 +599,7 @@ func (r *ChaosEngineReconciler) initEngine(engine *chaosTypes.EngineInfo) error

// reconcileForCreationAndRunning reconciles for Chaos execution of Chaos Engine
func (r *ChaosEngineReconciler) reconcileForCreationAndRunning(engine *chaosTypes.EngineInfo, reqLogger logr.Logger) (reconcile.Result, error) {
if err := r.validateAnnontatedApplication(engine); err != nil {
if err := r.validateAnnotatedApplication(engine); err != nil {
if stopEngineWithAnnotationErrorMessage := r.updateEngineState(engine, litmuschaosv1alpha1.EngineStateStop); stopEngineWithAnnotationErrorMessage != nil {
r.Recorder.Eventf(engine.Instance, corev1.EventTypeWarning, "ChaosResourcesOperationFailed", "(chaos stop) Unable to update chaosengine")
return reconcile.Result{}, fmt.Errorf("unable to Update Engine State: %v", err)
Expand All @@ -611,6 +615,9 @@ func (r *ChaosEngineReconciler) reconcileForCreationAndRunning(engine *chaosType

isCompleted, err := r.checkRunnerContainerCompletedStatus(engine)
if err != nil {
if k8serrors.IsNotFound(err) {
return reconcile.Result{Requeue: true}, nil
}
r.Recorder.Eventf(engine.Instance, corev1.EventTypeWarning, "ChaosResourcesOperationFailed", "(chaos running) Unable to check chaos status")
return reconcile.Result{}, err
}
Expand Down Expand Up @@ -643,7 +650,7 @@ func startReqLogger(request reconcile.Request) logr.Logger {
return reqLogger
}

func (r *ChaosEngineReconciler) validateAnnontatedApplication(engine *chaosTypes.EngineInfo) error {
func (r *ChaosEngineReconciler) validateAnnotatedApplication(engine *chaosTypes.EngineInfo) error {
// Get the image for runner pod from chaosengine spec,operator env or default values.
setChaosResourceImage(engine)

Expand Down Expand Up @@ -746,6 +753,9 @@ func (r *ChaosEngineReconciler) updateChaosResult(engine *chaosTypes.EngineInfo,

for _, result := range chaosresultList.Items {
if result.Labels["chaosUID"] == string(engine.Instance.UID) {
if len(result.ObjectMeta.Annotations) == 0 {
return nil
}
targetsList, annotations := getChaosStatus(result)
result.Status.History.Targets = targetsList
result.ObjectMeta.Annotations = annotations
Expand Down
2 changes: 1 addition & 1 deletion controllers/chaosengine_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ func TestValidateAnnontatedApplication(t *testing.T) {
if err != nil {
fmt.Printf("Unable to create engine: %v", err)
}
err = r.validateAnnontatedApplication(&mock.engine)
err = r.validateAnnotatedApplication(&mock.engine)
if mock.isErr && err == nil {
t.Fatalf("Test %q failed: expected error not to be nil", name)
}
Expand Down

0 comments on commit e91b010

Please sign in to comment.