-
Notifications
You must be signed in to change notification settings - Fork 10
Improve ManagementCommand status conditions #983
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
Open
jessejlt
wants to merge
2
commits into
main
Choose a base branch
from
jesse/mgmt-status
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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,103 @@ | ||
| package v1 | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "sort" | ||
| "strings" | ||
|
|
||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| ) | ||
|
|
||
| // ConditionedResource is a ConditionedObject that also exposes generation tracking for status updates. | ||
| // +k8s:deepcopy-gen=false | ||
| type ConditionedResource interface { | ||
| ConditionedObject | ||
| GetGeneration() int64 | ||
| } | ||
|
|
||
| // FormatClusterLabels returns a stable, human-readable summary of cluster labels for status messages. | ||
| // Keys are sorted to make comparisons deterministic. | ||
| func FormatClusterLabels(labels map[string]string) string { | ||
| if len(labels) == 0 { | ||
| return "(none)" | ||
| } | ||
| keys := make([]string, 0, len(labels)) | ||
| for k := range labels { | ||
| keys = append(keys, k) | ||
| } | ||
| sort.Strings(keys) | ||
| parts := make([]string, 0, len(keys)) | ||
| for _, k := range keys { | ||
| parts = append(parts, fmt.Sprintf("%s=%s", k, labels[k])) | ||
| } | ||
| return strings.Join(parts, ", ") | ||
| } | ||
|
|
||
| // NewCriteriaCondition constructs a metav1.Condition encapsulating the outcome of criteria expression evaluation. | ||
| // The condition uses the shared Reason constants defined in conditions.go for consistent signaling across CRDs. | ||
| func NewCriteriaCondition(conditionType string, generation int64, matched bool, expression string, evaluationErr error, clusterLabels map[string]string) metav1.Condition { | ||
| labelSummary := FormatClusterLabels(clusterLabels) | ||
|
|
||
| condition := metav1.Condition{ | ||
| Type: conditionType, | ||
| ObservedGeneration: generation, | ||
| LastTransitionTime: metav1.Now(), | ||
| } | ||
|
|
||
| switch { | ||
| case expression == "": | ||
| condition.Status = metav1.ConditionTrue | ||
| condition.Reason = ReasonCriteriaMatched | ||
| condition.Message = "No criteria expression configured; defaulting to match" | ||
| case evaluationErr != nil: | ||
| condition.Status = metav1.ConditionFalse | ||
| condition.Reason = ReasonCriteriaExpressionError | ||
| condition.Message = fmt.Sprintf("Criteria expression %q failed: %v (cluster labels: %s)", expression, evaluationErr, labelSummary) | ||
| case matched: | ||
| condition.Status = metav1.ConditionTrue | ||
| condition.Reason = ReasonCriteriaMatched | ||
| condition.Message = fmt.Sprintf("Criteria expression %q matched cluster labels: %s", expression, labelSummary) | ||
| default: | ||
| condition.Status = metav1.ConditionFalse | ||
| condition.Reason = ReasonCriteriaNotMatched | ||
| condition.Message = fmt.Sprintf("Criteria expression %q evaluated to false for cluster labels: %s", expression, labelSummary) | ||
| } | ||
|
|
||
| return condition | ||
| } | ||
|
|
||
| // NewOwnerCondition constructs a metav1.Condition for owner-type signaling (execution success/failure) with sensible defaults. | ||
| func NewOwnerCondition(conditionType string, generation int64, status metav1.ConditionStatus, reason, message string) metav1.Condition { | ||
| condition := metav1.Condition{ | ||
| Type: conditionType, | ||
| Status: status, | ||
| Message: message, | ||
| ObservedGeneration: generation, | ||
| LastTransitionTime: metav1.Now(), | ||
| } | ||
|
|
||
| if reason != "" { | ||
| condition.Reason = reason | ||
| } else { | ||
| condition.Reason = defaultOwnerReason(status) | ||
| } | ||
|
|
||
| return condition | ||
| } | ||
|
|
||
| // SetOwnerCondition applies an owner-style condition to the provided resource using the supplied parameters. | ||
| func SetOwnerCondition(resource ConditionedResource, conditionType string, status metav1.ConditionStatus, reason, message string) { | ||
| condition := NewOwnerCondition(conditionType, resource.GetGeneration(), status, reason, message) | ||
| resource.SetCondition(condition) | ||
| } | ||
|
|
||
| func defaultOwnerReason(status metav1.ConditionStatus) string { | ||
| switch status { | ||
| case metav1.ConditionTrue: | ||
| return ConditionCompleted | ||
| case metav1.ConditionFalse: | ||
| return ConditionFailed | ||
| default: | ||
| return ReasonProgressing | ||
| } | ||
| } |
This file contains hidden or 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,57 @@ | ||
| package v1 | ||
|
|
||
| import ( | ||
| "errors" | ||
| "testing" | ||
|
|
||
| meta "k8s.io/apimachinery/pkg/api/meta" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestFormatClusterLabels(t *testing.T) { | ||
| labels := map[string]string{"environment": "prod", "region": "eastus"} | ||
| formatted := FormatClusterLabels(labels) | ||
| require.Equal(t, "environment=prod, region=eastus", formatted) | ||
|
|
||
| require.Equal(t, "(none)", FormatClusterLabels(nil)) | ||
| } | ||
|
|
||
| func TestNewCriteriaCondition(t *testing.T) { | ||
| labels := map[string]string{"region": "westus"} | ||
|
|
||
| cond := NewCriteriaCondition("test", 7, true, "region == 'westus'", nil, labels) | ||
| require.Equal(t, metav1.ConditionTrue, cond.Status) | ||
| require.Equal(t, ReasonCriteriaMatched, cond.Reason) | ||
| require.Contains(t, cond.Message, "region == 'westus'") | ||
| require.Equal(t, int64(7), cond.ObservedGeneration) | ||
|
|
||
| cond = NewCriteriaCondition("test", 7, false, "region == 'westus'", nil, labels) | ||
| require.Equal(t, metav1.ConditionFalse, cond.Status) | ||
| require.Equal(t, ReasonCriteriaNotMatched, cond.Reason) | ||
|
|
||
| cond = NewCriteriaCondition("test", 7, false, "", nil, labels) | ||
| require.Equal(t, metav1.ConditionTrue, cond.Status) | ||
| require.Equal(t, ReasonCriteriaMatched, cond.Reason) | ||
| require.Equal(t, "No criteria expression configured; defaulting to match", cond.Message) | ||
|
|
||
| cond = NewCriteriaCondition("test", 7, false, "region == 'westus'", errors.New("parse"), labels) | ||
| require.Equal(t, metav1.ConditionFalse, cond.Status) | ||
| require.Equal(t, ReasonCriteriaExpressionError, cond.Reason) | ||
| require.Contains(t, cond.Message, "parse") | ||
| } | ||
|
|
||
| func TestSetOwnerConditionHelper(t *testing.T) { | ||
| fn := &Function{} | ||
| fn.Generation = 3 | ||
|
|
||
| SetOwnerCondition(fn, "demo", metav1.ConditionTrue, "", "completed") | ||
|
|
||
| cond := meta.FindStatusCondition(fn.Status.Conditions, "demo") | ||
| require.NotNil(t, cond) | ||
| require.Equal(t, metav1.ConditionTrue, cond.Status) | ||
| require.Equal(t, ConditionCompleted, cond.Reason) | ||
| require.Equal(t, "completed", cond.Message) | ||
| require.Equal(t, int64(3), cond.ObservedGeneration) | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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,79 @@ | ||
| package v1 | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| meta "k8s.io/apimachinery/pkg/api/meta" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestManagementCommandSetConditionDefaults(t *testing.T) { | ||
| cmd := &ManagementCommand{} | ||
| cmd.Generation = 4 | ||
|
|
||
| cmd.SetCondition(metav1.Condition{Status: metav1.ConditionTrue}) | ||
| owner := meta.FindStatusCondition(cmd.Status.Conditions, ManagementCommandConditionOwner) | ||
| require.NotNil(t, owner) | ||
| require.Equal(t, metav1.ConditionTrue, owner.Status) | ||
| require.Equal(t, ConditionCompleted, owner.Reason) | ||
| require.Equal(t, int64(4), owner.ObservedGeneration) | ||
|
|
||
| // Simulate UpdateStatus call with empty reason/message should preserve existing reason/message | ||
| ownerReason := "custom" | ||
| ownerMessage := "custom message" | ||
| cmd.SetExecutionCondition(metav1.ConditionFalse, ownerReason, ownerMessage) | ||
|
|
||
| cmd.SetCondition(metav1.Condition{Status: metav1.ConditionFalse}) | ||
| owner = meta.FindStatusCondition(cmd.Status.Conditions, ManagementCommandConditionOwner) | ||
| require.Equal(t, ownerReason, owner.Reason) | ||
| require.Equal(t, ownerMessage, owner.Message) | ||
| } | ||
|
|
||
| func TestManagementCommandCriteriaMatchCondition(t *testing.T) { | ||
| labels := map[string]string{"environment": "prod"} | ||
| cmd := &ManagementCommand{} | ||
| cmd.Generation = 2 | ||
|
|
||
| cmd.SetCriteriaMatchCondition(true, "environment == 'prod'", nil, labels) | ||
| crit := meta.FindStatusCondition(cmd.Status.Conditions, ManagementCommandCriteriaMatch) | ||
| require.NotNil(t, crit) | ||
| require.Equal(t, metav1.ConditionTrue, crit.Status) | ||
| require.Equal(t, ReasonCriteriaMatched, crit.Reason) | ||
|
|
||
| cmd.SetCriteriaMatchCondition(false, "environment == 'prod'", assertiveErr("boom"), labels) | ||
| crit = meta.FindStatusCondition(cmd.Status.Conditions, ManagementCommandCriteriaMatch) | ||
| require.NotNil(t, crit) | ||
| require.Equal(t, metav1.ConditionFalse, crit.Status) | ||
| require.Equal(t, ReasonCriteriaExpressionError, crit.Reason) | ||
| require.Contains(t, crit.Message, "boom") | ||
| } | ||
|
|
||
| func TestManagementCommandExecutionCondition(t *testing.T) { | ||
| cmd := &ManagementCommand{} | ||
| cmd.Generation = 10 | ||
|
|
||
| cmd.SetExecutionCondition(metav1.ConditionFalse, ReasonManagementCommandExecutionFailed, "failed") | ||
| exec := meta.FindStatusCondition(cmd.Status.Conditions, ManagementCommandExecuted) | ||
| require.NotNil(t, exec) | ||
| require.Equal(t, metav1.ConditionFalse, exec.Status) | ||
| require.Equal(t, ReasonManagementCommandExecutionFailed, exec.Reason) | ||
| require.Equal(t, "failed", exec.Message) | ||
|
|
||
| owner := meta.FindStatusCondition(cmd.Status.Conditions, ManagementCommandConditionOwner) | ||
| require.NotNil(t, owner) | ||
| require.Equal(t, metav1.ConditionFalse, owner.Status) | ||
| require.Equal(t, ReasonManagementCommandExecutionFailed, owner.Reason) | ||
| require.Equal(t, "failed", owner.Message) | ||
|
|
||
| cmd.SetExecutionCondition(metav1.ConditionTrue, ReasonManagementCommandExecutionSucceeded, "ok") | ||
| exec = meta.FindStatusCondition(cmd.Status.Conditions, ManagementCommandExecuted) | ||
| require.Equal(t, metav1.ConditionTrue, exec.Status) | ||
| require.Equal(t, ReasonManagementCommandExecutionSucceeded, exec.Reason) | ||
| require.Equal(t, "ok", exec.Message) | ||
|
|
||
| owner = meta.FindStatusCondition(cmd.Status.Conditions, ManagementCommandConditionOwner) | ||
| require.Equal(t, metav1.ConditionTrue, owner.Status) | ||
| require.Equal(t, ReasonManagementCommandExecutionSucceeded, owner.Reason) | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.