diff --git a/pkg/controllers/apps/pod.go b/pkg/controllers/apps/pod.go index caa58511..3a91a832 100644 --- a/pkg/controllers/apps/pod.go +++ b/pkg/controllers/apps/pod.go @@ -78,6 +78,17 @@ func (r *SidekickReconciler) removePodFinalizerIfMarkedForDeletion(ctx context.C return false, nil } +func (r *SidekickReconciler) removePodFinalizer(ctx context.Context, pod *corev1.Pod) error { + _, err := cu.CreateOrPatch(ctx, r.Client, pod, + func(in client.Object, createOp bool) client.Object { + po := in.(*corev1.Pod) + po.ObjectMeta = core_util.RemoveFinalizer(po.ObjectMeta, getFinalizerName()) + return po + }, + ) + return client.IgnoreNotFound(err) +} + func (r *SidekickReconciler) deletePod(ctx context.Context, pod *corev1.Pod) error { err := r.setDeletionInitiatorAnnotation(ctx, pod) if err != nil { @@ -86,25 +97,6 @@ func (r *SidekickReconciler) deletePod(ctx context.Context, pod *corev1.Pod) err return r.Delete(ctx, pod) } -func getContainerRestartCounts(pod *corev1.Pod) int32 { - restartCounter := int32(0) - for _, cs := range pod.Status.ContainerStatuses { - restartCounter += cs.RestartCount - } - for _, ics := range pod.Status.InitContainerStatuses { - restartCounter += ics.RestartCount - } - return restartCounter -} - -func getTotalContainerRestartCounts(sidekick *appsv1alpha1.Sidekick) int32 { - totalContainerRestartCount := int32(0) - for _, value := range sidekick.Status.ContainerRestartCountsPerPod { - totalContainerRestartCount += value - } - return totalContainerRestartCount -} - func (r *SidekickReconciler) setDeletionInitiatorAnnotation(ctx context.Context, pod *corev1.Pod) error { _, err := cu.CreateOrPatch(ctx, r.Client, pod, func(in client.Object, createOp bool) client.Object { @@ -118,13 +110,13 @@ func (r *SidekickReconciler) setDeletionInitiatorAnnotation(ctx context.Context, return err } -func (r *SidekickReconciler) removePodFinalizer(ctx context.Context, pod *corev1.Pod) error { - _, err := cu.CreateOrPatch(ctx, r.Client, pod, - func(in client.Object, createOp bool) client.Object { - po := in.(*corev1.Pod) - po.ObjectMeta = core_util.RemoveFinalizer(po.ObjectMeta, getFinalizerName()) - return po - }, - ) - return client.IgnoreNotFound(err) +func getContainerRestartCounts(pod *corev1.Pod) int32 { + restartCounter := int32(0) + for _, cs := range pod.Status.ContainerStatuses { + restartCounter += cs.RestartCount + } + for _, ics := range pod.Status.InitContainerStatuses { + restartCounter += ics.RestartCount + } + return restartCounter } diff --git a/pkg/controllers/apps/sidekick.go b/pkg/controllers/apps/sidekick.go index 1b5fb993..9f69a605 100644 --- a/pkg/controllers/apps/sidekick.go +++ b/pkg/controllers/apps/sidekick.go @@ -83,54 +83,6 @@ func (r *SidekickReconciler) updateSidekickPhase(ctx context.Context, req ctrl.R return false, nil } -func (r *SidekickReconciler) updateSidekickStatus(ctx context.Context, sidekick *appsv1alpha1.Sidekick) error { - _, err := cu.PatchStatus(ctx, r.Client, sidekick, func(obj client.Object) client.Object { - sk := obj.(*appsv1alpha1.Sidekick) - sk.Status = sidekick.Status - return sk - }) - return err -} - -func getFailureCountFromSidekickStatus(sidekick *appsv1alpha1.Sidekick) int32 { - failureCount := int32(0) - for _, val := range sidekick.Status.FailureCount { - if val { - failureCount++ - } - } - return failureCount -} - -func (r *SidekickReconciler) getSidekickPhase(sidekick *appsv1alpha1.Sidekick, pod *corev1.Pod) appsv1alpha1.SideKickPhase { - // if restartPolicy is always, we will always try to keep a pod running - // if pod.status.phase == failed, then we will start a new pod - // TODO: which of these two should come first? - if sidekick.Spec.RestartPolicy == corev1.RestartPolicyAlways { - return appsv1alpha1.SideKickPhaseCurrent - } - if sidekick.Status.Phase == appsv1alpha1.SidekickPhaseSucceeded { - return appsv1alpha1.SidekickPhaseSucceeded - } - - // now restartPolicy onFailure & Never remaining - // In both cases we return phase as succeeded if our - // pod return with exit code 0 - if pod != nil && pod.Status.Phase == corev1.PodSucceeded && pod.ObjectMeta.DeletionTimestamp == nil { - return appsv1alpha1.SidekickPhaseSucceeded - } - - // Now we will figure if we should update the sidekick phase - // as failed or not by checking the backOffLimit - - backOffCounts := getTotalBackOffCounts(sidekick) - // TODO: is it > or >= ? - if backOffCounts > *sidekick.Spec.BackoffLimit { - return appsv1alpha1.SideKickPhaseFailed - } - return appsv1alpha1.SideKickPhaseCurrent -} - func (r *SidekickReconciler) calculateSidekickPhase(ctx context.Context, sidekick *appsv1alpha1.Sidekick) (appsv1alpha1.SideKickPhase, error) { if sidekick.Status.ContainerRestartCountsPerPod == nil { sidekick.Status.ContainerRestartCountsPerPod = make(map[string]int32) @@ -169,6 +121,44 @@ func (r *SidekickReconciler) calculateSidekickPhase(ctx context.Context, sidekic return phase, nil } +func (r *SidekickReconciler) getSidekickPhase(sidekick *appsv1alpha1.Sidekick, pod *corev1.Pod) appsv1alpha1.SideKickPhase { + // if restartPolicy is always, we will always try to keep a pod running + // if pod.status.phase == failed, then we will start a new pod + // TODO: which of these two should come first? + if sidekick.Spec.RestartPolicy == corev1.RestartPolicyAlways { + return appsv1alpha1.SideKickPhaseCurrent + } + if sidekick.Status.Phase == appsv1alpha1.SidekickPhaseSucceeded { + return appsv1alpha1.SidekickPhaseSucceeded + } + + // now restartPolicy onFailure & Never remaining + // In both cases we return phase as succeeded if our + // pod return with exit code 0 + if pod != nil && pod.Status.Phase == corev1.PodSucceeded && pod.ObjectMeta.DeletionTimestamp == nil { + return appsv1alpha1.SidekickPhaseSucceeded + } + + // Now we will figure if we should update the sidekick phase + // as failed or not by checking the backOffLimit + + backOffCounts := getTotalBackOffCounts(sidekick) + // TODO: is it > or >= ? + if backOffCounts > *sidekick.Spec.BackoffLimit { + return appsv1alpha1.SideKickPhaseFailed + } + return appsv1alpha1.SideKickPhaseCurrent +} + +func (r *SidekickReconciler) updateSidekickStatus(ctx context.Context, sidekick *appsv1alpha1.Sidekick) error { + _, err := cu.PatchStatus(ctx, r.Client, sidekick, func(obj client.Object) client.Object { + sk := obj.(*appsv1alpha1.Sidekick) + sk.Status = sidekick.Status + return sk + }) + return err +} + func getTotalBackOffCounts(sidekick *appsv1alpha1.Sidekick) int32 { // failureCount keeps track of the total number of pods that had pod.status.phase == failed failureCount := getFailureCountFromSidekickStatus(sidekick) @@ -182,3 +172,21 @@ func getTotalBackOffCounts(sidekick *appsv1alpha1.Sidekick) int32 { klog.Infoln(*sidekick.Spec.BackoffLimit, totalContainerRestartCount, failureCount) return failureCount + totalContainerRestartCount } + +func getFailureCountFromSidekickStatus(sidekick *appsv1alpha1.Sidekick) int32 { + failureCount := int32(0) + for _, val := range sidekick.Status.FailureCount { + if val { + failureCount++ + } + } + return failureCount +} + +func getTotalContainerRestartCounts(sidekick *appsv1alpha1.Sidekick) int32 { + totalContainerRestartCount := int32(0) + for _, value := range sidekick.Status.ContainerRestartCountsPerPod { + totalContainerRestartCount += value + } + return totalContainerRestartCount +}