From 7163d723351718fcdd666acc3c4a707f1f7fcfb1 Mon Sep 17 00:00:00 2001 From: Rui Vieira Date: Thu, 10 Oct 2024 16:02:17 +0100 Subject: [PATCH] Add check for DestinationRule CRD --- controllers/destination_rule.go | 20 ++++++++++++++++++++ controllers/inference_services.go | 20 ++++++++++++++++++-- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/controllers/destination_rule.go b/controllers/destination_rule.go index f683373e..ce17552f 100644 --- a/controllers/destination_rule.go +++ b/controllers/destination_rule.go @@ -7,6 +7,7 @@ import ( trustyaiopendatahubiov1alpha1 "github.com/trustyai-explainability/trustyai-service-operator/api/v1alpha1" templateParser "github.com/trustyai-explainability/trustyai-service-operator/controllers/templates" + apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/types" @@ -16,6 +17,7 @@ import ( const ( destinationRuleTemplatePath = "service/destination-rule.tmpl.yaml" + destinationRuleCDRName = "destinationrules.networking.istio.io" ) // DestinationRuleConfig has the variables for the DestinationRule template @@ -25,7 +27,25 @@ type DestinationRuleConfig struct { DestinationRuleName string } +// isDestinationRuleCRDPresent returns true if the DestinationRule CRD is present, false otherwise +func (r *TrustyAIServiceReconciler) isDestinationRuleCRDPresent(ctx context.Context) (bool, error) { + crd := &apiextensionsv1.CustomResourceDefinition{} + + err := r.Get(ctx, types.NamespacedName{Name: destinationRuleCDRName}, crd) + if err != nil { + if !errors.IsNotFound(err) { + return false, fmt.Errorf("error getting "+destinationRuleCDRName+" CRD: %v", err) + } + // Not found + return false, nil + } + + // Found + return true, nil +} + func (r *TrustyAIServiceReconciler) ensureDestinationRule(ctx context.Context, instance *trustyaiopendatahubiov1alpha1.TrustyAIService) error { + destinationRuleName := instance.Name + "-internal" existingDestinationRule := &unstructured.Unstructured{} diff --git a/controllers/inference_services.go b/controllers/inference_services.go index a4cad7be..ee8745bf 100644 --- a/controllers/inference_services.go +++ b/controllers/inference_services.go @@ -275,10 +275,26 @@ func (r *TrustyAIServiceReconciler) patchKServe(ctx context.Context, instance *t // Only if the Istio sidecar annotation is set annotations := infService.GetAnnotations() if inject, exists := annotations["sidecar.istio.io/inject"]; exists && inject == "true" { - err := r.ensureDestinationRule(ctx, instance) + + // Check if DestinationRule CRD is present. If there's an error, don't proceed and return the error + exists, err := r.isDestinationRuleCRDPresent(ctx) if err != nil { - return fmt.Errorf("failed to ensure DestinationRule: %v", err) + log.FromContext(ctx).Error(err, "Error verifying DestinationRule CRD is present") + return err + } + + // Try to create the DestinationRule, since CRD exists + if exists { + err := r.ensureDestinationRule(ctx, instance) + if err != nil { + return fmt.Errorf("failed to ensure DestinationRule: %v", err) + } + } else { + // DestinationRule CRD does not exist. Do not attempt to create it and log error + err := fmt.Errorf("the DestinationRule CRD is not present in this cluster") + log.FromContext(ctx).Error(err, "InferenceService has service mesh annotation but DestinationRule CRD not found") } + } // Update the InferenceService