|
| 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 deployment |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "strings" |
| 21 | + |
| 22 | + "github.com/NVIDIA/eidos/pkg/defaults" |
| 23 | + "github.com/NVIDIA/eidos/pkg/errors" |
| 24 | + "github.com/NVIDIA/eidos/pkg/recipe" |
| 25 | + "github.com/NVIDIA/eidos/pkg/validator/checks" |
| 26 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 27 | + "k8s.io/client-go/kubernetes" |
| 28 | +) |
| 29 | + |
| 30 | +func init() { |
| 31 | + // Register this check |
| 32 | + checks.RegisterCheck(&checks.Check{ |
| 33 | + Name: "expected-resources", |
| 34 | + Description: "Verify expected Kubernetes resources exist and are healthy after component deployment", |
| 35 | + Phase: "deployment", |
| 36 | + Func: validateExpectedResources, |
| 37 | + TestName: "TestCheckExpectedResources", |
| 38 | + }) |
| 39 | +} |
| 40 | + |
| 41 | +// validateExpectedResources verifies that all expected Kubernetes resources declared |
| 42 | +// in the recipe's componentRefs exist and are healthy in the live cluster. |
| 43 | +func validateExpectedResources(ctx *checks.ValidationContext) error { |
| 44 | + if ctx.Clientset == nil { |
| 45 | + return errors.New(errors.ErrCodeInvalidRequest, "kubernetes client is not available") |
| 46 | + } |
| 47 | + if ctx.Recipe == nil { |
| 48 | + return errors.New(errors.ErrCodeInvalidRequest, "recipe is not available") |
| 49 | + } |
| 50 | + |
| 51 | + var failures []string |
| 52 | + |
| 53 | + for _, ref := range ctx.Recipe.ComponentRefs { |
| 54 | + for _, er := range ref.ExpectedResources { |
| 55 | + if err := verifyResource(ctx.Context, ctx.Clientset, er); err != nil { |
| 56 | + failures = append(failures, fmt.Sprintf("%s %s/%s (%s): %s", |
| 57 | + er.Kind, er.Namespace, er.Name, ref.Name, err.Error())) |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + if len(failures) > 0 { |
| 63 | + return errors.New(errors.ErrCodeNotFound, |
| 64 | + fmt.Sprintf("expected resource check failed:\n %s", strings.Join(failures, "\n "))) |
| 65 | + } |
| 66 | + return nil |
| 67 | +} |
| 68 | + |
| 69 | +// verifyResource checks that a single expected resource exists and is healthy. |
| 70 | +func verifyResource(ctx context.Context, clientset kubernetes.Interface, er recipe.ExpectedResource) error { |
| 71 | + ctx, cancel := context.WithTimeout(ctx, defaults.ResourceVerificationTimeout) |
| 72 | + defer cancel() |
| 73 | + |
| 74 | + switch er.Kind { |
| 75 | + case "Deployment": |
| 76 | + deploy, err := clientset.AppsV1().Deployments(er.Namespace).Get(ctx, er.Name, metav1.GetOptions{}) |
| 77 | + if err != nil { |
| 78 | + return errors.Wrap(errors.ErrCodeNotFound, "not found", err) |
| 79 | + } |
| 80 | + expected := int32(1) |
| 81 | + if deploy.Spec.Replicas != nil { |
| 82 | + expected = *deploy.Spec.Replicas |
| 83 | + } |
| 84 | + if deploy.Status.AvailableReplicas < expected { |
| 85 | + return errors.New(errors.ErrCodeInternal, |
| 86 | + fmt.Sprintf("not healthy: %d/%d replicas available", |
| 87 | + deploy.Status.AvailableReplicas, expected)) |
| 88 | + } |
| 89 | + |
| 90 | + case "DaemonSet": |
| 91 | + ds, err := clientset.AppsV1().DaemonSets(er.Namespace).Get(ctx, er.Name, metav1.GetOptions{}) |
| 92 | + if err != nil { |
| 93 | + return errors.Wrap(errors.ErrCodeNotFound, "not found", err) |
| 94 | + } |
| 95 | + if ds.Status.NumberReady < ds.Status.DesiredNumberScheduled { |
| 96 | + return errors.New(errors.ErrCodeInternal, |
| 97 | + fmt.Sprintf("not healthy: %d/%d pods ready", |
| 98 | + ds.Status.NumberReady, ds.Status.DesiredNumberScheduled)) |
| 99 | + } |
| 100 | + |
| 101 | + case "StatefulSet": |
| 102 | + ss, err := clientset.AppsV1().StatefulSets(er.Namespace).Get(ctx, er.Name, metav1.GetOptions{}) |
| 103 | + if err != nil { |
| 104 | + return errors.Wrap(errors.ErrCodeNotFound, "not found", err) |
| 105 | + } |
| 106 | + expected := int32(1) |
| 107 | + if ss.Spec.Replicas != nil { |
| 108 | + expected = *ss.Spec.Replicas |
| 109 | + } |
| 110 | + if ss.Status.ReadyReplicas < expected { |
| 111 | + return errors.New(errors.ErrCodeInternal, |
| 112 | + fmt.Sprintf("not healthy: %d/%d replicas ready", |
| 113 | + ss.Status.ReadyReplicas, expected)) |
| 114 | + } |
| 115 | + |
| 116 | + case "Service": |
| 117 | + _, err := clientset.CoreV1().Services(er.Namespace).Get(ctx, er.Name, metav1.GetOptions{}) |
| 118 | + if err != nil { |
| 119 | + return errors.Wrap(errors.ErrCodeNotFound, "not found", err) |
| 120 | + } |
| 121 | + |
| 122 | + case "ConfigMap": |
| 123 | + _, err := clientset.CoreV1().ConfigMaps(er.Namespace).Get(ctx, er.Name, metav1.GetOptions{}) |
| 124 | + if err != nil { |
| 125 | + return errors.Wrap(errors.ErrCodeNotFound, "not found", err) |
| 126 | + } |
| 127 | + |
| 128 | + case "Secret": |
| 129 | + _, err := clientset.CoreV1().Secrets(er.Namespace).Get(ctx, er.Name, metav1.GetOptions{}) |
| 130 | + if err != nil { |
| 131 | + return errors.Wrap(errors.ErrCodeNotFound, "not found", err) |
| 132 | + } |
| 133 | + |
| 134 | + default: |
| 135 | + return errors.New(errors.ErrCodeInvalidRequest, |
| 136 | + fmt.Sprintf("unsupported resource kind %q", er.Kind)) |
| 137 | + } |
| 138 | + |
| 139 | + return nil |
| 140 | +} |
0 commit comments