Skip to content

Commit 9057ff2

Browse files
committed
fix: make xr.spec.writeConnectionSecretToRef the highest priority for v2 XR connection details
Signed-off-by: Jared Watts <[email protected]>
1 parent 57ad58d commit 9057ff2

File tree

3 files changed

+51
-24
lines changed

3 files changed

+51
-24
lines changed

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,14 @@ Crossplane documentation.
184184

185185
### Setting name/namespace
186186

187-
You can control the name and namespace of this connection secret in a few ways,
188-
in order of precedence:
187+
For v2 XRs, you can control the name and namespace of this connection secret in
188+
a few ways, in order of precedence:
189+
190+
**XR reference:**
191+
192+
If you've manually included a `spec.writeConnectionSecretToRef` in your XR's
193+
schema, this function will use that reference. This can be useful for maintaining
194+
consistency with existing XR configurations.
189195

190196
**Function `input`:**
191197

@@ -201,12 +207,6 @@ input:
201207
namespace: production
202208
```
203209

204-
**XR reference:**
205-
206-
If you've manually included a `spec.writeConnectionSecretToRef` in your XR's
207-
schema, this function will use that reference. This can be useful for maintaining
208-
consistency with existing XR configurations.
209-
210210
**Default auto generated**
211211

212212
If none of the above options are provided, the function generates a name based
@@ -217,9 +217,9 @@ namespace for cluster scoped XRs if you want connection secret functionality.
217217

218218
### Patching secret name/namespace
219219

220-
You can also use patches to dynamically construct the secret name or namespace
221-
from XR fields. This is useful when you want the secret name to include
222-
environment-specific information or other metadata:
220+
For v2 XRs, you can also use patches to dynamically construct the secret name or
221+
namespace from XR fields. This is useful when you want the secret name to
222+
include environment-specific information or other metadata:
223223

224224
```yaml
225225
writeConnectionSecretToRef:

connection.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,25 +143,25 @@ func getConnectionSecretRef(xr *resource.Composite, input *v1beta1.WriteConnecti
143143
// getBaseConnectionSecretRef determines the base connection secret reference
144144
// without any patches. This reference is generated with the following
145145
// precedence:
146-
// 1. input.writeConnectionSecretToRef - if name or namespace is provided
147-
// then the whole ref will be used
148-
// 2. xr.writeConnectionSecretToRef - this is no longer automatically added
146+
// 1. xr.spec.writeConnectionSecretToRef - this is no longer automatically added
149147
// to v2 XR schemas, but the community has been adding it manually, so if
150148
// it's present we will use it.
149+
// 2. function input.writeConnectionSecretToRef - if name or namespace is provided
150+
// then the whole ref will be used
151151
// 3. generate the reference from scratch, based on the XR name and namespace
152152
func getBaseConnectionSecretRef(xr *resource.Composite, input *v1beta1.WriteConnectionSecretToRef) xpv1.SecretReference {
153-
// Use the input values if at least one of name or namespace has been provided
154-
if input != nil && (input.Name != "" || input.Namespace != "") {
155-
return xpv1.SecretReference{Name: input.Name, Namespace: input.Namespace}
156-
}
157-
158153
// Check if XR author manually added writeConnectionSecretToRef to the XR's
159154
// schema and just use that if it exists
160155
xrRef := xr.Resource.GetWriteConnectionSecretToReference()
161156
if xrRef != nil {
162157
return *xrRef
163158
}
164159

160+
// Use the input values if at least one of name or namespace has been provided
161+
if input != nil && (input.Name != "" || input.Namespace != "") {
162+
return xpv1.SecretReference{Name: input.Name, Namespace: input.Namespace}
163+
}
164+
165165
// Nothing has been provided, so generate a default name using the name of the XR
166166
return xpv1.SecretReference{
167167
Name: xr.Resource.GetName() + "-connection",

connection_test.go

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,32 @@ func TestGetConnectionSecretRef(t *testing.T) {
156156
args args
157157
want want
158158
}{
159-
"InputProvided": {
160-
reason: "Should use the provided name and namespace from function input when provided",
159+
"XRRefProvidedNoInput": {
160+
reason: "Should use the XR's writeConnectionSecretToRef when no input is provided",
161+
args: args{
162+
xr: &resource.Composite{
163+
Resource: func() *composite.Unstructured {
164+
xr := composite.New()
165+
_ = json.Unmarshal([]byte(`{
166+
"apiVersion":"example.org/v1",
167+
"kind":"XR",
168+
"metadata":{"uid":"test-uid-456"},
169+
"spec":{"writeConnectionSecretToRef":{"name":"xr-secret","namespace":"xr-namespace"}}
170+
}`), xr)
171+
return xr
172+
}(),
173+
},
174+
input: nil,
175+
},
176+
want: want{
177+
ref: xpv1.SecretReference{
178+
Name: "xr-secret",
179+
Namespace: "xr-namespace",
180+
},
181+
},
182+
},
183+
"InputProvidedNoXRRef": {
184+
reason: "Should use the provided name and namespace from function input when no XR ref is provided",
161185
args: args{
162186
xr: &resource.Composite{
163187
Resource: func() *composite.Unstructured {
@@ -182,8 +206,8 @@ func TestGetConnectionSecretRef(t *testing.T) {
182206
},
183207
},
184208
},
185-
"XRHasWriteConnectionSecretToRef": {
186-
reason: "Should use the XR's writeConnectionSecretToRef when no input is provided",
209+
"XRRefAndInputProvided": {
210+
reason: "Should use the XR's writeConnectionSecretToRef even when function input is provided",
187211
args: args{
188212
xr: &resource.Composite{
189213
Resource: func() *composite.Unstructured {
@@ -197,7 +221,10 @@ func TestGetConnectionSecretRef(t *testing.T) {
197221
return xr
198222
}(),
199223
},
200-
input: nil,
224+
input: &v1beta1.WriteConnectionSecretToRef{
225+
Name: "my-custom-secret",
226+
Namespace: "custom-namespace",
227+
},
201228
},
202229
want: want{
203230
ref: xpv1.SecretReference{

0 commit comments

Comments
 (0)