Skip to content

Commit

Permalink
ssa: Skip suspended resources from wait result errors
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Prodan <[email protected]>
  • Loading branch information
stefanprodan committed Apr 23, 2024
1 parent 92c1348 commit 55e053d
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
6 changes: 6 additions & 0 deletions ssa/manager_wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ func (m *ResourceManager) WaitForSet(set object.ObjMetadataSet, opts WaitOptions
errs = append(errs, builder.String())
case errors.Is(ctx.Err(), context.DeadlineExceeded) && lastStatus[id].Status != status.CurrentStatus:
var builder strings.Builder
if utils.IsSuspended(lastStatus[id].Resource) {
// skip suspended resources that are not in a failed state
// as they are not expected to be reconciled and
// their observed generation will always be behind
continue
}
builder.WriteString(fmt.Sprintf("%s status: '%s'",
utils.FmtObjMetadata(rs.Identifier), lastStatus[id].Status))
if rs.Error != nil {
Expand Down
39 changes: 38 additions & 1 deletion ssa/manager_wait_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ func TestWaitForSet(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

waitOps := DefaultWaitOptions()
waitOps.Timeout = timeout

id := generateName("wait")
objects, err := readManifest("testdata/test5.yaml", id)
if err != nil {
Expand Down Expand Up @@ -99,7 +102,41 @@ func TestWaitForSet(t *testing.T) {
t.Fatal(err)
}

if err := manager.WaitForSet(changeSet.ToObjMetadataSet(), DefaultWaitOptions()); err != nil {
if err := manager.WaitForSet(changeSet.ToObjMetadataSet(), waitOps); err != nil {
t.Errorf("wait error: %v", err)
}
})

t.Run("skips suspended CRs", func(t *testing.T) {
clusterCR := &unstructured.Unstructured{}
clusterCR.SetGroupVersionKind(schema.GroupVersionKind{
Group: "testing.fluxcd.io",
Kind: "ClusterTest",
Version: "v1",
})
if err := manager.client.Get(ctx, client.ObjectKeyFromObject(cr), clusterCR); err != nil {
t.Fatal(err)
}

clusterCR.SetManagedFields(nil)
err = unstructured.SetNestedField(clusterCR.Object, true, "spec", "suspend")
if err != nil {
t.Fatal(err)
}

opts := &client.SubResourcePatchOptions{
PatchOptions: client.PatchOptions{
FieldManager: manager.owner.Field,
},
}

// Suspend the CR and thus bumping the generation.
if err := manager.client.Patch(ctx, clusterCR, client.Apply, opts); err != nil {
t.Fatal(err)
}

metaSet := object.UnstructuredSetToObjMetadataSet([]*unstructured.Unstructured{clusterCR})
if err := manager.WaitForSet(metaSet, waitOps); err != nil {
t.Errorf("wait error: %v", err)
}
})
Expand Down
2 changes: 2 additions & 0 deletions ssa/testdata/test5.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ spec:
spec:
description: TestSpec defines the desired state of a test run
properties:
suspend:
type: boolean
type:
description: Type of test
type: string
Expand Down
18 changes: 18 additions & 0 deletions ssa/utils/is.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,21 @@ func IsKustomization(object *unstructured.Unstructured) bool {
func IsSecret(object *unstructured.Unstructured) bool {
return strings.ToLower(object.GetKind()) == "secret" && object.GetAPIVersion() == "v1"
}

// IsSuspended returns true if the given Flux object has '.spec.suspend' set to true.
func IsSuspended(object *unstructured.Unstructured) bool {
if object == nil {
return false
}

if !strings.Contains(object.GetAPIVersion(), "fluxcd") {
return false
}

suspended, found, err := unstructured.NestedBool(object.Object, "spec", "suspend")
if err != nil || !found {
return false
}

return suspended
}

0 comments on commit 55e053d

Please sign in to comment.