|
| 1 | +// Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package crstatus |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "log/slog" |
| 20 | + |
| 21 | + "github.com/nvidia/nvsentinel/fault-remediation/pkg/config" |
| 22 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 23 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 24 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 25 | + "k8s.io/client-go/dynamic" |
| 26 | + "k8s.io/client-go/restmapper" |
| 27 | +) |
| 28 | + |
| 29 | +type CRStatusChecker struct { |
| 30 | + dynamicClient dynamic.Interface |
| 31 | + restMapper *restmapper.DeferredDiscoveryRESTMapper |
| 32 | + config *config.MaintenanceResource |
| 33 | + dryRun bool |
| 34 | +} |
| 35 | + |
| 36 | +func NewCRStatusChecker( |
| 37 | + dynamicClient dynamic.Interface, |
| 38 | + restMapper *restmapper.DeferredDiscoveryRESTMapper, |
| 39 | + cfg *config.MaintenanceResource, |
| 40 | + dryRun bool, |
| 41 | +) *CRStatusChecker { |
| 42 | + return &CRStatusChecker{ |
| 43 | + dynamicClient: dynamicClient, |
| 44 | + restMapper: restMapper, |
| 45 | + config: cfg, |
| 46 | + dryRun: dryRun, |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func (c *CRStatusChecker) ShouldSkipCRCreation(ctx context.Context, crName string) bool { |
| 51 | + if c.dryRun { |
| 52 | + slog.Info("DRY-RUN: CR doesn't exist (dry-run mode)", "crName", crName) |
| 53 | + return false |
| 54 | + } |
| 55 | + |
| 56 | + gvk := schema.GroupVersionKind{ |
| 57 | + Group: c.config.ApiGroup, |
| 58 | + Version: c.config.Version, |
| 59 | + Kind: c.config.Kind, |
| 60 | + } |
| 61 | + |
| 62 | + mapping, err := c.restMapper.RESTMapping(gvk.GroupKind(), gvk.Version) |
| 63 | + if err != nil { |
| 64 | + slog.Error("Failed to get REST mapping", "gvk", gvk.String(), "error", err) |
| 65 | + return false |
| 66 | + } |
| 67 | + |
| 68 | + resource, err := c.dynamicClient.Resource(mapping.Resource).Get(ctx, crName, metav1.GetOptions{}) |
| 69 | + if err != nil { |
| 70 | + slog.Warn("Failed to get CR, allowing create", "crName", crName, "error", err) |
| 71 | + return false |
| 72 | + } |
| 73 | + |
| 74 | + return c.checkCondition(resource) |
| 75 | +} |
| 76 | + |
| 77 | +func (c *CRStatusChecker) checkCondition(obj *unstructured.Unstructured) bool { |
| 78 | + status, found, err := unstructured.NestedMap(obj.Object, "status") |
| 79 | + if err != nil || !found { |
| 80 | + return true |
| 81 | + } |
| 82 | + |
| 83 | + conditions, found, err := unstructured.NestedSlice(status, "conditions") |
| 84 | + if err != nil || !found { |
| 85 | + return true |
| 86 | + } |
| 87 | + |
| 88 | + conditionStatus := c.findConditionStatus(conditions) |
| 89 | + |
| 90 | + return !c.isTerminal(conditionStatus) |
| 91 | +} |
| 92 | + |
| 93 | +func (c *CRStatusChecker) findConditionStatus(conditions []any) string { |
| 94 | + for _, cond := range conditions { |
| 95 | + condition, ok := cond.(map[string]interface{}) |
| 96 | + if !ok { |
| 97 | + continue |
| 98 | + } |
| 99 | + |
| 100 | + condType, _ := condition["type"].(string) |
| 101 | + if condType == c.config.CompleteConditionType { |
| 102 | + condStatus, _ := condition["status"].(string) |
| 103 | + return condStatus |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + return "" |
| 108 | +} |
| 109 | + |
| 110 | +func (c *CRStatusChecker) isTerminal(conditionStatus string) bool { |
| 111 | + return conditionStatus == "True" || conditionStatus == "False" |
| 112 | +} |
0 commit comments