diff --git a/components/notebook-controller/pkg/apis/notebook/v1alpha1/notebook_types.go b/components/notebook-controller/pkg/apis/notebook/v1alpha1/notebook_types.go index 11a052dd..07d890a3 100644 --- a/components/notebook-controller/pkg/apis/notebook/v1alpha1/notebook_types.go +++ b/components/notebook-controller/pkg/apis/notebook/v1alpha1/notebook_types.go @@ -46,12 +46,19 @@ type NotebookStatus struct { } type NotebookCondition struct { - // Type of the confition/ - Type NotebookConditionType `json:"type"` + // Type is the type of the condition. Possible values are Running|Waiting|Terminated + Type string `json:"type"` + // Last time we probed the condition. + // +optional + LastProbeTime metav1.Time `json:"lastProbeTime,omitempty"` + // (brief) reason the container is in the current state + // +optional + Reason string `json:"reason,omitempty"` + // Message regarding why the container is in the current state. + // +optional + Message string `json:"message,omitempty"` } -type NotebookConditionType string - // +genclient // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object diff --git a/components/notebook-controller/pkg/controller/notebook/notebook_controller.go b/components/notebook-controller/pkg/controller/notebook/notebook_controller.go index 387cf391..b44b81d0 100644 --- a/components/notebook-controller/pkg/controller/notebook/notebook_controller.go +++ b/components/notebook-controller/pkg/controller/notebook/notebook_controller.go @@ -241,19 +241,6 @@ func (r *ReconcileNotebook) Reconcile(request reconcile.Request) (reconcile.Resu } } - // Update the status if previous condition is not "Ready" - oldConditions := instance.Status.Conditions - if len(oldConditions) == 0 || oldConditions[0].Type != "Ready" { - newCondition := v1alpha1.NotebookCondition{ - Type: "Ready", - } - instance.Status.Conditions = append([]v1alpha1.NotebookCondition{newCondition}, oldConditions...) - // Using context.Background as: https://book.kubebuilder.io/basics/status_subresource.html - err = r.Status().Update(context.Background(), instance) - if err != nil { - return reconcile.Result{}, err - } - } // Update the readyReplicas if the status is changed if foundStateful.Status.ReadyReplicas != instance.Status.ReadyReplicas { log.Info("Updating Status", "namespace", instance.Namespace, "name", instance.Name) @@ -268,7 +255,7 @@ func (r *ReconcileNotebook) Reconcile(request reconcile.Request) (reconcile.Resu pod := &corev1.Pod{} err = r.Get(context.TODO(), types.NamespacedName{Name: ss.Name + "-0", Namespace: ss.Namespace}, pod) if err != nil && errors.IsNotFound(err) { - // This should be reconcile by the StatefulSet + // This should be reconciled by the StatefulSet log.Info("Pod not found...") } else if err != nil { return reconcile.Result{}, err @@ -277,7 +264,17 @@ func (r *ReconcileNotebook) Reconcile(request reconcile.Request) (reconcile.Resu if len(pod.Status.ContainerStatuses) > 0 && pod.Status.ContainerStatuses[0].State != instance.Status.ContainerState { log.Info("Updating container state: ", "namespace", instance.Namespace, "name", instance.Name) - instance.Status.ContainerState = pod.Status.ContainerStatuses[0].State + cs := pod.Status.ContainerStatuses[0].State + instance.Status.ContainerState = cs + oldConditions := instance.Status.Conditions + newCondition := getNextCondition(cs) + // Append new condition + if len(oldConditions) == 0 || oldConditions[0].Type != newCondition.Type || + oldConditions[0].Reason != newCondition.Reason || + oldConditions[0].Message != newCondition.Message { + log.Info("Appending to conditions: ", "namespace", instance.Namespace, "name", instance.Name, "type", newCondition.Type, "reason", newCondition.Reason, "message", newCondition.Message) + instance.Status.Conditions = append([]v1alpha1.NotebookCondition{newCondition}, oldConditions...) + } err = r.Status().Update(context.Background(), instance) if err != nil { return reconcile.Result{}, err @@ -288,6 +285,32 @@ func (r *ReconcileNotebook) Reconcile(request reconcile.Request) (reconcile.Resu return reconcile.Result{}, nil } +func getNextCondition(cs corev1.ContainerState) v1alpha1.NotebookCondition { + var nbtype = "" + var nbreason = "" + var nbmsg = "" + + if cs.Running != nil { + nbtype = "Running" + } else if cs.Waiting != nil { + nbtype = "Waiting" + nbreason = cs.Waiting.Reason + nbmsg = cs.Waiting.Message + } else { + nbtype = "Terminated" + nbreason = cs.Terminated.Reason + nbmsg = cs.Terminated.Reason + } + + newCondition := v1alpha1.NotebookCondition{ + Type: nbtype, + LastProbeTime: metav1.Now(), + Reason: nbreason, + Message: nbmsg, + } + return newCondition + +} func generateStatefulSet(instance *v1alpha1.Notebook) *appsv1.StatefulSet { ss := &appsv1.StatefulSet{ ObjectMeta: metav1.ObjectMeta{