Skip to content

Commit 7b3914d

Browse files
committed
fix for issue # 119
1 parent e568f6c commit 7b3914d

File tree

3 files changed

+367
-32
lines changed

3 files changed

+367
-32
lines changed

api/v1alpha1/sandbox_types.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,22 @@ type SandboxSpec struct {
119119
Replicas *int32 `json:"replicas,omitempty"`
120120
}
121121

122+
// SandboxPhase is a label for the condition of a sandbox at the current time.
123+
type SandboxPhase string
124+
125+
const (
126+
// SandboxPhasePending means the sandbox is being created.
127+
SandboxPhasePending SandboxPhase = "Pending"
128+
// SandboxPhaseRunning means the sandbox is ready.
129+
SandboxPhaseRunning SandboxPhase = "Running"
130+
// SandboxPhasePaused means the sandbox is paused.
131+
SandboxPhasePaused SandboxPhase = "Paused"
132+
// SandboxPhaseTerminating means the sandbox is terminating.
133+
SandboxPhaseTerminating SandboxPhase = "Terminating"
134+
// SandboxPhaseFailed means the sandbox has failed.
135+
SandboxPhaseFailed SandboxPhase = "Failed"
136+
)
137+
122138
// SandboxStatus defines the observed state of Sandbox.
123139
type SandboxStatus struct {
124140
// FQDN that is valid for default cluster settings
@@ -132,6 +148,10 @@ type SandboxStatus struct {
132148
// status conditions array
133149
Conditions []metav1.Condition `json:"conditions,omitempty"`
134150

151+
// Phase is the current phase of the sandbox.
152+
// +optional
153+
Phase SandboxPhase `json:"phase,omitempty"`
154+
135155
// Replicas is the number of actual replicas.
136156
Replicas int32 `json:"replicas"`
137157

controllers/sandbox_controller.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ func (r *SandboxReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
9595

9696
if !sandbox.ObjectMeta.DeletionTimestamp.IsZero() {
9797
log.Info("Sandbox is being deleted")
98+
sandbox.Status.Phase = sandboxv1alpha1.SandboxPhaseTerminating
9899
return ctrl.Result{}, nil
99100
}
100101

@@ -126,6 +127,7 @@ func (r *SandboxReconciler) reconcileChildResources(ctx context.Context, sandbox
126127
nameHash := NameHash(sandbox.Name)
127128

128129
var allErrors error
130+
sandbox.Status.Phase = sandboxv1alpha1.SandboxPhasePending
129131

130132
// Reconcile PVCs
131133
err := r.reconcilePVCs(ctx, sandbox)
@@ -172,6 +174,14 @@ func (r *SandboxReconciler) computeReadyCondition(sandbox *sandboxv1alpha1.Sandb
172174
podReady := false
173175
if pod != nil {
174176
message = "Pod exists with phase: " + string(pod.Status.Phase)
177+
switch pod.Status.Phase {
178+
case corev1.PodRunning:
179+
sandbox.Status.Phase = sandboxv1alpha1.SandboxPhaseRunning
180+
case corev1.PodFailed, corev1.PodUnknown:
181+
sandbox.Status.Phase = sandboxv1alpha1.SandboxPhaseFailed
182+
default:
183+
sandbox.Status.Phase = sandboxv1alpha1.SandboxPhasePending
184+
}
175185
// Check if pod Ready condition is true
176186
if pod.Status.Phase == corev1.PodRunning {
177187
message = "Pod is Running but not Ready"
@@ -318,7 +328,14 @@ func (r *SandboxReconciler) reconcilePod(ctx context.Context, sandbox *sandboxv1
318328
if pod != nil {
319329
log.Info("Found Pod", "Pod.Namespace", pod.Namespace, "Pod.Name", pod.Name)
320330
// TODO - Do we enfore (change) spec if a pod exists ?
321-
// r.Patch(ctx, pod, client.Apply, client.ForceOwnership, client.FieldOwner("sandbox-controller"))
331+
patch := client.MergeFrom(pod.DeepCopy())
332+
if err := ctrl.SetControllerReference(sandbox, pod, r.Scheme); err != nil {
333+
return nil, fmt.Errorf("SetControllerReference for Pod failed: %w", err)
334+
}
335+
if err := r.Patch(ctx, pod, patch, client.FieldOwner(sandboxControllerFieldOwner)); err != nil {
336+
log.Error(err, "Failed to patch", "Pod.Namespace", pod.Namespace, "Pod.Name", pod.Name)
337+
return nil, err
338+
}
322339
return pod, nil
323340
}
324341

@@ -402,6 +419,7 @@ func (r *SandboxReconciler) reconcilePVCs(ctx context.Context, sandbox *sandboxv
402419

403420
func (r *SandboxReconciler) handleSandboxExpiry(ctx context.Context, sandbox *sandboxv1alpha1.Sandbox) error {
404421
var allErrors error
422+
sandbox.Status.Phase = sandboxv1alpha1.SandboxPhaseTerminating
405423
pod := &corev1.Pod{
406424
ObjectMeta: metav1.ObjectMeta{
407425
Name: sandbox.Name,

0 commit comments

Comments
 (0)