diff --git a/pkg/argocd/applications.go b/pkg/argocd/applications.go index 92317cd05..6e7e8bd98 100644 --- a/pkg/argocd/applications.go +++ b/pkg/argocd/applications.go @@ -44,7 +44,7 @@ func PullApplication(apiClient *clients.Settings, name, nsname string) (*Applica return nil, fmt.Errorf("application 'apiClient' cannot be empty") } - builder := ApplicationBuilder{ + builder := &ApplicationBuilder{ apiClient: apiClient, Definition: &argocdtypes.Application{ ObjectMeta: metav1.ObjectMeta{ @@ -72,7 +72,7 @@ func PullApplication(apiClient *clients.Settings, name, nsname string) (*Applica builder.Definition = builder.Object - return &builder, nil + return builder, nil } // Exists checks whether the given argocd application exists. @@ -237,18 +237,24 @@ func (builder *ApplicationBuilder) WithGitDetails(gitRepo, gitBranch, gitPath st glog.V(100).Infof("The 'gitRepo' of the argocd application is empty") builder.errorMsg = "'gitRepo' parameter is empty" + + return builder } if gitBranch == "" { glog.V(100).Infof("The 'gitBranch' of the argocd application is empty") builder.errorMsg = "'gitBranch' parameter is empty" + + return builder } if gitPath == "" { glog.V(100).Infof("The 'gitPath' of the argocd application is empty") builder.errorMsg = "'gitPath' parameter is empty" + + return builder } glog.V(100).Infof( @@ -257,10 +263,6 @@ func (builder *ApplicationBuilder) WithGitDetails(gitRepo, gitBranch, gitPath st gitRepo, gitBranch, gitPath, ) - if builder.errorMsg != "" { - return builder - } - builder.Definition.Spec.Source.RepoURL = gitRepo builder.Definition.Spec.Source.TargetRevision = gitBranch builder.Definition.Spec.Source.Path = gitPath @@ -289,13 +291,13 @@ func (builder *ApplicationBuilder) 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 != "" { @@ -320,5 +322,5 @@ func (builder *ApplicationBuilder) convertToStructured( return nil, err } - return application, err + return application, nil } diff --git a/pkg/argocd/argocd.go b/pkg/argocd/argocd.go index 47c2926f5..1e68548d2 100644 --- a/pkg/argocd/argocd.go +++ b/pkg/argocd/argocd.go @@ -28,7 +28,7 @@ type Builder struct { // NewBuilder creates a new instance of Builder. func NewBuilder(apiClient *clients.Settings, name, nsname string) *Builder { - builder := Builder{ + builder := &Builder{ apiClient: apiClient.Client, Definition: &argocdoperatorv1alpha1.ArgoCD{ Spec: argocdoperatorv1alpha1.ArgoCDSpec{}, @@ -43,15 +43,19 @@ func NewBuilder(apiClient *clients.Settings, name, nsname string) *Builder { glog.V(100).Infof("The name of the argocd is empty") builder.errorMsg = "argocd 'name' cannot be empty" + + return builder } if nsname == "" { glog.V(100).Infof("The namespace of the argocd is empty") builder.errorMsg = "argocd 'nsname' cannot be empty" + + return builder } - return &builder + return builder } // Pull pulls existing argocd from cluster. @@ -228,13 +232,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 != "" { diff --git a/pkg/assisted/agent.go b/pkg/assisted/agent.go index acb9aad5f..0e0c866e0 100644 --- a/pkg/assisted/agent.go +++ b/pkg/assisted/agent.go @@ -17,7 +17,7 @@ import ( ) const ( - nonExistentMsg = "Cannot update non-existent agent" + nonExistentMsg = "cannot update non-existent agent" ) // agentBuilder provides struct for the agent object containing connection to @@ -74,13 +74,13 @@ func PullAgent(apiClient *clients.Settings, name, nsname string) (*agentBuilder, if name == "" { glog.V(100).Infof("The name of the agent is empty") - builder.errorMsg = "agent 'name' cannot be empty" + return nil, fmt.Errorf("agent 'name' cannot be empty") } if nsname == "" { glog.V(100).Infof("The namespace of the agent is empty") - builder.errorMsg = "agent 'namespace' cannot be empty" + return nil, fmt.Errorf("agent 'namespace' cannot be empty") } if !builder.Exists() { @@ -106,9 +106,7 @@ func (builder *agentBuilder) WithHostName(hostname string) *agentBuilder { builder.Definition.Name, builder.Definition.Namespace) builder.errorMsg = nonExistentMsg - } - if builder.errorMsg != "" { return builder } @@ -131,9 +129,7 @@ func (builder *agentBuilder) WithRole(role string) *agentBuilder { builder.Definition.Name, builder.Definition.Namespace) builder.errorMsg = nonExistentMsg - } - if builder.errorMsg != "" { return builder } @@ -304,11 +300,7 @@ func (builder *agentBuilder) Update() (*agentBuilder, error) { glog.V(100).Infof("agent %s in namespace %s does not exist", builder.Definition.Name, builder.Definition.Namespace) - builder.errorMsg = nonExistentMsg - } - - if builder.errorMsg != "" { - return nil, fmt.Errorf(builder.errorMsg) + return builder, fmt.Errorf(nonExistentMsg) } err := builder.apiClient.Update(context.TODO(), builder.Definition) @@ -372,13 +364,13 @@ func (builder *agentBuilder) 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/assisted/agentclusterinstall.go b/pkg/assisted/agentclusterinstall.go index 725dad2e2..4015e207f 100644 --- a/pkg/assisted/agentclusterinstall.go +++ b/pkg/assisted/agentclusterinstall.go @@ -50,7 +50,7 @@ func NewAgentClusterInstallBuilder( return nil } - builder := AgentClusterInstallBuilder{ + builder := &AgentClusterInstallBuilder{ apiClient: apiClient.Client, Definition: &hiveextV1Beta1.AgentClusterInstall{ ObjectMeta: metav1.ObjectMeta{ @@ -74,21 +74,27 @@ func NewAgentClusterInstallBuilder( glog.V(100).Infof("The name of the agentclusterinstall is empty") builder.errorMsg = "agentclusterinstall 'name' cannot be empty" + + return builder } if nsname == "" { glog.V(100).Infof("The namespace of the agentclusterinstall is empty") builder.errorMsg = "agentclusterinstall 'namespace' cannot be empty" + + return builder } if clusterDeployment == "" { glog.V(100).Infof("The clusterDeployment ref for the agentclusterinstall is empty") builder.errorMsg = "agentclusterinstall 'clusterDeployment' cannot be empty" + + return builder } - return &builder + return builder } // WithAPIVip sets the apiVIP to use during multi-node installations. @@ -101,9 +107,7 @@ func (builder *AgentClusterInstallBuilder) WithAPIVip(apiVIP string) *AgentClust glog.V(100).Infof("The apiVIP is not a properly formatted IP address") builder.errorMsg = "agentclusterinstall apiVIP incorrectly formatted" - } - if builder.errorMsg != "" { return builder } @@ -122,9 +126,7 @@ func (builder *AgentClusterInstallBuilder) WithAdditionalAPIVip(apiVIP string) * glog.V(100).Infof("The apiVIP is not a properly formatted IP address") builder.errorMsg = "agentclusterinstall apiVIP incorrectly formatted" - } - if builder.errorMsg != "" { return builder } @@ -143,9 +145,7 @@ func (builder *AgentClusterInstallBuilder) WithIngressVip(ingressVIP string) *Ag glog.V(100).Infof("The ingressVIP is not a properly formatted IP address") builder.errorMsg = "agentclusterinstall ingressVIP incorrectly formatted" - } - if builder.errorMsg != "" { return builder } @@ -164,9 +164,7 @@ func (builder *AgentClusterInstallBuilder) WithAdditionalIngressVip(ingressVIP s glog.V(100).Infof("The ingressVIP is not a properly formatted IP address") builder.errorMsg = "agentclusterinstall ingressVIP incorrectly formatted" - } - if builder.errorMsg != "" { return builder } @@ -265,9 +263,7 @@ func (builder *AgentClusterInstallBuilder) WithAdditionalClusterNetwork( glog.V(100).Infof("The agentclusterinstall passed invalid clusterNetwork cidr: %s", cidr) builder.errorMsg = "Got invalid cidr for clusternetwork" - } - if builder.errorMsg != "" { return builder } @@ -288,9 +284,7 @@ func (builder *AgentClusterInstallBuilder) WithAdditionalServiceNetwork(cidr str glog.V(100).Infof("The agentclusterinstall passed invalid serviceNetwork cidr: %s", cidr) builder.errorMsg = "Got invalid cidr for servicenetwork" - } - if builder.errorMsg != "" { return builder } @@ -512,13 +506,13 @@ func PullAgentClusterInstall(apiClient *clients.Settings, name, nsname string) ( if name == "" { glog.V(100).Infof("The name of the agentclusterinstall is empty") - builder.errorMsg = "agentclusterinstall 'name' cannot be empty" + return nil, fmt.Errorf("agentclusterinstall 'name' cannot be empty") } if nsname == "" { glog.V(100).Infof("The namespace of the agentclusterinstall is empty") - builder.errorMsg = "agentclusterinstall 'namespace' cannot be empty" + return nil, fmt.Errorf("agentclusterinstall 'namespace' cannot be empty") } if !builder.Exists() { @@ -560,11 +554,7 @@ func (builder *AgentClusterInstallBuilder) Update(force bool) (*AgentClusterInst builder.Definition.Name, builder.Definition.Namespace) if !builder.Exists() { - builder.errorMsg = "Cannot update non-existent agentclusterinstall" - } - - if builder.errorMsg != "" { - return nil, fmt.Errorf(builder.errorMsg) + return nil, fmt.Errorf("cannot update non-existent agentclusterinstall") } err := builder.apiClient.Update(context.TODO(), builder.Definition) @@ -576,7 +566,6 @@ func (builder *AgentClusterInstallBuilder) Update(force bool) (*AgentClusterInst err = builder.DeleteAndWait(time.Second * 10) builder.Definition.ResourceVersion = "" - // fmt.Printf("agentclusterinstall exists: %v\n", builder.Exists()) if err != nil { glog.V(100).Infof( @@ -710,13 +699,13 @@ func (builder *AgentClusterInstallBuilder) 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/assisted/agentserviceconfig.go b/pkg/assisted/agentserviceconfig.go index 9f2b1a9dc..db787fef8 100644 --- a/pkg/assisted/agentserviceconfig.go +++ b/pkg/assisted/agentserviceconfig.go @@ -80,7 +80,7 @@ func NewDefaultAgentServiceConfigBuilder(apiClient *clients.Settings) *AgentServ return nil } - builder := AgentServiceConfigBuilder{ + builder := &AgentServiceConfigBuilder{ apiClient: apiClient.Client, Definition: &agentInstallV1Beta1.AgentServiceConfig{ ObjectMeta: metav1.ObjectMeta{ @@ -95,6 +95,8 @@ func NewDefaultAgentServiceConfigBuilder(apiClient *clients.Settings) *AgentServ glog.V(100).Infof("The ImageStorage size is in wrong format") builder.errorMsg = fmt.Sprintf("error retrieving the storage size: %v", err) + + return builder } builder.Definition.Spec.ImageStorage = &imageStorageSpec @@ -104,6 +106,8 @@ func NewDefaultAgentServiceConfigBuilder(apiClient *clients.Settings) *AgentServ glog.V(100).Infof("The DatabaseStorage size is in wrong format") builder.errorMsg = fmt.Sprintf("error retrieving the storage size: %v", err) + + return builder } builder.Definition.Spec.DatabaseStorage = databaseStorageSpec @@ -113,11 +117,13 @@ func NewDefaultAgentServiceConfigBuilder(apiClient *clients.Settings) *AgentServ glog.V(100).Infof("The FileSystemStorage size is in wrong format") builder.errorMsg = fmt.Sprintf("error retrieving the storage size: %v", err) + + return builder } builder.Definition.Spec.FileSystemStorage = fileSystemStorageSpec - return &builder + return builder } // WithImageStorage sets the imageStorageSpec used by the agentserviceconfig. @@ -146,9 +152,7 @@ func (builder *AgentServiceConfigBuilder) WithMirrorRegistryRef(configMapName st glog.V(100).Infof("The configMapName is empty") builder.errorMsg = "cannot add agentserviceconfig mirrorRegistryRef with empty configmap name" - } - if builder.errorMsg != "" { return builder } @@ -235,17 +239,13 @@ func (builder *AgentServiceConfigBuilder) WaitUntilDeployed(timeout time.Duratio if builder.Definition == nil { glog.V(100).Infof("The agentserviceconfig is undefined") - builder.errorMsg = msg.UndefinedCrdObjectErrString("AgentServiceConfig") + return builder, fmt.Errorf(msg.UndefinedCrdObjectErrString("AgentServiceConfig")) } if !builder.Exists() { glog.V(100).Infof("The agentserviceconfig does not exist on the cluster") - builder.errorMsg = "cannot wait for non-existent agentserviceconfig to be deployed" - } - - if builder.errorMsg != "" { - return builder, fmt.Errorf(builder.errorMsg) + return builder, fmt.Errorf("cannot wait for non-existent agentserviceconfig to be deployed") } // Polls every retryInterval to determine if agentserviceconfig is in desired state. @@ -292,7 +292,7 @@ func PullAgentServiceConfig(apiClient *clients.Settings) (*AgentServiceConfigBui return nil, fmt.Errorf("the apiClient is nil") } - builder := AgentServiceConfigBuilder{ + builder := &AgentServiceConfigBuilder{ apiClient: apiClient.Client, Definition: &agentInstallV1Beta1.AgentServiceConfig{ ObjectMeta: metav1.ObjectMeta{ @@ -307,7 +307,7 @@ func PullAgentServiceConfig(apiClient *clients.Settings) (*AgentServiceConfigBui builder.Definition = builder.Object - return &builder, nil + return builder, nil } // Get fetches the defined agentserviceconfig from the cluster. @@ -365,11 +365,7 @@ func (builder *AgentServiceConfigBuilder) Update(force bool) (*AgentServiceConfi glog.V(100).Infof("agentserviceconfig %s does not exist", builder.Definition.Name) - builder.errorMsg = "Cannot update non-existent agentserviceconfig" - } - - if builder.errorMsg != "" { - return nil, fmt.Errorf(builder.errorMsg) + return nil, fmt.Errorf("cannot update non-existent agentserviceconfig") } err := builder.apiClient.Update(context.TODO(), builder.Definition) @@ -505,13 +501,13 @@ func (builder *AgentServiceConfigBuilder) 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/assisted/infraenv.go b/pkg/assisted/infraenv.go index 203f5c916..4e0280973 100644 --- a/pkg/assisted/infraenv.go +++ b/pkg/assisted/infraenv.go @@ -51,7 +51,7 @@ func NewInfraEnvBuilder(apiClient *clients.Settings, name, nsname, psName string return nil } - builder := InfraEnvBuilder{ + builder := &InfraEnvBuilder{ apiClient: apiClient.Client, Definition: &agentInstallV1Beta1.InfraEnv{ ObjectMeta: metav1.ObjectMeta{ @@ -70,21 +70,27 @@ func NewInfraEnvBuilder(apiClient *clients.Settings, name, nsname, psName string glog.V(100).Infof("The name of the infraenv is empty") builder.errorMsg = "infraenv 'name' cannot be empty" + + return builder } if nsname == "" { glog.V(100).Infof("The namespace of the infraenv is empty") builder.errorMsg = "infraenv 'namespace' cannot be empty" + + return builder } if psName == "" { glog.V(100).Infof("The pull-secret ref of the infraenv is empty") builder.errorMsg = "infraenv 'pull-secret' cannot be empty" + + return builder } - return &builder + return builder } // WithClusterRef sets the cluster reference to be used by the infraenv. @@ -99,15 +105,15 @@ func (builder *InfraEnvBuilder) WithClusterRef(name, nsname string) *InfraEnvBui glog.V(100).Infof("The name of the infraenv clusterRef is empty") builder.errorMsg = "infraenv clusterRef 'name' cannot be empty" + + return builder } if nsname == "" { glog.V(100).Infof("The namespace of the infraenv clusterRef is empty") builder.errorMsg = "infraenv clusterRef 'namespace' cannot be empty" - } - if builder.errorMsg != "" { return builder } @@ -733,13 +739,13 @@ func PullInfraEnvInstall(apiClient *clients.Settings, name, nsname string) (*Inf if name == "" { glog.V(100).Infof("The name of the infraenv is empty") - builder.errorMsg = "infraenv 'name' cannot be empty" + return nil, fmt.Errorf("infraenv 'name' cannot be empty") } if nsname == "" { glog.V(100).Infof("The namespace of the infraenv is empty") - builder.errorMsg = "infraenv 'namespace' cannot be empty" + return nil, fmt.Errorf("infraenv 'namespace' cannot be empty") } if !builder.Exists() { @@ -784,11 +790,7 @@ func (builder *InfraEnvBuilder) Update(force bool) (*InfraEnvBuilder, error) { glog.V(100).Infof("infraenv %s in namespace %s does not exist", builder.Definition.Name, builder.Definition.Namespace) - builder.errorMsg = "Cannot update non-existent infraenv" - } - - if builder.errorMsg != "" { - return nil, fmt.Errorf(builder.errorMsg) + return nil, fmt.Errorf("cannot update non-existent infraenv") } err := builder.apiClient.Update(context.TODO(), builder.Definition) @@ -901,13 +903,13 @@ func (builder *InfraEnvBuilder) 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/assisted/nmstateconfig.go b/pkg/assisted/nmstateconfig.go index ae488613c..ee5b7d0bb 100644 --- a/pkg/assisted/nmstateconfig.go +++ b/pkg/assisted/nmstateconfig.go @@ -37,7 +37,7 @@ func NewNmStateConfigBuilder(apiClient *clients.Settings, name, namespace string return nil } - builder := NmStateConfigBuilder{ + builder := &NmStateConfigBuilder{ apiClient: apiClient.Client, Definition: &assistedv1beta1.NMStateConfig{ ObjectMeta: metav1.ObjectMeta{ @@ -51,15 +51,19 @@ func NewNmStateConfigBuilder(apiClient *clients.Settings, name, namespace string glog.V(100).Infof("The name of the nmstateconfig is empty") builder.errorMsg = "nmstateconfig 'name' cannot be empty" + + return builder } if namespace == "" { glog.V(100).Infof("The namespace of the nmstateconfig is empty") builder.errorMsg = "nmstateconfig namespace's name is empty" + + return builder } - return &builder + return builder } // Exists checks whether the given NMStateConfig exists. @@ -228,13 +232,13 @@ func (builder *NmStateConfigBuilder) 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/bmh/baremetalhost.go b/pkg/bmh/baremetalhost.go index 9675ec74e..76cb5fc51 100644 --- a/pkg/bmh/baremetalhost.go +++ b/pkg/bmh/baremetalhost.go @@ -33,8 +33,14 @@ type AdditionalOptions func(builder *BmhBuilder) (*BmhBuilder, error) // NewBuilder creates a new instance of BmhBuilder. func NewBuilder( - apiClient *clients.Settings, name, nsname, bmcAddress, bmcSecretName, bootMacAddress, bootMode string) *BmhBuilder { - builder := BmhBuilder{ + apiClient *clients.Settings, + name string, + nsname string, + bmcAddress string, + bmcSecretName string, + bootMacAddress string, + bootMode string) *BmhBuilder { + builder := &BmhBuilder{ apiClient: apiClient.Client, Definition: &bmhv1alpha1.BareMetalHost{ Spec: bmhv1alpha1.BareMetalHostSpec{ @@ -60,36 +66,48 @@ func NewBuilder( glog.V(100).Infof("The name of the baremetalhost is empty") builder.errorMsg = "BMH 'name' cannot be empty" + + return builder } if nsname == "" { glog.V(100).Infof("The namespace of the baremetalhost is empty") builder.errorMsg = "BMH 'nsname' cannot be empty" + + return builder } if bmcAddress == "" { glog.V(100).Infof("The bootmacaddress of the baremetalhost is empty") builder.errorMsg = "BMH 'bmcAddress' cannot be empty" + + return builder } if bmcSecretName == "" { glog.V(100).Infof("The bmcsecret of the baremetalhost is empty") builder.errorMsg = "BMH 'bmcSecretName' cannot be empty" + + return builder } bootModeAcceptable := []string{"UEFI", "UEFISecureBoot", "legacy"} if !slices.Contains(bootModeAcceptable, bootMode) { builder.errorMsg = "not acceptable 'bootMode' value" + + return builder } if bootMacAddress == "" { builder.errorMsg = "BMH 'bootMacAddress' cannot be empty" + + return builder } - return &builder + return builder } // WithRootDeviceDeviceName sets rootDeviceHints DeviceName to specified value. @@ -102,9 +120,7 @@ func (builder *BmhBuilder) WithRootDeviceDeviceName(deviceName string) *BmhBuild glog.V(100).Infof("The baremetalhost rootDeviceHint deviceName is empty") builder.errorMsg = "the baremetalhost rootDeviceHint deviceName cannot be empty" - } - if builder.errorMsg != "" { return builder } @@ -127,9 +143,7 @@ func (builder *BmhBuilder) WithRootDeviceHTCL(hctl string) *BmhBuilder { glog.V(100).Infof("The baremetalhost rootDeviceHint hctl is empty") builder.errorMsg = "the baremetalhost rootDeviceHint hctl cannot be empty" - } - if builder.errorMsg != "" { return builder } @@ -152,9 +166,7 @@ func (builder *BmhBuilder) WithRootDeviceModel(model string) *BmhBuilder { glog.V(100).Infof("The baremetalhost rootDeviceHint model is empty") builder.errorMsg = "the baremetalhost rootDeviceHint model cannot be empty" - } - if builder.errorMsg != "" { return builder } @@ -177,9 +189,7 @@ func (builder *BmhBuilder) WithRootDeviceVendor(vendor string) *BmhBuilder { glog.V(100).Infof("The baremetalhost rootDeviceHint vendor is empty") builder.errorMsg = "the baremetalhost rootDeviceHint vendor cannot be empty" - } - if builder.errorMsg != "" { return builder } @@ -202,9 +212,7 @@ func (builder *BmhBuilder) WithRootDeviceSerialNumber(serialNumber string) *BmhB glog.V(100).Infof("The baremetalhost rootDeviceHint serialNumber is empty") builder.errorMsg = "the baremetalhost rootDeviceHint serialNumber cannot be empty" - } - if builder.errorMsg != "" { return builder } @@ -227,9 +235,7 @@ func (builder *BmhBuilder) WithRootDeviceMinSizeGigabytes(size int) *BmhBuilder glog.V(100).Infof("The baremetalhost rootDeviceHint size is less than 0") builder.errorMsg = "the baremetalhost rootDeviceHint size cannot be less than 0" - } - if builder.errorMsg != "" { return builder } @@ -252,9 +258,7 @@ func (builder *BmhBuilder) WithRootDeviceWWN(wwn string) *BmhBuilder { glog.V(100).Infof("The baremetalhost rootDeviceHint wwn is empty") builder.errorMsg = "the baremetalhost rootDeviceHint wwn cannot be empty" - } - if builder.errorMsg != "" { return builder } @@ -277,9 +281,7 @@ func (builder *BmhBuilder) WithRootDeviceWWNWithExtension(wwnWithExtension strin glog.V(100).Infof("The baremetalhost rootDeviceHint wwnWithExtension is empty") builder.errorMsg = "the baremetalhost rootDeviceHint wwnWithExtension cannot be empty" - } - if builder.errorMsg != "" { return builder } @@ -302,9 +304,7 @@ func (builder *BmhBuilder) WithRootDeviceWWNVendorExtension(wwnVendorExtension s glog.V(100).Infof("The baremetalhost rootDeviceHint wwnVendorExtension is empty") builder.errorMsg = "the baremetalhost rootDeviceHint wwnVendorExtension cannot be empty" - } - if builder.errorMsg != "" { return builder } @@ -367,7 +367,7 @@ func Pull(apiClient *clients.Settings, name, nsname string) (*BmhBuilder, error) return nil, fmt.Errorf("baremetalhost 'apiClient' cannot be empty") } - builder := BmhBuilder{ + builder := &BmhBuilder{ apiClient: apiClient.Client, Definition: &bmhv1alpha1.BareMetalHost{ ObjectMeta: metav1.ObjectMeta{ @@ -395,7 +395,7 @@ func Pull(apiClient *clients.Settings, name, nsname string) (*BmhBuilder, error) builder.Definition = builder.Object - return &builder, nil + return builder, nil } // Create makes a bmh in the cluster and stores the created object in struct. @@ -643,13 +643,13 @@ func (builder *BmhBuilder) 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/cgu/cgu.go b/pkg/cgu/cgu.go index 13fb2ce99..12b9ceed6 100644 --- a/pkg/cgu/cgu.go +++ b/pkg/cgu/cgu.go @@ -39,7 +39,14 @@ func NewCguBuilder(apiClient *clients.Settings, name, nsname string, maxConcurre "Initializing new CGU structure with the following params: name: %s, nsname: %s, maxConcurrency: %d", name, nsname, maxConcurrency) + if apiClient == nil { + glog.V(100).Infof("The apiClient cannot be nil") + + return nil + } + builder := &CguBuilder{ + apiClient: apiClient.ClientCgu, Definition: &v1alpha1.ClusterGroupUpgrade{ ObjectMeta: metav1.ObjectMeta{ Name: name, @@ -352,13 +359,13 @@ func (builder *CguBuilder) 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/cgu/cgu_test.go b/pkg/cgu/cgu_test.go index 9fa6c400a..c126341f7 100644 --- a/pkg/cgu/cgu_test.go +++ b/pkg/cgu/cgu_test.go @@ -175,8 +175,15 @@ func TestNewCguBuilder(t *testing.T) { testCase.cguNamespace, testCase.cguMaxConcurrency) - assert.NotNil(t, testCguStructure) - assert.Equal(t, testCguStructure.errorMsg, testCase.expectedErrorText) + if testCase.expectedErrorText == "" { + assert.NotNil(t, testCguStructure) + } + + if testCase.client { + assert.Equal(t, testCguStructure.errorMsg, testCase.expectedErrorText) + } else { + assert.Nil(t, testCguStructure) + } } } diff --git a/pkg/clusterlogging/clusterlogforwarder.go b/pkg/clusterlogging/clusterlogforwarder.go index 823607564..c46d290c0 100644 --- a/pkg/clusterlogging/clusterlogforwarder.go +++ b/pkg/clusterlogging/clusterlogforwarder.go @@ -46,12 +46,16 @@ func NewClusterLogForwarderBuilder( glog.V(100).Infof("The name of the clusterlogforwarder is empty") builder.errorMsg = "clusterlogforwarder 'name' cannot be empty" + + return builder } if nsname == "" { glog.V(100).Infof("The namespace of the clusterlogforwarder is empty") builder.errorMsg = "clusterlogforwarder 'nsname' cannot be empty" + + return builder } return builder @@ -70,9 +74,7 @@ func (builder *ClusterLogForwarderBuilder) WithOutput(outputSpec *clov1.OutputSp glog.V(100).Infof("The 'outputSpec' of the deployment is empty") builder.errorMsg = "'outputSpec' parameter is empty" - } - if builder.errorMsg != "" { return builder } @@ -98,9 +100,7 @@ func (builder *ClusterLogForwarderBuilder) WithPipeline(pipelineSpec *clov1.Pipe glog.V(100).Infof("The 'pipelineSpec' of the deployment is empty") builder.errorMsg = "'pipelineSpec' parameter is empty" - } - if builder.errorMsg != "" { return builder } diff --git a/pkg/clusterlogging/clusterlogging.go b/pkg/clusterlogging/clusterlogging.go index f7b2ff6d4..76fde12fa 100644 --- a/pkg/clusterlogging/clusterlogging.go +++ b/pkg/clusterlogging/clusterlogging.go @@ -45,12 +45,16 @@ func NewBuilder( glog.V(100).Infof("The name of the clusterLogging is empty") builder.errorMsg = "the clusterLogging 'name' cannot be empty" + + return builder } if nsname == "" { glog.V(100).Infof("The namespace of the clusterLogging is empty") builder.errorMsg = "the clusterLogging 'nsname' cannot be empty" + + return builder } return builder @@ -229,13 +233,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 != "" { diff --git a/pkg/clusteroperator/clusteroperator.go b/pkg/clusteroperator/clusteroperator.go index 75dfac45d..7b45fb52b 100644 --- a/pkg/clusteroperator/clusteroperator.go +++ b/pkg/clusteroperator/clusteroperator.go @@ -237,13 +237,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 != "" { diff --git a/pkg/console/console.go b/pkg/console/console.go index 6ee3d6c87..c30b4f021 100644 --- a/pkg/console/console.go +++ b/pkg/console/console.go @@ -29,7 +29,7 @@ type Builder struct { func NewBuilder(apiClient *clients.Settings, name string) *Builder { glog.V(100).Info("Initializing new console %s structure", name) - builder := Builder{ + builder := &Builder{ apiClient: apiClient, Definition: &v1.Console{ ObjectMeta: metav1.ObjectMeta{ @@ -42,14 +42,16 @@ func NewBuilder(apiClient *clients.Settings, name string) *Builder { glog.V(100).Info("The name of the Console is empty") builder.errorMsg = "console 'name' cannot be empty" + + return builder } - return &builder + return builder } // Pull loads an existing console into the Builder struct. func Pull(apiClient *clients.Settings, name string) (*Builder, error) { - builder := Builder{ + builder := &Builder{ apiClient: apiClient, Definition: &v1.Console{ ObjectMeta: metav1.ObjectMeta{ @@ -61,7 +63,7 @@ func Pull(apiClient *clients.Settings, name string) (*Builder, error) { if name == "" { glog.V(100).Info("The name of the Console is empty") - builder.errorMsg = "console 'name' cannot be empty" + return nil, fmt.Errorf("console 'name' cannot be empty") } glog.V(100).Infof("Pulling cluster console %s", name) @@ -72,7 +74,7 @@ func Pull(apiClient *clients.Settings, name string) (*Builder, error) { builder.Definition = builder.Object - return &builder, nil + return builder, nil } // Create makes a console in cluster and stores the created object in struct. diff --git a/pkg/daemonset/daemonset.go b/pkg/daemonset/daemonset.go index 5212af32f..8c5c2c1f9 100644 --- a/pkg/daemonset/daemonset.go +++ b/pkg/daemonset/daemonset.go @@ -40,7 +40,7 @@ func NewBuilder( "name: %s, namespace: %s, labels: %s, containerSpec %v", name, nsname, labels, containerSpec) - builder := Builder{ + builder := &Builder{ apiClient: apiClient, Definition: &appsv1.DaemonSet{ Spec: appsv1.DaemonSetSpec{ @@ -66,21 +66,27 @@ func NewBuilder( glog.V(100).Infof("The name of the daemonset is empty") builder.errorMsg = "daemonset 'name' cannot be empty" + + return builder } if nsname == "" { glog.V(100).Infof("The namespace of the daemonset is empty") builder.errorMsg = "daemonset 'namespace' cannot be empty" + + return builder } if len(labels) == 0 { glog.V(100).Infof("There are no labels for the daemonset") builder.errorMsg = "daemonset 'labels' cannot be empty" + + return builder } - return &builder + return builder } // Pull loads an existing daemonSet into the Builder struct. @@ -98,11 +104,15 @@ func Pull(apiClient *clients.Settings, name, nsname string) (*Builder, error) { } if name == "" { - builder.errorMsg = "daemonset 'name' cannot be empty" + glog.V(100).Infof("The name of the daemonset is empty") + + return nil, fmt.Errorf("daemonset 'name' cannot be empty") } if nsname == "" { - builder.errorMsg = "daemonset 'namespace' cannot be empty" + glog.V(100).Infof("The namespace of the daemonset is empty") + + return nil, fmt.Errorf("daemonset 'namespace' cannot be empty") } if !builder.Exists() { @@ -127,9 +137,7 @@ func (builder *Builder) WithNodeSelector(selector map[string]string) *Builder { glog.V(100).Infof("The nodeselector is empty") builder.errorMsg = "cannot accept empty map as nodeselector" - } - if builder.errorMsg != "" { return builder } @@ -189,9 +197,7 @@ func (builder *Builder) WithAdditionalContainerSpecs(specs []corev1.Container) * glog.V(100).Infof("The container specs are empty") builder.errorMsg = "cannot accept empty list as container specs" - } - if builder.errorMsg != "" { return builder } @@ -425,13 +431,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 != "" { diff --git a/pkg/deployment/deployment.go b/pkg/deployment/deployment.go index 12844e330..2f5f9c6c9 100644 --- a/pkg/deployment/deployment.go +++ b/pkg/deployment/deployment.go @@ -43,7 +43,7 @@ func NewBuilder( "name: %s, namespace: %s, labels: %s, containerSpec %v", name, nsname, labels, containerSpec) - builder := Builder{ + builder := &Builder{ apiClient: apiClient.AppsV1Interface, Definition: &appsv1.Deployment{ Spec: appsv1.DeploymentSpec{ @@ -69,21 +69,27 @@ func NewBuilder( glog.V(100).Infof("The name of the deployment is empty") builder.errorMsg = "deployment 'name' cannot be empty" + + return builder } if nsname == "" { glog.V(100).Infof("The namespace of the deployment is empty") builder.errorMsg = "deployment 'namespace' cannot be empty" + + return builder } if len(labels) == 0 { glog.V(100).Infof("There are no labels for the deployment") builder.errorMsg = "deployment 'labels' cannot be empty" + + return builder } - return &builder + return builder } // Pull loads an existing deployment into Builder struct. @@ -97,7 +103,7 @@ func Pull(apiClient *clients.Settings, name, nsname string) (*Builder, error) { glog.V(100).Infof("Pulling existing deployment name: %s under namespace: %s", name, nsname) - builder := Builder{ + builder := &Builder{ apiClient: apiClient.AppsV1Interface, Definition: &appsv1.Deployment{ ObjectMeta: metav1.ObjectMeta{ @@ -125,7 +131,7 @@ func Pull(apiClient *clients.Settings, name, nsname string) (*Builder, error) { builder.Definition = builder.Object - return &builder, nil + return builder, nil } // WithNodeSelector applies a nodeSelector to the deployment definition. diff --git a/pkg/events/events.go b/pkg/events/events.go index 853a0e7d1..5500d08c9 100644 --- a/pkg/events/events.go +++ b/pkg/events/events.go @@ -90,7 +90,7 @@ func (builder *Builder) validate() (bool, error) { 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/hive/clusterdeployment.go b/pkg/hive/clusterdeployment.go index ce7b7cfde..b5471bfc1 100644 --- a/pkg/hive/clusterdeployment.go +++ b/pkg/hive/clusterdeployment.go @@ -43,7 +43,7 @@ func NewABMClusterDeploymentBuilder( clusterName: %s, baseDomain: %s, clusterInstallRef: %s, agentSelector: %s`, name, nsname, clusterName, baseDomain, clusterInstallRef, agentSelector) - builder := ClusterDeploymentBuilder{ + builder := &ClusterDeploymentBuilder{ apiClient: apiClient, Definition: &hiveV1.ClusterDeployment{ ObjectMeta: metav1.ObjectMeta{ @@ -72,33 +72,43 @@ func NewABMClusterDeploymentBuilder( glog.V(100).Infof("The name of the clusterdeployment is empty") builder.errorMsg = "clusterdeployment 'name' cannot be empty" + + return builder } if nsname == "" { glog.V(100).Infof("The namespace of the clusterdeployment is empty") builder.errorMsg = "clusterdeployment 'namespace' cannot be empty" + + return builder } if clusterName == "" { glog.V(100).Infof("The clusterName of the clusterdeployment is empty") builder.errorMsg = "clusterdeployment 'clusterName' cannot be empty" + + return builder } if baseDomain == "" { glog.V(100).Infof("The baseDomain of the clusterdeployment is empty") builder.errorMsg = "clusterdeployment 'baseDomain' cannot be empty" + + return builder } if clusterInstallRef == "" { glog.V(100).Infof("The clusterInstallRef of the clusterdeployment is empty") builder.errorMsg = "clusterdeployment 'clusterInstallRef' cannot be empty" + + return builder } - return &builder + return builder } // WithAdditionalAgentSelectorLabels inserts additional labels @@ -117,15 +127,15 @@ func (builder *ClusterDeploymentBuilder) WithAdditionalAgentSelectorLabels( glog.V(100).Infof("The clusterdeployment platform is not agentBareMetal") builder.errorMsg = "clusterdeployment type must be AgentBareMetal to use agentSelector" + + return builder } if len(agentSelector) == 0 { glog.V(100).Infof("The clusterdeployment agentSelector is empty") builder.errorMsg = "agentSelector cannot be empty" - } - if builder.errorMsg != "" { return builder } @@ -194,13 +204,13 @@ func PullClusterDeployment(apiClient *clients.Settings, name, nsname string) (*C if name == "" { glog.V(100).Infof("The name of the clusterdeployment is empty") - builder.errorMsg = "clusterdeployment 'name' cannot be empty" + return nil, fmt.Errorf("clusterdeployment object name cannot be empty") } if nsname == "" { glog.V(100).Infof("The namespace of the clusterdeployment is empty") - builder.errorMsg = "clusterdeployment 'namespace' cannot be empty" + return nil, fmt.Errorf("clusterdeployment 'namespace' cannot be empty") } if !builder.Exists() { @@ -347,13 +357,13 @@ func (builder *ClusterDeploymentBuilder) 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/hive/clusterimageset.go b/pkg/hive/clusterimageset.go index d48894b29..c9672968e 100644 --- a/pkg/hive/clusterimageset.go +++ b/pkg/hive/clusterimageset.go @@ -31,7 +31,7 @@ func NewClusterImageSetBuilder(apiClient *clients.Settings, name, releaseImage s `Initializing new clusterimageset structure with the following params: name: %s, releaseImage: %s`, name, releaseImage) - builder := ClusterImageSetBuilder{ + builder := &ClusterImageSetBuilder{ apiClient: apiClient, Definition: &hiveV1.ClusterImageSet{ ObjectMeta: metav1.ObjectMeta{ @@ -47,21 +47,27 @@ func NewClusterImageSetBuilder(apiClient *clients.Settings, name, releaseImage s glog.V(100).Infof("The apiClient is nil") builder.errorMsg = "clusterimageset cannot have nil apiClient" + + return builder } if name == "" { glog.V(100).Infof("The name of the clusterimageset is empty") builder.errorMsg = "clusterimageset 'name' cannot be empty" + + return builder } if releaseImage == "" { glog.V(100).Infof("The releaseImage of the clusterimageset is empty") builder.errorMsg = "clusterimageset 'releaseImage' cannot be empty" + + return builder } - return &builder + return builder } // WithReleaseImage sets the releaseImage for the clusterimageset. @@ -77,9 +83,7 @@ func (builder *ClusterImageSetBuilder) WithReleaseImage(image string) *ClusterIm glog.V(100).Infof("The clusterimageset releaseImage is empty") builder.errorMsg = "cannot set releaseImage to empty string" - } - if builder.errorMsg != "" { return builder } @@ -118,7 +122,7 @@ func (builder *ClusterImageSetBuilder) WithOptions( func PullClusterImageSet(apiClient *clients.Settings, name string) (*ClusterImageSetBuilder, error) { glog.V(100).Infof("Pulling existing clusterimageset name: %s", name) - builder := ClusterImageSetBuilder{ + builder := &ClusterImageSetBuilder{ apiClient: apiClient, Definition: &hiveV1.ClusterImageSet{ ObjectMeta: metav1.ObjectMeta{ @@ -128,7 +132,9 @@ func PullClusterImageSet(apiClient *clients.Settings, name string) (*ClusterImag } if name == "" { - builder.errorMsg = "clusterimageset 'name' cannot be empty" + glog.V(100).Infof("The name of the clusterimageset is empty") + + return nil, fmt.Errorf("clusterimageset 'name' cannot be empty") } if !builder.Exists() { @@ -137,7 +143,7 @@ func PullClusterImageSet(apiClient *clients.Settings, name string) (*ClusterImag builder.Definition = builder.Object - return &builder, nil + return builder, nil } // Get fetches the defined clusterimageset from the cluster. @@ -267,13 +273,13 @@ func (builder *ClusterImageSetBuilder) 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/hive/hiveconfig.go b/pkg/hive/hiveconfig.go index de40b42d5..24ca0ca22 100644 --- a/pkg/hive/hiveconfig.go +++ b/pkg/hive/hiveconfig.go @@ -30,7 +30,7 @@ func NewHiveConfigBuilder(apiClient *clients.Settings, name string) *HiveConfigB glog.V(100).Infof( `Initializing new HiveConfig structure with the following params: name: %s`, name) - builder := HiveConfigBuilder{ + builder := &HiveConfigBuilder{ apiClient: apiClient.Client, Definition: &hiveV1.HiveConfig{ ObjectMeta: metav1.ObjectMeta{ @@ -44,9 +44,11 @@ func NewHiveConfigBuilder(apiClient *clients.Settings, name string) *HiveConfigB glog.V(100).Infof("The name of the HiveConfig is empty") builder.errorMsg = "hiveconfig 'name' cannot be empty" + + return builder } - return &builder + return builder } // WithOptions creates ClusterDeployment with generic mutation options. @@ -89,7 +91,9 @@ func PullHiveConfig(apiClient *clients.Settings, name string) (*HiveConfigBuilde } if name == "" { - builder.errorMsg = "hiveconfig 'name' cannot be empty" + glog.V(100).Infof("The name of the HiveConfig is empty") + + return nil, fmt.Errorf("hiveconfig 'name' cannot be empty") } if !builder.Exists() { @@ -188,13 +192,13 @@ func (builder *HiveConfigBuilder) 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/icsp/icsp.go b/pkg/icsp/icsp.go index f6cbc4037..d0c4f2c4b 100644 --- a/pkg/icsp/icsp.go +++ b/pkg/icsp/icsp.go @@ -55,18 +55,24 @@ func NewICSPBuilder(apiClient *clients.Settings, name, source string, mirrors [] glog.V(100).Infof("The name of the ImageContentSourcePolicy is empty") icspBuilder.errorMsg = "ImageContentSourcePolicy 'name' cannot be empty" + + return icspBuilder } if source == "" { glog.V(100).Infof("The Source of the ImageContentSourcePolicy is empty") icspBuilder.errorMsg = "ImageContentSourcePolicy 'source' cannot be empty" + + return icspBuilder } if len(mirrors) == 0 { glog.V(100).Infof("The mirrors of the ImageContentSourcePolicy are empty") icspBuilder.errorMsg = "ImageContentSourcePolicy 'mirrors' cannot be empty" + + return icspBuilder } return icspBuilder @@ -92,7 +98,7 @@ func (builder *ICSPBuilder) Exists() bool { func Pull(apiClient *clients.Settings, name string) (*ICSPBuilder, error) { glog.V(100).Infof("Pulling existing ImageContentSourcePolicy: %s", name) - builder := ICSPBuilder{ + builder := &ICSPBuilder{ apiClient: apiClient, Definition: &v1alpha1.ImageContentSourcePolicy{ ObjectMeta: metav1.ObjectMeta{ @@ -102,7 +108,9 @@ func Pull(apiClient *clients.Settings, name string) (*ICSPBuilder, error) { } if name == "" { - builder.errorMsg = "ImageContentSourcePolicy 'name' cannot be empty" + glog.V(100).Infof("The name of the ImageContentSourcePolicy is empty") + + return builder, fmt.Errorf("ImageContentSourcePolicy 'name' cannot be empty") } if !builder.Exists() { @@ -111,7 +119,7 @@ func Pull(apiClient *clients.Settings, name string) (*ICSPBuilder, error) { builder.Definition = builder.Object - return &builder, nil + return builder, nil } // Create makes a ImageContentSourcePolicy in the cluster and stores the created object in struct. @@ -180,15 +188,15 @@ func (builder *ICSPBuilder) WithRepositoryDigestMirror(source string, mirrors [] glog.V(100).Infof("The source is empty") builder.errorMsg = "'source' cannot be empty" + + return builder } if len(mirrors) == 0 { glog.V(100).Infof("Mirrors is empty") builder.errorMsg = "'mirrors' cannot be empty" - } - if builder.errorMsg != "" { return builder } @@ -237,13 +245,13 @@ func (builder *ICSPBuilder) 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/kmm/containers.go b/pkg/kmm/containers.go index 063762b6d..b847643ad 100644 --- a/pkg/kmm/containers.go +++ b/pkg/kmm/containers.go @@ -40,6 +40,8 @@ func NewModLoaderContainerBuilder(modName string) *ModuleLoaderContainerBuilder glog.V(100).Infof("The modName of the NewModLoaderContainerBuilder is empty") builder.errorMsg = "'modName' cannot be empty" + + return builder } return builder @@ -196,13 +198,7 @@ func (builder *ModuleLoaderContainerBuilder) validate() (bool, error) { if builder.definition == nil { glog.V(100).Infof("The %s is undefined", resourceCRD) - builder.errorMsg = msg.UndefinedCrdObjectErrString(resourceCRD) - } - - if builder.errorMsg != "" { - glog.V(100).Infof("The %s builder has error message: %s", resourceCRD, builder.errorMsg) - - return false, fmt.Errorf(builder.errorMsg) + return false, fmt.Errorf(msg.UndefinedCrdObjectErrString(resourceCRD)) } return true, nil @@ -222,7 +218,7 @@ func NewDevicePluginContainerBuilder(image string) *DevicePluginContainerBuilder glog.V(100).Infof( "Initializing new DevPluginContainerBuilder structure with the following params: %s", image) - builder := DevicePluginContainerBuilder{ + builder := &DevicePluginContainerBuilder{ definition: &moduleV1Beta1.DevicePluginContainerSpec{ Image: image, }, @@ -232,9 +228,11 @@ func NewDevicePluginContainerBuilder(image string) *DevicePluginContainerBuilder glog.V(100).Infof("The image of NewDevicePluginContainerBuilder is empty") builder.errorMsg = "invalid parameter 'image' cannot be empty" + + return builder } - return &builder + return builder } // WithEnv adds specific env to DevicePlugin Container. @@ -250,15 +248,15 @@ func (builder *DevicePluginContainerBuilder) WithEnv(name, value string) *Device glog.V(100).Infof("The name of WithEnv is empty") builder.errorMsg = "'name' can not be empty for DevicePlugin Env" + + return builder } if value == "" { glog.V(100).Infof("The value of WithEnv is empty") builder.errorMsg = "'value' can not be empty for DevicePlugin Env" - } - if builder.errorMsg != "" { return builder } @@ -281,15 +279,15 @@ func (builder *DevicePluginContainerBuilder) WithVolumeMount(mountPath, name str glog.V(100).Infof("The name of WithVolumeMount is empty") builder.errorMsg = "'name' can not be empty for DevicePlugin mountPath" + + return builder } if mountPath == "" { glog.V(100).Infof("The mountPath of WithVolumeMount is empty") builder.errorMsg = "'mountPath' can not be empty for DevicePlugin mountPath" - } - if builder.errorMsg != "" { return builder } @@ -323,7 +321,7 @@ func (builder *DevicePluginContainerBuilder) validate() (bool, error) { if builder.definition == nil { glog.V(100).Infof("The %s is undefined", strings.ToLower(resourceCRD)) - builder.errorMsg = msg.UndefinedCrdObjectErrString(resourceCRD) + return false, fmt.Errorf(msg.UndefinedCrdObjectErrString(resourceCRD)) } if builder.errorMsg != "" { diff --git a/pkg/kmm/kernelmapping.go b/pkg/kmm/kernelmapping.go index 3ee47a326..e53fa9128 100644 --- a/pkg/kmm/kernelmapping.go +++ b/pkg/kmm/kernelmapping.go @@ -26,7 +26,7 @@ func NewRegExKernelMappingBuilder(regex string) *KernelMappingBuilder { glog.V(100).Infof( "Initializing new regex KernelMapping parameter structure with the following regex param: %s", regex) - builder := KernelMappingBuilder{ + builder := &KernelMappingBuilder{ definition: &moduleV1Beta1.KernelMapping{ Regexp: regex, }, @@ -36,9 +36,11 @@ func NewRegExKernelMappingBuilder(regex string) *KernelMappingBuilder { glog.V(100).Infof("The regex of NewRegExKernelMappingBuilder is empty") builder.errorMsg = "'regex' parameter can not be empty" + + return builder } - return &builder + return builder } // NewLiteralKernelMappingBuilder create new kernel mapping element based on literal. @@ -46,7 +48,7 @@ func NewLiteralKernelMappingBuilder(literal string) *KernelMappingBuilder { glog.V(100).Infof( "Initializing new literal KernelMapping parameter structure with following literal param: %s", literal) - builder := KernelMappingBuilder{ + builder := &KernelMappingBuilder{ definition: &moduleV1Beta1.KernelMapping{ Literal: literal, }, @@ -56,9 +58,11 @@ func NewLiteralKernelMappingBuilder(literal string) *KernelMappingBuilder { glog.V(100).Infof("The literal of NewLiteralKernelMappingBuilder is empty") builder.errorMsg = "'literal' parameter can not be empty" + + return builder } - return &builder + return builder } // BuildKernelMappingConfig returns kernel mapping config if error is not occur. @@ -86,9 +90,7 @@ func (builder *KernelMappingBuilder) WithContainerImage(image string) *KernelMap glog.V(100).Infof("The image of WithContainerImage is empty") builder.errorMsg = "'image' parameter can not be empty for KernelMapping" - } - if builder.errorMsg != "" { return builder } @@ -110,15 +112,15 @@ func (builder *KernelMappingBuilder) WithBuildArg(argName, argValue string) *Ker glog.V(100).Infof("The argName of WithBuildArg is empty") builder.errorMsg = "'argName' parameter can not be empty for KernelMapping BuildArg" + + return builder } if argValue == "" { glog.V(100).Infof("The argValue of WithBuildArg is empty") builder.errorMsg = "'argValue' parameter can not be empty for KernelMapping BuildArg" - } - if builder.errorMsg != "" { return builder } @@ -143,9 +145,7 @@ func (builder *KernelMappingBuilder) WithBuildSecret(secret string) *KernelMappi glog.V(100).Infof("The secret of WithBuildSecret is empty") builder.errorMsg = "'secret' parameter can not be empty for KernelMapping Secret" - } - if builder.errorMsg != "" { return builder } @@ -186,9 +186,7 @@ func (builder *KernelMappingBuilder) WithBuildDockerCfgFile(name string) *Kernel glog.V(100).Infof("The name of WithBuildDockerCfgFile is empty") builder.errorMsg = "'name' parameter can not be empty for KernelMapping Docker file" - } - if builder.errorMsg != "" { return builder } @@ -212,21 +210,23 @@ func (builder *KernelMappingBuilder) WithSign(certSecret, keySecret string, file glog.V(100).Infof("The certSecret of WithSign is empty") builder.errorMsg = "'certSecret' parameter can not be empty for KernelMapping Sign" + + return builder } if keySecret == "" { glog.V(100).Infof("The keySecret of WithSign is empty") builder.errorMsg = "'keySecret' parameter can not be empty for KernelMapping Sign" + + return builder } if len(fileToSign) < 1 { glog.V(100).Infof("The fileToSign of WithSign is empty") builder.errorMsg = "'fileToSign' parameter can not be empty for KernelMapping Sign" - } - if builder.errorMsg != "" { return builder } @@ -322,7 +322,7 @@ func (builder *KernelMappingBuilder) 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.errorMsg != "" { diff --git a/pkg/kmm/managedclustermodule.go b/pkg/kmm/managedclustermodule.go index 07b3d3154..7b94916ff 100644 --- a/pkg/kmm/managedclustermodule.go +++ b/pkg/kmm/managedclustermodule.go @@ -32,7 +32,7 @@ func NewManagedClusterModuleBuilder(apiClient *clients.Settings, name, nsname st glog.V(100).Infof( "Initializing new ManagedClusterModule structure with following params: %s, %s", name, nsname) - builder := ManagedClusterModuleBuilder{ + builder := &ManagedClusterModuleBuilder{ apiClient: apiClient, Definition: &mcmV1Beta1.ManagedClusterModule{ ObjectMeta: metav1.ObjectMeta{ @@ -46,15 +46,19 @@ func NewManagedClusterModuleBuilder(apiClient *clients.Settings, name, nsname st glog.V(100).Infof("The name of the ManagedClusterModule is empty") builder.errorMsg = "ManagedClusterModule 'name' cannot be empty" + + return builder } if nsname == "" { glog.V(100).Infof("The namespace of the ManagedClusterModule is empty") builder.errorMsg = "ManagedClusterModule 'nsname' cannot be empty" + + return builder } - return &builder + return builder } // WithModuleSpec sets the ModuleSpec. @@ -78,9 +82,7 @@ func (builder *ManagedClusterModuleBuilder) WithSpokeNamespace( if spokeNamespace == "" { builder.errorMsg = "invalid 'spokeNamespace' argument cannot be nil" - } - if builder.errorMsg != "" { return builder } @@ -98,9 +100,7 @@ func (builder *ManagedClusterModuleBuilder) WithSelector( if len(selector) == 0 { builder.errorMsg = "invalid 'selector' argument cannot be empty" - } - if builder.errorMsg != "" { return builder } @@ -139,7 +139,7 @@ func (builder *ManagedClusterModuleBuilder) WithOptions( func PullManagedClusterModule(apiClient *clients.Settings, name, nsname string) (*ManagedClusterModuleBuilder, error) { glog.V(100).Infof("Pulling existing module name %s under namespace %s from cluster", name, nsname) - builder := ManagedClusterModuleBuilder{ + builder := &ManagedClusterModuleBuilder{ apiClient: apiClient, Definition: &mcmV1Beta1.ManagedClusterModule{ ObjectMeta: metav1.ObjectMeta{ @@ -152,13 +152,13 @@ func PullManagedClusterModule(apiClient *clients.Settings, name, nsname string) if name == "" { glog.V(100).Infof("The name of the managedclustermodule is empty") - builder.errorMsg = "managedclustermodule 'name' cannot be empty" + return nil, fmt.Errorf("managedclustermodule 'name' cannot be empty") } if nsname == "" { glog.V(100).Infof("The namespace of the managedclustermodule is empty") - builder.errorMsg = "managedclustermodule 'namespace' cannot be empty" + return nil, fmt.Errorf("managedclustermodule 'namespace' cannot be empty") } if !builder.Exists() { @@ -167,7 +167,7 @@ func PullManagedClusterModule(apiClient *clients.Settings, name, nsname string) builder.Definition = builder.Object - return &builder, nil + return builder, nil } // Create builds managedclustermodule in the cluster and stores object in struct. @@ -286,13 +286,13 @@ func (builder *ManagedClusterModuleBuilder) 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/kmm/module.go b/pkg/kmm/module.go index 089c9002a..7b24deca4 100644 --- a/pkg/kmm/module.go +++ b/pkg/kmm/module.go @@ -36,7 +36,7 @@ func NewModuleBuilder( glog.V(100).Infof( "Initializing new Module structure with following params: %s, %s", name, nsname) - builder := ModuleBuilder{ + builder := &ModuleBuilder{ apiClient: apiClient, Definition: &moduleV1Beta1.Module{ ObjectMeta: metav1.ObjectMeta{ @@ -50,15 +50,19 @@ func NewModuleBuilder( glog.V(100).Infof("The name of the Module is empty") builder.errorMsg = "Module 'name' cannot be empty" + + return builder } if nsname == "" { glog.V(100).Infof("The namespace of the module is empty") builder.errorMsg = "Module 'namespace' cannot be empty" + + return builder } - return &builder + return builder } // WithNodeSelector adds the specified NodeSelector to the Module. @@ -75,9 +79,7 @@ func (builder *ModuleBuilder) WithNodeSelector(nodeSelector map[string]string) * glog.V(100).Infof("Can not redefine Module with empty nodeSelector map") builder.errorMsg = "Module 'nodeSelector' cannot be empty map" - } - if builder.errorMsg != "" { return builder } @@ -120,9 +122,7 @@ func (builder *ModuleBuilder) WithImageRepoSecret(imageRepoSecret string) *Modul if imageRepoSecret == "" { builder.errorMsg = "can not redefine module with empty imageRepoSecret" - } - if builder.errorMsg != "" { return builder } @@ -139,13 +139,13 @@ func (builder *ModuleBuilder) WithDevicePluginVolume(name string, configMapName if name == "" { builder.errorMsg = "cannot redefine with empty volume 'name'" + + return builder } if configMapName == "" { builder.errorMsg = "cannot redefine with empty 'configMapName'" - } - if builder.errorMsg != "" { return builder } @@ -175,9 +175,7 @@ func (builder *ModuleBuilder) WithModuleLoaderContainer( if container == nil { builder.errorMsg = "invalid 'container' argument can not be nil" - } - if builder.errorMsg != "" { return builder } @@ -195,9 +193,7 @@ func (builder *ModuleBuilder) WithDevicePluginContainer( if container == nil { builder.errorMsg = "invalid 'container' argument can not be nil" - } - if builder.errorMsg != "" { return builder } @@ -251,7 +247,7 @@ func (builder *ModuleBuilder) WithOptions(options ...ModuleAdditionalOptions) *M func Pull(apiClient *clients.Settings, name, nsname string) (*ModuleBuilder, error) { glog.V(100).Infof("Pulling existing module name %s under namespace %s from cluster", name, nsname) - builder := ModuleBuilder{ + builder := &ModuleBuilder{ apiClient: apiClient, Definition: &moduleV1Beta1.Module{ ObjectMeta: metav1.ObjectMeta{ @@ -264,13 +260,13 @@ func Pull(apiClient *clients.Settings, name, nsname string) (*ModuleBuilder, err if name == "" { glog.V(100).Infof("The name of the module is empty") - builder.errorMsg = "module 'name' cannot be empty" + return nil, fmt.Errorf("module 'name' cannot be empty") } if nsname == "" { glog.V(100).Infof("The namespace of the module is empty") - builder.errorMsg = "module 'namespace' cannot be empty" + return nil, fmt.Errorf("module 'namespace' cannot be empty") } if !builder.Exists() { @@ -279,7 +275,7 @@ func Pull(apiClient *clients.Settings, name, nsname string) (*ModuleBuilder, err builder.Definition = builder.Object - return &builder, nil + return builder, nil } // Create builds module in the cluster and stores object in struct. @@ -384,9 +380,7 @@ func (builder *ModuleBuilder) withServiceAccount(srvAccountName string, accountT if srvAccountName == "" { builder.errorMsg = "can not redefine module with empty ServiceAccount" - } - if builder.errorMsg != "" { return builder } @@ -401,6 +395,8 @@ func (builder *ModuleBuilder) withServiceAccount(srvAccountName string, accountT builder.Definition.Spec.DevicePlugin.ServiceAccountName = srvAccountName default: builder.errorMsg = "invalid account type parameter. Supported parameters are: 'module', 'device'" + + return builder } return builder @@ -420,13 +416,13 @@ func (builder *ModuleBuilder) 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/kmm/preflightvalidationocp.go b/pkg/kmm/preflightvalidationocp.go index f690c13e4..4cd3e129b 100644 --- a/pkg/kmm/preflightvalidationocp.go +++ b/pkg/kmm/preflightvalidationocp.go @@ -36,7 +36,7 @@ func NewPreflightValidationOCPBuilder( glog.V(100).Infof("Initializing new PreflightValidationOCP structure with following params: %s, %s", name, nsname) - builder := PreflightValidationOCPBuilder{ + builder := &PreflightValidationOCPBuilder{ apiClient: apiClient, Definition: &moduleV1Beta1.PreflightValidationOCP{ ObjectMeta: metav1.ObjectMeta{ @@ -50,15 +50,19 @@ func NewPreflightValidationOCPBuilder( glog.V(100).Infof("The name of the PreflightValidationOCP is empty") builder.errorMsg = "PreflightValidationOCP 'name' cannot be empty" + + return builder } if nsname == "" { glog.V(100).Infof("The namespace of the PreflightValidationOCP is empty") builder.errorMsg = "PreflightValidationOCP 'nsname' cannot be empty" + + return builder } - return &builder + return builder } // WithReleaseImage sets the image for which the preflightvalidationocp checks the module. @@ -152,17 +156,17 @@ func PullPreflightValidationOCP(apiClient *clients.Settings, if name == "" { glog.V(100).Infof("The name of the preflightvalidationocp is empty") - builder.errorMsg = "preflightvalidationocp 'name' cannot be empty" + return nil, fmt.Errorf("preflightvalidationocp 'name' cannot be empty") } if nsname == "" { glog.V(100).Infof("The namespace of the preflightvalidationocp is empty") - builder.errorMsg = "preflightvalidationocp 'nsname' cannot be empty" + return nil, fmt.Errorf("preflightvalidationocp 'nsname' cannot be empty") } if !builder.Exists() { - return nil, fmt.Errorf("preflightvalidationocp object %s doens't exist in namespace %s", + return nil, fmt.Errorf("preflightvalidationocp object %s doesn't exist in namespace %s", name, nsname) } @@ -278,13 +282,13 @@ func (builder *PreflightValidationOCPBuilder) 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/lca/imagebasedupgrade.go b/pkg/lca/imagebasedupgrade.go index 7518a6628..0bba98068 100644 --- a/pkg/lca/imagebasedupgrade.go +++ b/pkg/lca/imagebasedupgrade.go @@ -82,7 +82,7 @@ func PullImageBasedUpgrade(apiClient *clients.Settings) (*ImageBasedUpgradeBuild return nil, fmt.Errorf("the apiClient is nil") } - builder := ImageBasedUpgradeBuilder{ + builder := &ImageBasedUpgradeBuilder{ apiClient: apiClient.Client, Definition: &lcav1.ImageBasedUpgrade{ ObjectMeta: metav1.ObjectMeta{ @@ -97,7 +97,7 @@ func PullImageBasedUpgrade(apiClient *clients.Settings) (*ImageBasedUpgradeBuild builder.Definition = builder.Object - return &builder, nil + return builder, nil } // Update modifies the imagebasedupgrade resource on the cluster @@ -114,11 +114,7 @@ func (builder *ImageBasedUpgradeBuilder) Update() (*ImageBasedUpgradeBuilder, er glog.V(100).Infof("imagebasedupgrade %s does not exist", builder.Definition.Name) - builder.errorMsg = "Unable to update non-existing imagebasedupgrade" - } - - if builder.errorMsg != "" { - return nil, fmt.Errorf(builder.errorMsg) + return nil, fmt.Errorf("unable to update non-existing imagebasedupgrade") } err := builder.apiClient.Update(context.TODO(), builder.Definition) @@ -461,13 +457,13 @@ func (builder *ImageBasedUpgradeBuilder) 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/lca/seedgenerator.go b/pkg/lca/seedgenerator.go index 8135f59a2..505250e32 100644 --- a/pkg/lca/seedgenerator.go +++ b/pkg/lca/seedgenerator.go @@ -46,7 +46,7 @@ func NewSeedGeneratorBuilder( return nil } - builder := SeedGeneratorBuilder{ + builder := &SeedGeneratorBuilder{ apiClient: apiClient.Client, Definition: &lcasgv1.SeedGenerator{ ObjectMeta: metav1.ObjectMeta{ @@ -59,9 +59,11 @@ func NewSeedGeneratorBuilder( glog.V(100).Infof("The name of the seedgenerator must be " + seedImageName) builder.errorMsg = "SeedGenerator name must be " + seedImageName + + return builder } - return &builder + return builder } // WithOptions creates seedgenerator with generic mutation options. @@ -131,7 +133,7 @@ func PullSeedGenerator(apiClient *clients.Settings, name string) (*SeedGenerator if name == "" { glog.V(100).Infof("The name of the seedgenerator is empty") - builder.errorMsg = "seedgenerator 'name' cannot be empty" + return nil, fmt.Errorf("seedgenerator 'name' cannot be empty") } if !builder.Exists() { @@ -288,13 +290,13 @@ func (builder *SeedGeneratorBuilder) 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/lso/localvolumediscovery.go b/pkg/lso/localvolumediscovery.go index 3edf71f55..9b37752ff 100644 --- a/pkg/lso/localvolumediscovery.go +++ b/pkg/lso/localvolumediscovery.go @@ -33,7 +33,7 @@ func NewLocalVolumeDiscoveryBuilder(apiClient *clients.Settings, name, nsname st glog.V(100).Infof("Initializing new localVolumeDiscovery structure with the following params: name: "+ "%s, namespace: %s", name, nsname) - builder := LocalVolumeDiscoveryBuilder{ + builder := &LocalVolumeDiscoveryBuilder{ apiClient: apiClient, Definition: &lsoV1alpha1.LocalVolumeDiscovery{ ObjectMeta: metav1.ObjectMeta{ @@ -47,15 +47,19 @@ func NewLocalVolumeDiscoveryBuilder(apiClient *clients.Settings, name, nsname st glog.V(100).Infof("The name of the localVolumeDiscovery is empty") builder.errorMsg = "localVolumeDiscovery 'name' cannot be empty" + + return builder } if nsname == "" { glog.V(100).Infof("The nsname of the localVolumeDiscovery is empty") builder.errorMsg = "localVolumeDiscovery 'nsname' cannot be empty" + + return builder } - return &builder + return builder } // PullLocalVolumeDiscovery retrieves an existing localVolumeDiscovery object from the cluster. @@ -76,13 +80,13 @@ func PullLocalVolumeDiscovery(apiClient *clients.Settings, name, nsname string) if name == "" { glog.V(100).Infof("The name of the localVolumeDiscovery is empty") - builder.errorMsg = "localVolumeDiscovery 'name' cannot be empty" + return nil, fmt.Errorf("localVolumeDiscovery 'name' cannot be empty") } if nsname == "" { glog.V(100).Infof("The namespace of the localVolumeDiscovery is empty") - builder.errorMsg = "localVolumeDiscovery 'nsname' cannot be empty" + return nil, fmt.Errorf("localVolumeDiscovery 'nsname' cannot be empty") } if !builder.Exists() { diff --git a/pkg/lso/localvolumeset.go b/pkg/lso/localvolumeset.go index 91eb9ae57..a23e12dff 100644 --- a/pkg/lso/localvolumeset.go +++ b/pkg/lso/localvolumeset.go @@ -31,7 +31,7 @@ type LocalVolumeSetBuilder struct { func NewLocalVolumeSetBuilder(apiClient *clients.Settings, name, nsname string) *LocalVolumeSetBuilder { glog.V(100).Infof("Initializing new %s localVolumeSet structure in %s namespace", name, nsname) - builder := LocalVolumeSetBuilder{ + builder := &LocalVolumeSetBuilder{ apiClient: apiClient, Definition: &lsoV1alpha1.LocalVolumeSet{ ObjectMeta: metav1.ObjectMeta{ @@ -45,15 +45,19 @@ func NewLocalVolumeSetBuilder(apiClient *clients.Settings, name, nsname string) glog.V(100).Infof("The name of the localVolumeSet is empty") builder.errorMsg = "localVolumeSet 'name' cannot be empty" + + return builder } if nsname == "" { glog.V(100).Infof("The nsname of the localVolumeSet is empty") builder.errorMsg = "localVolumeSet 'nsname' cannot be empty" + + return builder } - return &builder + return builder } // PullLocalVolumeSet retrieves an existing localVolumeSet object from the cluster. @@ -61,7 +65,7 @@ func PullLocalVolumeSet(apiClient *clients.Settings, name, nsname string) (*Loca glog.V(100).Infof( "Pulling localVolumeSet object name: %s in namespace: %s", name, nsname) - builder := LocalVolumeSetBuilder{ + builder := &LocalVolumeSetBuilder{ apiClient: apiClient, Definition: &lsoV1alpha1.LocalVolumeSet{ ObjectMeta: metav1.ObjectMeta{ @@ -74,13 +78,13 @@ func PullLocalVolumeSet(apiClient *clients.Settings, name, nsname string) (*Loca if name == "" { glog.V(100).Infof("The name of the localVolumeSet is empty") - builder.errorMsg = "localVolumeSet 'name' cannot be empty" + return nil, fmt.Errorf("localVolumeSet 'name' cannot be empty") } if nsname == "" { glog.V(100).Infof("The namespace of the localVolumeSet is empty") - builder.errorMsg = "localVolumeSet 'nsname' cannot be empty" + return nil, fmt.Errorf("localVolumeSet 'nsname' cannot be empty") } if !builder.Exists() { @@ -89,7 +93,7 @@ func PullLocalVolumeSet(apiClient *clients.Settings, name, nsname string) (*Loca builder.Definition = builder.Object - return &builder, nil + return builder, nil } // Get fetches existing localVolumeSet from cluster. diff --git a/pkg/machine/machineset.go b/pkg/machine/machineset.go index f2a9d07c0..ea3937dd7 100644 --- a/pkg/machine/machineset.go +++ b/pkg/machine/machineset.go @@ -52,7 +52,7 @@ func NewSetBuilderFromCopy( " params: namespace: %s, instanceType: %s, workerLabel: %s, and replicas: %v", nsName, instanceType, workerLabel, replicas) - builder := SetBuilder{ + builder := &SetBuilder{ apiClient: apiClient, } @@ -63,7 +63,7 @@ func NewSetBuilderFromCopy( builder.errorMsg = fmt.Sprintf("Error initializing MachineSet from copy: %s", err.Error()) - return &builder + return builder } builder.Definition = newSetBuilder.Definition @@ -72,6 +72,8 @@ func NewSetBuilderFromCopy( if err != nil { builder.errorMsg = fmt.Sprintf("error getting the public cloud kind: %v", err.Error()) + + return builder } glog.V(100).Infof("Updating copied MachineSet provider instanceType to: %s", instanceType) @@ -80,46 +82,58 @@ func NewSetBuilderFromCopy( if err != nil { builder.errorMsg = fmt.Sprintf("error changing the instanceType: %v", err.Error()) + + return builder } if nsName == "" { glog.V(100).Infof("The Namespace of the MachineSet is empty") builder.errorMsg = "MachineSet 'nsName' cannot be empty" + + return builder } if instanceType == "" { glog.V(100).Infof("The instanceType of the MachineSet is empty") builder.errorMsg = "MachineSet 'instanceType' cannot be empty" + + return builder } if replicas == 0 { glog.V(100).Infof("The replicas of the MachineSet is zero") builder.errorMsg = "MachineSet 'replicas' cannot be zero" + + return builder } if workerLabel == "" { glog.V(100).Infof("The workerLabel of the MachineSet is empty") builder.errorMsg = "MachineSet 'workerLabel' cannot be empty" + + return builder } if builder.Definition == nil { glog.V(100).Infof("The MachineSet object definition is nil") builder.errorMsg = "MachineSet 'Object.Definition' is nil" + + return builder } - return &builder + return builder } // PullSet loads an existing MachineSet into Builder struct. func PullSet(apiClient *clients.Settings, name, namespace string) (*SetBuilder, error) { glog.V(100).Infof("Pulling existing machineSet name %s in namespace %s", name, namespace) - builder := SetBuilder{ + builder := &SetBuilder{ apiClient: apiClient, Definition: &machinev1beta1.MachineSet{ ObjectMeta: metav1.ObjectMeta{ @@ -130,11 +144,15 @@ func PullSet(apiClient *clients.Settings, name, namespace string) (*SetBuilder, } if name == "" { - builder.errorMsg = "MachineSet 'name' cannot be empty" + glog.V(100).Infof("The name of the machineSet is empty") + + return nil, fmt.Errorf("machineSet 'name' cannot be empty") } if namespace == "" { - builder.errorMsg = "MachineSet 'namespace' cannot be empty" + glog.V(100).Infof("The namespace of the machineSet is empty") + + return nil, fmt.Errorf("machineSet 'namespace' cannot be empty") } if !builder.Exists() { @@ -143,7 +161,7 @@ func PullSet(apiClient *clients.Settings, name, namespace string) (*SetBuilder, builder.Definition = builder.Object - return &builder, nil + return builder, nil } // Exists checks whether the given MachineSet exists. @@ -565,13 +583,13 @@ func (builder *SetBuilder) 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/mco/kubeletconfig.go b/pkg/mco/kubeletconfig.go index 92f23a0c3..8601b2df5 100644 --- a/pkg/mco/kubeletconfig.go +++ b/pkg/mco/kubeletconfig.go @@ -36,7 +36,7 @@ type AdditionalOptions func(builder *KubeletConfigBuilder) (*KubeletConfigBuilde func NewKubeletConfigBuilder(apiClient *clients.Settings, name string) *KubeletConfigBuilder { glog.V(100).Infof("Initializing new KubeletConfigBuilder structure with the name: %s", name) - builder := KubeletConfigBuilder{ + builder := &KubeletConfigBuilder{ apiClient: apiClient, Definition: &mcv1.KubeletConfig{ ObjectMeta: metav1.ObjectMeta{ @@ -49,16 +49,18 @@ func NewKubeletConfigBuilder(apiClient *clients.Settings, name string) *KubeletC glog.V(100).Infof("The name of the KubeletConfig is empty") builder.errorMsg = "KubeletConfig 'name' cannot be empty" + + return builder } - return &builder + return builder } // PullKubeletConfig fetches existing kubeletconfig from cluster. func PullKubeletConfig(apiClient *clients.Settings, name string) (*KubeletConfigBuilder, error) { glog.V(100).Infof("Pulling existing kubeletconfig name %s from cluster", name) - builder := KubeletConfigBuilder{ + builder := &KubeletConfigBuilder{ apiClient: apiClient, Definition: &mcv1.KubeletConfig{ ObjectMeta: metav1.ObjectMeta{ @@ -70,7 +72,7 @@ func PullKubeletConfig(apiClient *clients.Settings, name string) (*KubeletConfig if name == "" { glog.V(100).Infof("The name of the kubeletconfig is empty") - builder.errorMsg = "kubeletconfig 'name' cannot be empty" + return nil, fmt.Errorf("kubeletconfig 'name' cannot be empty") } if !builder.Exists() { @@ -79,7 +81,7 @@ func PullKubeletConfig(apiClient *clients.Settings, name string) (*KubeletConfig builder.Definition = builder.Object - return &builder, nil + return builder, nil } // Create generates a kubeletconfig in the cluster and stores the created object in struct. @@ -180,15 +182,15 @@ func (builder *KubeletConfigBuilder) WithSystemReserved(cpu, memory string) *Kub glog.V(100).Infof("The cpu cannot be empty") builder.errorMsg = "'cpu' cannot be empty" + + return builder } if memory == "" { glog.V(100).Infof("The memory cannot be empty") builder.errorMsg = "'memory' cannot be empty" - } - if builder.errorMsg != "" { return builder } @@ -245,13 +247,13 @@ func (builder *KubeletConfigBuilder) 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/mco/machineconfig.go b/pkg/mco/machineconfig.go index a7072dd89..3c8c1b090 100644 --- a/pkg/mco/machineconfig.go +++ b/pkg/mco/machineconfig.go @@ -33,7 +33,7 @@ type MCAdditionalOptions func(builder *MCBuilder) (*MCBuilder, error) func NewMCBuilder(apiClient *clients.Settings, name string) *MCBuilder { glog.V(100).Infof("Initializing new MCBuilder structure with following params: %s", name) - builder := MCBuilder{ + builder := &MCBuilder{ apiClient: apiClient, Definition: &mcv1.MachineConfig{ ObjectMeta: metav1.ObjectMeta{ @@ -46,16 +46,18 @@ func NewMCBuilder(apiClient *clients.Settings, name string) *MCBuilder { glog.V(100).Infof("The name of the MachineConfig is empty") builder.errorMsg = "MachineConfig 'name' cannot be empty" + + return builder } - return &builder + return builder } // PullMachineConfig fetches existing machineconfig from cluster. func PullMachineConfig(apiClient *clients.Settings, name string) (*MCBuilder, error) { glog.V(100).Infof("Pulling existing machineconfig name %s from cluster", name) - builder := MCBuilder{ + builder := &MCBuilder{ apiClient: apiClient, Definition: &mcv1.MachineConfig{ ObjectMeta: metav1.ObjectMeta{ @@ -67,7 +69,7 @@ func PullMachineConfig(apiClient *clients.Settings, name string) (*MCBuilder, er if name == "" { glog.V(100).Infof("The name of the machineconfig is empty") - builder.errorMsg = "machineconfig 'name' cannot be empty" + return nil, fmt.Errorf("machineconfig 'name' cannot be empty") } if !builder.Exists() { @@ -76,7 +78,7 @@ func PullMachineConfig(apiClient *clients.Settings, name string) (*MCBuilder, er builder.Definition = builder.Object - return &builder, nil + return builder, nil } // Create generates a machineconfig in the cluster and stores the created object in struct. @@ -117,7 +119,7 @@ func (builder *MCBuilder) Delete() error { builder.Object = nil - return err + return nil } // Update renovates the existing machineconfig object with machineconfig definition in builder. @@ -288,13 +290,13 @@ func (builder *MCBuilder) 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/mco/mcp.go b/pkg/mco/mcp.go index eda56ffc8..254757d49 100644 --- a/pkg/mco/mcp.go +++ b/pkg/mco/mcp.go @@ -56,6 +56,8 @@ func NewMCPBuilder(apiClient *clients.Settings, mcpName string) *MCPBuilder { glog.V(100).Infof("The name of the MachineConfigPool is empty") builder.errorMsg = "MachineConfigPool 'name' cannot be empty" + + return builder } return builder @@ -65,7 +67,7 @@ func NewMCPBuilder(apiClient *clients.Settings, mcpName string) *MCPBuilder { func Pull(apiClient *clients.Settings, name string) (*MCPBuilder, error) { glog.V(100).Infof("Pulling existing machineconfigpool name %s from cluster", name) - builder := MCPBuilder{ + builder := &MCPBuilder{ apiClient: apiClient, Definition: &mcov1.MachineConfigPool{ ObjectMeta: metav1.ObjectMeta{ @@ -77,7 +79,7 @@ func Pull(apiClient *clients.Settings, name string) (*MCPBuilder, error) { if name == "" { glog.V(100).Infof("The name of the machineconfigpool is empty") - builder.errorMsg = "machineconfigpool 'name' cannot be empty" + return nil, fmt.Errorf("machineconfigpool 'name' cannot be empty") } if !builder.Exists() { @@ -86,7 +88,7 @@ func Pull(apiClient *clients.Settings, name string) (*MCPBuilder, error) { builder.Definition = builder.Object - return &builder, nil + return builder, nil } // Create makes a MachineConfigPool in cluster and stores the created object in struct. @@ -127,7 +129,7 @@ func (builder *MCPBuilder) Delete() error { return fmt.Errorf("cannot delete MachineConfigPool: %w", err) } - return err + return nil } // Exists checks whether the given MachineConfigPool exists. @@ -157,9 +159,7 @@ func (builder *MCPBuilder) WithMcSelector(mcSelector map[string]string) *MCPBuil if len(mcSelector) == 0 { builder.errorMsg = "'machineConfigSelector MatchLabels' field cannot be empty" - } - if builder.errorMsg != "" { return builder } @@ -372,13 +372,13 @@ func (builder *MCPBuilder) 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/metallb/addresspool.go b/pkg/metallb/addresspool.go index db4f54964..79010b61b 100644 --- a/pkg/metallb/addresspool.go +++ b/pkg/metallb/addresspool.go @@ -38,7 +38,7 @@ func NewIPAddressPoolBuilder( "Initializing new IPAddressPool structure with the following params: %s, %s %s", name, nsname, addrPool) - builder := IPAddressPoolBuilder{ + builder := &IPAddressPoolBuilder{ apiClient: apiClient, Definition: &mlbtypes.IPAddressPool{ TypeMeta: metav1.TypeMeta{ @@ -58,21 +58,27 @@ func NewIPAddressPoolBuilder( glog.V(100).Infof("The name of the IPAddressPool is empty") builder.errorMsg = "IPAddressPool 'name' cannot be empty" + + return builder } if nsname == "" { glog.V(100).Infof("The namespace of the IPAddressPool is empty") builder.errorMsg = "IPAddressPool 'nsname' cannot be empty" + + return builder } if len(addrPool) < 1 { glog.V(100).Infof("The addrPool of the IPAddressPool is empty list") builder.errorMsg = "IPAddressPool 'addrPool' cannot be empty list" + + return builder } - return &builder + return builder } // Get returns IPAddressPool object if found. @@ -126,7 +132,7 @@ func PullAddressPool(apiClient *clients.Settings, name, nsname string) (*IPAddre return nil, fmt.Errorf("addresspool 'apiClient' cannot be empty") } - builder := IPAddressPoolBuilder{ + builder := &IPAddressPoolBuilder{ apiClient: apiClient, Definition: &mlbtypes.IPAddressPool{ ObjectMeta: metav1.ObjectMeta{ @@ -154,7 +160,7 @@ func PullAddressPool(apiClient *clients.Settings, name, nsname string) (*IPAddre builder.Definition = builder.Object - return &builder, nil + return builder, nil } // Create makes a IPAddressPool in the cluster and stores the created object in struct. @@ -167,7 +173,6 @@ func (builder *IPAddressPoolBuilder) Create() (*IPAddressPoolBuilder, error) { builder.Definition.Name, builder.Definition.Namespace, ) - var err error if !builder.Exists() { unstructuredIPAddressPool, err := runtime.DefaultUnstructuredConverter.ToUnstructured(builder.Definition) @@ -194,7 +199,7 @@ func (builder *IPAddressPoolBuilder) Create() (*IPAddressPoolBuilder, error) { } } - return builder, err + return builder, nil } // Delete removes IPAddressPool object from a cluster. @@ -309,9 +314,7 @@ func (builder *IPAddressPoolBuilder) WithOptions(options ...IPAddressPoolAdditio glog.V(100).Infof("The IPAddressPool is undefined") builder.errorMsg = msg.UndefinedCrdObjectErrString("IPAddressPool") - } - if builder.errorMsg != "" { return builder } @@ -353,13 +356,13 @@ func (builder *IPAddressPoolBuilder) 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/metallb/bfdprofile.go b/pkg/metallb/bfdprofile.go index fbe4a32d4..abdb98fd6 100644 --- a/pkg/metallb/bfdprofile.go +++ b/pkg/metallb/bfdprofile.go @@ -37,7 +37,7 @@ func NewBFDBuilder(apiClient *clients.Settings, name, nsname string) *BFDBuilder "Initializing new BFDBuilder structure with the following params: %s, %s", name, nsname) - builder := BFDBuilder{ + builder := &BFDBuilder{ apiClient: apiClient, Definition: &mlbtypes.BFDProfile{ TypeMeta: metav1.TypeMeta{ @@ -55,15 +55,19 @@ func NewBFDBuilder(apiClient *clients.Settings, name, nsname string) *BFDBuilder glog.V(100).Infof("The name of the BFDProfile is empty") builder.errorMsg = "BFDProfile 'name' cannot be empty" + + return builder } if nsname == "" { glog.V(100).Infof("The namespace of the BFDProfile is empty") builder.errorMsg = "BFDProfile 'nsname' cannot be empty" + + return builder } - return &builder + return builder } // Get returns BFDProfile object if found. @@ -361,6 +365,8 @@ func (builder *BFDBuilder) withBoolFlagFor(flagName string, flagValue bool) *BFD builder.Definition.Spec.PassiveMode = &flagValue default: builder.errorMsg = "invalid bool flag name parameter" + + return builder } return builder @@ -384,6 +390,8 @@ func (builder *BFDBuilder) withInterval(intervalName string, interval uint32) *B builder.Definition.Spec.EchoInterval = &interval default: builder.errorMsg = "invalid interval parameters" + + return builder } return builder @@ -403,13 +411,13 @@ func (builder *BFDBuilder) 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/metallb/bgpadvertisement.go b/pkg/metallb/bgpadvertisement.go index e45328667..94c90e7f8 100644 --- a/pkg/metallb/bgpadvertisement.go +++ b/pkg/metallb/bgpadvertisement.go @@ -37,7 +37,7 @@ func NewBGPAdvertisementBuilder(apiClient *clients.Settings, name, nsname string "Initializing new BGPAdvertisement structure with the following params: %s, %s", name, nsname) - builder := BGPAdvertisementBuilder{ + builder := &BGPAdvertisementBuilder{ apiClient: apiClient, Definition: &mlbtypes.BGPAdvertisement{ TypeMeta: metav1.TypeMeta{ @@ -55,15 +55,19 @@ func NewBGPAdvertisementBuilder(apiClient *clients.Settings, name, nsname string glog.V(100).Infof("The name of the BGPAdvertisement is empty") builder.errorMsg = "BGPAdvertisement 'name' cannot be empty" + + return builder } if nsname == "" { glog.V(100).Infof("The namespace of the BGPAdvertisement is empty") builder.errorMsg = "BGPAdvertisement 'nsname' cannot be empty" + + return builder } - return &builder + return builder } // Exists checks whether the given BGPAdvertisement exists. @@ -158,7 +162,6 @@ func (builder *BGPAdvertisementBuilder) Create() (*BGPAdvertisementBuilder, erro builder.Definition.Name, builder.Definition.Namespace, ) - var err error if !builder.Exists() { unstructuredBgpAdvertisement, err := runtime.DefaultUnstructuredConverter.ToUnstructured(builder.Definition) @@ -185,7 +188,7 @@ func (builder *BGPAdvertisementBuilder) Create() (*BGPAdvertisementBuilder, erro } } - return builder, err + return builder, nil } // Delete removes BGPAdvertisement object from a cluster. @@ -266,7 +269,7 @@ func (builder *BGPAdvertisementBuilder) Update(force bool) (*BGPAdvertisementBui } } - return builder, err + return builder, nil } // WithAggregationLength4 adds the specified AggregationLength to the BGPAdvertisement. @@ -282,9 +285,7 @@ func (builder *BGPAdvertisementBuilder) WithAggregationLength4(aggregationLength if aggregationLength < 0 || aggregationLength > 32 { builder.errorMsg = fmt.Sprintf("AggregationLength %d is invalid, the value shoud be in range 0...32", aggregationLength) - } - if builder.errorMsg != "" { return builder } @@ -307,9 +308,7 @@ func (builder *BGPAdvertisementBuilder) WithAggregationLength6(aggregationLength fmt.Printf("%d", aggregationLength) builder.errorMsg = fmt.Sprintf("AggregationLength %d is invalid, the value shoud be in range 0...128", aggregationLength) - } - if builder.errorMsg != "" { return builder } @@ -345,9 +344,7 @@ func (builder *BGPAdvertisementBuilder) WithCommunities(communities []string) *B if len(communities) < 1 { builder.errorMsg = "error: community setting is empty list, the list should contain at least one element" - } - if builder.errorMsg != "" { return builder } @@ -368,9 +365,7 @@ func (builder *BGPAdvertisementBuilder) WithIPAddressPools(ipAddressPools []stri if len(ipAddressPools) < 1 { builder.errorMsg = "error: IPAddressPools setting is empty list, the list should contain at least one element" - } - if builder.errorMsg != "" { return builder } @@ -392,9 +387,7 @@ func (builder *BGPAdvertisementBuilder) WithIPAddressPoolsSelectors( if len(poolSelector) < 1 { builder.errorMsg = "error: IPAddressPoolSelectors setting is empty list, the list should contain at least one element" - } - if builder.errorMsg != "" { return builder } @@ -416,9 +409,7 @@ func (builder *BGPAdvertisementBuilder) WithNodeSelector( if len(nodeSelectors) < 1 { builder.errorMsg = "error: nodeSelectors setting is empty list, the list should contain at least one element" - } - if builder.errorMsg != "" { return builder } @@ -439,9 +430,7 @@ func (builder *BGPAdvertisementBuilder) WithPeers(peers []string) *BGPAdvertisem if len(peers) < 1 { builder.errorMsg = "error: peers setting is empty list, the list should contain at least one element" - } - if builder.errorMsg != "" { return builder } @@ -497,13 +486,13 @@ func (builder *BGPAdvertisementBuilder) 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 != "" { @@ -528,5 +517,5 @@ func (builder *BGPAdvertisementBuilder) convertToStructured( return nil, err } - return bgpAdvertisement, err + return bgpAdvertisement, nil } diff --git a/pkg/metallb/bgppeer.go b/pkg/metallb/bgppeer.go index d6e63be78..846b4d33a 100644 --- a/pkg/metallb/bgppeer.go +++ b/pkg/metallb/bgppeer.go @@ -40,7 +40,7 @@ func NewBPGPeerBuilder( "Initializing new BGPPeer structure with the following params: %s, %s %s %d %d", name, nsname, peerIP, asn, remoteASN) - builder := BGPPeerBuilder{ + builder := &BGPPeerBuilder{ apiClient: apiClient, Definition: &mlbtypes.BGPPeer{ TypeMeta: metav1.TypeMeta{ @@ -62,21 +62,27 @@ func NewBPGPeerBuilder( glog.V(100).Infof("The name of the BGPPeer is empty") builder.errorMsg = "BGPPeer 'name' cannot be empty" + + return builder } if nsname == "" { glog.V(100).Infof("The namespace of the BGPPeer is empty") builder.errorMsg = "BGPPeer 'nsname' cannot be empty" + + return builder } if net.ParseIP(peerIP) == nil { glog.V(100).Infof("The peerIP of the BGPPeer contains invalid ip address %s", peerIP) builder.errorMsg = "BGPPeer 'peerIP' of the BGPPeer contains invalid ip address" + + return builder } - return &builder + return builder } // Get returns BGPPeer object if found. @@ -124,7 +130,7 @@ func (builder *BGPPeerBuilder) Exists() bool { func PullBGPPeer(apiClient *clients.Settings, name, nsname string) (*BGPPeerBuilder, error) { glog.V(100).Infof("Pulling existing bgppeer name %s under namespace %s from cluster", name, nsname) - builder := BGPPeerBuilder{ + builder := &BGPPeerBuilder{ apiClient: apiClient, Definition: &mlbtypes.BGPPeer{ ObjectMeta: metav1.ObjectMeta{ @@ -137,13 +143,13 @@ func PullBGPPeer(apiClient *clients.Settings, name, nsname string) (*BGPPeerBuil if name == "" { glog.V(100).Infof("The name of the bgppeer is empty") - builder.errorMsg = "bgppeer 'name' cannot be empty" + return nil, fmt.Errorf("bgppeer object name cannot be empty") } if nsname == "" { glog.V(100).Infof("The namespace of the bgppeer is empty") - builder.errorMsg = "bgppeer 'namespace' cannot be empty" + return nil, fmt.Errorf("bgppeer object namespace cannot be empty") } if !builder.Exists() { @@ -152,7 +158,7 @@ func PullBGPPeer(apiClient *clients.Settings, name, nsname string) (*BGPPeerBuil builder.Definition = builder.Object - return &builder, nil + return builder, nil } // Create makes a BGPPeer in the cluster and stores the created object in struct. @@ -165,7 +171,6 @@ func (builder *BGPPeerBuilder) Create() (*BGPPeerBuilder, error) { builder.Definition.Name, builder.Definition.Namespace, ) - var err error if !builder.Exists() { unstructuredBgpPeer, err := runtime.DefaultUnstructuredConverter.ToUnstructured(builder.Definition) @@ -192,7 +197,7 @@ func (builder *BGPPeerBuilder) Create() (*BGPPeerBuilder, error) { } } - return builder, err + return builder, nil } // Delete removes BGPPeer object from a cluster. @@ -280,9 +285,7 @@ func (builder *BGPPeerBuilder) WithRouterID(routerID string) *BGPPeerBuilder { "routerID should be present in ip address format", routerID) builder.errorMsg = fmt.Sprintf("the routerID of the BGPPeer contains invalid ip address %s", routerID) - } - if builder.errorMsg != "" { return builder } @@ -305,9 +308,7 @@ func (builder *BGPPeerBuilder) WithBFDProfile(bfdProfile string) *BGPPeerBuilder glog.V(100).Infof("The bfdProfile of the BGPPeer can not be empty string") builder.errorMsg = "The bfdProfile is empty string" - } - if builder.errorMsg != "" { return builder } @@ -331,9 +332,7 @@ func (builder *BGPPeerBuilder) WithSRCAddress(srcAddress string) *BGPPeerBuilder "srcAddress should be present in ip address format", srcAddress) builder.errorMsg = fmt.Sprintf("the srcAddress of the BGPPeer contains invalid ip address %s", srcAddress) - } - if builder.errorMsg != "" { return builder } @@ -401,9 +400,7 @@ func (builder *BGPPeerBuilder) WithNodeSelector(nodeSelector map[string]string) glog.V(100).Infof("Can not redefine BGPPeer with empty nodeSelector map") builder.errorMsg = "BGPPeer 'nodeSelector' cannot be empty map" - } - if builder.errorMsg != "" { return builder } @@ -427,9 +424,7 @@ func (builder *BGPPeerBuilder) WithPassword(password string) *BGPPeerBuilder { glog.V(100).Infof("Can not redefine BGPPeer with empty password") builder.errorMsg = "password can not be empty string" - } - if builder.errorMsg != "" { return builder } @@ -499,13 +494,13 @@ func (builder *BGPPeerBuilder) 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 != "" { @@ -529,5 +524,5 @@ func (builder *BGPPeerBuilder) convertToStructured(unsObject *unstructured.Unstr return nil, err } - return bgpPeer, err + return bgpPeer, nil } diff --git a/pkg/metallb/l2advertisement.go b/pkg/metallb/l2advertisement.go index 30d1e7193..e3666201b 100644 --- a/pkg/metallb/l2advertisement.go +++ b/pkg/metallb/l2advertisement.go @@ -43,7 +43,7 @@ func NewL2AdvertisementBuilder(apiClient *clients.Settings, name, nsname string) return nil } - builder := L2AdvertisementBuilder{ + builder := &L2AdvertisementBuilder{ apiClient: apiClient, Definition: &mlbtypes.L2Advertisement{ TypeMeta: metaV1.TypeMeta{ @@ -61,15 +61,19 @@ func NewL2AdvertisementBuilder(apiClient *clients.Settings, name, nsname string) glog.V(100).Infof("The name of the L2Advertisement is empty") builder.errorMsg = "L2Advertisement 'name' cannot be empty" + + return builder } if nsname == "" { glog.V(100).Infof("The namespace of the L2Advertisement is empty") builder.errorMsg = "L2Advertisement 'nsname' cannot be empty" + + return builder } - return &builder + return builder } // PullL2Advertisement pulls existing L2Advertisement from cluster. @@ -82,7 +86,7 @@ func PullL2Advertisement(apiClient *clients.Settings, name, nsname string) (*L2A return nil, fmt.Errorf("l2Advertisement 'apiClient' cannot be empty") } - builder := L2AdvertisementBuilder{ + builder := &L2AdvertisementBuilder{ apiClient: apiClient, Definition: &mlbtypes.L2Advertisement{ ObjectMeta: metaV1.ObjectMeta{ @@ -110,7 +114,7 @@ func PullL2Advertisement(apiClient *clients.Settings, name, nsname string) (*L2A builder.Definition = builder.Object - return &builder, nil + return builder, nil } // Exists checks whether the given L2Advertisement exists. @@ -272,7 +276,7 @@ func (builder *L2AdvertisementBuilder) Update(force bool) (*L2AdvertisementBuild } } - return builder, err + return builder, nil } // WithNodeSelector adds the specified NodeSelectors to the L2Advertisement. @@ -287,9 +291,7 @@ func (builder *L2AdvertisementBuilder) WithNodeSelector(nodeSelectors []metaV1.L if len(nodeSelectors) < 1 { builder.errorMsg = "error: nodeSelectors setting is empty list, the list should contain at least one element" - } - if builder.errorMsg != "" { return builder } @@ -310,9 +312,7 @@ func (builder *L2AdvertisementBuilder) WithIPAddressPools(ipAddressPools []strin if len(ipAddressPools) < 1 { builder.errorMsg = "error: IPAddressPools setting is empty list, the list should contain at least one element" - } - if builder.errorMsg != "" { return builder } @@ -335,9 +335,7 @@ func (builder *L2AdvertisementBuilder) WithIPAddressPoolsSelectors( if len(poolSelector) < 1 { builder.errorMsg = "error: IPAddressPoolSelectors setting is empty list, " + "the list should contain at least one element" - } - if builder.errorMsg != "" { return builder } @@ -393,13 +391,13 @@ func (builder *L2AdvertisementBuilder) 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/metallb/metallb.go b/pkg/metallb/metallb.go index f66ce0abb..59e9e6085 100644 --- a/pkg/metallb/metallb.go +++ b/pkg/metallb/metallb.go @@ -37,7 +37,7 @@ func NewBuilder(apiClient *clients.Settings, name, nsname string, nodeSelector m "Initializing new metallb structure with the following params: %s, %s, %v", name, nsname, nodeSelector) - builder := Builder{ + builder := &Builder{ apiClient: apiClient, Definition: &mlbtypes.MetalLB{ TypeMeta: metav1.TypeMeta{ @@ -58,21 +58,27 @@ func NewBuilder(apiClient *clients.Settings, name, nsname string, nodeSelector m glog.V(100).Infof("The name of the metallb is empty") builder.errorMsg = "metallb 'name' cannot be empty" + + return builder } if nsname == "" { glog.V(100).Infof("The namespace of the metallb is empty") builder.errorMsg = "metallb 'nsname' cannot be empty" + + return builder } if len(nodeSelector) < 1 { glog.V(100).Infof("The SpeakerNodeSelector of the metallb is empty") builder.errorMsg = "metallb 'nodeSelector' cannot be empty" + + return builder } - return &builder + return builder } // Pull retrieves an existing metallb.io object from the cluster. @@ -86,7 +92,7 @@ func Pull(apiClient *clients.Settings, name, nsname string) (*Builder, error) { return nil, fmt.Errorf("metallb 'apiClient' cannot be empty") } - builder := Builder{ + builder := &Builder{ apiClient: apiClient, Definition: &mlbtypes.MetalLB{ ObjectMeta: metav1.ObjectMeta{ @@ -114,7 +120,7 @@ func Pull(apiClient *clients.Settings, name, nsname string) (*Builder, error) { builder.Definition = builder.Object - return &builder, nil + return builder, nil } // Exists checks whether the given MetalLb exists. @@ -171,7 +177,6 @@ func (builder *Builder) Create() (*Builder, error) { builder.Definition.Name, builder.Definition.Namespace, ) - var err error if !builder.Exists() { unstructuredMetalLb, err := runtime.DefaultUnstructuredConverter.ToUnstructured(builder.Definition) @@ -198,7 +203,7 @@ func (builder *Builder) Create() (*Builder, error) { } } - return builder, err + return builder, nil } // Delete removes MetalLb object from a cluster. @@ -242,10 +247,6 @@ func (builder *Builder) Update(force bool) (*Builder, error) { builder.Definition.Name, builder.Definition.Namespace, ) - if builder.errorMsg != "" { - return nil, fmt.Errorf(builder.errorMsg) - } - builder.Definition.ResourceVersion = builder.Object.ResourceVersion builder.Definition.ObjectMeta.ResourceVersion = builder.Object.ObjectMeta.ResourceVersion @@ -292,9 +293,7 @@ func (builder *Builder) RemoveLabel(key string) *Builder { if key == "" { glog.V(100).Infof("Failed to remove empty label's key from metalLbIo %s", builder.Definition.Name) builder.errorMsg = "error to remove empty key from metalLbIo" - } - if builder.errorMsg != "" { return builder } @@ -315,9 +314,7 @@ func (builder *Builder) WithSpeakerNodeSelector(label map[string]string) *Builde if len(label) < 1 { builder.errorMsg = "can not accept empty label and redefine metallb NodeSelector" - } - if builder.errorMsg != "" { return builder } @@ -372,13 +369,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 != "" { @@ -402,5 +399,5 @@ func (builder *Builder) convertToStructured(unsObject *unstructured.Unstructured return nil, err } - return metalLb, err + return metalLb, nil } diff --git a/pkg/nad/builder.go b/pkg/nad/builder.go index fcd5a3326..3f755f62f 100644 --- a/pkg/nad/builder.go +++ b/pkg/nad/builder.go @@ -36,7 +36,7 @@ func NewBuilder(apiClient *clients.Settings, name, nsname string) *Builder { "name: %s, namespace: %s", name, nsname) - builder := Builder{ + builder := &Builder{ apiClient: apiClient, Definition: &nadV1.NetworkAttachmentDefinition{ ObjectMeta: metav1.ObjectMeta{ @@ -50,22 +50,26 @@ func NewBuilder(apiClient *clients.Settings, name, nsname string) *Builder { glog.V(100).Infof("The name of the NetworkAttachmentDefinition is empty") builder.errorMsg = "NAD name is empty" + + return builder } if builder.Definition.Namespace == "" { glog.V(100).Infof("The namespace of the NetworkAttachmentDefinition is empty") builder.errorMsg = "NAD namespace is empty" + + return builder } - return &builder + return builder } // Pull pulls existing networkattachmentdefinition from cluster. func Pull(apiClient *clients.Settings, name, nsname string) (*Builder, error) { glog.V(100).Infof("Pulling existing networkattachmentdefinition name %s under namespace %s from cluster", name, nsname) - builder := Builder{ + builder := &Builder{ apiClient: apiClient, Definition: &nadV1.NetworkAttachmentDefinition{ ObjectMeta: metav1.ObjectMeta{ @@ -78,13 +82,13 @@ func Pull(apiClient *clients.Settings, name, nsname string) (*Builder, error) { if name == "" { glog.V(100).Infof("The name of the networkattachmentdefinition is empty") - builder.errorMsg = "networkattachmentdefinition 'name' cannot be empty" + return nil, fmt.Errorf("networkattachmentdefinition 'name' cannot be empty") } if nsname == "" { glog.V(100).Infof("The namespace of the networkattachmentdefinition is empty") - builder.errorMsg = "networkattachmentdefinition 'namespace' cannot be empty" + return nil, fmt.Errorf("networkattachmentdefinition 'namespace' cannot be empty") } if !builder.Exists() { @@ -93,7 +97,7 @@ func Pull(apiClient *clients.Settings, name, nsname string) (*Builder, error) { builder.Definition = builder.Object - return &builder, nil + return builder, nil } // Create builds a NetworkAttachmentDefinition resource with the builder configuration. @@ -207,7 +211,7 @@ func (builder *Builder) GetString() (string, error) { return "", err } - return string(nadByte), err + return string(nadByte), nil } // fillConfigureString adds a configuration string to builder definition specs configuration if needed. @@ -255,12 +259,16 @@ func (builder *Builder) WithMasterPlugin(masterPlugin *MasterPlugin) *Builder { if builder.Definition.Spec != emptyNadConfig { builder.errorMsg = "error to redefine predefine NAD" + + return builder } masterPluginSting, err := json.Marshal(masterPlugin) if err != nil { builder.errorMsg = err.Error() + + return builder } builder.Definition.Spec.Config = string(masterPluginSting) @@ -286,6 +294,8 @@ func (builder *Builder) WithPlugins(name string, plugins *[]Plugin) *Builder { if err != nil { builder.errorMsg = err.Error() + + return builder } builder.Definition.Spec.Config = string(pluginsConfigString) @@ -314,13 +324,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 != "" { diff --git a/pkg/nad/masterplugin.go b/pkg/nad/masterplugin.go index 0162c03d6..f9968bc3d 100644 --- a/pkg/nad/masterplugin.go +++ b/pkg/nad/masterplugin.go @@ -25,7 +25,7 @@ func NewMasterMacVlanPlugin(name string) *MasterMacVlanPlugin { glog.V(100).Infof( "Initializing new MasterVlanPlugin structure with the following param: %s", name) - builder := MasterMacVlanPlugin{ + builder := &MasterMacVlanPlugin{ masterPlugin: &MasterPlugin{ CniVersion: "0.3.1", Name: name, @@ -37,9 +37,11 @@ func NewMasterMacVlanPlugin(name string) *MasterMacVlanPlugin { glog.V(100).Infof("error MasterMacVlanPlugin can not be empty") builder.errorMsg = "MasterMacVlanPlugin name is empty" + + return builder } - return &builder + return builder } // WithMode defines macvlan type to MasterMacVlanPlugin. Default is bridge. @@ -136,7 +138,7 @@ func NewMasterBridgePlugin(name, bridgeName string) *MasterBridgePlugin { glog.V(100).Infof( "Initializing new MasterBridgePlugin structure %s, with bridge %s", name, bridgeName) - builder := MasterBridgePlugin{ + builder := &MasterBridgePlugin{ masterPlugin: &MasterPlugin{ CniVersion: "0.3.1", Name: name, @@ -149,9 +151,11 @@ func NewMasterBridgePlugin(name, bridgeName string) *MasterBridgePlugin { glog.V(100).Infof("error MasterBridgePlugin can not be empty") builder.errorMsg = "MasterBridgePlugin name is empty" + + return builder } - return &builder + return builder } // GetMasterPluginConfig returns master plugin if error does not occur. @@ -194,7 +198,7 @@ func NewMasterVlanPlugin(name string, vlanID uint16) *MasterVlanPlugin { glog.V(100).Infof( "Initializing new MasterVlanPlugin structure %s, with vlanId %s", name, vlanID) - builder := MasterVlanPlugin{ + builder := &MasterVlanPlugin{ masterPlugin: &MasterPlugin{ CniVersion: "0.3.1", Name: name, @@ -207,15 +211,19 @@ func NewMasterVlanPlugin(name string, vlanID uint16) *MasterVlanPlugin { glog.V(100).Infof("error vlan id can not be greater than 4094") builder.errorMsg = "MasterVlanPlugin vlanID is greater than 4094" + + return builder } if builder.masterPlugin.Name == "" { glog.V(100).Infof("error MasterVlanPlugin name can not be empty") builder.errorMsg = "MasterVlanPlugin name is empty" + + return builder } - return &builder + return builder } // WithIPAM defines IPAM configuration to MasterVlanPlugin. Default is empty. @@ -302,7 +310,7 @@ func NewMasterIPVlanPlugin(name string) *MasterIPVlanPlugin { glog.V(100).Infof( "Initializing new MasterIPVlanPlugin structure %s", name) - builder := MasterIPVlanPlugin{ + builder := &MasterIPVlanPlugin{ masterPlugin: &MasterPlugin{ CniVersion: "0.3.1", Name: name, @@ -314,9 +322,11 @@ func NewMasterIPVlanPlugin(name string) *MasterIPVlanPlugin { glog.V(100).Infof("error MasterIPVlanPlugin can not be empty") builder.errorMsg = "MasterIPVlanPlugin name is empty" + + return builder } - return &builder + return builder } // WithIPAM defines IPAM configuration to MasterIPVlanPlugin. Default is empty. @@ -410,7 +420,7 @@ func NewMasterBondPlugin(name, mode string) *MasterBondPlugin { "balance-xor": true, } - builder := MasterBondPlugin{ + builder := &MasterBondPlugin{ masterPlugin: &MasterPlugin{ CniVersion: "0.3.1", Name: name, @@ -424,15 +434,19 @@ func NewMasterBondPlugin(name, mode string) *MasterBondPlugin { glog.V(100).Infof("error: invalid mode type %s used for MasterBondPlugin bond interface", mode) builder.errorMsg = "Bond mode type is not valid" + + return builder } if builder.masterPlugin.Name == "" { glog.V(100).Infof("error NewMasterBondPlugin name can not be empty") builder.errorMsg = "NewMasterBondPlugin name is empty" + + return builder } - return &builder + return builder } // WithLinksInContainer defines linksInContainer configuration to MasterBondPlugin. diff --git a/pkg/namespace/namespace.go b/pkg/namespace/namespace.go index 527aa44b0..9c674d09b 100644 --- a/pkg/namespace/namespace.go +++ b/pkg/namespace/namespace.go @@ -38,7 +38,7 @@ func NewBuilder(apiClient *clients.Settings, name string) *Builder { glog.V(100).Infof( "Initializing new namespace structure with the following param: %s", name) - builder := Builder{ + builder := &Builder{ apiClient: apiClient, Definition: &corev1.Namespace{ ObjectMeta: metav1.ObjectMeta{ @@ -51,9 +51,11 @@ func NewBuilder(apiClient *clients.Settings, name string) *Builder { glog.V(100).Infof("The name of the namespace is empty") builder.errorMsg = "namespace 'name' cannot be empty" + + return builder } - return &builder + return builder } // WithLabel redefines namespace definition with the given label. @@ -167,7 +169,7 @@ func (builder *Builder) Delete() error { builder.Object = nil - return err + return nil } // DeleteAndWait deletes a namespace and waits until it is removed from the cluster. @@ -212,7 +214,7 @@ func (builder *Builder) Exists() bool { func Pull(apiClient *clients.Settings, nsname string) (*Builder, error) { glog.V(100).Infof("Pulling existing namespace: %s from cluster", nsname) - builder := Builder{ + builder := &Builder{ apiClient: apiClient, Definition: &corev1.Namespace{ ObjectMeta: metav1.ObjectMeta{ @@ -222,7 +224,9 @@ func Pull(apiClient *clients.Settings, nsname string) (*Builder, error) { } if nsname == "" { - builder.errorMsg = "'namespace' cannot be empty" + glog.V(100).Infof("Namespace name is empty") + + return nil, fmt.Errorf("namespace name cannot be empty") } if !builder.Exists() { @@ -231,7 +235,7 @@ func Pull(apiClient *clients.Settings, nsname string) (*Builder, error) { builder.Definition = builder.Object - return &builder, nil + return builder, nil } // CleanObjects removes given objects from the namespace. @@ -339,13 +343,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 != "" { diff --git a/pkg/network/operator.go b/pkg/network/operator.go index 78149f82a..86cce4e8e 100644 --- a/pkg/network/operator.go +++ b/pkg/network/operator.go @@ -216,13 +216,13 @@ func (builder *OperatorBuilder) 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/networkpolicy/multinetegressrule.go b/pkg/networkpolicy/multinetegressrule.go index a44c1cf4b..07a95f5c8 100644 --- a/pkg/networkpolicy/multinetegressrule.go +++ b/pkg/networkpolicy/multinetegressrule.go @@ -79,10 +79,6 @@ func (builder *EgressRuleBuilder) WithOptions(options ...EgressAdditionalOptions func (builder *EgressRuleBuilder) WithPeerPodSelector(podSelector metav1.LabelSelector) *EgressRuleBuilder { glog.V(100).Infof("Adding peer pod selector %v to EgressRule", podSelector) - if builder.errorMsg != "" { - return builder - } - builder.definition.To = append(builder.definition.To, v1beta1.MultiNetworkPolicyPeer{PodSelector: &podSelector}) return builder diff --git a/pkg/networkpolicy/multinetegressrule_test.go b/pkg/networkpolicy/multinetegressrule_test.go index 1edb1cd23..80c06e6e3 100644 --- a/pkg/networkpolicy/multinetegressrule_test.go +++ b/pkg/networkpolicy/multinetegressrule_test.go @@ -72,6 +72,7 @@ func TestEgressWithPeerPodSelector(t *testing.T) { assert.Len(t, builder.definition.To, 1) assert.Equal(t, builder.definition.To[0].PodSelector.MatchLabels["app"], "nginx") + builder = NewEgressRuleBuilder() //nolint:goconst builder.errorMsg = "error" @@ -98,6 +99,7 @@ func TestEgressWithPeerPodSelectorAndCIDR(t *testing.T) { assert.Equal(t, builder.definition.To[0].IPBlock.CIDR, "192.168.0.1/24") // Test invalid CIDR + builder = NewEgressRuleBuilder() builder.WithPeerPodSelectorAndCIDR(metav1.LabelSelector{ MatchLabels: map[string]string{ "app": "nginx", @@ -107,6 +109,7 @@ func TestEgressWithPeerPodSelectorAndCIDR(t *testing.T) { assert.Equal(t, builder.errorMsg, "Invalid CIDR argument 192.55.55.55") // Test with exception + builder = NewEgressRuleBuilder() builder.WithPeerPodSelectorAndCIDR(metav1.LabelSelector{ MatchLabels: map[string]string{ "app": "nginx", diff --git a/pkg/networkpolicy/multinetingressrule.go b/pkg/networkpolicy/multinetingressrule.go index 819662a71..76cc3893d 100644 --- a/pkg/networkpolicy/multinetingressrule.go +++ b/pkg/networkpolicy/multinetingressrule.go @@ -78,10 +78,6 @@ func (builder *IngressRuleBuilder) WithOptions(options ...IngressAdditionalOptio func (builder *IngressRuleBuilder) WithPeerPodSelector(podSelector metav1.LabelSelector) *IngressRuleBuilder { glog.V(100).Infof("Adding peer pod selector %v to Ingress Rule", podSelector) - if builder.errorMsg != "" { - return builder - } - builder.definition.From = append( builder.definition.From, v1beta1.MultiNetworkPolicyPeer{ PodSelector: &podSelector, @@ -127,10 +123,6 @@ func (builder *IngressRuleBuilder) WithPeerPodSelectorAndCIDR( podSelector metav1.LabelSelector, cidr string, except ...[]string) *IngressRuleBuilder { glog.V(100).Infof("Adding peer pod selector %v and CIDR %s to IngressRule", podSelector, cidr) - if builder.errorMsg != "" { - return builder - } - builder.WithPeerPodSelector(podSelector) builder.WithCIDR(cidr, except...) diff --git a/pkg/networkpolicy/multinetingressrule_test.go b/pkg/networkpolicy/multinetingressrule_test.go index 5b48d0ab8..62c1c6df7 100644 --- a/pkg/networkpolicy/multinetingressrule_test.go +++ b/pkg/networkpolicy/multinetingressrule_test.go @@ -71,18 +71,6 @@ func TestIngressWithPeerPodSelector(t *testing.T) { assert.Len(t, builder.definition.From, 1) assert.Equal(t, builder.definition.From[0].PodSelector.MatchLabels["app"], "nginx") - - builder = NewIngressRuleBuilder() - - builder.errorMsg = "error" - - builder.WithPeerPodSelector(metav1.LabelSelector{ - MatchLabels: map[string]string{ - "app": "nginx", - }, - }) - - assert.Len(t, builder.definition.From, 0) } func TestIngressWithCIDR(t *testing.T) { diff --git a/pkg/networkpolicy/multinetworkpolicy.go b/pkg/networkpolicy/multinetworkpolicy.go index a1ba60ce2..768de9ccb 100644 --- a/pkg/networkpolicy/multinetworkpolicy.go +++ b/pkg/networkpolicy/multinetworkpolicy.go @@ -71,10 +71,6 @@ func (builder *MultiNetworkPolicyBuilder) WithPodSelector(podSelector metav1.Lab "Creating MultiNetworkPolicy %s in %s namespace with the podSelector defined: %v", builder.Definition.Name, builder.Definition.Namespace, podSelector) - if builder.errorMsg != "" { - return builder - } - builder.Definition.Spec.PodSelector = podSelector return builder @@ -182,7 +178,7 @@ func (builder *MultiNetworkPolicyBuilder) WithPolicyType( func PullMultiNetworkPolicy(apiClient *clients.Settings, name, nsname string) (*MultiNetworkPolicyBuilder, error) { glog.V(100).Infof("Pulling existing MultiNetworkPolicy name: %s, namespace: %s", name, nsname) - builder := MultiNetworkPolicyBuilder{ + builder := &MultiNetworkPolicyBuilder{ apiClient: apiClient.K8sCniCncfIoV1beta1Interface, Definition: &v1beta1.MultiNetworkPolicy{ ObjectMeta: metav1.ObjectMeta{ @@ -214,7 +210,7 @@ func PullMultiNetworkPolicy(apiClient *clients.Settings, name, nsname string) (* builder.Definition = builder.Object - return &builder, nil + return builder, nil } // Create makes a MultiNetworkPolicy in cluster and stores the created object in struct. diff --git a/pkg/nfd/nodefeaturediscovery.go b/pkg/nfd/nodefeaturediscovery.go index 7c865b744..e4fc9bba3 100644 --- a/pkg/nfd/nodefeaturediscovery.go +++ b/pkg/nfd/nodefeaturediscovery.go @@ -38,7 +38,7 @@ func NewBuilderFromObjectString(apiClient *clients.Settings, almExample string) glog.V(100).Infof( "Initializing Builder definition to NodeFeatureDiscovery object") - builder := Builder{ + builder := &Builder{ apiClient: apiClient, Definition: nodeFeatureDiscovery, } @@ -49,15 +49,19 @@ func NewBuilderFromObjectString(apiClient *clients.Settings, almExample string) builder.errorMsg = fmt.Sprintf("Error initializing NodeFeatureDiscovery from alm-examples: %s", err.Error()) + + return builder } if builder.Definition == nil { glog.V(100).Infof("The NodeFeatureDiscovery object definition is nil") builder.errorMsg = "NodeFeatureDiscovery definition is nil" + + return builder } - return &builder + return builder } // Get returns NodeFeatureDiscovery object if found. @@ -82,14 +86,14 @@ func (builder *Builder) Get() (*nfdv1.NodeFeatureDiscovery, error) { return nil, err } - return nodeFeatureDiscovery, err + return nodeFeatureDiscovery, nil } // Pull loads an existing NodeFeatureDiscovery into Builder struct. func Pull(apiClient *clients.Settings, name, namespace string) (*Builder, error) { glog.V(100).Infof("Pulling existing nodeFeatureDiscovery name: %s in namespace: %s", name, namespace) - builder := Builder{ + builder := &Builder{ apiClient: apiClient, Definition: &nfdv1.NodeFeatureDiscovery{ ObjectMeta: metav1.ObjectMeta{ @@ -102,13 +106,13 @@ func Pull(apiClient *clients.Settings, name, namespace string) (*Builder, error) if name == "" { glog.V(100).Infof("NodeFeatureDiscovery name is empty") - builder.errorMsg = "NodeFeatureDiscovery 'name' cannot be empty" + return nil, fmt.Errorf("NodeFeatureDiscovery 'name' cannot be empty") } if namespace == "" { glog.V(100).Infof("NodeFeatureDiscovery namespace is empty") - builder.errorMsg = "NodeFeatureDiscovery 'namespace' cannot be empty" + return nil, fmt.Errorf("NodeFeatureDiscovery 'namespace' cannot be empty") } if !builder.Exists() { @@ -117,7 +121,7 @@ func Pull(apiClient *clients.Settings, name, namespace string) (*Builder, error) builder.Definition = builder.Object - return &builder, nil + return builder, nil } // Exists checks whether the given NodeFeatureDiscovery exists. @@ -214,7 +218,7 @@ func (builder *Builder) Update(force bool) (*Builder, error) { } } - return builder, err + return builder, nil } // getNodeFeatureDiscoveryFromAlmExample extracts the NodeFeatureDiscovery from the alm-examples block. diff --git a/pkg/nmstate/nmstate.go b/pkg/nmstate/nmstate.go index c8b116beb..074cf432d 100644 --- a/pkg/nmstate/nmstate.go +++ b/pkg/nmstate/nmstate.go @@ -33,7 +33,7 @@ type Builder struct { func NewBuilder(apiClient *clients.Settings, name string) *Builder { glog.V(100).Infof("Initializing new NMState structure with the name: %s", name) - builder := Builder{ + builder := &Builder{ apiClient: apiClient, Definition: &nmstateV1.NMState{ ObjectMeta: metav1.ObjectMeta{ @@ -46,9 +46,11 @@ func NewBuilder(apiClient *clients.Settings, name string) *Builder { glog.V(100).Infof("The name of the NMState is empty") builder.errorMsg = "NMState 'name' cannot be empty" + + return builder } - return &builder + return builder } // Exists checks whether the given NMState exists. @@ -86,7 +88,7 @@ func (builder *Builder) Get() (*nmstateV1.NMState, error) { return nil, err } - return nmstate, err + return nmstate, nil } // Create makes a NMState in the cluster and stores the created object in struct. @@ -162,7 +164,7 @@ func (builder *Builder) Update(force bool) (*Builder, error) { func PullNMstate(apiClient *clients.Settings, name string) (*Builder, error) { glog.V(100).Infof("Pulling NMState object name: %s", name) - builder := Builder{ + builder := &Builder{ apiClient: apiClient, Definition: &nmstateV1.NMState{ ObjectMeta: metav1.ObjectMeta{ @@ -174,7 +176,7 @@ func PullNMstate(apiClient *clients.Settings, name string) (*Builder, error) { if name == "" { glog.V(100).Infof("The name of the NMState is empty") - builder.errorMsg = "NMState 'name' cannot be empty" + return nil, fmt.Errorf("NMState 'name' cannot be empty") } if !builder.Exists() { @@ -183,7 +185,7 @@ func PullNMstate(apiClient *clients.Settings, name string) (*Builder, error) { builder.Definition = builder.Object - return &builder, nil + return builder, nil } // validate will check that the builder and builder definition are properly initialized before @@ -200,13 +202,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 != "" { diff --git a/pkg/nmstate/nodenetworkstate.go b/pkg/nmstate/nodenetworkstate.go index f34b3f112..50df0ba8a 100644 --- a/pkg/nmstate/nodenetworkstate.go +++ b/pkg/nmstate/nodenetworkstate.go @@ -219,13 +219,13 @@ func (builder *StateBuilder) validate() (bool, error) { if builder.Object == 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/nmstate/policy.go b/pkg/nmstate/policy.go index 12d6a89bd..fc7ec51d5 100644 --- a/pkg/nmstate/policy.go +++ b/pkg/nmstate/policy.go @@ -49,7 +49,7 @@ func NewPolicyBuilder(apiClient *clients.Settings, name string, nodeSelector map glog.V(100).Infof( "Initializing new NodeNetworkConfigurationPolicy structure with the following params: %s", name) - builder := PolicyBuilder{ + builder := &PolicyBuilder{ apiClient: apiClient, Definition: &nmstateV1.NodeNetworkConfigurationPolicy{ ObjectMeta: metav1.ObjectMeta{ @@ -64,15 +64,19 @@ func NewPolicyBuilder(apiClient *clients.Settings, name string, nodeSelector map glog.V(100).Infof("The name of the NodeNetworkConfigurationPolicy is empty") builder.errorMsg = "NodeNetworkConfigurationPolicy 'name' cannot be empty" + + return builder } if len(nodeSelector) == 0 { glog.V(100).Infof("The nodeSelector of the NodeNetworkConfigurationPolicy is empty") builder.errorMsg = "NodeNetworkConfigurationPolicy 'nodeSelector' cannot be empty map" + + return builder } - return &builder + return builder } // Get returns NodeNetworkConfigurationPolicy object if found. @@ -96,7 +100,7 @@ func (builder *PolicyBuilder) Get() (*nmstateV1.NodeNetworkConfigurationPolicy, return nil, err } - return nmstatePolicy, err + return nmstatePolicy, nil } // Exists checks whether the given NodeNetworkConfigurationPolicy exists. @@ -239,15 +243,15 @@ func (builder *PolicyBuilder) WithBondInterface(slavePorts []string, bondName, m glog.V(100).Infof("error to add Bond mode %s, allowed modes are %v", mode, allowedBondModes) builder.errorMsg = "invalid Bond mode parameter" + + return builder } if bondName == "" { glog.V(100).Infof("The bondName can not be empty string") builder.errorMsg = "The bondName is empty sting" - } - if builder.errorMsg != "" { return builder } @@ -277,13 +281,13 @@ func (builder *PolicyBuilder) WithVlanInterface(baseInterface string, vlanID uin glog.V(100).Infof("The baseInterface can not be empty string") builder.errorMsg = "nodenetworkconfigurationpolicy 'baseInterface' cannot be empty" + + return builder } if vlanID > 4094 { builder.errorMsg = "invalid vlanID, allowed vlanID values are between 0-4094" - } - if builder.errorMsg != "" { return builder } @@ -313,9 +317,7 @@ func (builder *PolicyBuilder) WithAbsentInterface(interfaceName string) *PolicyB glog.V(100).Infof("The interfaceName can not be empty string") builder.errorMsg = "nodenetworkconfigurationpolicy 'interfaceName' cannot be empty" - } - if builder.errorMsg != "" { return builder } @@ -401,13 +403,13 @@ func (builder *PolicyBuilder) 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/nodes/node.go b/pkg/nodes/node.go index 0942f9650..0e8441499 100644 --- a/pkg/nodes/node.go +++ b/pkg/nodes/node.go @@ -209,9 +209,7 @@ func (builder *Builder) WithNewLabel(key, value string) *Builder { if key == "" { glog.V(100).Infof("Failed to apply label with an empty key to node %s", builder.Definition.Name) builder.errorMsg = "error to set empty key to node" - } - if builder.errorMsg != "" { return builder } @@ -223,6 +221,8 @@ func (builder *Builder) WithNewLabel(key, value string) *Builder { builder.Definition.Labels[key] = value } else { builder.errorMsg = fmt.Sprintf("cannot overwrite existing node label: %s", key) + + return builder } } @@ -265,9 +265,7 @@ func (builder *Builder) RemoveLabel(key, value string) *Builder { if key == "" { glog.V(100).Infof("Failed to remove empty label's key from node %s", builder.Definition.Name) builder.errorMsg = "error to remove empty key from node" - } - if builder.errorMsg != "" { return builder } @@ -286,10 +284,8 @@ func (builder *Builder) ExternalIPv4Network() (string, error) { if builder.Object == nil { builder.errorMsg = "error to collect external networks from node" - } - if builder.errorMsg != "" { - return "", fmt.Errorf(builder.errorMsg) + return "", fmt.Errorf("node %s object is nil", builder.Definition.Name) } var extNetwork ExternalNetworks @@ -410,13 +406,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 != "" { diff --git a/pkg/nto/performanceprofile.go b/pkg/nto/performanceprofile.go index 43e1d61bc..9f0001a1c 100644 --- a/pkg/nto/performanceprofile.go +++ b/pkg/nto/performanceprofile.go @@ -57,24 +57,32 @@ func NewBuilder( glog.V(100).Infof("The name of the PerformanceProfile is empty") builder.errorMsg = "PerformanceProfile's name is empty" + + return builder } if cpuIsolated == "" { glog.V(100).Infof("Isolated CPU of the PerformanceProfile is empty") builder.errorMsg = "PerformanceProfile's 'cpuIsolated' is empty" + + return builder } if cpuReserved == "" { glog.V(100).Infof("Reserved CPU of the PerformanceProfile is empty") builder.errorMsg = "PerformanceProfile's 'cpuReserved' is empty" + + return builder } if len(nodeSelector) == 0 { glog.V(100).Infof("NodeSelector of the PerformanceProfile is empty") builder.errorMsg = "PerformanceProfile's 'nodeSelector' is empty" + + return builder } return builder @@ -84,7 +92,7 @@ func NewBuilder( func Pull(apiClient *clients.Settings, name string) (*Builder, error) { glog.V(100).Infof("Pulling existing PerformanceProfile name %s from cluster", name) - builder := Builder{ + builder := &Builder{ apiClient: apiClient, Definition: &v2.PerformanceProfile{ ObjectMeta: metav1.ObjectMeta{ @@ -96,7 +104,7 @@ func Pull(apiClient *clients.Settings, name string) (*Builder, error) { if name == "" { glog.V(100).Infof("The name of the PerformanceProfile is empty") - builder.errorMsg = "PerformanceProfile 'name' cannot be empty" + return nil, fmt.Errorf("PerformanceProfile 'name' cannot be empty") } if !builder.Exists() { @@ -105,7 +113,7 @@ func Pull(apiClient *clients.Settings, name string) (*Builder, error) { builder.Definition = builder.Object - return &builder, nil + return builder, nil } // WithHugePages defines the HugePages in the PerformanceProfile. hugePageSize allowed values are 2M, 1G. @@ -123,15 +131,15 @@ func (builder *Builder) WithHugePages(hugePageSize string, hugePages []v2.HugePa hugePageSize, allowedHugePageSize) builder.errorMsg = fmt.Sprintf("'hugePageSize' argument is not in allowed list %v", allowedHugePageSize) + + return builder } if len(hugePages) == 0 { glog.V(100).Infof("'hugePages' argument cannot be empty") builder.errorMsg = "'hugePageSize' argument cannot be empty" - } - if builder.errorMsg != "" { return builder } @@ -165,9 +173,7 @@ func (builder *Builder) WithMachineConfigPoolSelector(machineConfigPoolSelector glog.V(100).Infof("'machineConfigPoolSelector' argument cannot be empty") builder.errorMsg = "'machineConfigPoolSelector' argument cannot be empty" - } - if builder.errorMsg != "" { return builder } @@ -192,9 +198,7 @@ func (builder *Builder) WithNumaTopology(topologyPolicy string) *Builder { builder.errorMsg = fmt.Sprintf("'allowedTopologyPolicies' argument is not in allowed list %v", allowedTopologyPolicies) - } - if builder.errorMsg != "" { return builder } @@ -298,7 +302,7 @@ func (builder *Builder) Get() (*v2.PerformanceProfile, error) { return nil, err } - return module, err + return module, nil } // Delete removes the PerformanceProfile. @@ -321,7 +325,7 @@ func (builder *Builder) Delete() (*Builder, error) { builder.Object = nil - return builder, err + return builder, nil } // Update renovates the existing PerformanceProfile object with the PerformanceProfile definition in builder. @@ -375,13 +379,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 != "" { diff --git a/pkg/nvidiagpu/clusterpolicy.go b/pkg/nvidiagpu/clusterpolicy.go index ccdb5cc55..7dde35e2c 100644 --- a/pkg/nvidiagpu/clusterpolicy.go +++ b/pkg/nvidiagpu/clusterpolicy.go @@ -39,7 +39,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, Definition: clusterPolicy, } @@ -49,15 +49,19 @@ func NewBuilderFromObjectString(apiClient *clients.Settings, almExample string) "Error initializing ClusterPolicy from alm-examples: %s", err.Error()) builder.errorMsg = fmt.Sprintf("Error initializing ClusterPolicy from alm-examples: %s", err.Error()) + + return builder } if builder.Definition == nil { glog.V(100).Infof("The ClusterPolicy object definition is nil") builder.errorMsg = "ClusterPolicy 'Object.Definition' is nil" + + return builder } - return &builder + return builder } // Get returns clusterPolicy object if found. @@ -88,7 +92,7 @@ func (builder *Builder) Get() (*nvidiagpuv1.ClusterPolicy, error) { func Pull(apiClient *clients.Settings, name string) (*Builder, error) { glog.V(100).Infof("Pulling existing clusterPolicy name: %s", name) - builder := Builder{ + builder := &Builder{ apiClient: apiClient, Definition: &nvidiagpuv1.ClusterPolicy{ ObjectMeta: metav1.ObjectMeta{ @@ -100,7 +104,7 @@ func Pull(apiClient *clients.Settings, name string) (*Builder, error) { if name == "" { glog.V(100).Infof("ClusterPolicy name is empty") - builder.errorMsg = "ClusterPolicy 'name' cannot be empty" + return nil, fmt.Errorf("ClusterPolicy 'name' cannot be empty") } if !builder.Exists() { @@ -109,7 +113,7 @@ func Pull(apiClient *clients.Settings, name string) (*Builder, error) { builder.Definition = builder.Object - return &builder, nil + return builder, nil } // Exists checks whether the given ClusterPolicy exists. @@ -239,13 +243,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 != "" { diff --git a/pkg/ocm/placementbinding.go b/pkg/ocm/placementbinding.go index b23f6b1c7..2d2e9572e 100644 --- a/pkg/ocm/placementbinding.go +++ b/pkg/ocm/placementbinding.go @@ -36,7 +36,7 @@ func NewPlacementBindingBuilder( "Initializing new placement binding structure with the following params: name: %s, nsname: %s", name, nsname) - builder := PlacementBindingBuilder{ + builder := &PlacementBindingBuilder{ apiClient: apiClient, Definition: &policiesv1.PlacementBinding{ ObjectMeta: metav1.ObjectMeta{ @@ -50,21 +50,29 @@ func NewPlacementBindingBuilder( if name == "" { builder.errorMsg = "placementBinding's 'name' cannot be empty" + + return builder } if nsname == "" { builder.errorMsg = "placementBinding's 'nsname' cannot be empty" + + return builder } if placementRefErr := validatePlacementRef(placementRef); placementRefErr != "" { builder.errorMsg = placementRefErr + + return builder } if subjectErr := validateSubject(subject); subjectErr != "" { builder.errorMsg = subjectErr + + return builder } - return &builder + return builder } // PullPlacementBinding pulls existing placementBinding into Builder struct. @@ -77,7 +85,7 @@ func PullPlacementBinding(apiClient *clients.Settings, name, nsname string) (*Pl return nil, fmt.Errorf("placementBinding's 'apiClient' cannot be empty") } - builder := PlacementBindingBuilder{ + builder := &PlacementBindingBuilder{ apiClient: apiClient, Definition: &policiesv1.PlacementBinding{ ObjectMeta: metav1.ObjectMeta{ @@ -105,7 +113,7 @@ func PullPlacementBinding(apiClient *clients.Settings, name, nsname string) (*Pl builder.Definition = builder.Object - return &builder, nil + return builder, nil } // Exists checks whether the given placementBinding exists. @@ -311,13 +319,13 @@ func (builder *PlacementBindingBuilder) 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/ocm/placementrule.go b/pkg/ocm/placementrule.go index a32dbce9b..a1bd16eb2 100644 --- a/pkg/ocm/placementrule.go +++ b/pkg/ocm/placementrule.go @@ -32,7 +32,7 @@ func NewPlacementRuleBuilder(apiClient *clients.Settings, name, nsname string) * "Initializing new placement rule structure with the following params: name: %s, nsname: %s", name, nsname) - builder := PlacementRuleBuilder{ + builder := &PlacementRuleBuilder{ apiClient: apiClient, Definition: &placementrulev1.PlacementRule{ ObjectMeta: metav1.ObjectMeta{ @@ -46,15 +46,19 @@ func NewPlacementRuleBuilder(apiClient *clients.Settings, name, nsname string) * glog.V(100).Info("The name of the PlacementRule is empty") builder.errorMsg = "placementrule's 'name' cannot be empty" + + return builder } if nsname == "" { glog.V(100).Info("The namespace of the PlacementRule is empty") builder.errorMsg = "placementrule's 'nsname' cannot be empty" + + return builder } - return &builder + return builder } // PullPlacementRule pulls existing placementrule into Builder struct. @@ -67,7 +71,7 @@ func PullPlacementRule(apiClient *clients.Settings, name, nsname string) (*Place return nil, fmt.Errorf("placementrule's 'apiClient' cannot be empty") } - builder := PlacementRuleBuilder{ + builder := &PlacementRuleBuilder{ apiClient: apiClient, Definition: &placementrulev1.PlacementRule{ ObjectMeta: metav1.ObjectMeta{ @@ -95,7 +99,7 @@ func PullPlacementRule(apiClient *clients.Settings, name, nsname string) (*Place builder.Definition = builder.Object - return &builder, nil + return builder, nil } // Exists checks whether the given placementrule exists. @@ -133,7 +137,7 @@ func (builder *PlacementRuleBuilder) Get() (*placementrulev1.PlacementRule, erro return nil, err } - return placementRule, err + return placementRule, nil } // Create makes a placementrule in the cluster and stores the created object in struct. @@ -232,13 +236,13 @@ func (builder *PlacementRuleBuilder) 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/ocm/policy.go b/pkg/ocm/policy.go index 5f734ee8f..e59bba1d0 100644 --- a/pkg/ocm/policy.go +++ b/pkg/ocm/policy.go @@ -35,7 +35,7 @@ func NewPolicyBuilder( "Initializing new policy structure with the following params: name: %s, nsname: %s", name, nsname) - builder := PolicyBuilder{ + builder := &PolicyBuilder{ apiClient: apiClient, Definition: &policiesv1.Policy{ ObjectMeta: metav1.ObjectMeta{ @@ -52,21 +52,27 @@ func NewPolicyBuilder( glog.V(100).Info("The name of the Policy is empty") builder.errorMsg = "policy 'name' cannot be empty" + + return builder } if nsname == "" { glog.V(100).Info("The namespace of the Policy is empty") builder.errorMsg = "policy 'nsname' cannot be empty" + + return builder } if template == nil { glog.V(100).Info("The PolicyTemplate of the Policy is empty") builder.errorMsg = "policy 'template' cannot be empty" + + return builder } - return &builder + return builder } // PullPolicy pulls existing policy into Builder struct. @@ -79,7 +85,7 @@ func PullPolicy(apiClient *clients.Settings, name, nsname string) (*PolicyBuilde return nil, fmt.Errorf("policy 'apiClient' cannot be empty") } - builder := PolicyBuilder{ + builder := &PolicyBuilder{ apiClient: apiClient, Definition: &policiesv1.Policy{ ObjectMeta: metav1.ObjectMeta{ @@ -107,7 +113,7 @@ func PullPolicy(apiClient *clients.Settings, name, nsname string) (*PolicyBuilde builder.Definition = builder.Object - return &builder, nil + return builder, nil } // Exists checks whether the given policy exists. @@ -145,7 +151,7 @@ func (builder *PolicyBuilder) Get() (*policiesv1.Policy, error) { return nil, err } - return policy, err + return policy, nil } // Create makes a policy in the cluster and stores the created object in struct. @@ -341,13 +347,13 @@ func (builder *PolicyBuilder) 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/ocm/policyset.go b/pkg/ocm/policyset.go index 12680e9b2..c5925ec22 100644 --- a/pkg/ocm/policyset.go +++ b/pkg/ocm/policyset.go @@ -33,7 +33,7 @@ func NewPolicySetBuilder( "Initializing new policy set structure with the following params: name: %s, nsname: %s, policy: %v", name, nsname, policy) - builder := PolicySetBuilder{ + builder := &PolicySetBuilder{ apiClient: apiClient, Definition: &policiesv1beta1.PolicySet{ ObjectMeta: metav1.ObjectMeta{ @@ -50,21 +50,27 @@ func NewPolicySetBuilder( glog.V(100).Info("The name of the PolicySet is empty") builder.errorMsg = "policyset's 'name' cannot be empty" + + return builder } if nsname == "" { glog.V(100).Info("The namespace of the PolicySet is empty") builder.errorMsg = "policyset's 'nsname' cannot be empty" + + return builder } if policy == "" { glog.V(100).Info("The policy of the PolicySet is empty") builder.errorMsg = "policyset's 'policy' cannot be empty" + + return builder } - return &builder + return builder } // PullPolicySet pulls existing policySet into Builder struct. @@ -77,7 +83,7 @@ func PullPolicySet(apiClient *clients.Settings, name, nsname string) (*PolicySet return nil, fmt.Errorf("policyset's 'apiClient' cannot be empty") } - builder := PolicySetBuilder{ + builder := &PolicySetBuilder{ apiClient: apiClient, Definition: &policiesv1beta1.PolicySet{ ObjectMeta: metav1.ObjectMeta{ @@ -105,7 +111,7 @@ func PullPolicySet(apiClient *clients.Settings, name, nsname string) (*PolicySet builder.Definition = builder.Object - return &builder, nil + return builder, nil } // Exists checks whether the given policySet exists. @@ -143,7 +149,7 @@ func (builder *PolicySetBuilder) Get() (*policiesv1beta1.PolicySet, error) { return nil, err } - return policySet, err + return policySet, nil } // Create makes a policySet in the cluster and stores the created object in struct. @@ -262,13 +268,13 @@ func (builder *PolicySetBuilder) 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/olm/catalogsource.go b/pkg/olm/catalogsource.go index d53610e76..7ff287097 100644 --- a/pkg/olm/catalogsource.go +++ b/pkg/olm/catalogsource.go @@ -30,7 +30,7 @@ type CatalogSourceBuilder struct { func NewCatalogSourceBuilder(apiClient *clients.Settings, name, nsname string) *CatalogSourceBuilder { glog.V(100).Infof("Initializing new %s catalogsource structure", name) - builder := CatalogSourceBuilder{ + builder := &CatalogSourceBuilder{ apiClient: apiClient, Definition: &oplmV1alpha1.CatalogSource{ ObjectMeta: metav1.ObjectMeta{ @@ -44,15 +44,19 @@ func NewCatalogSourceBuilder(apiClient *clients.Settings, name, nsname string) * glog.V(100).Infof("The name of the catalogsource is empty") builder.errorMsg = "catalogsource 'name' cannot be empty" + + return builder } if nsname == "" { glog.V(100).Infof("The nsname of the catalogsource is empty") builder.errorMsg = "catalogsource 'nsname' cannot be empty" + + return builder } - return &builder + return builder } // PullCatalogSource loads an existing catalogsource into Builder struct. @@ -60,7 +64,7 @@ func PullCatalogSource(apiClient *clients.Settings, name, nsname string) (*Catal error) { glog.V(100).Infof("Pulling existing catalogsource name %s in namespace %s", name, nsname) - builder := CatalogSourceBuilder{ + builder := &CatalogSourceBuilder{ apiClient: apiClient, Definition: &oplmV1alpha1.CatalogSource{ ObjectMeta: metav1.ObjectMeta{ @@ -71,11 +75,15 @@ func PullCatalogSource(apiClient *clients.Settings, name, nsname string) (*Catal } if name == "" { - builder.errorMsg = "catalogsource 'name' cannot be empty" + glog.V(100).Infof("The name of the catalogsource is empty") + + return nil, fmt.Errorf("catalogsource 'name' cannot be empty") } if nsname == "" { - builder.errorMsg = "catalogsource 'namespace' cannot be empty" + glog.V(100).Infof("The namespace of the catalogsource is empty") + + return nil, fmt.Errorf("catalogsource 'namespace' cannot be empty") } if !builder.Exists() { @@ -84,7 +92,7 @@ func PullCatalogSource(apiClient *clients.Settings, name, nsname string) (*Catal builder.Definition = builder.Object - return &builder, nil + return builder, nil } // Create makes an CatalogSourceBuilder in cluster and stores the created object in struct. @@ -145,7 +153,7 @@ func (builder *CatalogSourceBuilder) Delete() error { builder.Object = nil - return err + return nil } // validate will check that the builder and builder definition are properly initialized before @@ -162,13 +170,13 @@ func (builder *CatalogSourceBuilder) 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/olm/clusterserviceversion.go b/pkg/olm/clusterserviceversion.go index 10714b5a1..4eec3741f 100644 --- a/pkg/olm/clusterserviceversion.go +++ b/pkg/olm/clusterserviceversion.go @@ -32,7 +32,7 @@ func PullClusterServiceVersion(apiClient *clients.Settings, name, namespace stri error) { glog.V(100).Infof("Pulling existing clusterserviceversion name %s in namespace %s", name, namespace) - builder := ClusterServiceVersionBuilder{ + builder := &ClusterServiceVersionBuilder{ apiClient: apiClient, Definition: &oplmV1alpha1.ClusterServiceVersion{ ObjectMeta: metav1.ObjectMeta{ @@ -43,11 +43,15 @@ func PullClusterServiceVersion(apiClient *clients.Settings, name, namespace stri } if name == "" { - builder.errorMsg = "clusterserviceversion 'name' cannot be empty" + glog.V(100).Infof("The name of the clusterserviceversion is empty") + + return nil, fmt.Errorf("clusterserviceversion 'name' cannot be empty") } if namespace == "" { - builder.errorMsg = "clusterserviceversion 'namespace' cannot be empty" + glog.V(100).Infof("The namespace of the clusterserviceversion is empty") + + return nil, fmt.Errorf("clusterserviceversion 'namespace' cannot be empty") } if !builder.Exists() { @@ -56,7 +60,7 @@ func PullClusterServiceVersion(apiClient *clients.Settings, name, namespace stri builder.Definition = builder.Object - return &builder, nil + return builder, nil } // Exists checks whether the given clusterserviceversion exists. @@ -99,7 +103,7 @@ func (builder *ClusterServiceVersionBuilder) Delete() error { builder.Object = nil - return err + return nil } // GetAlmExamples extracts and returns the alm-examples block from the clusterserviceversion. @@ -175,13 +179,13 @@ func (builder *ClusterServiceVersionBuilder) 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/olm/installplan.go b/pkg/olm/installplan.go index e9b62bc6e..f2946503d 100644 --- a/pkg/olm/installplan.go +++ b/pkg/olm/installplan.go @@ -30,7 +30,7 @@ type InstallPlanBuilder struct { func NewInstallPlanBuilder(apiClient *clients.Settings, name, nsname string) *InstallPlanBuilder { glog.V(100).Infof("Initializing new %s installplan structure", name) - builder := InstallPlanBuilder{ + builder := &InstallPlanBuilder{ apiClient: apiClient, Definition: &v1alpha1.InstallPlan{ ObjectMeta: metav1.ObjectMeta{ @@ -44,15 +44,19 @@ func NewInstallPlanBuilder(apiClient *clients.Settings, name, nsname string) *In glog.V(100).Infof("The name of the installplan is empty") builder.errorMsg = "installplan 'name' cannot be empty" + + return builder } if nsname == "" { glog.V(100).Infof("The nsname of the installplan is empty") builder.errorMsg = "installplan 'nsname' cannot be empty" + + return builder } - return &builder + return builder } // Create makes an InstallPlanBuilder in cluster and stores the created object in struct. @@ -111,7 +115,7 @@ func (builder *InstallPlanBuilder) Delete() error { builder.Object = nil - return err + return nil } // Update modifies the existing InstallPlanBuilder with the InstallPlan definition in InstallPlanBuilder. @@ -144,13 +148,13 @@ func (builder *InstallPlanBuilder) 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 builder %s 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/olm/operatorgroup.go b/pkg/olm/operatorgroup.go index 92ecabf2d..6870822a2 100644 --- a/pkg/olm/operatorgroup.go +++ b/pkg/olm/operatorgroup.go @@ -50,12 +50,16 @@ func NewOperatorGroupBuilder(apiClient *clients.Settings, groupName, nsName stri glog.V(100).Infof("The Name of the OperatorGroup is empty") builder.errorMsg = "OperatorGroup 'groupName' cannot be empty" + + return builder } if nsName == "" { glog.V(100).Infof("The Namespace of the OperatorGroup is empty") builder.errorMsg = "OperatorGroup 'Namespace' cannot be empty" + + return builder } return builder @@ -118,7 +122,7 @@ func (builder *OperatorGroupBuilder) Delete() error { builder.Object = nil - return err + return nil } // Update modifies the existing OperatorGroup with the OperatorGroup definition in OperatorGroupBuilder. @@ -156,13 +160,13 @@ func PullOperatorGroup(apiClient *clients.Settings, groupName, nsName string) (* if groupName == "" { glog.V(100).Infof("The name of the OperatorGroup is empty") - builder.errorMsg = "OperatorGroup 'Name' cannot be empty" + return nil, fmt.Errorf("OperatorGroup 'Name' cannot be empty") } if nsName == "" { glog.V(100).Infof("The namespace of the OperatorGroup is empty") - builder.errorMsg = "OperatorGroup 'Namespace' cannot be empty" + return nil, fmt.Errorf("OperatorGroup 'Namespace' cannot be empty") } if !builder.Exists() { @@ -188,13 +192,13 @@ func (builder *OperatorGroupBuilder) 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/olm/packagemanifest.go b/pkg/olm/packagemanifest.go index c84ad63ba..12bce09ac 100644 --- a/pkg/olm/packagemanifest.go +++ b/pkg/olm/packagemanifest.go @@ -43,13 +43,13 @@ func PullPackageManifest(apiClient *clients.Settings, name, nsname string) (*Pac if name == "" { glog.V(100).Infof("The Name of the PackageManifest is empty") - builder.errorMsg = "PackageManifest 'name' cannot be empty" + return nil, fmt.Errorf("PackageManifest 'name' cannot be empty") } if nsname == "" { glog.V(100).Infof("The Namespace of the PackageManifest is empty") - builder.errorMsg = "PackageManifest 'nsname' cannot be empty" + return builder, fmt.Errorf("PackageManifest 'nsname' cannot be empty") } if !builder.Exists() { @@ -132,7 +132,7 @@ func (builder *PackageManifestBuilder) Delete() error { builder.Object = nil - return err + return nil } // validate will check that the builder and builder definition are properly initialized before @@ -149,13 +149,13 @@ func (builder *PackageManifestBuilder) 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/olm/subscription.go b/pkg/olm/subscription.go index 29dd8390b..0029cd982 100644 --- a/pkg/olm/subscription.go +++ b/pkg/olm/subscription.go @@ -52,30 +52,40 @@ func NewSubscriptionBuilder(apiClient *clients.Settings, subName, subNamespace, glog.V(100).Infof("The Name of the Subscription is empty") builder.errorMsg = "Subscription 'subName' cannot be empty" + + return builder } if subNamespace == "" { glog.V(100).Infof("The Namespace of the Subscription is empty") builder.errorMsg = "Subscription 'subNamespace' cannot be empty" + + return builder } if catalogSource == "" { glog.V(100).Infof("The Catalogsource of the Subscription is empty") builder.errorMsg = "Subscription 'catalogSource' cannot be empty" + + return builder } if catalogSourceNamespace == "" { glog.V(100).Infof("The Catalogsource namespace of the Subscription is empty") builder.errorMsg = "Subscription 'catalogSourceNamespace' cannot be empty" + + return builder } if packageName == "" { glog.V(100).Infof("The Package name of the Subscription is empty") builder.errorMsg = "Subscription 'packageName' cannot be empty" + + return builder } return builder @@ -91,9 +101,7 @@ func (builder *SubscriptionBuilder) WithChannel(channel string) *SubscriptionBui if channel == "" { builder.errorMsg = "can not redefine subscription with empty channel" - } - if builder.errorMsg != "" { return builder } @@ -113,9 +121,7 @@ func (builder *SubscriptionBuilder) WithStartingCSV(startingCSV string) *Subscri if startingCSV == "" { builder.errorMsg = "can not redefine subscription with empty startingCSV" - } - if builder.errorMsg != "" { return builder } @@ -139,9 +145,7 @@ func (builder *SubscriptionBuilder) WithInstallPlanApproval( "or \"Manual\"") builder.errorMsg = "Subscription 'installPlanApproval' must be either \"Automatic\" or \"Manual\"" - } - if builder.errorMsg != "" { return builder } @@ -207,7 +211,7 @@ func (builder *SubscriptionBuilder) Delete() error { builder.Object = nil - return err + return nil } // Update modifies the existing Subscription with the Subscription definition in SubscriptionBuilder. @@ -250,13 +254,13 @@ func PullSubscription(apiClient *clients.Settings, subName, subNamespace string) if subName == "" { glog.V(100).Infof("The name of the Subscription is empty") - builder.errorMsg = "Subscription 'subName' cannot be empty" + return nil, fmt.Errorf("subscription 'subName' cannot be empty") } if subNamespace == "" { glog.V(100).Infof("The namespace of the Subscription is empty") - builder.errorMsg = "Subscription 'subNamespace' cannot be empty" + return nil, fmt.Errorf("subscription 'subNamespace' cannot be empty") } if !builder.Exists() { @@ -282,13 +286,13 @@ func (builder *SubscriptionBuilder) 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/pod/container.go b/pkg/pod/container.go index 9dabac5f4..3670e2a79 100644 --- a/pkg/pod/container.go +++ b/pkg/pod/container.go @@ -56,18 +56,24 @@ func NewContainerBuilder(name, image string, cmd []string) *ContainerBuilder { glog.V(100).Infof("The name of the container is empty") builder.errorMsg = "container's name is empty" + + return builder } if image == "" { glog.V(100).Infof("Container's image is empty") builder.errorMsg = "container's image is empty" + + return builder } if len(cmd) < 1 { glog.V(100).Infof("Container's cmd is empty") builder.errorMsg = "container's cmd is empty" + + return builder } return builder @@ -83,6 +89,8 @@ func (builder *ContainerBuilder) WithSecurityCapabilities(sCapabilities []string glog.V(100).Infof("Cannot modify pre-existing SecurityContext") builder.errorMsg = "can not modify pre-existing security context" + + return builder } builder.definition.SecurityContext = nil @@ -93,9 +101,7 @@ func (builder *ContainerBuilder) WithSecurityCapabilities(sCapabilities []string sCapabilities, AllowedSCList) builder.errorMsg = "one of the give securityCapabilities is invalid. Please extend allowed list or fix parameter" - } - if builder.errorMsg != "" { return builder } @@ -197,9 +203,7 @@ func (builder *ContainerBuilder) WithSecurityContext(securityContext *corev1.Sec glog.V(100).Infof("Cannot add empty securityContext to container structure") builder.errorMsg = "can not modify container config with empty securityContext" - } - if builder.errorMsg != "" { return builder } @@ -217,21 +221,23 @@ func (builder *ContainerBuilder) WithResourceLimit(hugePages, memory string, cpu glog.V(100).Infof("Container's resource limit hugePages is empty") builder.errorMsg = "container's resource limit 'hugePages' is empty" + + return builder } if memory == "" { glog.V(100).Infof("Container's resource limit memory is empty") builder.errorMsg = "container's resource limit 'memory' is empty" + + return builder } if cpu <= 0 { glog.V(100).Infof("Container's resource limit cpu can not be zero or negative number.") builder.errorMsg = "container's resource limit 'cpu' is invalid" - } - if builder.errorMsg != "" { return builder } @@ -253,21 +259,23 @@ func (builder *ContainerBuilder) WithResourceRequest(hugePages, memory string, c glog.V(100).Infof("Container's resource request hugePages is empty") builder.errorMsg = "container's resource request 'hugePages' is empty" + + return builder } if memory == "" { glog.V(100).Infof("Container's resource request memory is empty") builder.errorMsg = "container's resource request 'memory' is empty" + + return builder } if cpu <= 0 { glog.V(100).Infof("Container's resource request cpu can not be zero or negative number.") builder.errorMsg = "container's resource request 'cpu' is invalid" - } - if builder.errorMsg != "" { return builder } @@ -305,9 +313,7 @@ func (builder *ContainerBuilder) WithCustomResourcesLimits(resourceList corev1.R glog.V(100).Infof("Container's resource limit var 'resourceList' is empty") builder.errorMsg = "container's resource limit var 'resourceList' is empty" - } - if builder.errorMsg != "" { return builder } @@ -324,9 +330,7 @@ func (builder *ContainerBuilder) WithImagePullPolicy(pullPolicy corev1.PullPolic glog.V(100).Infof("Container's image pull policy 'pullPolicy' is empty") builder.errorMsg = "container's pull policy var 'pullPolicy' is empty" - } - if builder.errorMsg != "" { return builder } @@ -343,15 +347,15 @@ func (builder *ContainerBuilder) WithEnvVar(name, value string) *ContainerBuilde glog.V(100).Infof("Container's environment var 'name' is empty") builder.errorMsg = "container's environment var 'name' is empty" + + return builder } if value == "" { glog.V(100).Infof("Container's environment var 'value' is empty") builder.errorMsg = "container's environment var 'value' is empty" - } - if builder.errorMsg != name { return builder } @@ -374,15 +378,15 @@ func (builder *ContainerBuilder) WithVolumeMount(volMount corev1.VolumeMount) *C glog.V(100).Infof("Container's VolumeMount name cannot be empty") builder.errorMsg = "container's volume mount name is empty" + + return builder } if volMount.MountPath == "" { glog.V(100).Infof("Container's VolumeMount mount path cannot be empty") builder.errorMsg = "container's volume mount path is empty" - } - if builder.errorMsg != "" { return builder } diff --git a/pkg/pod/pod.go b/pkg/pod/pod.go index 808974ce7..51d72a43a 100644 --- a/pkg/pod/pod.go +++ b/pkg/pod/pod.go @@ -59,18 +59,24 @@ func NewBuilder(apiClient *clients.Settings, name, nsname, image string) *Builde glog.V(100).Infof("The name of the pod is empty") builder.errorMsg = "pod's name is empty" + + return builder } if nsname == "" { glog.V(100).Infof("The namespace of the pod is empty") builder.errorMsg = "namespace's name is empty" + + return builder } if image == "" { glog.V(100).Infof("The image of the pod is empty") builder.errorMsg = "pod's image is empty" + + return builder } defaultContainer, err := NewContainerBuilder("test", image, []string{"/bin/bash", "-c", "sleep INF"}).GetContainerCfg() @@ -79,6 +85,8 @@ func NewBuilder(apiClient *clients.Settings, name, nsname, image string) *Builde glog.V(100).Infof("Failed to define the default container settings") builder.errorMsg = err.Error() + + return builder } builder.Definition.Spec.Containers = append(builder.Definition.Spec.Containers, *defaultContainer) @@ -90,7 +98,7 @@ func NewBuilder(apiClient *clients.Settings, name, nsname, image string) *Builde func Pull(apiClient *clients.Settings, name, nsname string) (*Builder, error) { glog.V(100).Infof("Pulling existing pod name: %s namespace:%s", name, nsname) - builder := Builder{ + builder := &Builder{ apiClient: apiClient, Definition: &corev1.Pod{ ObjectMeta: metav1.ObjectMeta{ @@ -103,17 +111,13 @@ func Pull(apiClient *clients.Settings, name, nsname string) (*Builder, error) { if name == "" { glog.V(100).Infof("The name of the pod is empty") - builder.errorMsg = "pod 'name' cannot be empty" + return nil, fmt.Errorf("pod 'name' cannot be empty") } if nsname == "" { glog.V(100).Infof("The namespace of the pod is empty") - builder.errorMsg = "pod 'namespace' cannot be empty" - } - - if builder.errorMsg != "" { - return nil, fmt.Errorf("faield to pull pod object due to the following error: %s", builder.errorMsg) + return nil, fmt.Errorf("pod 'namespace' cannot be empty") } if !builder.Exists() { @@ -125,7 +129,7 @@ func Pull(apiClient *clients.Settings, name, nsname string) (*Builder, error) { builder.Definition = builder.Object - return &builder, nil + return builder, nil } // DefineOnNode adds nodeName to the pod's definition. @@ -142,18 +146,20 @@ func (builder *Builder) DefineOnNode(nodeName string) *Builder { builder.errorMsg = fmt.Sprintf( "can not redefine running pod. pod already running on node %s", builder.Object.Spec.NodeName) + + return builder } if nodeName == "" { glog.V(100).Infof("The node name is empty") builder.errorMsg = "can not define pod on empty node" - } - if builder.errorMsg == "" { - builder.Definition.Spec.NodeName = nodeName + return builder } + builder.Definition.Spec.NodeName = nodeName + return builder } @@ -543,9 +549,7 @@ func (builder *Builder) WithRestartPolicy(restartPolicy corev1.RestartPolicy) *B builder.Definition.Name, builder.Definition.Namespace) builder.errorMsg = "can not define pod with empty restart policy" - } - if builder.errorMsg != "" { return builder } @@ -638,9 +642,7 @@ func (builder *Builder) WithNodeSelector(nodeSelector map[string]string) *Builde builder.Definition.Name, builder.Definition.Namespace) builder.errorMsg = "can not define pod with empty nodeSelector" - } - if builder.errorMsg != "" { return builder } @@ -682,9 +684,7 @@ func (builder *Builder) WithVolume(volume corev1.Volume) *Builder { glog.V(100).Infof("The volume's Name cannot be empty") builder.errorMsg = "The volume's Name cannot be empty" - } - if builder.errorMsg != "" { return builder } @@ -711,12 +711,16 @@ func (builder *Builder) WithLocalVolume(volumeName, mountPath string) *Builder { glog.V(100).Infof("The 'volumeName' of the pod is empty") builder.errorMsg = "'volumeName' parameter is empty" + + return builder } if mountPath == "" { glog.V(100).Infof("The 'mountPath' of the pod is empty") builder.errorMsg = "'mountPath' parameter is empty" + + return builder } mountConfig := corev1.VolumeMount{Name: volumeName, MountPath: mountPath, ReadOnly: false} @@ -787,6 +791,8 @@ func (builder *Builder) WithAdditionalInitContainer(container *corev1.Container) glog.V(100).Infof("The 'container' parameter of the pod is empty") builder.errorMsg = "'container' parameter cannot be empty" + + return builder } if builder.errorMsg != "" { @@ -816,9 +822,7 @@ func (builder *Builder) WithSecondaryNetwork(network []*multus.NetworkSelectionE if err != nil { builder.errorMsg = fmt.Sprintf("error to unmarshal network annotation due to: %s", err.Error()) - } - if builder.errorMsg != "" { return builder } @@ -934,14 +938,16 @@ func (builder *Builder) WithSecurityContext(securityContext *corev1.PodSecurityC glog.V(100).Infof("The 'securityContext' of the pod is empty") builder.errorMsg = "'securityContext' parameter is empty" - } - if builder.errorMsg != "" { return builder } builder.isMutationAllowed("SecurityContext") + if builder.errorMsg != "" { + return builder + } + builder.Definition.Spec.SecurityContext = securityContext return builder @@ -1009,9 +1015,7 @@ func (builder *Builder) WithLabel(labelKey, labelValue string) *Builder { if labelKey == "" { builder.errorMsg = "can not apply empty labelKey" - } - if builder.errorMsg != "" { return builder } @@ -1212,13 +1216,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 != "" { diff --git a/pkg/rbac/clusterrole.go b/pkg/rbac/clusterrole.go index 18223f75b..bc79d8e0c 100644 --- a/pkg/rbac/clusterrole.go +++ b/pkg/rbac/clusterrole.go @@ -38,7 +38,7 @@ func NewClusterRoleBuilder(apiClient *clients.Settings, name string, rule v1.Pol "name: %s, policy rule: %v", name, rule) - builder := ClusterRoleBuilder{ + builder := &ClusterRoleBuilder{ apiClient: apiClient, Definition: &v1.ClusterRole{ ObjectMeta: metav1.ObjectMeta{ @@ -52,11 +52,13 @@ func NewClusterRoleBuilder(apiClient *clients.Settings, name string, rule v1.Pol glog.V(100).Infof("The name of the clusterrole is empty") builder.errorMsg = "clusterrole 'name' cannot be empty" + + return builder } builder.WithRules([]v1.PolicyRule{rule}) - return &builder + return builder } // WithRules appends additional rules to the clusterrole definition. @@ -72,9 +74,7 @@ func (builder *ClusterRoleBuilder) WithRules(rules []v1.PolicyRule) *ClusterRole glog.V(100).Infof("The list of rules is empty") builder.errorMsg = "cannot accept nil or empty slice as rules" - } - if builder.errorMsg != "" { return builder } @@ -83,9 +83,7 @@ func (builder *ClusterRoleBuilder) WithRules(rules []v1.PolicyRule) *ClusterRole glog.V(100).Infof("The clusterrole rule must contain at least one Verb entry") builder.errorMsg = "clusterrole rule must contain at least one Verb entry" - } - if builder.errorMsg != "" { return builder } } @@ -130,7 +128,7 @@ func (builder *ClusterRoleBuilder) WithOptions(options ...ClusterRoleAdditionalO func PullClusterRole(apiClient *clients.Settings, name string) (*ClusterRoleBuilder, error) { glog.V(100).Infof("Pulling existing clusterrole name %s from cluster", name) - builder := ClusterRoleBuilder{ + builder := &ClusterRoleBuilder{ apiClient: apiClient, Definition: &v1.ClusterRole{ ObjectMeta: metav1.ObjectMeta{ @@ -142,7 +140,7 @@ func PullClusterRole(apiClient *clients.Settings, name string) (*ClusterRoleBuil if name == "" { glog.V(100).Infof("The name of the clusterrole is empty") - builder.errorMsg = "clusterrole 'name' cannot be empty" + return nil, fmt.Errorf("clusterrole 'name' cannot be empty") } if !builder.Exists() { @@ -151,7 +149,7 @@ func PullClusterRole(apiClient *clients.Settings, name string) (*ClusterRoleBuil builder.Definition = builder.Object - return &builder, nil + return builder, nil } // Create generates a clusterrole in the cluster and stores the created object in struct. @@ -194,7 +192,7 @@ func (builder *ClusterRoleBuilder) Delete() error { builder.Object = nil - return err + return nil } // Update modifies a clusterrole object in the cluster. @@ -243,13 +241,13 @@ func (builder *ClusterRoleBuilder) 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/rbac/clusterrolebinding.go b/pkg/rbac/clusterrolebinding.go index 416fd4262..a3cfd47b4 100644 --- a/pkg/rbac/clusterrolebinding.go +++ b/pkg/rbac/clusterrolebinding.go @@ -37,7 +37,7 @@ func NewClusterRoleBindingBuilder( "name: %s, clusterrole: %s, subject %v", name, clusterRole, subject) - builder := ClusterRoleBindingBuilder{ + builder := &ClusterRoleBindingBuilder{ apiClient: apiClient, Definition: &v1.ClusterRoleBinding{ ObjectMeta: metav1.ObjectMeta{ @@ -57,9 +57,11 @@ func NewClusterRoleBindingBuilder( glog.V(100).Infof("The name of the clusterrolebinding is empty") builder.errorMsg = "clusterrolebinding 'name' cannot be empty" + + return builder } - return &builder + return builder } // WithSubjects appends additional subjects to clusterrolebinding definition. @@ -75,9 +77,7 @@ func (builder *ClusterRoleBindingBuilder) WithSubjects(subjects []v1.Subject) *C glog.V(100).Infof("The list of subjects is empty") builder.errorMsg = "cannot accept nil or empty slice as subjects" - } - if builder.errorMsg != "" { return builder } @@ -86,15 +86,15 @@ func (builder *ClusterRoleBindingBuilder) WithSubjects(subjects []v1.Subject) *C glog.V(100).Infof("The clusterrolebinding subject kind must be one of 'ServiceAccount', 'User', or 'Group'") builder.errorMsg = "clusterrolebinding subject kind must be one of 'ServiceAccount', 'User', or 'Group'" + + return builder } if subject.Name == "" { glog.V(100).Infof("The clusterrolebinding subject name cannot be empty") builder.errorMsg = "clusterrolebinding subject name cannot be empty" - } - if builder.errorMsg != "" { return builder } } @@ -134,7 +134,7 @@ func (builder *ClusterRoleBindingBuilder) WithOptions( func PullClusterRoleBinding(apiClient *clients.Settings, name string) (*ClusterRoleBindingBuilder, error) { glog.V(100).Infof("Pulling existing clusterrolebinding name %s from cluster", name) - builder := ClusterRoleBindingBuilder{ + builder := &ClusterRoleBindingBuilder{ apiClient: apiClient, Definition: &v1.ClusterRoleBinding{ ObjectMeta: metav1.ObjectMeta{ @@ -146,7 +146,7 @@ func PullClusterRoleBinding(apiClient *clients.Settings, name string) (*ClusterR if name == "" { glog.V(100).Infof("The name of the clusterrolebinding is empty") - builder.errorMsg = "clusterrolebinding 'name' cannot be empty" + return nil, fmt.Errorf("clusterrolebinding 'name' cannot be empty") } if !builder.Exists() { @@ -155,7 +155,7 @@ func PullClusterRoleBinding(apiClient *clients.Settings, name string) (*ClusterR builder.Definition = builder.Object - return &builder, nil + return builder, nil } // Create generates a clusterrolebinding in the cluster and stores the created object in struct. @@ -198,7 +198,7 @@ func (builder *ClusterRoleBindingBuilder) Delete() error { builder.Object = nil - return err + return nil } // Update modifies a clusterrolebinding object in the cluster. @@ -247,13 +247,13 @@ func (builder *ClusterRoleBindingBuilder) 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/rbac/role.go b/pkg/rbac/role.go index d53268128..b9bbcd35f 100644 --- a/pkg/rbac/role.go +++ b/pkg/rbac/role.go @@ -34,7 +34,7 @@ func NewRoleBuilder(apiClient *clients.Settings, name, nsname string, rule v1.Po "Initializing new role structure with the following params: "+ "name: %s, namespace: %s, rule %v", name, nsname, rule) - builder := RoleBuilder{ + builder := &RoleBuilder{ apiClient: apiClient, Definition: &v1.Role{ ObjectMeta: metav1.ObjectMeta{ @@ -48,17 +48,21 @@ func NewRoleBuilder(apiClient *clients.Settings, name, nsname string, rule v1.Po glog.V(100).Infof("The name of the role is empty") builder.errorMsg = "Role 'name' cannot be empty" + + return builder } if nsname == "" { glog.V(100).Infof("The namespace of the role is empty") builder.errorMsg = "Role 'nsname' cannot be empty" + + return builder } builder.WithRules([]v1.PolicyRule{rule}) - return &builder + return builder } // WithRules adds the specified PolicyRule to the Role. @@ -74,9 +78,7 @@ func (builder *RoleBuilder) WithRules(rules []v1.PolicyRule) *RoleBuilder { glog.V(100).Infof("The list of rules is empty") builder.errorMsg = "cannot create role with empty rule" - } - if builder.errorMsg != "" { return builder } @@ -85,21 +87,23 @@ func (builder *RoleBuilder) WithRules(rules []v1.PolicyRule) *RoleBuilder { glog.V(100).Infof("The role has no verbs") builder.errorMsg = "role must contain at least one Verb" + + return builder } if len(rule.Resources) == 0 { glog.V(100).Infof("The role has no resources") builder.errorMsg = "role must contain at least one Resource" + + return builder } if len(rule.APIGroups) == 0 { glog.V(100).Infof("The role has no apigroups") builder.errorMsg = "role must contain at least one APIGroup" - } - if builder.errorMsg != "" { return builder } } @@ -143,7 +147,7 @@ func (builder *RoleBuilder) WithOptions( func PullRole(apiClient *clients.Settings, name, nsname string) (*RoleBuilder, error) { glog.V(100).Infof("Pulling existing role name %s under namespace %s from cluster", name, nsname) - builder := RoleBuilder{ + builder := &RoleBuilder{ apiClient: apiClient, Definition: &v1.Role{ ObjectMeta: metav1.ObjectMeta{ @@ -156,13 +160,13 @@ func PullRole(apiClient *clients.Settings, name, nsname string) (*RoleBuilder, e if name == "" { glog.V(100).Infof("The name of the role is empty") - builder.errorMsg = "role 'name' cannot be empty" + return nil, fmt.Errorf("role 'name' cannot be empty") } if nsname == "" { glog.V(100).Infof("The namespace of the role is empty") - builder.errorMsg = "role 'namespace' cannot be empty" + return nil, fmt.Errorf("role 'nsname' cannot be empty") } if !builder.Exists() { @@ -171,7 +175,7 @@ func PullRole(apiClient *clients.Settings, name, nsname string) (*RoleBuilder, e builder.Definition = builder.Object - return &builder, nil + return builder, nil } // Create makes a Role in the cluster and stores the created object in struct. @@ -214,7 +218,7 @@ func (builder *RoleBuilder) Delete() error { builder.Object = nil - return err + return nil } // Update modifies the existing Role object with role definition in builder. @@ -263,13 +267,13 @@ func (builder *RoleBuilder) 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/rbac/rolebinding.go b/pkg/rbac/rolebinding.go index 2e9103524..eff2949d1 100644 --- a/pkg/rbac/rolebinding.go +++ b/pkg/rbac/rolebinding.go @@ -38,7 +38,7 @@ func NewRoleBindingBuilder(apiClient *clients.Settings, "Initializing new rolebinding structure with the following params: "+ "name: %s, namespace: %s, role: %s, subject %v", name, nsname, role, subject) - builder := RoleBindingBuilder{ + builder := &RoleBindingBuilder{ apiClient: apiClient, Definition: &v1.RoleBinding{ ObjectMeta: metav1.ObjectMeta{ @@ -57,17 +57,21 @@ func NewRoleBindingBuilder(apiClient *clients.Settings, glog.V(100).Infof("The name of the rolebinding is empty") builder.errorMsg = "RoleBinding 'name' cannot be empty" + + return builder } if nsname == "" { glog.V(100).Infof("The namespace of the rolebinding is empty") builder.errorMsg = "RoleBinding 'nsname' cannot be empty" + + return builder } builder.WithSubjects([]v1.Subject{subject}) - return &builder + return builder } // WithSubjects adds specified Subject to the RoleBinding. @@ -83,9 +87,7 @@ func (builder *RoleBindingBuilder) WithSubjects(subjects []v1.Subject) *RoleBind glog.V(100).Infof("The list of subjects is empty") builder.errorMsg = "cannot create rolebinding with empty subject" - } - if builder.errorMsg != "" { return builder } @@ -94,15 +96,15 @@ func (builder *RoleBindingBuilder) WithSubjects(subjects []v1.Subject) *RoleBind glog.V(100).Infof("The rolebinding subject kind must be one of 'ServiceAccount', 'User', or 'Group'") builder.errorMsg = "rolebinding subject kind must be one of 'ServiceAccount', 'User', 'Group'" + + return builder } if subject.Name == "" { glog.V(100).Infof("The rolebinding subject name cannot be empty") builder.errorMsg = "rolebinding subject name cannot be empty" - } - if builder.errorMsg != "" { return builder } } @@ -140,7 +142,7 @@ func (builder *RoleBindingBuilder) WithOptions(options ...RoleBindingAdditionalO func PullRoleBinding(apiClient *clients.Settings, name, nsname string) (*RoleBindingBuilder, error) { glog.V(100).Infof("Pulling existing rolebinding name %s under namespace %s from cluster", name, nsname) - builder := RoleBindingBuilder{ + builder := &RoleBindingBuilder{ apiClient: apiClient, Definition: &v1.RoleBinding{ ObjectMeta: metav1.ObjectMeta{ @@ -153,13 +155,13 @@ func PullRoleBinding(apiClient *clients.Settings, name, nsname string) (*RoleBin if name == "" { glog.V(100).Infof("The name of the rolebinding is empty") - builder.errorMsg = "rolebinding 'name' cannot be empty" + return nil, fmt.Errorf("rolebinding 'name' cannot be empty") } if nsname == "" { glog.V(100).Infof("The namespace of the rolebinding is empty") - builder.errorMsg = "rolebinding 'namespace' cannot be empty" + return nil, fmt.Errorf("rolebinding 'namespace' cannot be empty") } if !builder.Exists() { @@ -168,7 +170,7 @@ func PullRoleBinding(apiClient *clients.Settings, name, nsname string) (*RoleBin builder.Definition = builder.Object - return &builder, nil + return builder, nil } // Create generates a RoleBinding and stores the created object in struct. @@ -205,9 +207,13 @@ func (builder *RoleBindingBuilder) Delete() error { err := builder.apiClient.RoleBindings(builder.Definition.Namespace).Delete( context.TODO(), builder.Object.Name, metav1.DeleteOptions{}) + if err != nil { + return err + } + builder.Object = nil - return err + return nil } // Update modifies an existing RoleBinding in the cluster. @@ -256,13 +262,13 @@ func (builder *RoleBindingBuilder) 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/route/route.go b/pkg/route/route.go index 8f9eeb084..34caa2434 100644 --- a/pkg/route/route.go +++ b/pkg/route/route.go @@ -40,7 +40,7 @@ func NewBuilder(apiClient *clients.Settings, name, nsname, serviceName string) * return nil } - builder := Builder{ + builder := &Builder{ apiClient: apiClient.Client, Definition: &routev1.Route{ ObjectMeta: metav1.ObjectMeta{ @@ -60,21 +60,27 @@ func NewBuilder(apiClient *clients.Settings, name, nsname, serviceName string) * glog.V(100).Infof("The name of the route is empty") builder.errorMsg = "route 'name' cannot be empty" + + return builder } if nsname == "" { glog.V(100).Infof("The namespace of the route is empty") builder.errorMsg = "route 'nsname' cannot be empty" + + return builder } if serviceName == "" { glog.V(100).Infof("The serviceName of the route is empty") builder.errorMsg = "route 'serviceName' cannot be empty" + + return builder } - return &builder + return builder } // Pull loads existing route from cluster. @@ -87,7 +93,7 @@ func Pull(apiClient *clients.Settings, name, nsname string) (*Builder, error) { return nil, fmt.Errorf("the apiClient cannot be nil") } - builder := Builder{ + builder := &Builder{ apiClient: apiClient.Client, Definition: &routev1.Route{ ObjectMeta: metav1.ObjectMeta{ @@ -115,7 +121,7 @@ func Pull(apiClient *clients.Settings, name, nsname string) (*Builder, error) { builder.Definition = builder.Object - return &builder, nil + return builder, nil } // WithTargetPortNumber adds a target port to the route by number. @@ -149,9 +155,7 @@ func (builder *Builder) WithTargetPortName(portName string) *Builder { glog.V(100).Infof("Received empty route portName") builder.errorMsg = "route target port name cannot be empty string" - } - if builder.errorMsg != "" { return builder } @@ -178,9 +182,7 @@ func (builder *Builder) WithWildCardPolicy(wildcardPolicy string) *Builder { builder.errorMsg = fmt.Sprintf("received unsupported route wildcardPolicy: supported policies %v", supportedWildCardPolicies()) - } - if builder.errorMsg != "" { return builder } @@ -226,7 +228,7 @@ func (builder *Builder) Get() (*routev1.Route, error) { return nil, err } - return route, err + return route, nil } // Create makes a route according to the route definition and stores the created object in the route builder. @@ -287,13 +289,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 != "" { diff --git a/pkg/scc/scc.go b/pkg/scc/scc.go index d7f7564ff..b611f6aac 100644 --- a/pkg/scc/scc.go +++ b/pkg/scc/scc.go @@ -37,7 +37,7 @@ func NewBuilder(apiClient *clients.Settings, name, runAsUser, selinuxContext str "Initializing new SecurityContextConstraints structure with the following params: "+ "name: %s, runAsUser type: %s, selinuxContext type: %s", name, runAsUser, selinuxContext) - builder := Builder{ + builder := &Builder{ apiClient: apiClient, Definition: &securityV1.SecurityContextConstraints{ ObjectMeta: metav1.ObjectMeta{ @@ -56,28 +56,34 @@ func NewBuilder(apiClient *clients.Settings, name, runAsUser, selinuxContext str glog.V(100).Infof("The name of the SecurityContextConstraints is empty") builder.errorMsg = "SecurityContextConstraints 'name' cannot be empty" + + return builder } if runAsUser == "" { glog.V(100).Infof("The runAsUser of the SecurityContextConstraints is empty") builder.errorMsg = "SecurityContextConstraints 'runAsUser' cannot be empty" + + return builder } if selinuxContext == "" { glog.V(100).Infof("The selinuxContext of the SecurityContextConstraints is empty") builder.errorMsg = "SecurityContextConstraints 'selinuxContext' cannot be empty" + + return builder } - return &builder + return builder } // Pull pulls existing SecurityContextConstraints from cluster. func Pull(apiClient *clients.Settings, name string) (*Builder, error) { glog.V(100).Infof("Pulling existing SecurityContextConstraints object name %s from cluster", name) - builder := Builder{ + builder := &Builder{ apiClient: apiClient, Definition: &securityV1.SecurityContextConstraints{ ObjectMeta: metav1.ObjectMeta{ @@ -89,7 +95,7 @@ func Pull(apiClient *clients.Settings, name string) (*Builder, error) { if name == "" { glog.V(100).Infof("The name of the SecurityContextConstraints is empty") - builder.errorMsg = "SecurityContextConstraints 'name' cannot be empty" + return nil, fmt.Errorf("SecurityContextConstraints 'name' cannot be empty") } if !builder.Exists() { @@ -98,7 +104,7 @@ func Pull(apiClient *clients.Settings, name string) (*Builder, error) { builder.Definition = builder.Object - return &builder, nil + return builder, nil } // WithPrivilegedContainer adds bool flag to the allowPrivilegedContainer of SecurityContextConstraints. @@ -524,9 +530,13 @@ func (builder *Builder) Delete() error { err := builder.apiClient.SecurityContextConstraints().Delete( context.TODO(), builder.Object.Name, metav1.DeleteOptions{}) + if err != nil { + return err + } + builder.Object = nil - return err + return nil } // Update modifies an existing SecurityContextConstraints in the cluster. @@ -573,13 +583,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 != "" { diff --git a/pkg/secret/secret.go b/pkg/secret/secret.go index a0b01f9c1..ef10819f4 100644 --- a/pkg/secret/secret.go +++ b/pkg/secret/secret.go @@ -33,7 +33,7 @@ func NewBuilder(apiClient *clients.Settings, name, nsname string, secretType cor "Initializing new secret structure with the following params: %s, %s, %s", name, nsname, string(secretType)) - builder := Builder{ + builder := &Builder{ apiClient: apiClient, Definition: &corev1.Secret{ ObjectMeta: metav1.ObjectMeta{ @@ -48,22 +48,26 @@ func NewBuilder(apiClient *clients.Settings, name, nsname string, secretType cor glog.V(100).Infof("The name of the secret is empty") builder.errorMsg = "secret 'name' cannot be empty" + + return builder } if nsname == "" { glog.V(100).Infof("The namespace of the secret is empty") builder.errorMsg = "secret 'nsname' cannot be empty" + + return builder } - return &builder + return builder } // Pull loads an existing secret into Builder struct. func Pull(apiClient *clients.Settings, name, nsname string) (*Builder, error) { glog.V(100).Infof("Pulling existing secret name: %s under namespace: %s", name, nsname) - builder := Builder{ + builder := &Builder{ apiClient: apiClient, Definition: &corev1.Secret{ ObjectMeta: metav1.ObjectMeta{ @@ -74,11 +78,15 @@ func Pull(apiClient *clients.Settings, name, nsname string) (*Builder, error) { } if name == "" { - builder.errorMsg = "secret 'name' cannot be empty" + glog.V(100).Infof("The name of the secret is empty") + + return nil, fmt.Errorf("secret 'name' cannot be empty") } if nsname == "" { - builder.errorMsg = "secret 'namespace' cannot be empty" + glog.V(100).Infof("The namespace of the secret is empty") + + return nil, fmt.Errorf("secret 'namespace' cannot be empty") } if !builder.Exists() { @@ -87,7 +95,7 @@ func Pull(apiClient *clients.Settings, name, nsname string) (*Builder, error) { builder.Definition = builder.Object - return &builder, nil + return builder, nil } // Create makes a secret in the cluster and stores the created object in struct. @@ -128,7 +136,7 @@ func (builder *Builder) Delete() error { builder.Object = nil - return err + return nil } // Exists checks whether the given secret exists. @@ -179,9 +187,7 @@ func (builder *Builder) WithData(data map[string][]byte) *Builder { glog.V(100).Infof("The data of the secret is empty") builder.errorMsg = "'data' cannot be empty" - } - if builder.errorMsg != "" { return builder } @@ -229,13 +235,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 != "" { diff --git a/pkg/service/service.go b/pkg/service/service.go index 862972644..0ce0f59fd 100644 --- a/pkg/service/service.go +++ b/pkg/service/service.go @@ -42,7 +42,7 @@ func NewBuilder( glog.V(100).Infof( "Initializing new service structure with the following params: %s, %s", name, nsname) - builder := Builder{ + builder := &Builder{ apiClient: apiClient, Definition: &corev1.Service{ ObjectMeta: metav1.ObjectMeta{ @@ -60,15 +60,19 @@ func NewBuilder( glog.V(100).Infof("The name of the service is empty") builder.errorMsg = "Service 'name' cannot be empty" + + return builder } if nsname == "" { glog.V(100).Infof("The namespace of the service is empty") builder.errorMsg = "Namespace 'nsname' cannot be empty" + + return builder } - return &builder + return builder } // WithNodePort redefines the service with NodePort service type. @@ -94,7 +98,7 @@ func (builder *Builder) WithNodePort() *Builder { func Pull(apiClient *clients.Settings, name, nsname string) (*Builder, error) { glog.V(100).Infof("Pulling existing service name: %s under namespace: %s", name, nsname) - builder := Builder{ + builder := &Builder{ apiClient: apiClient, Definition: &corev1.Service{ ObjectMeta: metav1.ObjectMeta{ @@ -105,11 +109,15 @@ func Pull(apiClient *clients.Settings, name, nsname string) (*Builder, error) { } if name == "" { - builder.errorMsg = "service 'name' cannot be empty" + glog.V(100).Infof("The name of the service is empty") + + return nil, fmt.Errorf("service 'name' cannot be empty") } if nsname == "" { - builder.errorMsg = "service 'namespace' cannot be empty" + glog.V(100).Infof("The namespace of the service is empty") + + return nil, fmt.Errorf("service 'namespace' cannot be empty") } if !builder.Exists() { @@ -118,7 +126,7 @@ func Pull(apiClient *clients.Settings, name, nsname string) (*Builder, error) { builder.Definition = builder.Object - return &builder, nil + return builder, nil } // Create the service in the cluster and store the created object in Object. @@ -176,7 +184,7 @@ func (builder *Builder) Delete() error { builder.Object = nil - return err + return nil } // WithOptions creates service with generic mutation options. @@ -220,9 +228,7 @@ func (builder *Builder) WithExternalTrafficPolicy(policyType corev1.ServiceExter builder.Definition.Name, builder.Definition.Namespace) builder.errorMsg = "ExternalTrafficPolicy can not be empty" - } - if builder.errorMsg != "" { return builder } @@ -247,9 +253,7 @@ func (builder *Builder) WithAnnotation(annotation map[string]string) *Builder { builder.Definition.Name, builder.Definition.Namespace) builder.errorMsg = "Annotation can not be empty map" - } - if builder.errorMsg != "" { return builder } @@ -271,6 +275,8 @@ func (builder *Builder) WithIPFamily(ipFamily []corev1.IPFamily, ipStackPolicy c builder.Definition.Name, builder.Definition.Namespace) builder.errorMsg = "failed to set empty ipFamily" + + return builder } if ipStackPolicy == "" { @@ -278,9 +284,7 @@ func (builder *Builder) WithIPFamily(ipFamily []corev1.IPFamily, ipStackPolicy c builder.Definition.Name, builder.Definition.Namespace) builder.errorMsg = "failed to set empty ipStackPolicy" - } - if builder.errorMsg != "" { return builder } @@ -343,13 +347,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 != "" { diff --git a/pkg/serviceaccount/serviceaccount.go b/pkg/serviceaccount/serviceaccount.go index bac3fc8d7..0a838e6d9 100644 --- a/pkg/serviceaccount/serviceaccount.go +++ b/pkg/serviceaccount/serviceaccount.go @@ -32,7 +32,7 @@ type AdditionalOptions func(builder *Builder) (*Builder, error) func NewBuilder(apiClient *clients.Settings, name, nsname string) *Builder { glog.V(100).Infof("Initializing new serviceaccount structure with the following params: %s, %s", name, nsname) - builder := Builder{ + builder := &Builder{ apiClient: apiClient, Definition: &corev1.ServiceAccount{ ObjectMeta: metav1.ObjectMeta{ @@ -46,22 +46,26 @@ func NewBuilder(apiClient *clients.Settings, name, nsname string) *Builder { glog.V(100).Infof("The name of the serviceaccount is empty") builder.errorMsg = "serviceaccount 'name' cannot be empty" + + return builder } if nsname == "" { glog.V(100).Infof("The namespace of the serviceaccount is empty") builder.errorMsg = "serviceaccount 'nsname' cannot be empty" + + return builder } - return &builder + return builder } // Pull loads an existing serviceaccount into Builder struct. func Pull(apiClient *clients.Settings, name, nsname string) (*Builder, error) { glog.V(100).Infof("Pulling existing serviceaccount name: %s under namespace: %s", name, nsname) - builder := Builder{ + builder := &Builder{ apiClient: apiClient, Definition: &corev1.ServiceAccount{ ObjectMeta: metav1.ObjectMeta{ @@ -72,11 +76,15 @@ func Pull(apiClient *clients.Settings, name, nsname string) (*Builder, error) { } if name == "" { - builder.errorMsg = "serviceaccount 'name' cannot be empty" + glog.V(100).Infof("The name of the serviceaccount is empty") + + return nil, fmt.Errorf("serviceaccount 'name' cannot be empty") } if nsname == "" { - builder.errorMsg = "serviceaccount 'namespace' cannot be empty" + glog.V(100).Infof("The namespace of the serviceaccount is empty") + + return nil, fmt.Errorf("serviceaccount 'namespace' cannot be empty") } if !builder.Exists() { @@ -85,7 +93,7 @@ func Pull(apiClient *clients.Settings, name, nsname string) (*Builder, error) { builder.Definition = builder.Object - return &builder, nil + return builder, nil } // Create makes a serviceaccount in cluster and stores the created object in struct. @@ -130,7 +138,7 @@ func (builder *Builder) Delete() error { builder.Object = nil - return err + return nil } // Exists checks whether the given serviceaccount exists. @@ -189,13 +197,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 != "" { diff --git a/pkg/servicemesh/controlplane.go b/pkg/servicemesh/controlplane.go index e56d45fa0..47b28ef59 100644 --- a/pkg/servicemesh/controlplane.go +++ b/pkg/servicemesh/controlplane.go @@ -46,12 +46,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 @@ -108,17 +112,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{ @@ -154,15 +158,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 } @@ -198,17 +202,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{ @@ -249,6 +253,8 @@ func (builder *ControlPlaneBuilder) WithPrometheusAddon( builder.errorMsg = "the Prometheus addon 'prometheusInstallConfig' cannot " + "be empty when Prometheus addon is enabled" + + return builder } if metricsExpiryDuration == "" { @@ -256,17 +262,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{ @@ -320,7 +326,7 @@ func PullControlPlane(apiClient *clients.Settings, name, nsname string) (*Contro return nil, fmt.Errorf("serviceMeshControlPlane 'apiClient' cannot be empty") } - builder := ControlPlaneBuilder{ + builder := &ControlPlaneBuilder{ apiClient: apiClient.Client, Definition: &istiov2.ServiceMeshControlPlane{ ObjectMeta: metav1.ObjectMeta{ @@ -342,17 +348,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. @@ -490,13 +492,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 31564bc02..0be262b01 100644 --- a/pkg/servicemesh/memberroll.go +++ b/pkg/servicemesh/memberroll.go @@ -50,12 +50,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 @@ -72,7 +76,7 @@ func PullMemberRoll(apiClient *clients.Settings, name, nsname string) (*MemberRo return nil, fmt.Errorf("serviceMeshMemberRoll 'apiClient' cannot be empty") } - builder := MemberRollBuilder{ + builder := &MemberRollBuilder{ apiClient: apiClient.Client, Definition: &istiov1.ServiceMeshMemberRoll{ ObjectMeta: metav1.ObjectMeta{ @@ -100,7 +104,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. @@ -230,9 +234,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 } @@ -307,13 +309,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 != "" { diff --git a/pkg/sriov-fec/nodeconfig.go b/pkg/sriov-fec/nodeconfig.go index d498e19f6..39bb68548 100644 --- a/pkg/sriov-fec/nodeconfig.go +++ b/pkg/sriov-fec/nodeconfig.go @@ -46,7 +46,7 @@ func NewNodeConfigBuilder( "Initializing new SriovFecNodeConfig structure with the following params: %s, %s, %v", name, nsname, label) - builder := NodeConfigBuilder{ + builder := &NodeConfigBuilder{ apiClient: apiClient, Definition: &sriovfectypes.SriovFecNodeConfig{ TypeMeta: metaV1.TypeMeta{ @@ -64,15 +64,19 @@ func NewNodeConfigBuilder( glog.V(100).Infof("The name of the SriovFecNodeConfig is empty") builder.errorMsg = "SriovFecNodeConfig 'name' cannot be empty" + + return builder } if nsname == "" { glog.V(100).Infof("The namespace of the SriovFecNodeConfig is empty") builder.errorMsg = "SriovFecNodeConfig 'nsname' cannot be empty" + + return builder } - return &builder + return builder } // Pull retrieves an existing SriovFecNodeConfig.io object from the cluster. @@ -80,7 +84,7 @@ func Pull(apiClient *clients.Settings, name, nsname string) (*NodeConfigBuilder, glog.V(100).Infof( "Pulling SriovFecNodeConfig.io object name: %s in namespace: %s", name, nsname) - builder := NodeConfigBuilder{ + builder := &NodeConfigBuilder{ apiClient: apiClient, Definition: &sriovfectypes.SriovFecNodeConfig{ ObjectMeta: metaV1.ObjectMeta{ @@ -91,10 +95,14 @@ func Pull(apiClient *clients.Settings, name, nsname string) (*NodeConfigBuilder, } if name == "" { + glog.V(100).Infof("The name of the SriovFecNodeConfig is empty") + return nil, fmt.Errorf("the name of the SriovFecNodeConfig is empty") } if nsname == "" { + glog.V(100).Infof("The namespace of the SriovFecNodeConfig is empty") + return nil, fmt.Errorf("the namespace of the SriovFecNodeConfig is empty") } @@ -104,7 +112,7 @@ func Pull(apiClient *clients.Settings, name, nsname string) (*NodeConfigBuilder, builder.Definition = builder.Object - return &builder, nil + return builder, nil } // Exists checks whether the given SriovFecNodeConfig exists. @@ -232,10 +240,6 @@ func (builder *NodeConfigBuilder) Update(force bool) (*NodeConfigBuilder, error) builder.Definition.Name, builder.Definition.Namespace, ) - if builder.errorMsg != "" { - return nil, fmt.Errorf(builder.errorMsg) - } - builder.Definition.ResourceVersion = builder.Object.ResourceVersion builder.Definition.ObjectMeta.ResourceVersion = builder.Object.ObjectMeta.ResourceVersion @@ -317,13 +321,13 @@ func (builder *NodeConfigBuilder) 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/sriov/network.go b/pkg/sriov/network.go index 3afbe7975..5ec700cdc 100644 --- a/pkg/sriov/network.go +++ b/pkg/sriov/network.go @@ -38,7 +38,7 @@ type NetworkAdditionalOptions func(builder *NetworkBuilder) (*NetworkBuilder, er // NewNetworkBuilder creates new instance of Builder. func NewNetworkBuilder( apiClient *clients.Settings, name, nsname, targetNsname, resName string) *NetworkBuilder { - builder := NetworkBuilder{ + builder := &NetworkBuilder{ apiClient: apiClient.ClientSrIov, Definition: &srIovV1.SriovNetwork{ ObjectMeta: metav1.ObjectMeta{ @@ -54,21 +54,29 @@ func NewNetworkBuilder( if name == "" { builder.errorMsg = "SrIovNetwork 'name' cannot be empty" + + return builder } if nsname == "" { builder.errorMsg = "SrIovNetwork 'nsname' cannot be empty" + + return builder } if targetNsname == "" { builder.errorMsg = "SrIovNetwork 'targetNsname' cannot be empty" + + return builder } if resName == "" { builder.errorMsg = "SrIovNetwork 'resName' cannot be empty" + + return builder } - return &builder + return builder } // WithVLAN sets vlan id in the SrIovNetwork definition. Allowed vlanId range is between 0-4094. @@ -79,9 +87,7 @@ func (builder *NetworkBuilder) WithVLAN(vlanID uint16) *NetworkBuilder { if vlanID > 4094 { builder.errorMsg = "invalid vlanID, allowed vlanID values are between 0-4094" - } - if builder.errorMsg != "" { return builder } @@ -101,6 +107,8 @@ func (builder *NetworkBuilder) WithVlanProto(vlanProtocol string) *NetworkBuilde allowedVlanProto := []string{"802.1q", "802.1Q", "802.1ad", "802.1AD"} if !slices.Contains(allowedVlanProto, vlanProtocol) { builder.errorMsg = "invalid 'vlanProtocol' parameters" + + return builder } builder.Definition.Spec.VlanProto = vlanProtocol @@ -131,10 +139,6 @@ func (builder *NetworkBuilder) WithMetaPluginAllMultiFlag(allMultiFlag bool) *Ne builder.Definition.Spec.MetaPluginsConfig = fmt.Sprintf(`{ "type": "tuning", "allmulti": %t }`, allMultiFlag) - if builder.errorMsg != "" { - return builder - } - return builder } @@ -148,9 +152,7 @@ func (builder *NetworkBuilder) WithLinkState(linkState string) *NetworkBuilder { if !slices.Contains(allowedLinkStates, linkState) { builder.errorMsg = "invalid 'linkState' parameters" - } - if builder.errorMsg != "" { return builder } @@ -207,9 +209,7 @@ func (builder *NetworkBuilder) WithVlanQoS(qoSClass uint16) *NetworkBuilder { if qoSClass > 7 { builder.errorMsg = "Invalid QoS class. Supported vlan QoS class values are between 0...7" - } - if builder.errorMsg != "" { return builder } @@ -270,7 +270,7 @@ func PullNetwork(apiClient *clients.Settings, name, nsname string) (*NetworkBuil return nil, fmt.Errorf("sriovnetwork 'apiClient' cannot be empty") } - builder := NetworkBuilder{ + builder := &NetworkBuilder{ apiClient: apiClient.ClientSrIov, Definition: &srIovV1.SriovNetwork{ ObjectMeta: metav1.ObjectMeta{ @@ -298,7 +298,7 @@ func PullNetwork(apiClient *clients.Settings, name, nsname string) (*NetworkBuil builder.Definition = builder.Object - return &builder, nil + return builder, nil } // Create generates SrIovNetwork in a cluster and stores the created object in struct. @@ -340,7 +340,7 @@ func (builder *NetworkBuilder) Delete() error { builder.Object = nil - return err + return nil } // Exists checks whether the given SrIovNetwork object exists in a cluster. @@ -417,9 +417,7 @@ func (builder *NetworkBuilder) withIpam(ipamType string) *NetworkBuilder { glog.V(100).Infof("sriov network 'ipamType' parameter can not be empty") builder.errorMsg = "failed to configure IPAM, 'ipamType' parameter is empty" - } - if builder.errorMsg != "" { return builder } @@ -442,13 +440,13 @@ func (builder *NetworkBuilder) 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/sriov/networknodestate.go b/pkg/sriov/networknodestate.go index 2a4bf81c0..9e0005d79 100644 --- a/pkg/sriov/networknodestate.go +++ b/pkg/sriov/networknodestate.go @@ -44,12 +44,16 @@ func NewNetworkNodeStateBuilder(apiClient *clients.Settings, nodeName, nsname st glog.V(100).Infof("The name of the nodeName is empty") builder.errorMsg = "SriovNetworkNodeState 'nodeName' is empty" + + return builder } if nsname == "" { glog.V(100).Infof("The namespace of the SriovNetworkNodeState is empty") builder.errorMsg = "SriovNetworkNodeState 'nsname' is empty" + + return builder } return builder @@ -195,17 +199,13 @@ func (builder *NetworkNodeStateBuilder) findInterfaceByName(sriovInterfaceName s if err := builder.Discover(); err != nil { glog.V(100).Infof("Error to discover sriov network node state for node %s", builder.nodeName) - builder.errorMsg = "failed to discover sriov network node state" + return nil, fmt.Errorf("failed to discover sriov network node state") } if sriovInterfaceName == "" { glog.V(100).Infof("The sriovInterface can not be empty string") - builder.errorMsg = "the sriovInterface is an empty sting" - } - - if builder.errorMsg != "" { - return nil, fmt.Errorf(builder.errorMsg) + return nil, fmt.Errorf("sriovInterface can not be empty string") } for _, interf := range builder.Objects.Status.Interfaces { @@ -231,7 +231,7 @@ func (builder *NetworkNodeStateBuilder) validate() (bool, error) { 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/sriov/operatorconfig.go b/pkg/sriov/operatorconfig.go index 1c02b4e95..b9f04b466 100644 --- a/pkg/sriov/operatorconfig.go +++ b/pkg/sriov/operatorconfig.go @@ -49,6 +49,8 @@ func NewOperatorConfigBuilder(apiClient *clients.Settings, nsname string) *Opera glog.V(100).Infof("The namespace of the SriovOperatorConfig is empty") builder.errorMsg = "SriovOperatorConfig 'nsname' is empty" + + return builder } return builder @@ -88,7 +90,7 @@ func PullOperatorConfig(apiClient *clients.Settings, nsname string) (*OperatorCo return nil, fmt.Errorf("SriovOperatorConfig 'apiClient' cannot be empty") } - builder := OperatorConfigBuilder{ + builder := &OperatorConfigBuilder{ apiClient: apiClient.ClientSrIov, Definition: &srIovV1.SriovOperatorConfig{ ObjectMeta: metaV1.ObjectMeta{ @@ -111,7 +113,7 @@ func PullOperatorConfig(apiClient *clients.Settings, nsname string) (*OperatorCo builder.Definition = builder.Object - return &builder, nil + return builder, nil } // Exists checks whether the given SriovOperatorConfig exists. diff --git a/pkg/sriov/policy.go b/pkg/sriov/policy.go index 3af995195..778b28873 100644 --- a/pkg/sriov/policy.go +++ b/pkg/sriov/policy.go @@ -41,7 +41,7 @@ func NewPolicyBuilder( vfsNumber int, nicNames []string, nodeSelector map[string]string) *PolicyBuilder { - builder := PolicyBuilder{ + builder := &PolicyBuilder{ apiClient: apiClient.ClientSrIov, Definition: &srIovV1.SriovNetworkNodePolicy{ ObjectMeta: metav1.ObjectMeta{ @@ -61,30 +61,54 @@ func NewPolicyBuilder( } if name == "" { + glog.V(100).Infof("The name of the sriovnetworknodepolicy is empty") + builder.errorMsg = "SriovNetworkNodePolicy 'name' cannot be empty" + + return builder } if nsname == "" { + glog.V(100).Infof("The namespace of the sriovnetworknodepolicy is empty") + builder.errorMsg = "SriovNetworkNodePolicy 'nsname' cannot be empty" + + return builder } if resName == "" { + glog.V(100).Infof("The resName of the sriovnetworknodepolicy is empty") + builder.errorMsg = "SriovNetworkNodePolicy 'resName' cannot be empty" + + return builder } if len(nicNames) == 0 { + glog.V(100).Infof("The nicNames of the sriovnetworknodepolicy is empty") + builder.errorMsg = "SriovNetworkNodePolicy 'nicNames' cannot be empty list" + + return builder } if len(nodeSelector) == 0 { + glog.V(100).Infof("The nodeSelector of the sriovnetworknodepolicy is empty") + builder.errorMsg = "SriovNetworkNodePolicy 'nodeSelector' cannot be empty map" + + return builder } if vfsNumber <= 0 { + glog.V(100).Infof("The vfsNumber of the sriovnetworknodepolicy is zero or negative") + builder.errorMsg = "SriovNetworkNodePolicy 'vfsNumber' cannot be zero of negative" + + return builder } - return &builder + return builder } // WithDevType sets device type in the SriovNetworkNodePolicy definition. Allowed devTypes are vfio-pci and netdevice. @@ -114,17 +138,19 @@ func (builder *PolicyBuilder) WithVFRange(firstVF, lastVF int) *PolicyBuilder { if firstVF < 0 || lastVF < 0 { builder.errorMsg = "firstPF or lastVF can not be less than 0" + + return builder } if firstVF > lastVF { builder.errorMsg = "firstPF argument can not be greater than lastPF" + + return builder } if lastVF > 63 { builder.errorMsg = "lastVF can not be greater than 63" - } - if builder.errorMsg != "" { return builder } @@ -146,9 +172,7 @@ func (builder *PolicyBuilder) WithMTU(mtu int) *PolicyBuilder { if 1 > mtu || mtu > 9192 { builder.errorMsg = fmt.Sprintf("invalid mtu size %d allowed mtu should be in range 1...9192", mtu) - } - if builder.errorMsg != "" { return builder } @@ -231,7 +255,7 @@ func PullPolicy(apiClient *clients.Settings, name, nsname string) (*PolicyBuilde return nil, fmt.Errorf("sriovnetworknodepolicy 'apiClient' cannot be empty") } - builder := PolicyBuilder{ + builder := &PolicyBuilder{ apiClient: apiClient.ClientSrIov, Definition: &srIovV1.SriovNetworkNodePolicy{ ObjectMeta: metav1.ObjectMeta{ @@ -259,7 +283,7 @@ func PullPolicy(apiClient *clients.Settings, name, nsname string) (*PolicyBuilde builder.Definition = builder.Object - return &builder, nil + return builder, nil } // Create generates an SriovNetworkNodePolicy in the cluster and stores the created object in struct. @@ -301,7 +325,7 @@ func (builder *PolicyBuilder) Delete() error { builder.Object = nil - return err + return nil } // Exists checks whether the given SriovNetworkNodePolicy object exists in the cluster. @@ -331,13 +355,13 @@ func (builder *PolicyBuilder) 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/sriov/poolconfig.go b/pkg/sriov/poolconfig.go index a4d5b1b7c..da1be63b3 100644 --- a/pkg/sriov/poolconfig.go +++ b/pkg/sriov/poolconfig.go @@ -37,7 +37,7 @@ func NewPoolConfigBuilder(apiClient *clients.Settings, name, nsname string) *Poo glog.V(100).Infof( "Initializing new SriovNetworkPoolConfig structure with the name %s in the namespace %s", name, nsname) - builder := PoolConfigBuilder{ + builder := &PoolConfigBuilder{ apiClient: apiClient, Definition: &srIovV1.SriovNetworkPoolConfig{ ObjectMeta: metav1.ObjectMeta{ @@ -49,16 +49,16 @@ func NewPoolConfigBuilder(apiClient *clients.Settings, name, nsname string) *Poo if name == "" { builder.errorMsg = "SriovNetworkPoolConfig 'name' cannot be empty" - return &builder + return builder } if nsname == "" { builder.errorMsg = "SriovNetworkPoolConfig 'nsname' cannot be empty" - return &builder + return builder } - return &builder + return builder } // Create generates an SriovNetworkPoolConfig in the cluster and stores the created object in struct. @@ -292,13 +292,13 @@ func (builder *PoolConfigBuilder) 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/statefulset/statefulset.go b/pkg/statefulset/statefulset.go index 3f5e419a4..a3b31a93e 100644 --- a/pkg/statefulset/statefulset.go +++ b/pkg/statefulset/statefulset.go @@ -43,7 +43,7 @@ func NewBuilder( "name: %s, namespace: %s, labels: %s, containerSpec %v", name, nsname, labels, containerSpec) - builder := Builder{ + builder := &Builder{ apiClient: apiClient, Definition: &appsv1.StatefulSet{ Spec: appsv1.StatefulSetSpec{ @@ -69,21 +69,27 @@ func NewBuilder( glog.V(100).Infof("The name of the statefulset is empty") builder.errorMsg = "statefulset 'name' cannot be empty" + + return builder } if nsname == "" { glog.V(100).Infof("The namespace of the statefulset is empty") builder.errorMsg = "statefulset 'namespace' cannot be empty" + + return builder } if labels == nil { glog.V(100).Infof("There are no labels for the statefulset") builder.errorMsg = "statefulset 'labels' cannot be empty" + + return builder } - return &builder + return builder } // WithAdditionalContainerSpecs appends a list of container specs to the statefulset definition. @@ -99,9 +105,7 @@ func (builder *Builder) WithAdditionalContainerSpecs(specs []corev1.Container) * glog.V(100).Infof("The container specs are empty") builder.errorMsg = "cannot accept nil or empty list as container specs" - } - if builder.errorMsg != "" { return builder } @@ -145,7 +149,7 @@ func (builder *Builder) WithOptions(options ...AdditionalOptions) *Builder { func Pull(apiClient *clients.Settings, name, nsname string) (*Builder, error) { glog.V(100).Infof("Pulling existing statefulset name: %s under namespace: %s", name, nsname) - builder := Builder{ + builder := &Builder{ apiClient: apiClient, Definition: &appsv1.StatefulSet{ ObjectMeta: metav1.ObjectMeta{ @@ -156,11 +160,19 @@ func Pull(apiClient *clients.Settings, name, nsname string) (*Builder, error) { } if name == "" { + glog.V(100).Infof("The name of the statefulset is empty") + builder.errorMsg = "statefulset 'name' cannot be empty" + + return nil, fmt.Errorf("statefulset 'name' cannot be empty") } if nsname == "" { + glog.V(100).Infof("The namespace of the statefulset is empty") + builder.errorMsg = "statefulset 'namespace' cannot be empty" + + return nil, fmt.Errorf("statefulset 'namespace' cannot be empty") } if !builder.Exists() { @@ -169,7 +181,7 @@ func Pull(apiClient *clients.Settings, name, nsname string) (*Builder, error) { builder.Definition = builder.Object - return &builder, nil + return builder, nil } // Create generates a statefulset in cluster and stores the created object in struct. @@ -257,13 +269,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 != "" { diff --git a/pkg/storage/pvc.go b/pkg/storage/pvc.go index 0bd016873..ed02f7be8 100644 --- a/pkg/storage/pvc.go +++ b/pkg/storage/pvc.go @@ -41,7 +41,7 @@ func NewPVCBuilder(apiClient *clients.Settings, name, nsname string) *PVCBuilder glog.V(100).Infof("Creating PersistentVolumeClaim %s in namespace %s", name, nsname) - builder := PVCBuilder{ + builder := &PVCBuilder{ Definition: &corev1.PersistentVolumeClaim{ ObjectMeta: metav1.ObjectMeta{ Name: name, @@ -57,15 +57,19 @@ func NewPVCBuilder(apiClient *clients.Settings, name, nsname string) *PVCBuilder glog.V(100).Infof("PVC name is empty") builder.errorMsg = "PVC name is empty" + + return builder } if nsname == "" { glog.V(100).Infof("PVC namespace is empty") builder.errorMsg = "PVC namespace is empty" + + return builder } - return &builder + return builder } // WithPVCAccessMode configure access mode for the PV. @@ -74,16 +78,14 @@ func (builder *PVCBuilder) WithPVCAccessMode(accessMode string) (*PVCBuilder, er if accessMode == "" { glog.V(100).Infof("Empty accessMode for PVC %s", builder.Definition.Name) - builder.errorMsg = "Empty accessMode for PVC requested" - return builder, fmt.Errorf(builder.errorMsg) + return nil, fmt.Errorf("empty accessMode for PVC requested") } if !validatePVCAccessMode(accessMode) { glog.V(100).Infof("Invalid accessMode for PVC %s", accessMode) - builder.errorMsg = fmt.Sprintf("Invalid accessMode for PVC %s", accessMode) - return builder, fmt.Errorf(builder.errorMsg) + return builder, fmt.Errorf("invalid accessMode for PVC %s", accessMode) } if builder.Definition.Spec.AccessModes != nil { @@ -111,17 +113,14 @@ func (builder *PVCBuilder) WithPVCCapacity(capacity string) (*PVCBuilder, error) if capacity == "" { glog.V(100).Infof("Capacity of the PersistentVolumeClaim is empty") - builder.errorMsg = "Capacity of the PersistentVolumeClaim is empty" - - return builder, fmt.Errorf(builder.errorMsg) + return nil, fmt.Errorf("capacity of the PersistentVolumeClaim is empty") } defer func() (*PVCBuilder, error) { if r := recover(); r != nil { glog.V(100).Infof("Failed to parse %v", capacity) - builder.errorMsg = fmt.Sprintf("Failed to parse: %v", capacity) - return builder, fmt.Errorf("failed to parse: %v", capacity) + return nil, fmt.Errorf("failed to parse: %v", capacity) } return builder, nil @@ -142,10 +141,8 @@ func (builder *PVCBuilder) WithStorageClass(storageClass string) (*PVCBuilder, e if storageClass == "" { glog.V(100).Infof("Empty storageClass requested for the PersistentVolumeClaim", storageClass) - builder.errorMsg = fmt.Sprintf("Empty storageClass requested for the PersistentVolumeClaim %s", + return nil, fmt.Errorf("empty storageClass requested for the PersistentVolumeClaim %s", builder.Definition.Name) - - return builder, fmt.Errorf(builder.errorMsg) } builder.Definition.Spec.StorageClassName = &storageClass @@ -161,19 +158,15 @@ func (builder *PVCBuilder) WithVolumeMode(volumeMode string) (*PVCBuilder, error glog.V(100).Infof(fmt.Sprintf("Empty volumeMode requested for the PersistentVolumeClaim %s in %s namespace", builder.Definition.Name, builder.Definition.Namespace)) - builder.errorMsg = fmt.Sprintf("Empty volumeMode requested for the PersistentVolumeClaim %s in %s namespace", + return nil, fmt.Errorf("empty volumeMode requested for the PersistentVolumeClaim %s in %s namespace", builder.Definition.Name, builder.Definition.Namespace) - - return builder, fmt.Errorf(builder.errorMsg) } if !validateVolumeMode(volumeMode) { glog.V(100).Infof(fmt.Sprintf("Unsupported VolumeMode: %s", volumeMode)) - builder.errorMsg = fmt.Sprintf("Unsupported VolumeMode %q requested for %s PersistentVolumeClaim in %s namespace", + return nil, fmt.Errorf("unsupported VolumeMode %q requested for %s PersistentVolumeClaim in %s namespace", volumeMode, builder.Definition.Name, builder.Definition.Name) - - return builder, fmt.Errorf(builder.errorMsg) } // volumeMode is string while Spec.VolumeMode requires pointer to corev1.PersistentVolumeMode, @@ -202,9 +195,7 @@ func (builder *PVCBuilder) Create() (*PVCBuilder, error) { if err != nil { glog.V(100).Infof("Error creating persistentVolumeClaim %s - %v", builder.Definition.Name, err) - builder.errorMsg = fmt.Sprintf("failed to create PVC: %v", err) - - return builder, err + return nil, fmt.Errorf("failed to create PVC: %w", err) } return builder, nil @@ -245,7 +236,7 @@ func (builder *PVCBuilder) Delete() error { builder.Object = nil - return err + return nil } // DeleteAndWait deletes PersistentVolumeClaim and waits until it is removed from the cluster. @@ -288,7 +279,7 @@ func PullPersistentVolumeClaim( glog.V(100).Infof("Pulling existing PersistentVolumeClaim object: %s from namespace %s", persistentVolumeClaim, nsname) - builder := PVCBuilder{ + builder := &PVCBuilder{ apiClient: apiClient, Definition: &corev1.PersistentVolumeClaim{ ObjectMeta: metav1.ObjectMeta{ @@ -305,7 +296,7 @@ func PullPersistentVolumeClaim( builder.Definition = builder.Object - return &builder, nil + return builder, nil } // Exists checks whether the given PersistentVolumeClaim exists. diff --git a/pkg/storage/storageclass.go b/pkg/storage/storageclass.go index ce94e2c40..f872a03d6 100644 --- a/pkg/storage/storageclass.go +++ b/pkg/storage/storageclass.go @@ -35,7 +35,7 @@ func NewClassBuilder(apiClient *clients.Settings, name, provisioner string) *Cla "Initializing new storageclass structure with the following params: "+ "name: %s, provisioner: %s", name, provisioner) - builder := ClassBuilder{ + builder := &ClassBuilder{ apiClient: apiClient, Definition: &storageV1.StorageClass{ ObjectMeta: metav1.ObjectMeta{ @@ -49,15 +49,19 @@ func NewClassBuilder(apiClient *clients.Settings, name, provisioner string) *Cla glog.V(100).Infof("The name of the storageclass is empty") builder.errorMsg = "storageclass 'name' cannot be empty" + + return builder } if provisioner == "" { glog.V(100).Infof("The provisioner of the storageclass is empty") builder.errorMsg = "storageclass 'provisioner' cannot be empty" + + return builder } - return &builder + return builder } // WithReclaimPolicy adds a reclaimPolicy to the storageclass definition. @@ -71,9 +75,7 @@ func (builder *ClassBuilder) WithReclaimPolicy( glog.V(100).Infof("The reclaimPolicy of the storageclass is empty") builder.errorMsg = "storageclass 'reclaimPolicy' cannot be empty" - } - if builder.errorMsg != "" { return builder } @@ -93,9 +95,7 @@ func (builder *ClassBuilder) WithVolumeBindingMode( glog.V(100).Infof("The bindingMode of the storageclass is empty") builder.errorMsg = "storageclass 'bindingMode' cannot be empty" - } - if builder.errorMsg != "" { return builder } @@ -114,15 +114,15 @@ func (builder *ClassBuilder) WithParameter(parameterKey, parameterValue string) glog.V(100).Infof("The parameter key of the storageclass is empty") builder.errorMsg = "storageclass parameter key cannot be empty" + + return builder } if parameterValue == "" { glog.V(100).Infof("The parameter value of the storageclass is empty") builder.errorMsg = "storageclass parameter value cannot be empty" - } - if builder.errorMsg != "" { return builder } @@ -214,7 +214,7 @@ func (builder *ClassBuilder) Delete() error { builder.Object = nil - return err + return nil } // Update renovates the existing storageclass object with the storageclass definition in builder. @@ -230,11 +230,7 @@ func (builder *ClassBuilder) Update(force bool) (*ClassBuilder, error) { glog.V(100).Infof("storageclass %s does not exist", builder.Definition.Name) - builder.errorMsg = "Cannot update non-existent storageclass" - } - - if builder.errorMsg != "" { - return nil, fmt.Errorf(builder.errorMsg) + return nil, fmt.Errorf("cannot update non-existent storageclass") } err := builder.apiClient.Update(context.TODO(), builder.Definition) @@ -282,13 +278,13 @@ func (builder *ClassBuilder) 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/velero/backup.go b/pkg/velero/backup.go index 3450eb70e..fdac1f0fd 100644 --- a/pkg/velero/backup.go +++ b/pkg/velero/backup.go @@ -46,12 +46,16 @@ func NewBackupBuilder(apiClient *clients.Settings, name, nsname string) *BackupB glog.V(100).Infof("The name of the backup is empty") builder.errorMsg = "backup name cannot be an empty string" + + return builder } if nsname == "" { glog.V(100).Infof("The namespace of the backup is empty") builder.errorMsg = "backup namespace cannot be an empty string" + + return builder } return builder @@ -61,7 +65,7 @@ func NewBackupBuilder(apiClient *clients.Settings, name, nsname string) *BackupB func PullBackup(apiClient *clients.Settings, name, nsname string) (*BackupBuilder, error) { glog.V(100).Infof("Pulling existing backup name: %s under namespace: %s", name, nsname) - builder := BackupBuilder{ + builder := &BackupBuilder{ apiClient: apiClient.VeleroClient, Definition: &velerov1.Backup{ ObjectMeta: metav1.ObjectMeta{ @@ -72,10 +76,14 @@ func PullBackup(apiClient *clients.Settings, name, nsname string) (*BackupBuilde } if name == "" { + glog.V(100).Infof("Backup name is empty") + return nil, fmt.Errorf("backup name cannot be empty") } if nsname == "" { + glog.V(100).Infof("Backup namespace is empty") + return nil, fmt.Errorf("backup namespace cannot be empty") } @@ -85,7 +93,7 @@ func PullBackup(apiClient *clients.Settings, name, nsname string) (*BackupBuilde builder.Definition = builder.Object - return &builder, nil + return builder, nil } // WithStorageLocation adds a storage location to the backup. @@ -102,9 +110,7 @@ func (builder *BackupBuilder) WithStorageLocation(location string) *BackupBuilde glog.V(100).Infof("Backup storage location is empty") builder.errorMsg = "backup storage location cannot be an empty string" - } - if builder.errorMsg != "" { return builder } @@ -131,9 +137,7 @@ func (builder *BackupBuilder) WithIncludedNamespace(namespace string) *BackupBui glog.V(100).Infof("Backup includedNamespace is empty") builder.errorMsg = "backup includedNamespace cannot be an empty string" - } - if builder.errorMsg != "" { return builder } @@ -156,9 +160,7 @@ func (builder *BackupBuilder) WithIncludedClusterScopedResource(crd string) *Bac glog.V(100).Infof("Backup includedClusterScopedResource is empty") builder.errorMsg = "backup includedClusterScopedResource cannot be an empty string" - } - if builder.errorMsg != "" { return builder } @@ -182,9 +184,7 @@ func (builder *BackupBuilder) WithIncludedNamespaceScopedResource(crd string) *B glog.V(100).Infof("Backup includedNamespaceScopedResource is empty") builder.errorMsg = "backup includedNamespaceScopedResource cannot be an empty string" - } - if builder.errorMsg != "" { return builder } @@ -208,9 +208,7 @@ func (builder *BackupBuilder) WithExcludedClusterScopedResource(crd string) *Bac glog.V(100).Infof("Backup excludedClusterScopedResource is empty") builder.errorMsg = "backup excludedClusterScopedResource cannot be an empty string" - } - if builder.errorMsg != "" { return builder } @@ -234,9 +232,7 @@ func (builder *BackupBuilder) WithExcludedNamespaceScopedResources(crd string) * glog.V(100).Infof("Backup excludedNamespaceScopedResource is empty") builder.errorMsg = "backup excludedNamespaceScopedResource cannot be an empty string" - } - if builder.errorMsg != "" { return builder } @@ -334,13 +330,13 @@ func (builder *BackupBuilder) 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/velero/restore.go b/pkg/velero/restore.go index 771365a19..bdb25576f 100644 --- a/pkg/velero/restore.go +++ b/pkg/velero/restore.go @@ -48,18 +48,24 @@ func NewRestoreBuilder(apiClient *clients.Settings, name, nsname, backupName str glog.V(100).Infof("The name of the restore is empty") builder.errorMsg = "restore name cannot be an empty string" + + return builder } if nsname == "" { glog.V(100).Infof("The namespace of the restore is empty") builder.errorMsg = "restore namespace cannot be an empty string" + + return builder } if backupName == "" { glog.V(100).Infof("The backupName of the restore is empty") builder.errorMsg = "restore backupName cannot be an empty string" + + return builder } return builder @@ -69,7 +75,7 @@ func NewRestoreBuilder(apiClient *clients.Settings, name, nsname, backupName str func PullRestore(apiClient *clients.Settings, name, nsname string) (*RestoreBuilder, error) { glog.V(100).Infof("Pulling existing restore name: %s under namespace: %s", name, nsname) - builder := RestoreBuilder{ + builder := &RestoreBuilder{ apiClient: apiClient.VeleroClient, Definition: &velerov1.Restore{ ObjectMeta: metav1.ObjectMeta{ @@ -80,10 +86,14 @@ func PullRestore(apiClient *clients.Settings, name, nsname string) (*RestoreBuil } if name == "" { + glog.V(100).Infof("The name of the restore is empty") + return nil, fmt.Errorf("restore name cannot be empty") } if nsname == "" { + glog.V(100).Infof("The namespace of the restore is empty") + return nil, fmt.Errorf("restore namespace cannot be empty") } @@ -93,7 +103,7 @@ func PullRestore(apiClient *clients.Settings, name, nsname string) (*RestoreBuil builder.Definition = builder.Object - return &builder, nil + return builder, nil } // WithStorageLocation adds a storage location to the restore. @@ -110,9 +120,7 @@ func (builder *RestoreBuilder) WithStorageLocation(location string) *RestoreBuil glog.V(100).Infof("Backup storage location is empty") builder.errorMsg = "restore storage location cannot be an empty string" - } - if builder.errorMsg != "" { return builder } @@ -213,13 +221,13 @@ func (builder *RestoreBuilder) 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/webhook/mutatingwebhook.go b/pkg/webhook/mutatingwebhook.go index 6ecb0dfdc..cab8f498b 100644 --- a/pkg/webhook/mutatingwebhook.go +++ b/pkg/webhook/mutatingwebhook.go @@ -31,7 +31,7 @@ type MutatingConfigurationBuilder struct { func PullMutatingConfiguration(apiClient *clients.Settings, name string) (*MutatingConfigurationBuilder, error) { glog.V(100).Infof("Pulling existing MutatingWebhookConfiguration name %s from cluster", name) - builder := MutatingConfigurationBuilder{ + builder := &MutatingConfigurationBuilder{ apiClient: apiClient, Definition: &admregv1.MutatingWebhookConfiguration{ ObjectMeta: metav1.ObjectMeta{ @@ -43,7 +43,7 @@ func PullMutatingConfiguration(apiClient *clients.Settings, name string) (*Mutat if name == "" { glog.V(100).Infof("The name of the MutatingWebhookConfiguration is empty") - builder.errorMsg = "MutatingWebhookConfiguration 'name' cannot be empty" + return nil, fmt.Errorf("MutatingWebhookConfiguration 'name' cannot be empty") } if !builder.Exists() { @@ -52,7 +52,7 @@ func PullMutatingConfiguration(apiClient *clients.Settings, name string) (*Mutat builder.Definition = builder.Object - return &builder, nil + return builder, nil } // Exists checks whether the given MutatingWebhookConfiguration object exists in the cluster. @@ -141,7 +141,7 @@ func (builder *MutatingConfigurationBuilder) validate() (bool, error) { 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/webhook/validatingwebhook.go b/pkg/webhook/validatingwebhook.go index 505ec6ec7..08bbe67ed 100644 --- a/pkg/webhook/validatingwebhook.go +++ b/pkg/webhook/validatingwebhook.go @@ -32,7 +32,7 @@ func PullValidatingConfiguration(apiClient *clients.Settings, name string) ( *ValidatingConfigurationBuilder, error) { glog.V(100).Infof("Pulling existing ValidatingWebhookConfiguration name %s from cluster", name) - builder := ValidatingConfigurationBuilder{ + builder := &ValidatingConfigurationBuilder{ apiClient: apiClient, Definition: &admregv1.ValidatingWebhookConfiguration{ ObjectMeta: metav1.ObjectMeta{ @@ -45,6 +45,8 @@ func PullValidatingConfiguration(apiClient *clients.Settings, name string) ( glog.V(100).Infof("The name of the ValidatingWebhookConfiguration is empty") builder.errorMsg = "ValidatingWebhookConfiguration 'name' cannot be empty" + + return builder, nil } if !builder.Exists() { @@ -53,7 +55,7 @@ func PullValidatingConfiguration(apiClient *clients.Settings, name string) ( builder.Definition = builder.Object - return &builder, nil + return builder, nil } // Exists checks whether the given ValidatingWebhookConfiguration object exists in the cluster. @@ -142,7 +144,7 @@ func (builder *ValidatingConfigurationBuilder) validate() (bool, error) { 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 != "" {