Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
britaniar committed Nov 16, 2024
1 parent 30e90de commit 35036ae
Show file tree
Hide file tree
Showing 2 changed files with 1,414 additions and 285 deletions.
39 changes: 23 additions & 16 deletions pkg/controllers/workgenerator/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -781,42 +781,35 @@ func setBindingStatus(works map[string]*fleetv1beta1.Work, resourceBinding *flee
}

resourceBinding.Status.DriftedPlacements = nil
// collect and set the drifted resource placements to the binding
resourceBinding.Status.DiffedPlacements = nil
driftedResourcePlacements := make([]fleetv1beta1.DriftedResourcePlacement, 0, maxDriftedResourcePlacementLimit) // preallocate the memory
diffedResourcePlacements := make([]fleetv1beta1.DiffedResourcePlacement, 0, maxDiffedResourcePlacementLimit) // preallocate the memory
for _, w := range works {
if w.DeletionTimestamp != nil {
klog.V(2).InfoS("Ignoring the deleting work", "clusterResourceBinding", bindingRef, "work", klog.KObj(w))
continue // ignore the deleting work
}
driftedManifests := extractDriftedResourcePlacementsFromWork(w)
driftedResourcePlacements = append(driftedResourcePlacements, driftedManifests...)

diffedManifests := extractDiffedResourcePlacementsFromWork(w)
diffedResourcePlacements = append(diffedResourcePlacements, diffedManifests...)
}
// cut the list to keep only the max limit
if len(driftedResourcePlacements) > maxDriftedResourcePlacementLimit {
driftedResourcePlacements = driftedResourcePlacements[0:maxDriftedResourcePlacementLimit]
}
resourceBinding.Status.DriftedPlacements = driftedResourcePlacements
if len(driftedResourcePlacements) > 0 {
resourceBinding.Status.DriftedPlacements = driftedResourcePlacements
klog.V(2).InfoS("Populated drifted manifests", "clusterResourceBinding", bindingRef, "numberOfDriftedPlacements", len(driftedResourcePlacements))
}

resourceBinding.Status.DiffedPlacements = nil
// collect and set the diffed resource placements to the binding
diffedResourcePlacements := make([]fleetv1beta1.DiffedResourcePlacement, 0, maxDiffedResourcePlacementLimit) // preallocate the memory
for _, w := range works {
if w.DeletionTimestamp != nil {
klog.V(2).InfoS("Ignoring the deleting work", "clusterResourceBinding", bindingRef, "work", klog.KObj(w))
continue // ignore the deleting work
}
diffedManifests := extractDiffedResourcePlacementsFromWork(w)
diffedResourcePlacements = append(diffedResourcePlacements, diffedManifests...)
}
// cut the list to keep only the max limit
if len(diffedResourcePlacements) > maxDiffedResourcePlacementLimit {
diffedResourcePlacements = diffedResourcePlacements[0:maxDiffedResourcePlacementLimit]
}
resourceBinding.Status.DiffedPlacements = diffedResourcePlacements
if len(diffedResourcePlacements) > 0 {
resourceBinding.Status.DiffedPlacements = diffedResourcePlacements
klog.V(2).InfoS("Populated diffed manifests", "clusterResourceBinding", bindingRef, "numberOfDiffedPlacements", len(diffedResourcePlacements))
}
}
Expand Down Expand Up @@ -1011,6 +1004,9 @@ func extractDriftedResourcePlacementsFromWork(work *fleetv1beta1.Work) []fleetv1
}
res := make([]fleetv1beta1.DriftedResourcePlacement, 0, len(work.Status.ManifestConditions))
for _, manifestCondition := range work.Status.ManifestConditions {
if manifestCondition.DriftDetails == nil {
continue
}
driftedManifest := fleetv1beta1.DriftedResourcePlacement{
ResourceIdentifier: fleetv1beta1.ResourceIdentifier{
Group: manifestCondition.Identifier.Group,
Expand All @@ -1026,6 +1022,11 @@ func extractDriftedResourcePlacementsFromWork(work *fleetv1beta1.Work) []fleetv1
}

if isEnveloped {
driftedManifest.ResourceIdentifier.Envelope = &fleetv1beta1.EnvelopeIdentifier{
Name: envelopObjName,
Namespace: envelopObjNamespace,
Type: fleetv1beta1.EnvelopeType(envelopeType),
}
klog.V(2).InfoS("Found a drifted enveloped manifest",
"manifestName", manifestCondition.Identifier.Name,
"group", manifestCondition.Identifier.Group,
Expand All @@ -1037,7 +1038,6 @@ func extractDriftedResourcePlacementsFromWork(work *fleetv1beta1.Work) []fleetv1
"version", manifestCondition.Identifier.Version, "kind", manifestCondition.Identifier.Kind)
}
res = append(res, driftedManifest)
continue //jump to the next manifest
}
return res
}
Expand All @@ -1054,6 +1054,9 @@ func extractDiffedResourcePlacementsFromWork(work *fleetv1beta1.Work) []fleetv1b
}
res := make([]fleetv1beta1.DiffedResourcePlacement, 0, len(work.Status.ManifestConditions))
for _, manifestCondition := range work.Status.ManifestConditions {
if manifestCondition.DiffDetails == nil {
continue
}
diffedManifest := fleetv1beta1.DiffedResourcePlacement{
ResourceIdentifier: fleetv1beta1.ResourceIdentifier{
Group: manifestCondition.Identifier.Group,
Expand All @@ -1069,6 +1072,11 @@ func extractDiffedResourcePlacementsFromWork(work *fleetv1beta1.Work) []fleetv1b
}

if isEnveloped {
diffedManifest.ResourceIdentifier.Envelope = &fleetv1beta1.EnvelopeIdentifier{
Name: envelopObjName,
Namespace: envelopObjNamespace,
Type: fleetv1beta1.EnvelopeType(envelopeType),
}
klog.V(2).InfoS("Found a diffed enveloped manifest",
"manifestName", manifestCondition.Identifier.Name,
"group", manifestCondition.Identifier.Group,
Expand All @@ -1080,7 +1088,6 @@ func extractDiffedResourcePlacementsFromWork(work *fleetv1beta1.Work) []fleetv1b
"version", manifestCondition.Identifier.Version, "kind", manifestCondition.Identifier.Kind)
}
res = append(res, diffedManifest)
continue //jump to the next manifest
}
return res
}
Expand Down
Loading

0 comments on commit 35036ae

Please sign in to comment.