prevent panic when the restore hook init container command annotation is empty (#10371)

* prevent panic when the restore hook init container command annotation is empty

Signed-off-by: samay43 <samayrbhat43@gmail.com>

* add changelog entry

Signed-off-by: samay43 <samayrbhat43@gmail.com>

---------

Signed-off-by: samay43 <samayrbhat43@gmail.com>
Co-authored-by: Daniel Jiang <daniel.jiang@broadcom.com>
This commit is contained in:
R4mbo
2026-08-24 11:24:04 -07:00
committed by GitHub
co-authored by Daniel Jiang
parent cc7b1dbaef
commit bc49963f1e
3 changed files with 26 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
prevent panic when the restore hook init container command annotation is empty
+6
View File
@@ -365,6 +365,12 @@ func getPodExecHookFromAnnotations(annotations map[string]string, phase HookPhas
func parseStringToCommand(commandValue string) []string {
var command []string
// An empty command means the container image's own entrypoint should be used.
// Callers that require a command already return early; getInitContainerFromAnnotation
// deliberately allows this case, so return nil rather than indexing an empty string.
if commandValue == "" {
return nil
}
// check for json array
if commandValue[0] == '[' {
if err := json.Unmarshal([]byte(commandValue), &command); err != nil {
+19
View File
@@ -1287,6 +1287,25 @@ func TestGetInitContainerFromAnnotations(t *testing.T) {
podRestoreHookInitContainerCommandAnnotationKey: "[foobarbaz",
},
},
{
name: "should use the image's default entrypoint when the command annotation is empty",
expectNil: false,
expected: builder.ForContainer("restore-init1", "busy-box").Result(),
inputAnnotations: map[string]string{
podRestoreHookInitContainerImageAnnotationKey: "busy-box",
podRestoreHookInitContainerNameAnnotationKey: "restore-init",
podRestoreHookInitContainerCommandAnnotationKey: "",
},
},
{
name: "should use the image's default entrypoint when the command annotation is missing",
expectNil: false,
expected: builder.ForContainer("restore-init1", "busy-box").Result(),
inputAnnotations: map[string]string{
podRestoreHookInitContainerImageAnnotationKey: "busy-box",
podRestoreHookInitContainerNameAnnotationKey: "restore-init",
},
},
}
for _, tc := range testCases {