Skip to content

Commit

Permalink
pod: adjust return consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
sebrandon1 committed Jan 10, 2025
1 parent 976a70e commit a8f9747
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 45 deletions.
40 changes: 20 additions & 20 deletions pkg/pod/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,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
Expand All @@ -84,6 +90,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
Expand All @@ -94,9 +102,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
}

Expand Down Expand Up @@ -198,9 +204,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
}

Expand All @@ -218,21 +222,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
}

Expand All @@ -254,21 +260,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
}

Expand Down Expand Up @@ -306,9 +314,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
}

Expand All @@ -325,9 +331,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
}

Expand All @@ -344,15 +348,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
}

Expand All @@ -375,15 +379,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
}

Expand All @@ -401,9 +405,7 @@ func (builder *ContainerBuilder) WithPorts(ports []corev1.ContainerPort) *Contai
glog.V(100).Infof("Ports can not be empty")

builder.errorMsg = "can not modify container config without any port"
}

if builder.errorMsg != "" {
return builder
}

Expand All @@ -420,9 +422,7 @@ func (builder *ContainerBuilder) WithReadinessProbe(readinessProbe *corev1.Probe
glog.V(100).Infof("Container's readinessProbe name cannot be empty")

builder.errorMsg = "container's readinessProbe is empty"
}

if builder.errorMsg != "" {
return builder
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/pod/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ func TestPodContainerWithVolumeMount(t *testing.T) {
},
{
mount: corev1.VolumeMount{},
expectedError: "container's volume mount path is empty",
expectedError: "container's volume mount name is empty",
},
}

Expand Down Expand Up @@ -511,7 +511,7 @@ func TestPodContainerGetContainerCfg(t *testing.T) {
{
builder: NewContainerBuilder("container", "test", []string{"/bin/bash", "-c", "sleep"}).
WithEnvVar("", ""),
expectedError: fmt.Errorf("container's environment var 'value' is empty"),
expectedError: fmt.Errorf("container's environment var 'name' is empty"),
},
}

Expand Down
34 changes: 11 additions & 23 deletions pkg/pod/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func Pull(apiClient *clients.Settings, name, nsname string) (*Builder, error) {
return nil, fmt.Errorf("pod 'apiClient' cannot be empty")
}

builder := Builder{
builder := &Builder{
apiClient: apiClient,
Definition: &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -141,7 +141,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.
Expand All @@ -159,12 +159,12 @@ func (builder *Builder) DefineOnNode(nodeName string) *Builder {
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
}

Expand Down Expand Up @@ -679,9 +679,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
}

Expand Down Expand Up @@ -774,9 +772,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
}

Expand Down Expand Up @@ -818,9 +814,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
}

Expand All @@ -847,12 +841,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}
Expand Down Expand Up @@ -898,9 +896,7 @@ func (builder *Builder) WithAdditionalContainer(container *corev1.Container) *Bu

if container == nil {
builder.errorMsg = "'container' parameter cannot be empty"
}

if builder.errorMsg != "" {
return builder
}

Expand All @@ -923,9 +919,7 @@ 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"
}

if builder.errorMsg != "" {
return builder
}

Expand All @@ -952,9 +946,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
}

Expand Down Expand Up @@ -1070,9 +1062,7 @@ 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
}

Expand Down Expand Up @@ -1145,9 +1135,7 @@ func (builder *Builder) WithLabel(labelKey, labelValue string) *Builder {

if labelKey == "" {
builder.errorMsg = "can not apply empty labelKey"
}

if builder.errorMsg != "" {
return builder
}

Expand Down Expand Up @@ -1348,13 +1336,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 != "" {
Expand Down

0 comments on commit a8f9747

Please sign in to comment.