Skip to content

Commit

Permalink
Restructure
Browse files Browse the repository at this point in the history
Signed-off-by: Arnob Kumar Saha <[email protected]>
  • Loading branch information
ArnobKumarSaha committed Nov 3, 2024
1 parent 8b67c33 commit d2d1348
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 76 deletions.
48 changes: 20 additions & 28 deletions pkg/controllers/apps/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
}
104 changes: 56 additions & 48 deletions pkg/controllers/apps/sidekick.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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
}

0 comments on commit d2d1348

Please sign in to comment.