Skip to content

Commit

Permalink
Fix references
Browse files Browse the repository at this point in the history
  • Loading branch information
jveski committed Nov 17, 2023
1 parent 12ababd commit 09694e1
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
6 changes: 3 additions & 3 deletions internal/reconstitution/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (c *cache) Get(ctx context.Context, ref *ResourceRef, gen int64) (*Resource
func (c *cache) HasSynthesis(ctx context.Context, comp types.NamespacedName, synthesis *apiv1.Synthesis) bool {
key := synthesisKey{
Composition: comp,
Generation: synthesis.ObservedGeneration,
Generation: synthesis.ObservedCompositionGeneration,
}

c.mut.Lock()
Expand All @@ -74,7 +74,7 @@ func (c *cache) Fill(ctx context.Context, comp types.NamespacedName, synthesis *
c.mut.Lock()
defer c.mut.Unlock()

synKey := synthesisKey{Composition: comp, Generation: synthesis.ObservedGeneration}
synKey := synthesisKey{Composition: comp, Generation: synthesis.ObservedCompositionGeneration}
c.resources[synKey] = resources
c.synthesesByComposition[comp] = append(c.synthesesByComposition[comp], synKey.Generation)

Expand Down Expand Up @@ -165,7 +165,7 @@ func (c *cache) Purge(ctx context.Context, compNSN types.NamespacedName, comp *a
remainingSyns := []int64{}
for _, syn := range c.synthesesByComposition[compNSN] {
// Don't touch any syntheses still referenced by the composition
if comp != nil && ((comp.Status.CurrentState != nil && comp.Status.CurrentState.ObservedGeneration == syn) || (comp.Status.PreviousState != nil && comp.Status.PreviousState.ObservedGeneration == syn)) {
if comp != nil && ((comp.Status.CurrentState != nil && comp.Status.CurrentState.ObservedCompositionGeneration == syn) || (comp.Status.PreviousState != nil && comp.Status.PreviousState.ObservedCompositionGeneration == syn)) {
remainingSyns = append(remainingSyns, syn)
continue // still referenced by the Generation
}
Expand Down
22 changes: 11 additions & 11 deletions internal/reconstitution/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ func TestCacheBasics(t *testing.T) {
assert.True(t, c.HasSynthesis(ctx, comp, synth))

// negative
assert.False(t, c.HasSynthesis(ctx, comp, &apiv1.Synthesis{ObservedGeneration: 123}))
assert.False(t, c.HasSynthesis(ctx, comp, &apiv1.Synthesis{ObservedCompositionGeneration: 123}))
})

t.Run("get", func(t *testing.T) {
// positive
resource, exists := c.Get(ctx, &expectedReqs[0].ResourceRef, synth.ObservedGeneration)
resource, exists := c.Get(ctx, &expectedReqs[0].ResourceRef, synth.ObservedCompositionGeneration)
require.True(t, exists)
assert.NotEmpty(t, resource.Manifest)
assert.Equal(t, "ConfigMap", resource.Object().GetKind())
Expand All @@ -56,7 +56,7 @@ func TestCacheBasics(t *testing.T) {
c.Purge(ctx, comp, nil)

// confirm
_, exists := c.Get(ctx, &expectedReqs[0].ResourceRef, synth.ObservedGeneration)
_, exists := c.Get(ctx, &expectedReqs[0].ResourceRef, synth.ObservedCompositionGeneration)
assert.False(t, exists)

assert.Len(t, c.resources, 0)
Expand Down Expand Up @@ -103,7 +103,7 @@ func TestCacheSecret(t *testing.T) {
assert.Equal(t, expectedReqs, reqs)

// Confirm cache was filled correctly
resource, exists := c.Get(ctx, &expectedReqs[0].ResourceRef, synth.ObservedGeneration)
resource, exists := c.Get(ctx, &expectedReqs[0].ResourceRef, synth.ObservedCompositionGeneration)
require.True(t, exists)
assert.NotEmpty(t, resource.Manifest)
assert.Equal(t, "ConfigMap", resource.Object().GetKind())
Expand Down Expand Up @@ -150,7 +150,7 @@ func TestCacheReconcileInterval(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, expectedReqs, reqs)

resource, exists := c.Get(ctx, &expectedReqs[0].ResourceRef, synth.ObservedGeneration)
resource, exists := c.Get(ctx, &expectedReqs[0].ResourceRef, synth.ObservedCompositionGeneration)
require.True(t, exists)
assert.Equal(t, interval, resource.ReconcileInterval)
}
Expand All @@ -165,12 +165,12 @@ func TestCachePartialPurge(t *testing.T) {
compNSN, synth, resources, _ := newCacheTestFixtures(3, 4)
_, err := c.Fill(ctx, compNSN, synth, resources)
require.NoError(t, err)
originalGen := synth.ObservedGeneration
originalGen := synth.ObservedCompositionGeneration

// Add another resource to the composition but synthesized from a newer generation
_, _, resources, expectedReqs := newCacheTestFixtures(1, 1)
synth.ObservedGeneration++
resources[0].Spec.CompositionGeneration = synth.ObservedGeneration
synth.ObservedCompositionGeneration++
resources[0].Spec.CompositionGeneration = synth.ObservedCompositionGeneration
expectedReqs[0].Composition = compNSN
_, err = c.Fill(ctx, compNSN, synth, resources)
require.NoError(t, err)
Expand All @@ -191,7 +191,7 @@ func TestCachePartialPurge(t *testing.T) {
c.Purge(ctx, compNSN, comp)

// The newer resource should still exist
_, exists := c.Get(ctx, &expectedReqs[0].ResourceRef, synth.ObservedGeneration)
_, exists := c.Get(ctx, &expectedReqs[0].ResourceRef, synth.ObservedCompositionGeneration)
assert.True(t, exists)

// The older resource is not referenced by the composition and should have been removed
Expand All @@ -208,7 +208,7 @@ func TestCachePartialPurge(t *testing.T) {

func newCacheTestFixtures(sliceCount, resPerSliceCount int) (types.NamespacedName, *apiv1.Synthesis, []apiv1.ResourceSlice, []*Request) {
comp := types.NamespacedName{Namespace: string(uuid.NewUUID()), Name: string(uuid.NewUUID())}
synth := &apiv1.Synthesis{ObservedGeneration: 3} // just not 0
synth := &apiv1.Synthesis{ObservedCompositionGeneration: 3} // just not 0

resources := make([]apiv1.ResourceSlice, sliceCount)
requests := []*Request{}
Expand All @@ -217,7 +217,7 @@ func newCacheTestFixtures(sliceCount, resPerSliceCount int) (types.NamespacedNam
slice.Name = string(uuid.NewUUID())
slice.Namespace = "slice-ns"
slice.Spec.Resources = make([]apiv1.Manifest, resPerSliceCount)
slice.Spec.CompositionGeneration = synth.ObservedGeneration
slice.Spec.CompositionGeneration = synth.ObservedCompositionGeneration

for j := 0; j < resPerSliceCount; j++ {
resource := &corev1.ConfigMap{}
Expand Down
4 changes: 2 additions & 2 deletions internal/reconstitution/reconstituter.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (r *reconstituter) populateCache(ctx context.Context, comp *apiv1.Compositi
}
compNSN := types.NamespacedName{Namespace: comp.Namespace, Name: comp.Name}

logger = logger.WithValues("synthesisGen", synthesis.ObservedGeneration)
logger = logger.WithValues("synthesisGen", synthesis.ObservedCompositionGeneration)
ctx = logr.NewContext(ctx, logger)
if r.cache.HasSynthesis(ctx, compNSN, synthesis) {
logger.V(1).Info("this synthesis has already been cached")
Expand All @@ -97,7 +97,7 @@ func (r *reconstituter) populateCache(ctx context.Context, comp *apiv1.Compositi

slices := &apiv1.ResourceSliceList{}
err := r.client.List(ctx, slices, client.InNamespace(comp.Namespace), client.MatchingFields{
manager.IdxSlicesByCompositionGeneration: manager.NewSlicesByCompositionGenerationKey(comp.Name, synthesis.ObservedGeneration),
manager.IdxSlicesByCompositionGeneration: manager.NewSlicesByCompositionGenerationKey(comp.Name, synthesis.ObservedCompositionGeneration),
})
if err != nil {
return fmt.Errorf("listing resource slices: %w", err)
Expand Down
4 changes: 2 additions & 2 deletions internal/reconstitution/reconstituter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func TestReconstituterIntegration(t *testing.T) {

one := int64(1)
comp.Status.CurrentState = &apiv1.Synthesis{
ObservedGeneration: comp.Generation,
ResourceSliceCount: &one,
ObservedCompositionGeneration: comp.Generation,
ResourceSliceCount: &one,
}
require.NoError(t, client.Status().Update(ctx, comp))

Expand Down

0 comments on commit 09694e1

Please sign in to comment.