-
Notifications
You must be signed in to change notification settings - Fork 32
k8s API for healtheventwithstatus model #640
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
Draft
yavinash007
wants to merge
3
commits into
NVIDIA:main
Choose a base branch
from
yavinash007:healtheventwithstatus
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.
Draft
Changes from all commits
Commits
Show all changes
3 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
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,151 @@ | ||
| package v1alpha1 | ||
|
|
||
| import ( | ||
| "time" | ||
|
|
||
| "github.com/nvidia/nvsentinel/data-models/pkg/model" | ||
| "github.com/nvidia/nvsentinel/data-models/pkg/protos" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| ) | ||
|
|
||
| // ---------------------------- | ||
| // Helpers | ||
| // ---------------------------- | ||
|
|
||
| func toMetaTime(t *time.Time) *metav1.Time { | ||
| if t == nil { | ||
| return nil | ||
| } | ||
| mt := metav1.NewTime(*t) | ||
| return &mt | ||
| } | ||
|
|
||
| func fromMetaTime(mt *metav1.Time) *time.Time { | ||
| if mt == nil { | ||
| return nil | ||
| } | ||
| t := mt.Time | ||
| return &t | ||
| } | ||
|
|
||
| func recommendedActionFromProto(p protos.RecommendedAction) RecommendedAction { | ||
| switch p { | ||
| case protos.RecommendedAction_COMPONENT_RESET: | ||
| return RecommendedActionComponentReset | ||
| case protos.RecommendedAction_CONTACT_SUPPORT: | ||
| return RecommendedActionContactSupport | ||
| case protos.RecommendedAction_RUN_FIELDDIAG: | ||
| return RecommendedActionRunFieldDiag | ||
| case protos.RecommendedAction_RESTART_VM: | ||
| return RecommendedActionRestartVM | ||
| case protos.RecommendedAction_RESTART_BM: | ||
| return RecommendedActionRestartBM | ||
| case protos.RecommendedAction_REPLACE_VM: | ||
| return RecommendedActionReplaceVM | ||
| case protos.RecommendedAction_RUN_DCGMEUD: | ||
| return RecommendedActionRunDCGMEUD | ||
| case protos.RecommendedAction_NONE: | ||
| return RecommendedActionNone | ||
| default: | ||
| return RecommendedActionUnknown | ||
| } | ||
| } | ||
|
|
||
| // Converts proto HealthEvent β CRD HealthEventSpec | ||
| func HealthEventSpecFromProto(p *protos.HealthEvent) HealthEventSpec { | ||
| if p == nil { | ||
| return HealthEventSpec{} | ||
| } | ||
|
|
||
| return HealthEventSpec{ | ||
| NodeName: p.NodeName, | ||
| RecommendedAction: recommendedActionFromProto(p.RecommendedAction), | ||
| } | ||
| } | ||
|
|
||
| // SafeCopyHealthEventStatusCore - Converts model.HealthEventStatusCore β CRD HealthEventStatusCore safely (new pointers) | ||
| // SafeCopyHealthEventStatusCore creates a copy of model.HealthEventStatusCore | ||
| // suitable for use in CRDs. | ||
| // | ||
| // Details: | ||
| // - This function performs a shallow copy for most fields that are safe to copy by value. | ||
| // - Pointer fields (NodeQuarantined, UserPodsEvictionStatus, FaultRemediated) are | ||
| // explicitly recreated to ensure that the CRD object is independent of the original | ||
| // model object. This prevents unintended sharing of memory and avoids side-effects | ||
| // when the controller updates the status in-place. | ||
| // | ||
| // Usage: | ||
| // - Always use this function when converting from the internal model to the CRD's | ||
| // HealthEventStatusCore to safely populate the CRD without affecting the original model. | ||
| // - Do NOT rely on autogenerated deepcopy functions for this struct, because they | ||
| // could inadvertently create shared pointers, leading to subtle bugs in the controller. | ||
| func SafeCopyHealthEventStatusCore(src model.HealthEventStatusCore) model.HealthEventStatusCore { | ||
| var nq *Status | ||
| if src.NodeQuarantined != nil { | ||
| tmp := Status(*src.NodeQuarantined) | ||
| nq = &tmp | ||
| } | ||
|
|
||
| var ups *OperationStatus | ||
| if src.UserPodsEvictionStatus != nil { | ||
| tmp := &OperationStatus{ | ||
| Status: Status(src.UserPodsEvictionStatus.Status), | ||
| Message: src.UserPodsEvictionStatus.Message, | ||
| } | ||
| ups = tmp | ||
| } | ||
|
|
||
| var fr *bool | ||
| if src.FaultRemediated != nil { | ||
| tmp := *src.FaultRemediated | ||
| fr = &tmp | ||
| } | ||
|
|
||
| return model.HealthEventStatusCore{ | ||
| NodeQuarantined: nq, | ||
| UserPodsEvictionStatus: ups, | ||
| FaultRemediated: fr, | ||
| } | ||
| } | ||
|
|
||
| // Converts model.HealthEventStatus β CRD HealthEventStatus | ||
| func HealthEventStatusFromModel(heStatus model.HealthEventStatus) HealthEventStatus { | ||
| return HealthEventStatus{ | ||
| HealthEventStatusCore: SafeCopyHealthEventStatusCore(heStatus.HealthEventStatusCore), | ||
| LastRemediationTimestamp: toMetaTime(heStatus.LastRemediationTimestamp), | ||
| } | ||
| } | ||
|
|
||
| // Converts CRD HealthEventStatus β model.HealthEventStatus | ||
| func HealthEventStatusToModel(crStatus HealthEventStatus) model.HealthEventStatus { | ||
| return model.HealthEventStatus{ | ||
| HealthEventStatusCore: model.HealthEventStatusCore{ | ||
| NodeQuarantined: crStatus.NodeQuarantined, | ||
| UserPodsEvictionStatus: crStatus.UserPodsEvictionStatus, | ||
| FaultRemediated: crStatus.FaultRemediated, | ||
| }, | ||
| LastRemediationTimestamp: fromMetaTime(crStatus.LastRemediationTimestamp), | ||
| } | ||
| } | ||
|
|
||
| // Creates CR with both Spec and Status populated | ||
| func HealthEventCRFromModel(he *model.HealthEventWithStatus) *HealthEvent { | ||
| if he == nil || he.HealthEvent == nil { | ||
| return nil | ||
| } | ||
|
|
||
| return &HealthEvent{ | ||
| Spec: HealthEventSpecFromProto(he.HealthEvent), | ||
| Status: HealthEventStatusFromModel(he.HealthEventStatus), | ||
| } | ||
| } | ||
|
|
||
| // Converts CRD HealthEventSpec β proto HealthEvent fields (for testing / sync) | ||
| func HealthEventSpecToProto(spec HealthEventSpec, existing *protos.HealthEvent) *protos.HealthEvent { | ||
| if existing == nil { | ||
| existing = &protos.HealthEvent{} | ||
| } | ||
| existing.NodeName = spec.NodeName | ||
| // TODO: map RecommendedAction if needed | ||
| return existing | ||
| } |
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,34 @@ | ||
| // Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. | ||
| // | ||
| // 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 v1alpha1 contains API Schema definitions for the healthevents v1alpha1 API group | ||
| // +kubebuilder:object:generate=true | ||
| // +groupName=healthevents.dgxc.nvidia.com | ||
| package v1alpha1 | ||
|
|
||
| import ( | ||
| "k8s.io/apimachinery/pkg/runtime/schema" | ||
| "sigs.k8s.io/controller-runtime/pkg/scheme" | ||
| ) | ||
|
|
||
| var ( | ||
| // GroupVersion is group version used to register these objects | ||
| GroupVersion = schema.GroupVersion{Group: "healthevents.dgxc.nvidia.com", Version: "v1alpha1"} | ||
|
|
||
| // SchemeBuilder is used to add go types to the GroupVersionKind scheme | ||
| SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion} | ||
|
|
||
| // AddToScheme adds the types in this group-version to the given scheme. | ||
| AddToScheme = SchemeBuilder.AddToScheme | ||
| ) |
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,83 @@ | ||
| package v1alpha1 | ||
|
|
||
| import ( | ||
| "github.com/nvidia/nvsentinel/data-models/pkg/model" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| ) | ||
|
|
||
| // +kubebuilder:validation:Enum=NotStarted;InProgress;Failed;Succeeded;AlreadyDrained;UnQuarantined;Quarantined;AlreadyQuarantined;Cancelled | ||
| type Status = model.Status | ||
|
|
||
| type OperationStatus = model.OperationStatus | ||
|
|
||
| // RecommendedAction represents supported remediation actions | ||
| type RecommendedAction string | ||
|
|
||
| const ( | ||
| RecommendedActionNone RecommendedAction = "NONE" | ||
| RecommendedActionComponentReset RecommendedAction = "COMPONENT_RESET" | ||
| RecommendedActionContactSupport RecommendedAction = "CONTACT_SUPPORT" | ||
| RecommendedActionRunFieldDiag RecommendedAction = "RUN_FIELDDIAG" | ||
| RecommendedActionRestartVM RecommendedAction = "RESTART_VM" | ||
| RecommendedActionRestartBM RecommendedAction = "RESTART_BM" | ||
| RecommendedActionReplaceVM RecommendedAction = "REPLACE_VM" | ||
| RecommendedActionRunDCGMEUD RecommendedAction = "RUN_DCGMEUD" | ||
| RecommendedActionUnknown RecommendedAction = "UNKNOWN" | ||
| ) | ||
|
|
||
| // HealthEventStatus defines the observed state of HealthEvent | ||
| // +kubebuilder:object:skip | ||
| type HealthEventStatus struct { | ||
| // Node associated with this health event | ||
| // HealthEventStatusCore contains the pointer fields that require | ||
| // careful handling when copying to CRD objects. | ||
| // | ||
| // Note: | ||
| // We do NOT generate deepcopy for this field because: | ||
| // 1. The pointer fields (NodeQuarantined, UserPodsEvictionStatus, | ||
| // FaultRemediated) must be explicitly recreated to avoid sharing memory | ||
| // with the original model. | ||
| // 2. Controller-runtime may mutate the status in-place, and a shallow copy | ||
| // by default could cause unintended side-effects. | ||
| // | ||
| // When updating CRD status from the internal model, always use | ||
| // SafeCopyHealthEventStatusCore to safely convert the core status. | ||
|
|
||
| // +kubebuilder:object:skip | ||
| model.HealthEventStatusCore `json:",inline"` | ||
|
|
||
| // Timestamp of the last remediation attempt | ||
| LastRemediationTimestamp *metav1.Time `json:"lastRemediationTimestamp,omitempty"` | ||
| } | ||
|
|
||
| // HealthEventSpec defines the desired state of HealthEvent | ||
| type HealthEventSpec struct { | ||
|
|
||
| // +kubebuilder:validation:Required | ||
| NodeName string `json:"nodeName"` | ||
|
|
||
| // Recommended remediation action | ||
| // +kubebuilder:validation:Enum=NONE;COMPONENT_RESET;CONTACT_SUPPORT;RUN_FIELDDIAG;RESTART_VM;RESTART_BM;REPLACE_VM;RUN_DCGMEUD;UNKNOWN | ||
| RecommendedAction RecommendedAction `json:"recommendedAction,omitempty"` | ||
| } | ||
|
|
||
| // +kubebuilder:object:root=true | ||
| // +kubebuilder:subresource:status | ||
| type HealthEvent struct { | ||
| metav1.TypeMeta `json:",inline"` | ||
| metav1.ObjectMeta `json:"metadata,omitempty"` | ||
|
|
||
| Spec HealthEventSpec `json:"spec,omitempty"` | ||
| Status HealthEventStatus `json:"status,omitempty"` | ||
| } | ||
|
|
||
| // +kubebuilder:object:root=true | ||
| type HealthEventList struct { | ||
| metav1.TypeMeta `json:",inline"` | ||
| metav1.ListMeta `json:"metadata,omitempty"` | ||
| Items []HealthEvent `json:"items"` | ||
| } | ||
|
|
||
| func init() { | ||
| SchemeBuilder.Register(&HealthEvent{}, &HealthEventList{}) | ||
| } | ||
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.
This still looks like we're redefining the types, we ideally don't want to maintain two copies of the same types and keep them in sync