Skip to content

Commit

Permalink
namespace: adjust return consistency (#844)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebrandon1 authored Jan 3, 2025
1 parent f01dfc5 commit 15c7ecd
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions pkg/namespace/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -53,7 +53,7 @@ func NewBuilder(apiClient *clients.Settings, name string) *Builder {
builder.errorMsg = "namespace 'name' cannot be empty"
}

return &builder
return builder
}

// WithLabel redefines namespace definition with the given label.
Expand Down Expand Up @@ -146,13 +146,18 @@ func (builder *Builder) Create() (*Builder, error) {

glog.V(100).Infof("Creating namespace %s", builder.Definition.Name)

var err error
if !builder.Exists() {
builder.Object, err = builder.apiClient.Namespaces().Create(
context.TODO(), builder.Definition, metav1.CreateOptions{})
if builder.Exists() {
return builder, nil
}

return builder, err
err := builder.apiClient.Create(context.TODO(), builder.Definition)
if err != nil {
return builder, err
}

builder.Object = builder.Definition

return builder, nil
}

// Update renovates the existing namespace object with the namespace definition in builder.
Expand Down Expand Up @@ -243,7 +248,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{
Expand All @@ -253,7 +258,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() {
Expand All @@ -262,7 +269,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.
Expand Down Expand Up @@ -370,13 +377,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 15c7ecd

Please sign in to comment.