Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
Signed-off-by: souravbiswassanto <[email protected]>
  • Loading branch information
souravbiswassanto committed Oct 29, 2024
1 parent 82844c2 commit 0070267
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 31 deletions.
8 changes: 2 additions & 6 deletions apis/apps/v1alpha1/openapi_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions apis/apps/v1alpha1/sidekick_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,13 +537,13 @@ type VolumeMount struct {
}

type LeaderStatus struct {
Name string `json:"name"`
Name string `json:"name,omitempty"`
}

// SidekickStatus defines the observed state of Sidekick
type SidekickStatus struct {
Leader LeaderStatus `json:"leader"`
Pod core.PodPhase `json:"pod"`
Leader LeaderStatus `json:"leader,omitempty"`
Pod core.PodPhase `json:"pod,omitempty"`

// Specifies the current phase of the sidekick CR
// +optional
Expand Down
5 changes: 0 additions & 5 deletions crds/apps.k8s.appscode.com_sidekicks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7686,8 +7686,6 @@ spec:
properties:
name:
type: string
required:
- name
type: object
observedGeneration:
description: observedGeneration is the most recent generation observed
Expand All @@ -7707,9 +7705,6 @@ spec:
description: PodPhase is a label for the condition of a pod at the
current time.
type: string
required:
- leader
- pod
type: object
type: object
served: true
Expand Down
92 changes: 75 additions & 17 deletions pkg/controllers/apps/sidekick_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,16 @@ import (
)

const (
keyHash = "sidekick.appscode.com/hash"
keyLeader = "sidekick.appscode.com/leader"
podHash = "sidekick.appscode.com/pod-specific-hash"
keyHash = "sidekick.appscode.com/hash"
keyLeader = "sidekick.appscode.com/leader"
podHash = "sidekick.appscode.com/pod-specific-hash"
finalizerSuffix = "finalizer"
)

func getFinalizerName() string {
return appsv1alpha1.SchemeGroupVersion.Group + "/" + finalizerSuffix
}

// SidekickReconciler reconciles a Sidekick object
type SidekickReconciler struct {
client.Client
Expand All @@ -78,6 +83,23 @@ func (r *SidekickReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
klog.Infoln(fmt.Sprintf("reconciling %v ", req.NamespacedName))
logger := log.FromContext(ctx, "sidekick", req.Name, "ns", req.Namespace)
ctx = log.IntoContext(ctx, logger)

var pod corev1.Pod
err := r.Get(ctx, req.NamespacedName, &pod)
if err != nil && !errors.IsNotFound(err) {
return ctrl.Result{}, err
}

if err == nil && pod.DeletionTimestamp != nil {
if core_util.HasFinalizer(pod.ObjectMeta, getFinalizerName()) {
err = r.removeFinalizer(ctx, &pod)
if err != nil {
return ctrl.Result{}, err
}
return ctrl.Result{}, nil
}
}

var sidekick appsv1alpha1.Sidekick
if err := r.Get(ctx, req.NamespacedName, &sidekick); err != nil {
logger.Error(err, "unable to fetch Sidekick")
Expand All @@ -88,15 +110,15 @@ func (r *SidekickReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
}

if sidekick.DeletionTimestamp != nil {
if core_util.HasFinalizer(sidekick.ObjectMeta, appsv1alpha1.SchemeGroupVersion.Group) {
if core_util.HasFinalizer(sidekick.ObjectMeta, getFinalizerName()) {
return ctrl.Result{}, r.terminate(ctx, &sidekick)
}
}

_, err := cu.CreateOrPatch(context.TODO(), r.Client, &sidekick,
_, err = cu.CreateOrPatch(context.TODO(), r.Client, &sidekick,
func(in client.Object, createOp bool) client.Object {
sk := in.(*appsv1alpha1.Sidekick)
sk.ObjectMeta = core_util.AddFinalizer(sk.ObjectMeta, appsv1alpha1.SchemeGroupVersion.Group)
sk.ObjectMeta = core_util.AddFinalizer(sk.ObjectMeta, getFinalizerName())

return sk
},
Expand Down Expand Up @@ -148,7 +170,7 @@ func (r *SidekickReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c

e2 := r.Get(ctx, req.NamespacedName, &pod)
if e2 == nil {
err := r.Delete(ctx, &pod)
err := r.deletePod(ctx, &pod)
if err != nil {
return ctrl.Result{}, err
}
Expand All @@ -170,8 +192,6 @@ func (r *SidekickReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
} else if err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}

var pod corev1.Pod
e2 := r.Get(ctx, req.NamespacedName, &pod)
if e2 != nil && !errors.IsNotFound(e2) {
return ctrl.Result{}, client.IgnoreNotFound(e2)
Expand All @@ -190,7 +210,7 @@ func (r *SidekickReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
if expectedHash != actualHash ||
leader.Name != pod.Annotations[keyLeader] ||
leader.Spec.NodeName != pod.Spec.NodeName || (pod.Status.Phase == corev1.PodFailed && sidekick.Spec.RestartPolicy == corev1.RestartPolicyNever) {
err := r.Delete(ctx, &pod)
err := r.deletePod(ctx, &pod)
if err != nil {
return ctrl.Result{}, err
}
Expand Down Expand Up @@ -277,10 +297,10 @@ func (r *SidekickReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
}
curTime := time.Now().String()
curTimeHash := sha256.Sum256([]byte(curTime))
pod.Annotations[podHash] = hex.EncodeToString(curTimeHash[:])
pod.Annotations[podHash] = hex.EncodeToString(curTimeHash[:])[:16]
pod.Annotations[keyHash] = meta.GenerationHash(&sidekick)
pod.Annotations[keyLeader] = leader.Name
sidekick.Annotations[podHash] = hex.EncodeToString(curTimeHash[:])
sidekick.Annotations[podHash] = hex.EncodeToString(curTimeHash[:])[:16]
klog.Infoln("----------------------------------------------------------")
for _, c := range sidekick.Spec.Containers {
c2, err := convContainer(leader, c)
Expand All @@ -296,8 +316,18 @@ func (r *SidekickReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
}
pod.Spec.InitContainers = append(pod.Spec.InitContainers, *c2)
}
// Adding finalizer to pod because when user will delete this pod using
// kubectl delete, then pod will be gracefully terminated which will led
// to pod.status.phase: succeeded. We need to control this behaviour.
// By adding finalizer, we will know who is deleting the object
klog.Infoln("hello ")
e3 := r.Create(ctx, &pod)
_, e3 := cu.CreateOrPatch(context.TODO(), r.Client, &pod,
func(in client.Object, createOp bool) client.Object {
po := in.(*corev1.Pod)
po.ObjectMeta = core_util.AddFinalizer(po.ObjectMeta, getFinalizerName())
return po
},
)
klog.Infoln("hello from down")

if e3 != nil {
Expand All @@ -307,7 +337,7 @@ func (r *SidekickReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
_, err = cu.CreateOrPatch(context.TODO(), r.Client, &sidekick,
func(in client.Object, createOp bool) client.Object {
sk := in.(*appsv1alpha1.Sidekick)
sk.Annotations[podHash] = hex.EncodeToString(curTimeHash[:])
sk.Annotations[podHash] = hex.EncodeToString(curTimeHash[:])[:16]
return sk
},
)
Expand Down Expand Up @@ -544,7 +574,7 @@ func (r *SidekickReconciler) terminate(ctx context.Context, sidekick *appsv1alph
_, err = cu.CreateOrPatch(context.TODO(), r.Client, sidekick,
func(in client.Object, createOp bool) client.Object {
sk := in.(*appsv1alpha1.Sidekick)
sk.ObjectMeta = core_util.RemoveFinalizer(sk.ObjectMeta, appsv1alpha1.SchemeGroupVersion.Group)
sk.ObjectMeta = core_util.RemoveFinalizer(sk.ObjectMeta, getFinalizerName())

return sk
},
Expand Down Expand Up @@ -573,7 +603,8 @@ func (r *SidekickReconciler) sidekickStatus(ctx context.Context, sidekick *appsv
var pod corev1.Pod
err := r.Get(ctx, client.ObjectKeyFromObject(sidekick), &pod)
if err != nil {
return "", err
klog.Infoln("phase checking -----", sidekick.Status.Phase)
return sidekick.Status.Phase, err
}
podSpecificHash := pod.Annotations[podHash]
restartCounter := getContainerRestartCounts(&pod)
Expand Down Expand Up @@ -605,9 +636,14 @@ func (r *SidekickReconciler) getSidekickPhase(sidekick *appsv1alpha1.Sidekick, p
// now restartPolicy onFailure & Never remaining
// In both cases we return phase as succeeded if our
// pod return with exit code 0
if pod.Status.Phase == corev1.PodSucceeded {
if pod.Status.Phase == corev1.PodSucceeded && pod.ObjectMeta.DeletionTimestamp == nil {
klog.Infoln("pods succeeded", pod.Status)
return appsv1alpha1.SidekickPhaseSucceeded
}
if sidekick.Status.Phase != appsv1alpha1.SidekickPhaseSucceeded && pod.Status.Phase == corev1.PodSucceeded && pod.ObjectMeta.DeletionTimestamp != nil {
podSpecificHash := pod.Annotations[podHash]
sidekick.Status.FailureCount[podSpecificHash] = true
}
// Now we will figure if we should update the sidekick phase
// as failed or not by checking the backOffLimit

Expand Down Expand Up @@ -668,3 +704,25 @@ func (r *SidekickReconciler) updatePodFailureCount(ctx context.Context, sidekick
sidekick.Status.FailureCount[hash] = true
return r.updateSidekickStatus(ctx, sidekick)
}

func (r *SidekickReconciler) deletePod(ctx context.Context, pod *corev1.Pod) error {
err := r.removeFinalizer(ctx, pod)
if err != nil {
return err
}
return r.Delete(ctx, pod)
}

func (r *SidekickReconciler) removeFinalizer(ctx context.Context, pod *corev1.Pod) error {
klog.Infoln("here in removing finalizer")
_, 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
},
)
klog.Infoln("here in removing finalizer done")

return err
}

0 comments on commit 0070267

Please sign in to comment.