Skip to content
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ dist/
# IDE & Editor Configurations
# ============================================================================


### JetBrains IDEs (GoLand, PyCharm, IntelliJ) ###
### JetBrains IDEs (GoLand, PyCharm, IntelliJ) ###
# User-specific stuff
Expand Down
1 change: 1 addition & 0 deletions .versions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ go_tools:
setup_envtest: 'latest'
goimports: 'v0.30.0'
crane: 'v0.20.2'
controller_gen: 'v0.20.0'

# Protocol Buffers / gRPC
protobuf:
Expand Down
26 changes: 26 additions & 0 deletions data-models/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,36 @@ DOCKER_EXTRA_ARGS :=
include ../make/common.mk
include ../make/go.mk

# API and CRD paths
API_DIR := api/v1alpha1
CRD_OUTPUT_DIR := $(REPO_ROOT)/distros/kubernetes/nvsentinel/crds


# =============================================================================
# MODULE-SPECIFIC TARGETS
# =============================================================================

# generate: Generate deepcopy, CRD types, and other Kubernetes boilerplate
# Depends on tools being installed
.PHONY: generate
generate: ## Generate CRDs and move them to Helm chart directory
@echo "Generating CRDs for $(API_DIR)..."
@# Install controller-gen if not present
@which controller-gen > /dev/null || (echo "Installing controller-gen..." && \
go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_GEN_VERSION))
@# Generate deepcopy files for API types
go run sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_GEN_VERSION) object paths=./$(API_DIR)
@# Generate CRDs directly into API_DIR
go run sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_GEN_VERSION) \
crd paths=./$(API_DIR) output:crd:dir=./$(API_DIR)
@# Move generated CRDs to Helm chart directory
@echo "Moving generated CRDs to $(CRD_OUTPUT_DIR)..."
@mkdir -p $(CRD_OUTPUT_DIR)
@mv ./$(API_DIR)/*.yaml $(CRD_OUTPUT_DIR)/ || true
@echo "CRDs generated and moved to $(CRD_OUTPUT_DIR)"
@ls -1 $(CRD_OUTPUT_DIR)/*.yaml || echo "No CRD YAMLs generated"


# Generate Go protobuf files for data-models (shared across all Go modules)
.PHONY: protos-generate
protos-generate: protos-clean
Expand Down
151 changes: 151 additions & 0 deletions data-models/api/v1alpha1/conversion.go
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
}
34 changes: 34 additions & 0 deletions data-models/api/v1alpha1/groupversion_info.go
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
)
83 changes: 83 additions & 0 deletions data-models/api/v1alpha1/healthstatus_types.go
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
Copy link
Contributor

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

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{})
}
Loading