From 6cd17c35818c8fc6f2040539d29f083ac42b34c7 Mon Sep 17 00:00:00 2001 From: Brandon Palm Date: Fri, 3 Jan 2025 13:13:58 -0600 Subject: [PATCH] nvidiagpu: adjust return consistency (#850) --- pkg/nvidiagpu/clusterpolicy.go | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/pkg/nvidiagpu/clusterpolicy.go b/pkg/nvidiagpu/clusterpolicy.go index 2c25d793c..3fb6c6183 100644 --- a/pkg/nvidiagpu/clusterpolicy.go +++ b/pkg/nvidiagpu/clusterpolicy.go @@ -62,7 +62,7 @@ func NewBuilderFromObjectString(apiClient *clients.Settings, almExample string) "Initializing new Builder structure from almExample string with clusterPolicy name: %s", clusterPolicy.Name) - builder := Builder{ + builder := &Builder{ apiClient: apiClient.Client, Definition: clusterPolicy, } @@ -71,9 +71,11 @@ func NewBuilderFromObjectString(apiClient *clients.Settings, almExample string) glog.V(100).Infof("The ClusterPolicy object definition is nil") builder.errorMsg = "ClusterPolicy 'Object.Definition' is nil" + + return builder } - return &builder + return builder } // Pull loads an existing clusterPolicy into Builder struct. @@ -93,7 +95,7 @@ func Pull(apiClient *clients.Settings, name string) (*Builder, error) { return nil, err } - builder := Builder{ + builder := &Builder{ apiClient: apiClient.Client, Definition: &nvidiagpuv1.ClusterPolicy{ ObjectMeta: metav1.ObjectMeta{ @@ -114,7 +116,7 @@ func Pull(apiClient *clients.Settings, name string) (*Builder, error) { builder.Definition = builder.Object - return &builder, nil + return builder, nil } // Get returns clusterPolicy object if found. @@ -191,16 +193,18 @@ func (builder *Builder) Create() (*Builder, error) { glog.V(100).Infof("Creating the ClusterPolicy %s", builder.Definition.Name) - var err error - if !builder.Exists() { - err = builder.apiClient.Create(context.TODO(), builder.Definition) + if builder.Exists() { + return builder, nil + } - if err == nil { - builder.Object = builder.Definition - } + err := builder.apiClient.Create(context.TODO(), builder.Definition) + if err != nil { + return builder, err } - return builder, err + builder.Object = builder.Definition + + return builder, nil } // Update renovates the existing ClusterPolicy object with the definition in builder. @@ -230,7 +234,7 @@ func (builder *Builder) Update(force bool) (*Builder, error) { } } - return builder, err + return builder, nil } // validate will check that the builder and builder definition are properly initialized before @@ -247,13 +251,13 @@ func (builder *Builder) validate() (bool, error) { if builder.Definition == nil { glog.V(100).Infof("The %s is undefined", resourceCRD) - builder.errorMsg = msg.UndefinedCrdObjectErrString(resourceCRD) + return false, fmt.Errorf(msg.UndefinedCrdObjectErrString(resourceCRD)) } if builder.apiClient == nil { glog.V(100).Infof("The %s builder apiclient is nil", resourceCRD) - builder.errorMsg = fmt.Sprintf("%s builder cannot have nil apiClient", resourceCRD) + return false, fmt.Errorf("%s builder cannot have nil apiClient", resourceCRD) } if builder.errorMsg != "" {