Skip to content

Commit 7163d72

Browse files
committed
Add check for DestinationRule CRD
1 parent 97a5a5d commit 7163d72

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

controllers/destination_rule.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
trustyaiopendatahubiov1alpha1 "github.com/trustyai-explainability/trustyai-service-operator/api/v1alpha1"
99
templateParser "github.com/trustyai-explainability/trustyai-service-operator/controllers/templates"
10+
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
1011
"k8s.io/apimachinery/pkg/api/errors"
1112
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1213
"k8s.io/apimachinery/pkg/types"
@@ -16,6 +17,7 @@ import (
1617

1718
const (
1819
destinationRuleTemplatePath = "service/destination-rule.tmpl.yaml"
20+
destinationRuleCDRName = "destinationrules.networking.istio.io"
1921
)
2022

2123
// DestinationRuleConfig has the variables for the DestinationRule template
@@ -25,7 +27,25 @@ type DestinationRuleConfig struct {
2527
DestinationRuleName string
2628
}
2729

30+
// isDestinationRuleCRDPresent returns true if the DestinationRule CRD is present, false otherwise
31+
func (r *TrustyAIServiceReconciler) isDestinationRuleCRDPresent(ctx context.Context) (bool, error) {
32+
crd := &apiextensionsv1.CustomResourceDefinition{}
33+
34+
err := r.Get(ctx, types.NamespacedName{Name: destinationRuleCDRName}, crd)
35+
if err != nil {
36+
if !errors.IsNotFound(err) {
37+
return false, fmt.Errorf("error getting "+destinationRuleCDRName+" CRD: %v", err)
38+
}
39+
// Not found
40+
return false, nil
41+
}
42+
43+
// Found
44+
return true, nil
45+
}
46+
2847
func (r *TrustyAIServiceReconciler) ensureDestinationRule(ctx context.Context, instance *trustyaiopendatahubiov1alpha1.TrustyAIService) error {
48+
2949
destinationRuleName := instance.Name + "-internal"
3050

3151
existingDestinationRule := &unstructured.Unstructured{}

controllers/inference_services.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,10 +275,26 @@ func (r *TrustyAIServiceReconciler) patchKServe(ctx context.Context, instance *t
275275
// Only if the Istio sidecar annotation is set
276276
annotations := infService.GetAnnotations()
277277
if inject, exists := annotations["sidecar.istio.io/inject"]; exists && inject == "true" {
278-
err := r.ensureDestinationRule(ctx, instance)
278+
279+
// Check if DestinationRule CRD is present. If there's an error, don't proceed and return the error
280+
exists, err := r.isDestinationRuleCRDPresent(ctx)
279281
if err != nil {
280-
return fmt.Errorf("failed to ensure DestinationRule: %v", err)
282+
log.FromContext(ctx).Error(err, "Error verifying DestinationRule CRD is present")
283+
return err
284+
}
285+
286+
// Try to create the DestinationRule, since CRD exists
287+
if exists {
288+
err := r.ensureDestinationRule(ctx, instance)
289+
if err != nil {
290+
return fmt.Errorf("failed to ensure DestinationRule: %v", err)
291+
}
292+
} else {
293+
// DestinationRule CRD does not exist. Do not attempt to create it and log error
294+
err := fmt.Errorf("the DestinationRule CRD is not present in this cluster")
295+
log.FromContext(ctx).Error(err, "InferenceService has service mesh annotation but DestinationRule CRD not found")
281296
}
297+
282298
}
283299

284300
// Update the InferenceService

0 commit comments

Comments
 (0)