-
Notifications
You must be signed in to change notification settings - Fork 73
feat(nvca): emit events for MiniService abnormal status conditions #1939
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e46ef3b
feat(nvca): emit events for MiniService abnormal status conditions
estroz 145ea91
fix(nvca): correct MiniService event message length cap to 1024
estroz 1bbde8c
fix(nvca): truncate MiniService event messages on rune boundaries
estroz fd7fc03
fix(nvca): register events.go/events_test.go in BUILD.bazel
estroz 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
80 changes: 80 additions & 0 deletions
80
src/compute-plane-services/nvca/internal/miniservice/events.go
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,80 @@ | ||
| /* | ||
| SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package mscontroller | ||
|
|
||
| import ( | ||
| "strings" | ||
| "unicode/utf8" | ||
|
|
||
| corev1 "k8s.io/api/core/v1" | ||
| "k8s.io/apimachinery/pkg/api/meta" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
|
||
| "github.com/NVIDIA/nvcf/src/compute-plane-services/nvca/pkg/apis/nvca/v1alpha1" | ||
| ) | ||
|
|
||
| // maxEventMessageLen bounds recorded event messages so a verbose or multi-line | ||
| // condition message can't bloat the Event object stored in etcd. Matches the | ||
| // +kubebuilder:validation:MaxLength=1024 already enforced on metav1.Condition.Message. | ||
| const maxEventMessageLen = 1024 | ||
|
|
||
| // recordEvent emits a Kubernetes event on ms. It is a no-op if the reconciler has | ||
| // no event recorder configured, so callers don't need to guard against a nil recorder. | ||
| func (r *Reconciler) recordEvent(ms *v1alpha1.MiniService, eventType, reason, messageFmt string, args ...any) { | ||
| if r.eventRecorder == nil { | ||
| return | ||
| } | ||
| r.eventRecorder.Eventf(ms, eventType, reason, messageFmt, args...) | ||
| } | ||
|
|
||
| // emitConditionEvents records a Warning event for each MiniService status condition that | ||
| // transitioned to False during this reconcile, and a Normal recovery event for each | ||
| // condition that transitioned back to True. Conditions whose Status and Reason are | ||
| // unchanged are skipped, so an unresolved abnormal state is not re-announced on every | ||
| // reconcile. | ||
| func (r *Reconciler) emitConditionEvents(ms *v1alpha1.MiniService, oldConditions, newConditions []metav1.Condition) { | ||
| for _, cond := range newConditions { | ||
| oldCond := meta.FindStatusCondition(oldConditions, cond.Type) | ||
| if oldCond != nil && oldCond.Status == cond.Status && oldCond.Reason == cond.Reason { | ||
| continue | ||
| } | ||
|
|
||
| switch cond.Status { | ||
| case metav1.ConditionFalse: | ||
| r.recordEvent(ms, corev1.EventTypeWarning, cond.Reason, "%s", sanitizeEventMessage(cond.Message)) | ||
| case metav1.ConditionTrue: | ||
| if oldCond != nil && oldCond.Status == metav1.ConditionFalse { | ||
| r.recordEvent(ms, corev1.EventTypeNormal, cond.Reason, "%s condition recovered", cond.Type) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // sanitizeEventMessage collapses a condition message to a single line and truncates it, | ||
| // so it stays a concise, etcd-friendly event message rather than a raw multi-line error dump. | ||
| func sanitizeEventMessage(msg string) string { | ||
| msg = strings.Join(strings.Fields(msg), " ") | ||
| if len(msg) > maxEventMessageLen { | ||
| cut := maxEventMessageLen - 3 | ||
| for cut > 0 && !utf8.RuneStart(msg[cut]) { | ||
| cut-- | ||
| } | ||
| msg = msg[:cut] + "..." | ||
| } | ||
| return msg | ||
| } | ||
188 changes: 188 additions & 0 deletions
188
src/compute-plane-services/nvca/internal/miniservice/events_test.go
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,188 @@ | ||
| /* | ||
| SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package mscontroller | ||
|
|
||
| import ( | ||
| "strings" | ||
| "testing" | ||
| "unicode/utf8" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/client-go/tools/record" | ||
|
|
||
| "github.com/NVIDIA/nvcf/src/compute-plane-services/nvca/pkg/apis/nvca/v1alpha1" | ||
| ) | ||
|
|
||
| // drainEvents reads any events currently buffered on rec.Events without blocking. | ||
| func drainEvents(t *testing.T, rec *record.FakeRecorder) []string { | ||
| t.Helper() | ||
| var events []string | ||
| for { | ||
| select { | ||
| case e := <-rec.Events: | ||
| events = append(events, e) | ||
| default: | ||
| return events | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func newEventTestMiniService() *v1alpha1.MiniService { | ||
| return &v1alpha1.MiniService{ | ||
| ObjectMeta: metav1.ObjectMeta{Name: "test-ms", Namespace: "ns"}, | ||
| } | ||
| } | ||
|
|
||
| func TestEmitConditionEvents(t *testing.T) { | ||
| t.Run("unset to false emits a warning event", func(t *testing.T) { | ||
| rec := record.NewFakeRecorder(10) | ||
| r := &Reconciler{eventRecorder: rec} | ||
| ms := newEventTestMiniService() | ||
|
|
||
| newConds := []metav1.Condition{{ | ||
| Type: v1alpha1.MiniServiceConditionObjectsHealthy, | ||
| Status: metav1.ConditionFalse, | ||
| Reason: v1alpha1.MiniServiceStatusReasonDegradedWorker, | ||
| Message: "worker pod worker-0 crash looping", | ||
| }} | ||
|
|
||
| r.emitConditionEvents(ms, nil, newConds) | ||
|
|
||
| events := drainEvents(t, rec) | ||
| require.Len(t, events, 1) | ||
| assert.Equal(t, "Warning DegradedWorker worker pod worker-0 crash looping", events[0]) | ||
| }) | ||
|
|
||
| t.Run("unchanged false condition does not re-emit", func(t *testing.T) { | ||
| rec := record.NewFakeRecorder(10) | ||
| r := &Reconciler{eventRecorder: rec} | ||
| ms := newEventTestMiniService() | ||
|
|
||
| cond := metav1.Condition{ | ||
| Type: v1alpha1.MiniServiceConditionObjectsHealthy, | ||
| Status: metav1.ConditionFalse, | ||
| Reason: v1alpha1.MiniServiceStatusReasonObjectsFailed, | ||
| Message: "objects failed to deploy", | ||
| } | ||
|
|
||
| r.emitConditionEvents(ms, []metav1.Condition{cond}, []metav1.Condition{cond}) | ||
|
|
||
| assert.Empty(t, drainEvents(t, rec)) | ||
| }) | ||
|
|
||
| t.Run("false to false with a new reason emits a new warning event", func(t *testing.T) { | ||
| rec := record.NewFakeRecorder(10) | ||
| r := &Reconciler{eventRecorder: rec} | ||
| ms := newEventTestMiniService() | ||
|
|
||
| oldConds := []metav1.Condition{{ | ||
| Type: v1alpha1.MiniServiceConditionObjectsHealthy, | ||
| Status: metav1.ConditionFalse, | ||
| Reason: v1alpha1.MiniServiceStatusReasonWaitingObjectReadiness, | ||
| Message: "waiting on objects", | ||
| }} | ||
| newConds := []metav1.Condition{{ | ||
| Type: v1alpha1.MiniServiceConditionObjectsHealthy, | ||
| Status: metav1.ConditionFalse, | ||
| Reason: v1alpha1.MiniServiceStatusReasonPendingTimeout, | ||
| Message: "objects timed out pending", | ||
| }} | ||
|
|
||
| r.emitConditionEvents(ms, oldConds, newConds) | ||
|
|
||
| events := drainEvents(t, rec) | ||
| require.Len(t, events, 1) | ||
| assert.Equal(t, "Warning ObjectsTimedOutPending objects timed out pending", events[0]) | ||
| }) | ||
|
|
||
| t.Run("false to true emits a normal recovery event", func(t *testing.T) { | ||
| rec := record.NewFakeRecorder(10) | ||
| r := &Reconciler{eventRecorder: rec} | ||
| ms := newEventTestMiniService() | ||
|
|
||
| oldConds := []metav1.Condition{{ | ||
| Type: v1alpha1.MiniServiceConditionObjectsHealthy, | ||
| Status: metav1.ConditionFalse, | ||
| Reason: v1alpha1.MiniServiceStatusReasonDegradedWorker, | ||
| }} | ||
| newConds := []metav1.Condition{{ | ||
| Type: v1alpha1.MiniServiceConditionObjectsHealthy, | ||
| Status: metav1.ConditionTrue, | ||
| Reason: "ObjectsReady", | ||
| }} | ||
|
|
||
| r.emitConditionEvents(ms, oldConds, newConds) | ||
|
|
||
| events := drainEvents(t, rec) | ||
| require.Len(t, events, 1) | ||
| assert.Equal(t, "Normal ObjectsReady ObjectsHealthy condition recovered", events[0]) | ||
| }) | ||
|
|
||
| t.Run("unset to true does not emit a recovery event", func(t *testing.T) { | ||
| rec := record.NewFakeRecorder(10) | ||
| r := &Reconciler{eventRecorder: rec} | ||
| ms := newEventTestMiniService() | ||
|
|
||
| newConds := []metav1.Condition{{ | ||
| Type: v1alpha1.MiniServiceConditionObjectsHealthy, | ||
| Status: metav1.ConditionTrue, | ||
| Reason: "ObjectsReady", | ||
| }} | ||
|
|
||
| r.emitConditionEvents(ms, nil, newConds) | ||
|
|
||
| assert.Empty(t, drainEvents(t, rec)) | ||
| }) | ||
|
|
||
| t.Run("nil event recorder does not panic", func(t *testing.T) { | ||
| r := &Reconciler{} | ||
| ms := newEventTestMiniService() | ||
| newConds := []metav1.Condition{{ | ||
| Type: v1alpha1.MiniServiceConditionObjectsHealthy, | ||
| Status: metav1.ConditionFalse, | ||
| Reason: v1alpha1.MiniServiceStatusReasonObjectsFailed, | ||
| }} | ||
|
|
||
| assert.NotPanics(t, func() { r.emitConditionEvents(ms, nil, newConds) }) | ||
| }) | ||
| } | ||
|
|
||
| func TestSanitizeEventMessage(t *testing.T) { | ||
| t.Run("collapses newlines and extra whitespace", func(t *testing.T) { | ||
| msg := "line one\nline two\n\tline three" | ||
| assert.Equal(t, "line one line two line three", sanitizeEventMessage(msg)) | ||
| }) | ||
|
|
||
| t.Run("truncates overly long messages", func(t *testing.T) { | ||
| msg := strings.Repeat("a", maxEventMessageLen+50) | ||
| got := sanitizeEventMessage(msg) | ||
| assert.LessOrEqual(t, len(got), maxEventMessageLen) | ||
| assert.True(t, strings.HasSuffix(got, "...")) | ||
| }) | ||
|
|
||
| t.Run("truncates without splitting a multi-byte rune", func(t *testing.T) { | ||
| // "e" is a 3-byte rune (U+00e9 encoded as UTF-8 wouldn't apply here, use a | ||
| // genuine multi-byte character so the naive byte cut lands mid-rune). | ||
| msg := strings.Repeat("a", maxEventMessageLen-4) + "中文" // two 3-byte CJK runes | ||
| got := sanitizeEventMessage(msg) | ||
| assert.True(t, utf8.ValidString(got)) | ||
| assert.LessOrEqual(t, len(got), maxEventMessageLen) | ||
| }) | ||
| } |
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
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
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.