From 1ed7167175fcc63a3578e7b794ee2b8e102728ec Mon Sep 17 00:00:00 2001 From: Dhiraj Bokde Date: Sat, 6 Apr 2024 11:38:17 -0700 Subject: [PATCH 1/3] fix: delete istio resources when istio is disabled, fixes RHOAIENG-5158 --- .../controller/modelregistry_controller.go | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/internal/controller/modelregistry_controller.go b/internal/controller/modelregistry_controller.go index a35a295..52bf6c9 100644 --- a/internal/controller/modelregistry_controller.go +++ b/internal/controller/modelregistry_controller.go @@ -338,6 +338,14 @@ func (r *ModelRegistryReconciler) updateRegistryResources(ctx context.Context, p if result2 != ResourceUnchanged { result = result2 } + } else { + result2, err = r.deleteIstioConfig(ctx, params) + if err != nil { + return result2, err + } + if result2 != ResourceUnchanged { + result = result2 + } } return result, nil @@ -460,6 +468,39 @@ func (r *ModelRegistryReconciler) createOrUpdateIstioConfig(ctx context.Context, return result, nil } +func (r *ModelRegistryReconciler) deleteIstioConfig(ctx context.Context, params *ModelRegistryParams) (OperationResult, error) { + var err error + + objectMeta := metav1.ObjectMeta{Name: params.Name, Namespace: params.Namespace} + virtualService := networking.VirtualService{ObjectMeta: objectMeta} + if err = r.Client.Delete(ctx, &virtualService); client.IgnoreNotFound(err) != nil { + return ResourceUpdated, err + } + + destinationRule := networking.DestinationRule{ObjectMeta: objectMeta} + if err = r.Client.Delete(ctx, &destinationRule); client.IgnoreNotFound(err) != nil { + return ResourceUpdated, err + } + + authorizationPolicy := security.AuthorizationPolicy{ObjectMeta: objectMeta} + authorizationPolicy.Name = authorizationPolicy.Name + "-authorino" + if err = r.Client.Delete(ctx, &authorizationPolicy); client.IgnoreNotFound(err) != nil { + return ResourceUpdated, err + } + + authConfig := authorino.AuthConfig{ObjectMeta: objectMeta} + if err = r.Client.Delete(ctx, &authConfig); client.IgnoreNotFound(err) != nil { + return ResourceUpdated, err + } + + gateway := networking.Gateway{ObjectMeta: objectMeta} + if err = r.Client.Delete(ctx, &gateway); client.IgnoreNotFound(err) != nil { + return ResourceUpdated, err + } + + return ResourceUnchanged, nil +} + func (r *ModelRegistryReconciler) createOrUpdateGateway(ctx context.Context, params *ModelRegistryParams, registry *modelregistryv1alpha1.ModelRegistry, templateName string) (result OperationResult, err error) { result = ResourceUnchanged From 6f31fa8eceffd3481cbf52c203ac6497730acef1 Mon Sep 17 00:00:00 2001 From: Dhiraj Bokde Date: Sat, 6 Apr 2024 11:55:14 -0700 Subject: [PATCH 2/3] fix: delete gateway if missing in istio config --- .../controller/modelregistry_controller.go | 30 ++++++++++++++----- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/internal/controller/modelregistry_controller.go b/internal/controller/modelregistry_controller.go index 52bf6c9..661b26a 100644 --- a/internal/controller/modelregistry_controller.go +++ b/internal/controller/modelregistry_controller.go @@ -457,12 +457,19 @@ func (r *ModelRegistryReconciler) createOrUpdateIstioConfig(ctx context.Context, result = result2 } - result2, err = r.createOrUpdateGateway(ctx, params, registry, "gateway.yaml.tmpl") - if err != nil { - return result2, err - } - if result2 != ResourceUnchanged { - result = result2 + if params.Spec.Istio.Gateway != nil { + result2, err = r.createOrUpdateGateway(ctx, params, registry, "gateway.yaml.tmpl") + if err != nil { + return result2, err + } + if result2 != ResourceUnchanged { + result = result2 + } + } else { + // remove gateway if it exists + if err = r.deleteGatewayConfig(ctx, params); err != nil { + return ResourceUpdated, err + } } return result, nil @@ -493,14 +500,21 @@ func (r *ModelRegistryReconciler) deleteIstioConfig(ctx context.Context, params return ResourceUpdated, err } - gateway := networking.Gateway{ObjectMeta: objectMeta} - if err = r.Client.Delete(ctx, &gateway); client.IgnoreNotFound(err) != nil { + if err = r.deleteGatewayConfig(ctx, params); err != nil { return ResourceUpdated, err } return ResourceUnchanged, nil } +func (r *ModelRegistryReconciler) deleteGatewayConfig(ctx context.Context, params *ModelRegistryParams) error { + gateway := networking.Gateway{ObjectMeta: metav1.ObjectMeta{Name: params.Name, Namespace: params.Namespace}} + if err := r.Client.Delete(ctx, &gateway); client.IgnoreNotFound(err) != nil { + return err + } + return nil +} + func (r *ModelRegistryReconciler) createOrUpdateGateway(ctx context.Context, params *ModelRegistryParams, registry *modelregistryv1alpha1.ModelRegistry, templateName string) (result OperationResult, err error) { result = ResourceUnchanged From c246f0ec6417e3b0906082fc7943096d23c5ed37 Mon Sep 17 00:00:00 2001 From: Dhiraj Bokde Date: Sat, 6 Apr 2024 11:58:49 -0700 Subject: [PATCH 3/3] fix: perform istio update/delete only when istio is present --- .../controller/modelregistry_controller.go | 32 ++++++++++--------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/internal/controller/modelregistry_controller.go b/internal/controller/modelregistry_controller.go index 661b26a..7158518 100644 --- a/internal/controller/modelregistry_controller.go +++ b/internal/controller/modelregistry_controller.go @@ -330,21 +330,23 @@ func (r *ModelRegistryReconciler) updateRegistryResources(ctx context.Context, p } } - if registry.Spec.Istio != nil { - result2, err = r.createOrUpdateIstioConfig(ctx, params, registry) - if err != nil { - return result2, err - } - if result2 != ResourceUnchanged { - result = result2 - } - } else { - result2, err = r.deleteIstioConfig(ctx, params) - if err != nil { - return result2, err - } - if result2 != ResourceUnchanged { - result = result2 + if r.HasIstio { + if registry.Spec.Istio != nil { + result2, err = r.createOrUpdateIstioConfig(ctx, params, registry) + if err != nil { + return result2, err + } + if result2 != ResourceUnchanged { + result = result2 + } + } else { + result2, err = r.deleteIstioConfig(ctx, params) + if err != nil { + return result2, err + } + if result2 != ResourceUnchanged { + result = result2 + } } }