-
Notifications
You must be signed in to change notification settings - Fork 79
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
feat: Add dependency filter #555
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
// Copyright 2020 The Kubernetes Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package filter | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"sigs.k8s.io/cli-utils/pkg/apis/actuation" | ||
"sigs.k8s.io/cli-utils/pkg/apply/taskrunner" | ||
"sigs.k8s.io/cli-utils/pkg/object" | ||
) | ||
|
||
//go:generate stringer -type=Relationship -linecomment | ||
type Relationship int | ||
|
||
const ( | ||
RelationshipDependent Relationship = iota // Dependent | ||
RelationshipDependency // Dependency | ||
) | ||
|
||
// DependencyFilter implements ValidationFilter interface to determine if an | ||
// object can be applied or deleted based on the status of it's dependencies. | ||
type DependencyFilter struct { | ||
TaskContext *taskrunner.TaskContext | ||
Strategy actuation.ActuationStrategy | ||
} | ||
|
||
const DependencyFilterName = "DependencyFilter" | ||
|
||
// Name returns the name of the filter for logs and events. | ||
func (dnrf DependencyFilter) Name() string { | ||
return DependencyFilterName | ||
} | ||
|
||
// Filter returns true if the specified object should be skipped because at | ||
// least one of its dependencies is Not Found or Not Reconciled. | ||
func (dnrf DependencyFilter) Filter(obj *unstructured.Unstructured) (bool, string, error) { | ||
id := object.UnstructuredToObjMetadata(obj) | ||
|
||
switch dnrf.Strategy { | ||
case actuation.ActuationStrategyApply: | ||
// For apply, check dependencies (outgoing) | ||
for _, depID := range dnrf.TaskContext.Graph().Dependencies(id) { | ||
skip, reason, err := dnrf.filterByRelationStatus(depID, RelationshipDependency) | ||
if err != nil { | ||
return false, "", err | ||
} | ||
if skip { | ||
return skip, reason, nil | ||
} | ||
} | ||
case actuation.ActuationStrategyDelete: | ||
// For delete, check dependents (incoming) | ||
for _, depID := range dnrf.TaskContext.Graph().Dependents(id) { | ||
skip, reason, err := dnrf.filterByRelationStatus(depID, RelationshipDependent) | ||
if err != nil { | ||
return false, "", err | ||
} | ||
if skip { | ||
return skip, reason, nil | ||
} | ||
} | ||
default: | ||
panic(fmt.Sprintf("invalid filter strategy: %q", dnrf.Strategy)) | ||
} | ||
return false, "", nil | ||
} | ||
|
||
func (dnrf DependencyFilter) filterByRelationStatus(id object.ObjMetadata, relationship Relationship) (bool, string, error) { | ||
// Dependency on an invalid object is considered an invalid dependency, making both objects invalid. | ||
// For applies: don't prematurely apply something that depends on something that hasn't been applied (because invalid). | ||
// For deletes: don't prematurely delete something that depends on something that hasn't been deleted (because invalid). | ||
// These can't be caught be subsequent checks, because invalid objects aren't in the inventory. | ||
if dnrf.TaskContext.IsInvalidObject(id) { | ||
// Skip! | ||
return true, fmt.Sprintf("%s invalid: %q", | ||
strings.ToLower(relationship.String()), | ||
id), nil | ||
} | ||
|
||
status, found := dnrf.TaskContext.InventoryManager().ObjectStatus(id) | ||
if !found { | ||
// Status is registered during planning. | ||
// So if status is not found, the object is external (NYI) or invalid. | ||
return false, "", fmt.Errorf("unknown %s actuation strategy: %v", | ||
strings.ToLower(relationship.String()), id) | ||
} | ||
|
||
// Dependencies must have the same actuation strategy. | ||
// If there is a mismatch, skip both. | ||
if status.Strategy != dnrf.Strategy { | ||
return true, fmt.Sprintf("%s skipped because %s is scheduled for %s: %q", | ||
strings.ToLower(dnrf.Strategy.String()), | ||
strings.ToLower(relationship.String()), | ||
strings.ToLower(status.Strategy.String()), | ||
id), nil | ||
} | ||
|
||
switch status.Actuation { | ||
case actuation.ActuationPending: | ||
// If actuation is still pending, dependency sorting is probably broken. | ||
return false, "", fmt.Errorf("premature %s: %s %s actuation %s: %q", | ||
strings.ToLower(dnrf.Strategy.String()), | ||
strings.ToLower(relationship.String()), | ||
strings.ToLower(status.Strategy.String()), | ||
strings.ToLower(status.Actuation.String()), | ||
id) | ||
case actuation.ActuationSkipped, actuation.ActuationFailed: | ||
// Skip! | ||
return true, fmt.Sprintf("%s %s actuation %s: %q", | ||
strings.ToLower(relationship.String()), | ||
strings.ToLower(dnrf.Strategy.String()), | ||
strings.ToLower(status.Actuation.String()), | ||
id), nil | ||
case actuation.ActuationSucceeded: | ||
// Don't skip! | ||
default: | ||
return false, "", fmt.Errorf("invalid %s apply status %q: %q", | ||
strings.ToLower(relationship.String()), | ||
strings.ToLower(status.Actuation.String()), | ||
id) | ||
} | ||
|
||
switch status.Reconcile { | ||
case actuation.ReconcilePending: | ||
// If reconcile is still pending, dependency sorting is probably broken. | ||
return false, "", fmt.Errorf("premature %s: %s %s reconcile %s: %q", | ||
strings.ToLower(dnrf.Strategy.String()), | ||
strings.ToLower(relationship.String()), | ||
strings.ToLower(status.Strategy.String()), | ||
strings.ToLower(status.Reconcile.String()), | ||
id) | ||
case actuation.ReconcileSkipped, actuation.ReconcileFailed, actuation.ReconcileTimeout: | ||
// Skip! | ||
return true, fmt.Sprintf("%s %s reconcile %s: %q", | ||
strings.ToLower(relationship.String()), | ||
strings.ToLower(dnrf.Strategy.String()), | ||
strings.ToLower(status.Reconcile.String()), | ||
id), nil | ||
case actuation.ReconcileSucceeded: | ||
// Don't skip! | ||
default: | ||
return false, "", fmt.Errorf("invalid dependency reconcile status %q: %q", | ||
strings.ToLower(status.Reconcile.String()), id) | ||
} | ||
|
||
// Don't skip! | ||
return false, "", nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe not something we need to address in this PR, but it feels like the TaskContext should be passed in as a parameter to the
Filter
function rather than being a property on the struct.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, but was trying to limit the amount of unrelated changes. That can be done in a separate PR.