Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix nil pointer dereference panics in mlserver #3047

Merged
merged 1 commit into from
Mar 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions operator/controllers/mlserver.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controllers

import (
"errors"
"fmt"
"strconv"

Expand All @@ -22,6 +23,13 @@ const (
)

func mergeMLServerContainer(existing *v1.Container, mlServer *v1.Container) *v1.Container {
if mlServer == nil {
// Nothing to merge.
return existing
}
if existing == nil {
existing = &v1.Container{}
}
// Overwrite core items if not existing or required
if existing.Image == "" {
existing.Image = mlServer.Image
Expand Down Expand Up @@ -58,6 +66,9 @@ func mergeMLServerContainer(existing *v1.Container, mlServer *v1.Container) *v1.
}

func getMLServerContainer(pu *machinelearningv1.PredictiveUnit) (*v1.Container, error) {
if pu == nil {
return nil, errors.New("received nil predictive unit")
}
image, err := getMLServerImage(pu)
if err != nil {
return nil, err
Expand Down Expand Up @@ -126,6 +137,9 @@ func getMLServerContainer(pu *machinelearningv1.PredictiveUnit) (*v1.Container,
}

func getMLServerImage(pu *machinelearningv1.PredictiveUnit) (string, error) {
if pu == nil {
return "", errors.New("received nil predictive unit")
}
prepackConfig := machinelearningv1.GetPrepackServerConfig(string(*pu.Implementation))
if prepackConfig == nil {
return "", fmt.Errorf("failed to get server config for %s", *pu.Implementation)
Expand All @@ -146,6 +160,9 @@ func getMLServerImage(pu *machinelearningv1.PredictiveUnit) (string, error) {
}

func getMLServerEnvVars(pu *machinelearningv1.PredictiveUnit) ([]v1.EnvVar, error) {
if pu == nil {
return nil, errors.New("received nil predictive unit")
}
httpPort := pu.Endpoint.HttpPort
grpcPort := pu.Endpoint.GrpcPort

Expand Down Expand Up @@ -184,6 +201,9 @@ func getMLServerEnvVars(pu *machinelearningv1.PredictiveUnit) ([]v1.EnvVar, erro
}

func getMLServerModelImplementation(pu *machinelearningv1.PredictiveUnit) (string, error) {
if pu == nil {
return "", errors.New("received nil predictive unit")
}
switch *pu.Implementation {
case machinelearningv1.PrepackSklearnName:
return MLServerSKLearnImplementation, nil
Expand Down
1 change: 1 addition & 0 deletions operator/controllers/mlserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ var _ = Describe("MLServer helpers", func() {
It("should merge containers adding extra env", func() {
merged := mergeMLServerContainer(existing, mlServer)

Expect(merged).ToNot(BeNil())
Expect(merged.Env).To(ContainElement(v1.EnvVar{Name: "FOO", Value: "BAR"}))
Expect(merged.Env).To(ContainElements(mlServer.Env))
Expect(merged.Image).To(Equal(mlServer.Image))
Expand Down