Skip to content

Commit 0fa3352

Browse files
authored
Simplify namespace selector for ResourceRefNamespaced (#762)
1 parent c231fa1 commit 0fa3352

File tree

3 files changed

+15
-25
lines changed

3 files changed

+15
-25
lines changed

pkg/api/v1/atlasproject_types.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -160,15 +160,7 @@ func (p *AtlasProject) UpdateStatus(conditions []status.Condition, options ...st
160160
}
161161

162162
func (p *AtlasProject) X509SecretObjectKey() *client.ObjectKey {
163-
if p.Spec.X509CertRef != nil {
164-
namespace := p.Spec.X509CertRef.Namespace
165-
if namespace == "" {
166-
namespace = p.Namespace
167-
}
168-
key := kube.ObjectKey(namespace, p.Spec.X509CertRef.Name)
169-
return &key
170-
}
171-
return nil
163+
return p.Spec.X509CertRef.GetObject(p.Namespace)
172164
}
173165

174166
// ************************************ Builder methods *************************************************

pkg/api/v1/common/common.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,7 @@ func (rn *ResourceRefNamespaced) GetObject(parentNamespace string) *client.Objec
4646
return nil
4747
}
4848

49-
ns := rn.Namespace
50-
if rn.Namespace != "" {
51-
ns = parentNamespace
52-
}
49+
ns := SelectNamespace(rn.Namespace, parentNamespace)
5350
key := kube.ObjectKey(ns, rn.Name)
5451
return &key
5552
}
@@ -72,3 +69,15 @@ func (rn *ResourceRefNamespaced) ReadPassword(kubeClient client.Client, parentNa
7269
}
7370
return "", nil
7471
}
72+
73+
// SelectNamespace returns first non-empty namespace from the list
74+
// "", "", "first", "second" => "first"
75+
func SelectNamespace(namespaces ...string) string {
76+
for _, namespace := range namespaces {
77+
if namespace != "" {
78+
return namespace
79+
}
80+
}
81+
82+
return ""
83+
}

pkg/api/v1/project/integration.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func (i Integration) ToAtlas(c client.Client, defaultNS string) (result *mongodb
8282
return
8383
}
8484

85-
*target, err = passwordField.ReadPassword(c, getNamespace(passwordField.Namespace, defaultNS))
85+
*target, err = passwordField.ReadPassword(c, defaultNS)
8686
storeError(err, errors)
8787
}
8888

@@ -114,14 +114,3 @@ func storeError(err error, errors *[]error) {
114114
*errors = append(*errors, err)
115115
}
116116
}
117-
118-
// getNamespace returns first non-empty namespace from the list
119-
func getNamespace(namespaces ...string) string {
120-
for _, namespace := range namespaces {
121-
if namespace != "" {
122-
return namespace
123-
}
124-
}
125-
126-
return ""
127-
}

0 commit comments

Comments
 (0)