From f17168881384bc5811b1829b14571f167a95bdab Mon Sep 17 00:00:00 2001 From: Brandon Palm Date: Tue, 7 Jan 2025 12:08:17 -0600 Subject: [PATCH] servicemesh: adjust return consistency (#865) --- pkg/servicemesh/controlplane.go | 46 +++++++++++++++++---------------- pkg/servicemesh/memberroll.go | 14 +++++----- 2 files changed, 32 insertions(+), 28 deletions(-) diff --git a/pkg/servicemesh/controlplane.go b/pkg/servicemesh/controlplane.go index 20b86246f..8dd445079 100644 --- a/pkg/servicemesh/controlplane.go +++ b/pkg/servicemesh/controlplane.go @@ -59,12 +59,16 @@ func NewControlPlaneBuilder(apiClient *clients.Settings, name, nsname string) *C glog.V(100).Infof("The name of the serviceMeshControlPlane is empty") builder.errorMsg = "serviceMeshControlPlane 'name' cannot be empty" + + return builder } if nsname == "" { glog.V(100).Infof("The namespace of the serviceMeshControlPlane is empty") builder.errorMsg = "serviceMeshControlPlane 'nsname' cannot be empty" + + return builder } return builder @@ -121,17 +125,17 @@ func (builder *ControlPlaneBuilder) WithGrafanaAddon( glog.V(100).Infof("The grafanaInstallConfig of the Grafana addon is empty") builder.errorMsg = "the Grafana addon 'grafanaInstallConfig' cannot be empty when Grafana addon is enabled" + + return builder } if address == "" { glog.V(100).Infof("The address of the Grafana addon is empty") builder.errorMsg = "the Grafana addon 'address' cannot be empty when Grafana addon is enabled" - } - } - if builder.errorMsg != "" { - return builder + return builder + } } addonConfig := &istiov2.GrafanaAddonConfig{ @@ -167,15 +171,15 @@ func (builder *ControlPlaneBuilder) WithJaegerAddon( glog.V(100).Infof("The name of the Jaeger addon is empty") builder.errorMsg = "the Jaeger addon 'name' cannot be empty" + + return builder } if jaegerInstallConfig == nil { glog.V(100).Infof("The jaegerInstallConfig of the Jaeger addon is empty") builder.errorMsg = "the Jaeger addon 'jaegerInstallConfig' cannot be empty" - } - if builder.errorMsg != "" { return builder } @@ -211,17 +215,17 @@ func (builder *ControlPlaneBuilder) WithKialiAddon( glog.V(100).Infof("The kialiInstallConfig of the Kiali addon is empty") builder.errorMsg = "the Kiali addon 'kialiInstallConfig' cannot be empty when Kiali addon is enabled" + + return builder } if name == "" { glog.V(100).Infof("The name of the Kiali addon is empty") builder.errorMsg = "the Kiali addon 'name' cannot be empty when Kiali addon is enabled" - } - } - if builder.errorMsg != "" { - return builder + return builder + } } addonConfig := &istiov2.KialiAddonConfig{ @@ -262,6 +266,8 @@ func (builder *ControlPlaneBuilder) WithPrometheusAddon( builder.errorMsg = "the Prometheus addon 'prometheusInstallConfig' cannot " + "be empty when Prometheus addon is enabled" + + return builder } if metricsExpiryDuration == "" { @@ -269,17 +275,17 @@ func (builder *ControlPlaneBuilder) WithPrometheusAddon( builder.errorMsg = "the Prometheus addon 'metricsExpiryDuration' cannot " + "be empty when Prometheus addon is enabled" + + return builder } if address == "" { glog.V(100).Info("The address of the Prometheus addon is empty") builder.errorMsg = "the Prometheus addon 'address' cannot be empty when Prometheus addon is enabled" - } - } - if builder.errorMsg != "" { - return builder + return builder + } } addonConfig := &istiov2.PrometheusAddonConfig{ @@ -340,7 +346,7 @@ func PullControlPlane(apiClient *clients.Settings, name, nsname string) (*Contro return nil, err } - builder := ControlPlaneBuilder{ + builder := &ControlPlaneBuilder{ apiClient: apiClient.Client, Definition: &istiov2.ServiceMeshControlPlane{ ObjectMeta: metav1.ObjectMeta{ @@ -362,17 +368,13 @@ func PullControlPlane(apiClient *clients.Settings, name, nsname string) (*Contro return nil, fmt.Errorf("serviceMeshControlPlane 'nsname' cannot be empty") } - if builder.errorMsg != "" { - return &builder, nil - } - if !builder.Exists() { return nil, fmt.Errorf("serviceMeshControlPlane object %s does not exist in namespace %s", name, nsname) } builder.Definition = builder.Object - return &builder, nil + return builder, nil } // Get fetches existing serviceMeshControlPlane from cluster. @@ -514,13 +516,13 @@ func (builder *ControlPlaneBuilder) 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 != "" { diff --git a/pkg/servicemesh/memberroll.go b/pkg/servicemesh/memberroll.go index 29db04f25..1d65f6177 100644 --- a/pkg/servicemesh/memberroll.go +++ b/pkg/servicemesh/memberroll.go @@ -63,12 +63,16 @@ func NewMemberRollBuilder(apiClient *clients.Settings, name, nsname string) *Mem glog.V(100).Infof("The name of the serviceMeshMemberRoll is empty") builder.errorMsg = "serviceMeshMemberRoll 'name' cannot be empty" + + return builder } if nsname == "" { glog.V(100).Infof("The namespace of the serviceMeshMemberRoll is empty") builder.errorMsg = "serviceMeshMemberRoll 'nsname' cannot be empty" + + return builder } return builder @@ -92,7 +96,7 @@ func PullMemberRoll(apiClient *clients.Settings, name, nsname string) (*MemberRo return nil, err } - builder := MemberRollBuilder{ + builder := &MemberRollBuilder{ apiClient: apiClient.Client, Definition: &istiov1.ServiceMeshMemberRoll{ ObjectMeta: metav1.ObjectMeta{ @@ -120,7 +124,7 @@ func PullMemberRoll(apiClient *clients.Settings, name, nsname string) (*MemberRo builder.Definition = builder.Object - return &builder, nil + return builder, nil } // Get fetches existing serviceMeshMemberRoll from cluster. @@ -254,9 +258,7 @@ func (builder *MemberRollBuilder) WithMembersList(membersList []string) *MemberR glog.V(100).Infof("Cannot add empty membersList to the memberRoll structure") builder.errorMsg = "can not modify memberRoll config with empty membersList" - } - if builder.errorMsg != "" { return builder } @@ -331,13 +333,13 @@ func (builder *MemberRollBuilder) 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 != "" {