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

ssa: Skip suspended resources from wait result errors #765

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it safe to do this? If I have several resources deployed by a kustomization, and I want them all healthy before triggering another dependent kustomization, skipping a suspended kustomization might have unforseen consequences.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's debatable, if you suspend any Flux object, than all it's dependants will fail to reconcile unless you resume it, even if the object is marked as Ready=True.

// 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
}
Loading