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,10 +206,22 @@ func (r *NotebookReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
} else { } else {
// Got the pod // Got the pod
podFound = true podFound = true
if len(pod.Status.ContainerStatuses) > 0 &&
pod.Status.ContainerStatuses[0].State != instance.Status.ContainerState { // Update status of the CR using the ContainerState of
log.Info("Updating container state: ", "namespace", instance.Namespace, "name", instance.Name) // the container that has the same name as the CR.
cs := pod.Status.ContainerStatuses[0].State // 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 instance.Status.ContainerState = cs
oldConditions := instance.Status.Conditions oldConditions := instance.Status.Conditions
newCondition := getNextCondition(cs) newCondition := getNextCondition(cs)
@ -224,6 +236,13 @@ func (r *NotebookReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
if err != nil { if err != nil {
return ctrl.Result{}, err return ctrl.Result{}, err
} }
notebookContainerFound = true
break
}
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)
}
} }
} }