Skip to content

Commit

Permalink
Fix panic in Pod Webhook (flyteorg#330)
Browse files Browse the repository at this point in the history
Signed-off-by: Haytham Abuelfutuh <[email protected]>
  • Loading branch information
EngHabu authored Oct 7, 2021
1 parent 2e97809 commit f14dcf5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/webhook/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func appendVolumeMountIfNotExists(volumes []corev1.VolumeMount, vol corev1.Volum
func AppendVolume(volumes []corev1.Volume, volume corev1.Volume) []corev1.Volume {
for _, v := range volumes {
// append secret items to existing volume for secret within same secret group
if v.Secret.SecretName == volume.Secret.SecretName {
if v.Secret != nil && v.Secret.SecretName == volume.Secret.SecretName {
v.Secret.Items = append(v.Secret.Items, volume.Secret.Items...)
return volumes
}
Expand Down
37 changes: 37 additions & 0 deletions pkg/webhook/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"reflect"
"testing"

"github.com/go-test/deep"

corev1 "k8s.io/api/core/v1"
)

Expand Down Expand Up @@ -176,3 +178,38 @@ func TestUpdateEnvVars(t *testing.T) {
})
}
}

func TestAppendVolume(t *testing.T) {
type args struct {
volumes []corev1.Volume
volume corev1.Volume
}
tests := []struct {
name string
args args
want []corev1.Volume
}{
{name: "append secret", args: args{volumes: []corev1.Volume{}, volume: corev1.Volume{Name: "new_secret"}}, want: []corev1.Volume{{Name: "new_secret"}}},
{name: "existing other volumes", args: args{volumes: []corev1.Volume{{Name: "existing"}}, volume: corev1.Volume{Name: "new_secret"}}, want: []corev1.Volume{{Name: "existing"}, {Name: "new_secret"}}},
{name: "existing secret volume",
args: args{
volumes: []corev1.Volume{{
Name: "existing", VolumeSource: corev1.VolumeSource{Secret: &corev1.SecretVolumeSource{SecretName: "foo", Items: []corev1.KeyToPath{{Key: "existingKey"}}}},
}},
volume: corev1.Volume{Name: "new_secret", VolumeSource: corev1.VolumeSource{Secret: &corev1.SecretVolumeSource{SecretName: "foo", Items: []corev1.KeyToPath{{Key: "newKey"}}}}}},
want: []corev1.Volume{{
Name: "existing", VolumeSource: corev1.VolumeSource{Secret: &corev1.SecretVolumeSource{SecretName: "foo", Items: []corev1.KeyToPath{{Key: "existingKey"}, {Key: "newKey"}}}},
}},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := AppendVolume(tt.args.volumes, tt.args.volume)
if diff := deep.Equal(got, tt.want); diff != nil {
t.Errorf("AppendVolume() = %v, want %v", got, tt.want)
t.Errorf("Diff: %v", diff)
}
})
}
}

0 comments on commit f14dcf5

Please sign in to comment.