Skip to content

Commit

Permalink
refactor: error checks and nil pointer check (#5964)
Browse files Browse the repository at this point in the history
* refactor: error checks and nil pointer check

* refactor: import sorting order

* refactor: lint update

* refactor: updating SetCondition to return early if condition is nil
  • Loading branch information
abhimanyu003 authored Oct 9, 2024
1 parent 864ad29 commit 9b7f7d4
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 16 deletions.
2 changes: 1 addition & 1 deletion operator/apis/mlops/v1alpha1/experiment_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (es *ExperimentStatus) IsConditionReady(t apis.ConditionType) bool {
func (es *ExperimentStatus) SetCondition(conditionType apis.ConditionType, condition *apis.Condition) {
switch {
case condition == nil:
experimentConditionSet.Manage(es).MarkUnknown(conditionType, "", "")
return
case condition.Status == v1.ConditionUnknown:
experimentConditionSet.Manage(es).MarkUnknown(conditionType, condition.Reason, condition.Message)
case condition.Status == v1.ConditionTrue:
Expand Down
69 changes: 69 additions & 0 deletions operator/apis/mlops/v1alpha1/experiment_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/pkg/apis"
duckv1 "knative.dev/pkg/apis/duck/v1"
"knative.dev/pkg/ptr"

"github.com/seldonio/seldon-core/apis/go/v2/mlops/scheduler"
)
Expand Down Expand Up @@ -210,3 +211,71 @@ func TestExperimentStatusPrintColumns(t *testing.T) {
})
}
}

func TestExperimentStatusSetCondition(t *testing.T) {
type args struct {
conditionType apis.ConditionType
condition *apis.Condition
}
tests := []struct {
name string
args args
want *v1.ConditionStatus
}{
{
name: "should not panic if condition is nil",
args: args{
conditionType: ExperimentReady,
condition: nil,
},
want: nil,
},
{
name: "ConditionUnknown",
args: args{
conditionType: ExperimentReady,
condition: &apis.Condition{
Status: "Unknown",
},
},
want: (*v1.ConditionStatus)(ptr.String("Unknown")),
},
{
name: "ConditionTrue",
args: args{
conditionType: ExperimentReady,
condition: &apis.Condition{
Status: "True",
},
},
want: (*v1.ConditionStatus)(ptr.String("True")),
},
{
name: "ConditionFalse",
args: args{
conditionType: ExperimentReady,
condition: &apis.Condition{
Status: "False",
},
},
want: (*v1.ConditionStatus)(ptr.String("False")),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
es := &ExperimentStatus{}
es.SetCondition(tt.args.conditionType, tt.args.condition)
if tt.want == nil {
if es.GetCondition(tt.args.conditionType) != nil {
t.Errorf("want %v : got %v", tt.want, es.GetCondition(tt.args.conditionType))
}
}
if tt.want != nil {
got := es.GetCondition(tt.args.conditionType).Status
if *tt.want != got {
t.Errorf("want %v : got %v", *tt.want, got)
}
}
})
}
}
2 changes: 1 addition & 1 deletion operator/apis/mlops/v1alpha1/pipeline_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ func (ps *PipelineStatus) IsConditionReady(t apis.ConditionType) bool {
func (ps *PipelineStatus) SetCondition(conditionType apis.ConditionType, condition *apis.Condition) {
switch {
case condition == nil:
pipelineConditionSet.Manage(ps).MarkUnknown(conditionType, "", "")
return
case condition.Status == v1.ConditionUnknown:
pipelineConditionSet.Manage(ps).MarkUnknown(conditionType, condition.Reason, condition.Message)
case condition.Status == v1.ConditionTrue:
Expand Down
2 changes: 1 addition & 1 deletion operator/apis/mlops/v1alpha1/seldonruntime_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (ss *SeldonRuntimeStatus) IsConditionReady(t apis.ConditionType) bool {
func (ss *SeldonRuntimeStatus) SetCondition(condition *apis.Condition) {
switch {
case condition == nil:
seldonRuntimeConditionSet.Manage(ss).MarkUnknown(condition.Type, "", "")
return
case condition.Status == v1.ConditionUnknown:
seldonRuntimeConditionSet.Manage(ss).MarkUnknown(condition.Type, condition.Reason, condition.Message)
case condition.Status == v1.ConditionTrue:
Expand Down
2 changes: 1 addition & 1 deletion operator/apis/mlops/v1alpha1/server_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (ss *ServerStatus) IsConditionReady(t apis.ConditionType) bool {
func (ss *ServerStatus) SetCondition(condition *apis.Condition) {
switch {
case condition == nil:
serverConditionSet.Manage(ss).MarkUnknown(condition.Type, "", "")
return
case condition.Status == v1.ConditionUnknown:
serverConditionSet.Manage(ss).MarkUnknown(condition.Type, condition.Reason, condition.Message)
case condition.Status == v1.ConditionTrue:
Expand Down
66 changes: 66 additions & 0 deletions operator/apis/mlops/v1alpha1/server_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import (

. "github.com/onsi/gomega"
"github.com/tidwall/gjson"
v1 "k8s.io/api/core/v1"
"knative.dev/pkg/apis"
"knative.dev/pkg/ptr"
)

/* WARNING: Read this first if test below fails (either at compile-time or while running the
Expand Down Expand Up @@ -71,3 +74,66 @@ func TestServerStatusPrintColumns(t *testing.T) {
})
}
}

func TestServerStatusSetCondition(t *testing.T) {
type args struct {
condition *apis.Condition
}
tests := []struct {
name string
args args
want *v1.ConditionStatus
}{
{
name: "should not panic if condition is nil",
args: args{
condition: nil,
},
want: nil,
},
{
name: "ConditionUnknown",
args: args{
condition: &apis.Condition{
Status: "Unknown",
},
},
want: (*v1.ConditionStatus)(ptr.String("Unknown")),
},
{
name: "ConditionTrue",
args: args{
condition: &apis.Condition{
Status: "True",
},
},
want: (*v1.ConditionStatus)(ptr.String("True")),
},
{
name: "ConditionFalse",
args: args{
condition: &apis.Condition{
Status: "False",
},
},
want: (*v1.ConditionStatus)(ptr.String("False")),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ss := &ServerStatus{}
ss.SetCondition(tt.args.condition)
if tt.want == nil {
if ss.GetCondition("") != nil {
t.Errorf("want %v : got %v", tt.want, ss.GetCondition(""))
}
}
if tt.want != nil {
got := ss.GetCondition("").Status
if *tt.want != got {
t.Errorf("want %v : got %v", *tt.want, got)
}
}
})
}
}
3 changes: 0 additions & 3 deletions operator/pkg/cli/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -595,9 +595,6 @@ func (sc *SchedulerClient) LoadPipeline(data []byte) (*scheduler.LoadPipelineRes
return nil, err
}
schPipeline := pipeline.AsSchedulerPipeline()
if err != nil {
return nil, err
}
req := &scheduler.LoadPipelineRequest{Pipeline: schPipeline}
if sc.verbose {
printProto(req)
Expand Down
4 changes: 0 additions & 4 deletions scheduler/pkg/kafka/gateway/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,7 @@ type mockGRPCMLServer struct {
}

func (m *mockGRPCMLServer) setup() error {
var err error
m.listener = bufconn.Listen(bufSize)
if err != nil {
return err
}
opts := []grpc.ServerOption{}
m.server = grpc.NewServer(opts...)
v2.RegisterGRPCInferenceServiceServer(m.server, m)
Expand Down
3 changes: 0 additions & 3 deletions scheduler/pkg/kafka/pipeline/status/model_rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ func NewModelRestStatusCaller(logger logrus.FieldLogger, envoyHost string, envoy
return nil, err
}
httpClient := util.GetHttpClientFromTLSOptions(tlsOptions)
if err != nil {
return nil, err
}
return &ModelRestCaller{
envoyHost: envoyHost,
envoyPort: envoyPort,
Expand Down
4 changes: 2 additions & 2 deletions scheduler/pkg/proxy/chainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ func (pc *ProxyChainer) SubscribePipelineUpdates(
Version: 1,
Uid: "1234",
Updates: []*chainer.PipelineStepUpdate{
&chainer.PipelineStepUpdate{
{
Sources: []*chainer.PipelineTopic{{PipelineName: "some-pipeline", TopicName: chainerInputTopic1}},
Sink: &chainer.PipelineTopic{PipelineName: "some-pipeline", TopicName: chainerOutputTopic1},
InputJoinTy: chainer.PipelineStepUpdate_Inner,
},
&chainer.PipelineStepUpdate{
{
Sources: []*chainer.PipelineTopic{{PipelineName: "some-pipeline", TopicName: chainerInputTopic2}},
Sink: &chainer.PipelineTopic{PipelineName: "some-pipeline", TopicName: chainerOutputTopic2},
InputJoinTy: chainer.PipelineStepUpdate_Inner,
Expand Down

0 comments on commit 9b7f7d4

Please sign in to comment.