Skip to content

Commit 115e423

Browse files
committed
add first tests for label selector, fix first bug
On-behalf-of: @SAP [email protected]
1 parent 32287a9 commit 115e423

File tree

2 files changed

+284
-1
lines changed

2 files changed

+284
-1
lines changed

internal/sync/syncer_related.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ func resolveResourceDestination(side syncSide, relRes syncagentv1alpha1.RelatedR
339339
}
340340

341341
namespace := side.object.GetNamespace()
342-
if relRes.Source.Namespace != nil {
342+
if relRes.Destination.Namespace != nil {
343343
namespace, err = resolveResourceDestinationSpec(string(jsonData), *relRes.Destination.Namespace)
344344
if err != nil {
345345
return nil, fmt.Errorf("failed to resolve namespace: %w", err)

test/e2e/sync/related_test.go

Lines changed: 283 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,289 @@ func TestSyncRelatedObjects(t *testing.T) {
194194
Type: corev1.SecretTypeOpaque,
195195
},
196196
},
197+
198+
//////////////////////////////////////////////////////////////////////////////////////////////
199+
200+
{
201+
name: "sync referenced Secret up into a new namspace",
202+
workspace: "sync-referenced-secret-up-namespace",
203+
mainResource: crds.Crontab{
204+
ObjectMeta: metav1.ObjectMeta{
205+
Name: "my-crontab",
206+
Namespace: "default",
207+
},
208+
Spec: crds.CrontabSpec{
209+
CronSpec: "* * *",
210+
Image: "ubuntu:latest",
211+
},
212+
},
213+
relatedConfig: syncagentv1alpha1.RelatedResourceSpec{
214+
Identifier: "credentials",
215+
Origin: "service",
216+
Kind: "Secret",
217+
Source: syncagentv1alpha1.RelatedResourceSource{
218+
RelatedResourceSourceSpec: syncagentv1alpha1.RelatedResourceSourceSpec{
219+
Reference: &syncagentv1alpha1.RelatedResourceReference{
220+
Path: "metadata.name", // irrelevant
221+
Regex: &syncagentv1alpha1.RegularExpression{
222+
Replacement: "my-credentials",
223+
},
224+
},
225+
},
226+
},
227+
Destination: syncagentv1alpha1.RelatedResourceDestination{
228+
RelatedResourceDestinationSpec: syncagentv1alpha1.RelatedResourceDestinationSpec{
229+
Reference: &syncagentv1alpha1.RelatedResourceReference{
230+
Path: "metadata.name", // irrelevant
231+
Regex: &syncagentv1alpha1.RegularExpression{
232+
Replacement: "my-credentials",
233+
},
234+
},
235+
},
236+
Namespace: &syncagentv1alpha1.RelatedResourceDestinationSpec{
237+
Reference: &syncagentv1alpha1.RelatedResourceReference{
238+
Path: "metadata.name", // irrelevant
239+
Regex: &syncagentv1alpha1.RegularExpression{
240+
Replacement: "new-namespace",
241+
},
242+
},
243+
},
244+
},
245+
},
246+
sourceRelatedObject: corev1.Secret{
247+
ObjectMeta: metav1.ObjectMeta{
248+
Name: "my-credentials",
249+
Namespace: "synced-default",
250+
},
251+
Data: map[string][]byte{
252+
"password": []byte("hunter2"),
253+
},
254+
Type: corev1.SecretTypeOpaque,
255+
},
256+
257+
expectedSyncedRelatedObject: corev1.Secret{
258+
ObjectMeta: metav1.ObjectMeta{
259+
Name: "my-credentials",
260+
Namespace: "new-namespace",
261+
},
262+
Data: map[string][]byte{
263+
"password": []byte("hunter2"),
264+
},
265+
Type: corev1.SecretTypeOpaque,
266+
},
267+
},
268+
269+
//////////////////////////////////////////////////////////////////////////////////////////////
270+
271+
{
272+
name: "sync referenced Secret down into a new namspace",
273+
workspace: "sync-referenced-secret-down-namespace",
274+
mainResource: crds.Crontab{
275+
ObjectMeta: metav1.ObjectMeta{
276+
Name: "my-crontab",
277+
Namespace: "default",
278+
},
279+
Spec: crds.CrontabSpec{
280+
CronSpec: "* * *",
281+
Image: "ubuntu:latest",
282+
},
283+
},
284+
relatedConfig: syncagentv1alpha1.RelatedResourceSpec{
285+
Identifier: "credentials",
286+
Origin: "kcp",
287+
Kind: "Secret",
288+
Source: syncagentv1alpha1.RelatedResourceSource{
289+
RelatedResourceSourceSpec: syncagentv1alpha1.RelatedResourceSourceSpec{
290+
Reference: &syncagentv1alpha1.RelatedResourceReference{
291+
Path: "metadata.name", // irrelevant
292+
Regex: &syncagentv1alpha1.RegularExpression{
293+
Replacement: "my-credentials",
294+
},
295+
},
296+
},
297+
},
298+
Destination: syncagentv1alpha1.RelatedResourceDestination{
299+
RelatedResourceDestinationSpec: syncagentv1alpha1.RelatedResourceDestinationSpec{
300+
Reference: &syncagentv1alpha1.RelatedResourceReference{
301+
Path: "metadata.name", // irrelevant
302+
Regex: &syncagentv1alpha1.RegularExpression{
303+
Replacement: "my-credentials",
304+
},
305+
},
306+
},
307+
Namespace: &syncagentv1alpha1.RelatedResourceDestinationSpec{
308+
Reference: &syncagentv1alpha1.RelatedResourceReference{
309+
Path: "metadata.name", // irrelevant
310+
Regex: &syncagentv1alpha1.RegularExpression{
311+
Replacement: "new-namespace",
312+
},
313+
},
314+
},
315+
},
316+
},
317+
sourceRelatedObject: corev1.Secret{
318+
ObjectMeta: metav1.ObjectMeta{
319+
Name: "my-credentials",
320+
Namespace: "default",
321+
},
322+
Data: map[string][]byte{
323+
"password": []byte("hunter2"),
324+
},
325+
Type: corev1.SecretTypeOpaque,
326+
},
327+
328+
expectedSyncedRelatedObject: corev1.Secret{
329+
ObjectMeta: metav1.ObjectMeta{
330+
Name: "my-credentials",
331+
Namespace: "new-namespace",
332+
},
333+
Data: map[string][]byte{
334+
"password": []byte("hunter2"),
335+
},
336+
Type: corev1.SecretTypeOpaque,
337+
},
338+
},
339+
340+
//////////////////////////////////////////////////////////////////////////////////////////////
341+
342+
{
343+
name: "sync referenced Secret up from a foreign namspace",
344+
workspace: "sync-referenced-secret-up-foreign-namespace",
345+
mainResource: crds.Crontab{
346+
ObjectMeta: metav1.ObjectMeta{
347+
Name: "my-crontab",
348+
Namespace: "default",
349+
},
350+
Spec: crds.CrontabSpec{
351+
CronSpec: "* * *",
352+
Image: "ubuntu:latest",
353+
},
354+
},
355+
relatedConfig: syncagentv1alpha1.RelatedResourceSpec{
356+
Identifier: "credentials",
357+
Origin: "service",
358+
Kind: "Secret",
359+
Source: syncagentv1alpha1.RelatedResourceSource{
360+
RelatedResourceSourceSpec: syncagentv1alpha1.RelatedResourceSourceSpec{
361+
Reference: &syncagentv1alpha1.RelatedResourceReference{
362+
Path: "metadata.name", // irrelevant
363+
Regex: &syncagentv1alpha1.RegularExpression{
364+
Replacement: "my-credentials",
365+
},
366+
},
367+
},
368+
Namespace: &syncagentv1alpha1.RelatedResourceSourceSpec{
369+
Reference: &syncagentv1alpha1.RelatedResourceReference{
370+
Path: "metadata.name", // irrelevant
371+
Regex: &syncagentv1alpha1.RegularExpression{
372+
Replacement: "other-namespace",
373+
},
374+
},
375+
},
376+
},
377+
Destination: syncagentv1alpha1.RelatedResourceDestination{
378+
RelatedResourceDestinationSpec: syncagentv1alpha1.RelatedResourceDestinationSpec{
379+
Reference: &syncagentv1alpha1.RelatedResourceReference{
380+
Path: "metadata.name", // irrelevant
381+
Regex: &syncagentv1alpha1.RegularExpression{
382+
Replacement: "my-credentials",
383+
},
384+
},
385+
},
386+
},
387+
},
388+
sourceRelatedObject: corev1.Secret{
389+
ObjectMeta: metav1.ObjectMeta{
390+
Name: "my-credentials",
391+
Namespace: "other-namespace",
392+
},
393+
Data: map[string][]byte{
394+
"password": []byte("hunter2"),
395+
},
396+
Type: corev1.SecretTypeOpaque,
397+
},
398+
399+
expectedSyncedRelatedObject: corev1.Secret{
400+
ObjectMeta: metav1.ObjectMeta{
401+
Name: "my-credentials",
402+
Namespace: "default",
403+
},
404+
Data: map[string][]byte{
405+
"password": []byte("hunter2"),
406+
},
407+
Type: corev1.SecretTypeOpaque,
408+
},
409+
},
410+
411+
//////////////////////////////////////////////////////////////////////////////////////////////
412+
413+
{
414+
name: "find Secret based on label selector",
415+
workspace: "sync-selected-secret-up",
416+
mainResource: crds.Crontab{
417+
ObjectMeta: metav1.ObjectMeta{
418+
Name: "my-crontab",
419+
Namespace: "default",
420+
},
421+
Spec: crds.CrontabSpec{
422+
CronSpec: "* * *",
423+
Image: "ubuntu:latest",
424+
},
425+
},
426+
relatedConfig: syncagentv1alpha1.RelatedResourceSpec{
427+
Identifier: "credentials",
428+
Origin: "service",
429+
Kind: "Secret",
430+
Source: syncagentv1alpha1.RelatedResourceSource{
431+
RelatedResourceSourceSpec: syncagentv1alpha1.RelatedResourceSourceSpec{
432+
Selector: &syncagentv1alpha1.RelatedResourceSelector{
433+
LabelSelector: metav1.LabelSelector{
434+
MatchLabels: map[string]string{
435+
"find": "me",
436+
},
437+
},
438+
},
439+
},
440+
},
441+
Destination: syncagentv1alpha1.RelatedResourceDestination{
442+
RelatedResourceDestinationSpec: syncagentv1alpha1.RelatedResourceDestinationSpec{
443+
Reference: &syncagentv1alpha1.RelatedResourceReference{
444+
Path: "metadata.name", // irrelevant
445+
Regex: &syncagentv1alpha1.RegularExpression{
446+
Replacement: "my-credentials",
447+
},
448+
},
449+
},
450+
},
451+
},
452+
sourceRelatedObject: corev1.Secret{
453+
ObjectMeta: metav1.ObjectMeta{
454+
Name: "unknown-name",
455+
Namespace: "synced-default",
456+
Labels: map[string]string{
457+
"find": "me",
458+
},
459+
},
460+
Data: map[string][]byte{
461+
"password": []byte("hunter2"),
462+
},
463+
Type: corev1.SecretTypeOpaque,
464+
},
465+
466+
expectedSyncedRelatedObject: corev1.Secret{
467+
ObjectMeta: metav1.ObjectMeta{
468+
Name: "my-credentials",
469+
Namespace: "default",
470+
Labels: map[string]string{
471+
"find": "me",
472+
},
473+
},
474+
Data: map[string][]byte{
475+
"password": []byte("hunter2"),
476+
},
477+
Type: corev1.SecretTypeOpaque,
478+
},
479+
},
197480
}
198481

199482
for _, testcase := range testcases {

0 commit comments

Comments
 (0)