Skip to content

Commit

Permalink
Add check for DestinationRule CRD
Browse files Browse the repository at this point in the history
  • Loading branch information
ruivieira committed Oct 10, 2024
1 parent 97a5a5d commit 7163d72
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
20 changes: 20 additions & 0 deletions controllers/destination_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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{}
Expand Down
20 changes: 18 additions & 2 deletions controllers/inference_services.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7163d72

Please sign in to comment.