Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workload status reflects updated supplychain resource order #1505

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions pkg/controllers/deliverable_reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ var _ = Describe("DeliverableReconciler", func() {
Name: "my-image-template",
APIVersion: "carto.run/v1alpha1",
},
}, nil, false)
}, 0, nil, false)
resourceStatuses.Add(
&v1alpha1.RealizedResource{
Name: "resource2",
Expand All @@ -287,7 +287,7 @@ var _ = Describe("DeliverableReconciler", func() {
Name: "my-config-template",
APIVersion: "carto.run/v1alpha1",
},
}, nil, false)
}, 1, nil, false)

rlzr.RealizeStub = func(ctx context.Context, resourceRealizer realizer.ResourceRealizer, deliveryName string, resources []realizer.OwnerResource, statuses statuses.ResourceStatuses) error {
statusesVal := reflect.ValueOf(statuses)
Expand Down Expand Up @@ -632,7 +632,7 @@ var _ = Describe("DeliverableReconciler", func() {
Name: "my-image-template",
APIVersion: "carto.run/v1alpha1",
},
}, nil, false,
}, 0, nil, false,
)
resourceStatuses.Add(
&v1alpha1.RealizedResource{
Expand All @@ -643,7 +643,7 @@ var _ = Describe("DeliverableReconciler", func() {
Name: "my-config-template",
APIVersion: "carto.run/v1alpha1",
},
}, nil, false,
}, 1, nil, false,
)

rlzr.RealizeStub = func(ctx context.Context, resourceRealizer realizer.ResourceRealizer, deliveryName string, resources []realizer.OwnerResource, statuses statuses.ResourceStatuses) error {
Expand Down Expand Up @@ -1592,7 +1592,7 @@ var _ = Describe("DeliverableReconciler", func() {
Kind: "some-template-kind",
Name: "some-template-name",
},
}, nil, false,
}, 0, nil, false,
)
rlzr.RealizeStub = func(ctx context.Context, resourceRealizer realizer.ResourceRealizer, deliveryName string, resources []realizer.OwnerResource, statuses statuses.ResourceStatuses) error {
statusesVal := reflect.ValueOf(statuses)
Expand Down
10 changes: 5 additions & 5 deletions pkg/controllers/workload_reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ var _ = Describe("WorkloadReconciler", func() {
Name: "my-image-template",
APIVersion: "carto.run/v1alpha1",
},
}, nil, false,
}, 0, nil, false,
)
resourceStatuses.Add(
&v1alpha1.RealizedResource{
Expand All @@ -288,7 +288,7 @@ var _ = Describe("WorkloadReconciler", func() {
Name: "my-config-template",
APIVersion: "carto.run/v1alpha1",
},
}, nil, false,
}, 1, nil, false,
)

rlzr.RealizeStub = func(ctx context.Context, resourceRealizer realizer.ResourceRealizer, deliveryName string, resources []realizer.OwnerResource, statuses statuses.ResourceStatuses) error {
Expand Down Expand Up @@ -632,7 +632,7 @@ var _ = Describe("WorkloadReconciler", func() {
Name: "my-image-template",
APIVersion: "carto.run/v1alpha1",
},
}, nil, false,
}, 0, nil, false,
)
resourceStatuses.Add(
&v1alpha1.RealizedResource{
Expand All @@ -643,7 +643,7 @@ var _ = Describe("WorkloadReconciler", func() {
Name: "my-config-template",
APIVersion: "carto.run/v1alpha1",
},
}, nil, false,
}, 1, nil, false,
)

rlzr.RealizeStub = func(ctx context.Context, resourceRealizer realizer.ResourceRealizer, deliveryName string, resources []realizer.OwnerResource, statuses statuses.ResourceStatuses) error {
Expand Down Expand Up @@ -1381,7 +1381,7 @@ var _ = Describe("WorkloadReconciler", func() {
Kind: "some-template-kind",
Name: "some-template-name",
},
}, nil, false,
}, 0, nil, false,
)
rlzr.RealizeStub = func(ctx context.Context, resourceRealizer realizer.ResourceRealizer, deliveryName string, resources []realizer.OwnerResource, statuses statuses.ResourceStatuses) error {
statusesVal := reflect.ValueOf(statuses)
Expand Down
4 changes: 2 additions & 2 deletions pkg/realizer/realizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (r *realizer) Realize(ctx context.Context, resourceRealizer ResourceRealize
outs := NewOutputs()
var firstError error

for _, resource := range ownerResources {
for resourceIndex, resource := range ownerResources {
log = log.WithValues("resource", resource.Name)
ctx = logr.NewContext(ctx, log)
template, stampedObject, out, isPassThrough, templateName, err := resourceRealizer.Do(ctx, resource, blueprintName, outs, r.mapper)
Expand Down Expand Up @@ -168,7 +168,7 @@ func (r *realizer) Realize(ctx context.Context, resourceRealizer ResourceRealize
}
}

resourceStatuses.Add(realizedResource, err, isPassThrough, additionalConditions...)
resourceStatuses.Add(realizedResource, resourceIndex, err, isPassThrough, additionalConditions...)

if slices.Contains(resourceStatuses.ChangedConditionTypes(realizedResource.Name), v1alpha1.ResourceHealthy) {
newStatus := metav1.ConditionUnknown
Expand Down
45 changes: 44 additions & 1 deletion pkg/realizer/realizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,7 @@ var _ = Describe("Realize", func() {
previousResources []v1alpha1.ResourceStatus
previousTime metav1.Time
supplyChain *v1alpha1.ClusterSupplyChain
resource2 v1alpha1.SupplyChainResource
)
BeforeEach(func() {
previousTime = metav1.NewTime(time.Now())
Expand Down Expand Up @@ -593,7 +594,7 @@ var _ = Describe("Realize", func() {
resource1 := v1alpha1.SupplyChainResource{
Name: "resource1",
}
resource2 := v1alpha1.SupplyChainResource{
resource2 = v1alpha1.SupplyChainResource{
Name: "resource2",
Sources: []v1alpha1.ResourceReference{
{
Expand Down Expand Up @@ -691,6 +692,10 @@ var _ = Describe("Realize", func() {
var resource2Status v1alpha1.ResourceStatus
var resource3Status v1alpha1.ResourceStatus

for i := range currentStatuses {
Expect(currentStatuses[i].Name).To(Equal(fmt.Sprintf("resource%d", i+1)))
}

for i := range currentStatuses {
switch currentStatuses[i].Name {
case "resource1":
Expand Down Expand Up @@ -743,6 +748,44 @@ var _ = Describe("Realize", func() {
Expect(resourceObj).To(Equal(stampedObj1))
})

Context("the supply chain has changed to have fewer resources", func() {
var currentStatuses []v1alpha1.ResourceStatus
BeforeEach(func() {
supplyChain = &v1alpha1.ClusterSupplyChain{
ObjectMeta: metav1.ObjectMeta{Name: "greatest-supply-chain"},
Spec: v1alpha1.SupplyChainSpec{
Resources: []v1alpha1.SupplyChainResource{resource2},
},
}

template2 := &v1alpha1.ClusterImageTemplate{
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{
Name: "my-image-template",
},
}
reader2, err := templates.NewReaderFromAPI(template2)
Expect(err).NotTo(HaveOccurred())

resourceRealizer.DoReturnsOnCall(0, reader2, &unstructured.Unstructured{}, nil, false, resource2.Name, nil)

oldOutput := &templates.Output{
Image: "whatever",
}
resourceRealizer.DoReturnsOnCall(0, reader2, &unstructured.Unstructured{}, oldOutput, false, "", nil)

resourceStatuses := statuses.NewResourceStatuses(previousResources, conditions.AddConditionForResourceSubmittedWorkload)
err = rlzr.Realize(ctx, resourceRealizer, supplyChain.Name, realizer.MakeSupplychainOwnerResources(supplyChain), resourceStatuses)
Expect(err).ToNot(HaveOccurred())

currentStatuses = resourceStatuses.GetCurrent()
})
It("only creates 1 resource status in currentStatuses", func() {
Expect(currentStatuses[0].Name).To(Equal("resource2"))
Expect(len(currentStatuses)).To(Equal(1))
})
})

Context("there is an error realizing resource 1 and resource 2", func() {
BeforeEach(func() {
resourceRealizer.DoReturnsOnCall(0, nil, nil, nil, false, "", errors.New("im in a bad state"))
Expand Down
8 changes: 5 additions & 3 deletions pkg/realizer/statuses/resource_statuses.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
type ResourceStatuses interface {
ChangedConditionTypes(realizedResourceName string) []string
GetPreviousResourceStatus(realizedResourceName string) *v1alpha1.ResourceStatus
Add(status *v1alpha1.RealizedResource, err error, isPassThrough bool, furtherConditions ...metav1.Condition)
Add(status *v1alpha1.RealizedResource, resourceIndex int, err error, isPassThrough bool, furtherConditions ...metav1.Condition)
GetCurrent() ResourceStatusList
IsChanged() bool
}
Expand Down Expand Up @@ -114,7 +114,7 @@ func (r *resourceStatuses) GetPreviousResourceStatus(realizedResourceName string
return nil
}

func (r *resourceStatuses) Add(realizedResource *v1alpha1.RealizedResource, err error, isPassThrough bool, furtherConditions ...metav1.Condition) {
func (r *resourceStatuses) Add(realizedResource *v1alpha1.RealizedResource, resourceIndex int, err error, isPassThrough bool, furtherConditions ...metav1.Condition) {
name := realizedResource.Name

var existingStatus *resourceStatus
Expand All @@ -129,7 +129,9 @@ func (r *resourceStatuses) Add(realizedResource *v1alpha1.RealizedResource, err
existingStatus = &resourceStatus{
name: name,
}
r.statuses = append(r.statuses, existingStatus)
r.statuses = append(r.statuses, &resourceStatus{})
copy(r.statuses[resourceIndex+1:], r.statuses[resourceIndex:len(r.statuses)-1])
r.statuses[resourceIndex] = existingStatus
}

existingStatus.current = &v1alpha1.ResourceStatus{
Expand Down
20 changes: 10 additions & 10 deletions pkg/realizer/statuses/resource_statuses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ var _ = Describe("ResourceStatuses", func() {
TemplateRef: nil,
Inputs: nil,
Outputs: nil,
}, nil, false)
}, 0, nil, false)
})

It("the resourceStatuses reports IsChanged is false", func() {
Expand All @@ -97,7 +97,7 @@ var _ = Describe("ResourceStatuses", func() {
TemplateRef: nil,
Inputs: nil,
Outputs: nil,
}, nil, false)
}, 1, nil, false)
Expect(resourceStatuses.IsChanged()).To(BeTrue())
})

Expand All @@ -108,7 +108,7 @@ var _ = Describe("ResourceStatuses", func() {
TemplateRef: nil,
Inputs: nil,
Outputs: nil,
}, nil, false)
}, 1, nil, false)
Expect(resourceStatuses.ChangedConditionTypes("resource2")).To(ConsistOf(v1alpha1.ResourceSubmitted, v1alpha1.ResourceReady))
})

Expand All @@ -119,7 +119,7 @@ var _ = Describe("ResourceStatuses", func() {
TemplateRef: nil,
Inputs: nil,
Outputs: nil,
}, nil, false, metav1.Condition{
}, 1, nil, false, metav1.Condition{
Type: v1alpha1.ResourceHealthy,
Status: "Unknown",
})
Expand All @@ -133,7 +133,7 @@ var _ = Describe("ResourceStatuses", func() {
TemplateRef: nil,
Inputs: nil,
Outputs: nil,
}, nil, false, metav1.Condition{
}, 1, nil, false, metav1.Condition{
Type: v1alpha1.ResourceHealthy,
Status: "False",
})
Expand All @@ -147,7 +147,7 @@ var _ = Describe("ResourceStatuses", func() {
TemplateRef: nil,
Inputs: nil,
Outputs: nil,
}, nil, false, metav1.Condition{
}, 1, nil, false, metav1.Condition{
Type: v1alpha1.ResourceHealthy,
Status: "True",
})
Expand All @@ -168,7 +168,7 @@ var _ = Describe("ResourceStatuses", func() {
TemplateRef: nil,
Inputs: nil,
Outputs: nil,
}, nil, false)
}, 0, nil, false)
Expect(resourceStatuses.IsChanged()).To(BeTrue())
})
})
Expand All @@ -181,7 +181,7 @@ var _ = Describe("ResourceStatuses", func() {
TemplateRef: nil,
Inputs: nil,
Outputs: nil,
}, errors.New("has an error"), false)
}, 0, errors.New("has an error"), false)
Expect(resourceStatuses.IsChanged()).To(BeTrue())
})

Expand All @@ -192,7 +192,7 @@ var _ = Describe("ResourceStatuses", func() {
TemplateRef: nil,
Inputs: nil,
Outputs: nil,
}, errors.New("has an error"), false)
}, 0, errors.New("has an error"), false)
Expect(resourceStatuses.ChangedConditionTypes("resource1")).To(ConsistOf("Ready", "ResourceSubmitted"))
})
})
Expand All @@ -217,7 +217,7 @@ var _ = Describe("ResourceStatuses", func() {
TemplateRef: nil,
Inputs: nil,
Outputs: nil,
}, nil, false)
}, 0, nil, false)
Expect(resourceStatuses.IsChanged()).To(BeTrue())
})
})
Expand Down
Loading
Loading