Skip to content

Fix container indexing bug in SetContainerImageFromRegistry function #7727 #626

@jiridanek

Description

@jiridanek

Problem

The SetContainerImageFromRegistry function in components/odh-notebook-controller/controllers/notebook_webhook.go has a bug where it operates on the wrong slice element and drops environment variable edits.

Details

Inside the for _, container := range notebook.Spec.Template.Spec.Containers loop (around lines 756-770), the code:

  1. Writes the image to notebook.Spec.Template.Spec.Containers[0], irrespective of which container actually matched the notebook name
  2. Modifies the copied container variable's Env slice, but never writes it back to the parent slice, so the JUPYTER_IMAGE update is lost

Solution

Refactor to iterate with index and update in place:

-for _, container := range notebook.Spec.Template.Spec.Containers {

  • if container.Name == notebook.Name {
    +for idx, container := range notebook.Spec.Template.Spec.Containers {
  • if container.Name == notebook.Name {
  •    notebook.Spec.Template.Spec.Containers[0].Image = imageHash
    
  •    notebook.Spec.Template.Spec.Containers[idx].Image = imageHash
       …
    
  •    for i, envVar := range container.Env {
    
  •    for i, envVar := range notebook.Spec.Template.Spec.Containers[idx].Env {
           if envVar.Name == "JUPYTER_IMAGE" {
    
  •            container.Env[i].Value = imageSelection
    
  •            notebook.Spec.Template.Spec.Containers[idx].Env[i].Value = imageSelection
               break
           }
       }
       …
    
    }
    }
    This guarantees the correct container is mutated and the environment variable change is persisted.

Context

This issue was identified during review of PR #625 but is a pre-existing bug unrelated to that PR's changes.

Backlinks:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions