Skip to content

Commit cf8d14a

Browse files
committed
add the unit tests for related resources
Signed-off-by: Karol Szwaj <karol.szwaj@gmail.com> On-behalf-of: @SAP karol.szwaj@sap.com
1 parent 97efc4c commit cf8d14a

File tree

6 files changed

+52
-82
lines changed

6 files changed

+52
-82
lines changed

deploy/crd/kcp.io/syncagent.kcp.io_publishedresources.yaml

-3
Original file line numberDiff line numberDiff line change
@@ -407,9 +407,6 @@ spec:
407407
type: object
408408
type: array
409409
type: object
410-
optional:
411-
description: Optional indicates whether the related resource must be referenced.
412-
type: boolean
413410
origin:
414411
description: '"service" or "kcp"'
415412
type: string

internal/sync/syncer_related.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,13 @@ import (
3535

3636
func (s *ResourceSyncer) processRelatedResources(log *zap.SugaredLogger, stateStore ObjectStateStore, remote, local syncSide) (requeue bool, err error) {
3737
for _, relatedResource := range s.pubRes.Spec.Related {
38-
if !relatedResource.Optional {
39-
requeue, err := s.processRelatedResource(log.With("identifier", relatedResource.Identifier), stateStore, remote, local, relatedResource)
40-
if err != nil {
41-
return false, fmt.Errorf("failed to process related resource %s: %w", relatedResource.Identifier, err)
42-
}
38+
requeue, err := s.processRelatedResource(log.With("identifier", relatedResource.Identifier), stateStore, remote, local, relatedResource)
39+
if err != nil {
40+
return false, fmt.Errorf("failed to process related resource %s: %w", relatedResource.Identifier, err)
41+
}
4342

44-
if requeue {
45-
return true, nil
46-
}
43+
if requeue {
44+
return true, nil
4745
}
4846
}
4947

@@ -139,6 +137,7 @@ func (s *ResourceSyncer) processRelatedResource(log *zap.SugaredLogger, stateSto
139137
dest := source.DeepCopy()
140138
dest.SetName(destKey.Name)
141139
dest.SetNamespace(destKey.Namespace)
140+
142141
return dest
143142
},
144143
// ConfigMaps and Secrets have no subresources
@@ -242,6 +241,7 @@ func resolveResourceLocator(jsonData string, loc syncagentv1alpha1.ResourceLocat
242241
if err != nil {
243242
return "", fmt.Errorf("invalid pattern %q: %w", re.Pattern, err)
244243
}
244+
245245
// this does apply some coalescing, like turning numbers into strings
246246
strVal := gval.String()
247247

internal/sync/syncer_related_test.go

+44-45
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,25 @@ import (
3737
"sigs.k8s.io/controller-runtime/pkg/kontext"
3838
)
3939

40+
func newPublishedResources(relatedResources []syncagentv1alpha1.RelatedResourceSpec) *syncagentv1alpha1.PublishedResource {
41+
return &syncagentv1alpha1.PublishedResource{
42+
Spec: syncagentv1alpha1.PublishedResourceSpec{
43+
Resource: syncagentv1alpha1.SourceResourceDescriptor{
44+
APIGroup: dummyv1alpha1.GroupName,
45+
Version: dummyv1alpha1.GroupVersion,
46+
Kind: "NamespacedThing",
47+
},
48+
Projection: &syncagentv1alpha1.ResourceProjection{
49+
Kind: "RemoteThing",
50+
},
51+
Naming: &syncagentv1alpha1.ResourceNaming{
52+
Name: "$remoteClusterName-$remoteName",
53+
},
54+
Related: relatedResources,
55+
},
56+
}
57+
}
58+
4059
func TestSyncerProcessingRelatedResources(t *testing.T) {
4160
const stateNamespace = "kcp-system"
4261

@@ -57,34 +76,12 @@ func TestSyncerProcessingRelatedResources(t *testing.T) {
5776

5877
clusterName := logicalcluster.Name("testcluster")
5978

60-
remoteThingPR := &syncagentv1alpha1.PublishedResource{
61-
Spec: syncagentv1alpha1.PublishedResourceSpec{
62-
Resource: syncagentv1alpha1.SourceResourceDescriptor{
63-
APIGroup: dummyv1alpha1.GroupName,
64-
Version: dummyv1alpha1.GroupVersion,
65-
Kind: "NamespacedThing",
66-
},
67-
Projection: &syncagentv1alpha1.ResourceProjection{
68-
Kind: "RemoteThing",
69-
},
70-
// include explicit naming rules to be independent of possible changes to the defaults
71-
Naming: &syncagentv1alpha1.ResourceNaming{
72-
Name: "$remoteClusterName-$remoteName", // Things are Cluster-scoped
73-
},
74-
Related: []syncagentv1alpha1.RelatedResourceSpec{
75-
{
76-
Identifier: "mandatory-credentials",
77-
Origin: "kcp",
78-
Kind: "Secret",
79-
Reference: syncagentv1alpha1.RelatedResourceReference{
80-
Name: syncagentv1alpha1.ResourceLocator{
81-
Path: "metadata.name",
82-
Regex: &syncagentv1alpha1.RegexResourceLocator{
83-
Replacement: "mandatory-credentials",
84-
},
85-
},
86-
},
87-
},
79+
testcases := []testcase{
80+
{
81+
name: "optional related resource does not exist",
82+
remoteAPIGroup: "remote.example.corp",
83+
localCRD: loadCRD("things"),
84+
pubRes: newPublishedResources([]syncagentv1alpha1.RelatedResourceSpec{
8885
{
8986
Identifier: "optional-secret",
9087
Origin: "service",
@@ -97,20 +94,9 @@ func TestSyncerProcessingRelatedResources(t *testing.T) {
9794
},
9895
},
9996
},
100-
Optional: true,
10197
},
102-
},
103-
},
104-
}
105-
106-
testcases := []testcase{
107-
{
108-
name: "optional related resource does not exist",
109-
remoteAPIGroup: "remote.example.corp",
110-
localCRD: loadCRD("things"),
111-
pubRes: remoteThingPR,
98+
}),
11299
performRequeues: true,
113-
114100
remoteObject: newUnstructured(&dummyv1alpha1.NamespacedThing{
115101
ObjectMeta: metav1.ObjectMeta{
116102
Name: "my-test-thing",
@@ -173,12 +159,25 @@ func TestSyncerProcessingRelatedResources(t *testing.T) {
173159
expectedState: `{"apiVersion":"remote.example.corp/v1alpha1","kind":"RemoteThing","metadata":{"name":"my-test-thing","namespace":"kcp-system"},"spec":{"username":"Colonel Mustard"}}`,
174160
},
175161
{
176-
name: "mandatory related resource does not exist",
177-
remoteAPIGroup: "remote.example.corp",
178-
localCRD: loadCRD("things"),
179-
pubRes: remoteThingPR,
162+
name: "mandatory related resource does not exist",
163+
remoteAPIGroup: "remote.example.corp",
164+
localCRD: loadCRD("things"),
165+
pubRes: newPublishedResources([]syncagentv1alpha1.RelatedResourceSpec{
166+
{
167+
Identifier: "mandatory-credentials",
168+
Origin: "kcp",
169+
Kind: "Secret",
170+
Reference: syncagentv1alpha1.RelatedResourceReference{
171+
Name: syncagentv1alpha1.ResourceLocator{
172+
Path: "metadata.name",
173+
Regex: &syncagentv1alpha1.RegexResourceLocator{
174+
Replacement: "mandatory-credentials",
175+
},
176+
},
177+
},
178+
},
179+
}),
180180
performRequeues: true,
181-
182181
remoteObject: newUnstructured(&dummyv1alpha1.NamespacedThing{
183182
ObjectMeta: metav1.ObjectMeta{
184183
Name: "my-test-thing",

sdk/apis/syncagent/v1alpha1/published_resource.go

-3
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,6 @@ type RelatedResourceSpec struct {
176176
// Mutation configures optional transformation rules for the related resource.
177177
// Status mutations are only performed when the related resource originates in kcp.
178178
Mutation *ResourceMutationSpec `json:"mutation,omitempty"`
179-
180-
// Optional indicates whether the related resource must be referenced.
181-
Optional bool `json:"optional,omitempty"`
182179
}
183180

184181
type RelatedResourceReference struct {

sdk/applyconfiguration/syncagent/v1alpha1/relatedresourcespec.go

-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/e2e/sync/related_test.go

-14
Original file line numberDiff line numberDiff line change
@@ -91,20 +91,6 @@ func TestSyncSecretBackToKcp(t *testing.T) {
9191
},
9292
},
9393
},
94-
{
95-
Identifier: "credentials",
96-
Origin: "service",
97-
Kind: "Secret",
98-
Reference: syncagentv1alpha1.RelatedResourceReference{
99-
Name: syncagentv1alpha1.ResourceLocator{
100-
Path: "metadata.name", // irrelevant
101-
Regex: &syncagentv1alpha1.RegexResourceLocator{
102-
Replacement: "my-optional-credentials",
103-
},
104-
},
105-
},
106-
Optional: true,
107-
},
10894
},
10995
},
11096
}

0 commit comments

Comments
 (0)