Correct ContainerStatus of Notebook CR (kubeflow/kubeflow#5314)

* Correct ContainerStatus of Notebook CR

The Notebook Controller doesn't set the State of the CR correctly. In some cases
the first container is the istio-sidecar which results in an incorrect state being
shown to the Notebook CR. This is fix now by showing the Notebook container
ContainerState to the Notebook CR ContainerState

* Changed log statement and added a comment
Implemented remarks of @yanniszark and @kimwnasptd

* Small reorganization of some if statements
This commit is contained in:
gilbeckers 2021-01-04 10:27:55 +01:00 committed by GitHub
parent 7badb2b424
commit 82c04b3be1
1 changed files with 35 additions and 16 deletions

View File

@ -206,23 +206,42 @@ func (r *NotebookReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
} else {
// Got the pod
podFound = true
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)
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([]v1beta1.NotebookCondition{newCondition}, oldConditions...)
// Update status of the CR using the ContainerState of
// the container that has the same name as the CR.
// If no container of same name is found, the state of the CR is not updated.
if len(pod.Status.ContainerStatuses) > 0 {
notebookContainerFound := false
for i := range pod.Status.ContainerStatuses {
if pod.Status.ContainerStatuses[i].Name != instance.Name {
continue
}
if pod.Status.ContainerStatuses[i].State == instance.Status.ContainerState {
continue
}
log.Info("Updating Notebook CR state: ", "namespace", instance.Namespace, "name", instance.Name)
cs := pod.Status.ContainerStatuses[i].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([]v1beta1.NotebookCondition{newCondition}, oldConditions...)
}
err = r.Status().Update(ctx, instance)
if err != nil {
return ctrl.Result{}, err
}
notebookContainerFound = true
break
}
err = r.Status().Update(ctx, instance)
if err != nil {
return ctrl.Result{}, err
if !notebookContainerFound {
log.Error(nil, "Could not find the Notebook container, will not update the status of the CR. No container has the same name as the CR.", "CR name:", instance.Name)
}
}
}