|
| 1 | +/* |
| 2 | +Copyright (c) 2024 Seldon Technologies Ltd. |
| 3 | +
|
| 4 | +Use of this software is governed BY |
| 5 | +(1) the license included in the LICENSE file or |
| 6 | +(2) if the license included in the LICENSE file is the Business Source License 1.1, |
| 7 | +the Change License after the Change Date as each is defined in accordance with the LICENSE file. |
| 8 | +*/ |
| 9 | + |
| 10 | +package steps |
| 11 | + |
| 12 | +import ( |
| 13 | + "context" |
| 14 | + "errors" |
| 15 | + "fmt" |
| 16 | + |
| 17 | + "github.com/seldonio/seldon-core/operator/v2/apis/mlops/v1alpha1" |
| 18 | + k8serrors "k8s.io/apimachinery/pkg/api/errors" |
| 19 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 20 | + "k8s.io/apimachinery/pkg/watch" |
| 21 | +) |
| 22 | + |
| 23 | +// deleteModel we have to wait for model to be deleted, as there is a finalizer attached so the scheduler can confirm |
| 24 | +// when model has been unloaded from inference pod, model-gw, dataflow-engine, pipeline-gw and controller will remove |
| 25 | +// finalizer so deletion can complete. |
| 26 | +func (m *Model) deleteModel(ctx context.Context, model string) error { |
| 27 | + modelCR, err := m.k8sClient.MlopsV1alpha1().Models(m.namespace).Get(ctx, model, metav1.GetOptions{}) |
| 28 | + if err != nil { |
| 29 | + if k8serrors.IsNotFound(err) { |
| 30 | + return fmt.Errorf("model %s can't be deleted, does not exist", model) |
| 31 | + } |
| 32 | + return fmt.Errorf("failed to get model %s", model) |
| 33 | + } |
| 34 | + |
| 35 | + if err := m.k8sClient.MlopsV1alpha1().Models(m.namespace).Delete(ctx, model, metav1.DeleteOptions{}); err != nil { |
| 36 | + return fmt.Errorf("failed deleting model: %w", err) |
| 37 | + } |
| 38 | + m.log.Debugf("Delete request for model %s sent", model) |
| 39 | + |
| 40 | + watcher, err := m.k8sClient.MlopsV1alpha1().Models(m.namespace).Watch(ctx, metav1.ListOptions{ |
| 41 | + FieldSelector: fmt.Sprintf("metadata.name=%s", model), |
| 42 | + ResourceVersion: modelCR.ResourceVersion, |
| 43 | + }) |
| 44 | + if err != nil { |
| 45 | + return fmt.Errorf("failed watching model: %w", err) |
| 46 | + } |
| 47 | + defer watcher.Stop() |
| 48 | + |
| 49 | + m.log.Debugf("Waiting for %s model deletion confirmation", model) |
| 50 | + |
| 51 | + for { |
| 52 | + select { |
| 53 | + case <-ctx.Done(): |
| 54 | + return ctx.Err() |
| 55 | + case event, ok := <-watcher.ResultChan(): |
| 56 | + if !ok { |
| 57 | + return errors.New("watcher channel closed") |
| 58 | + } |
| 59 | + if event.Type == watch.Error { |
| 60 | + return fmt.Errorf("watch error: %v", event.Object) |
| 61 | + } |
| 62 | + if event.Type == watch.Deleted { |
| 63 | + return nil |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func (m *Model) waitForModelReady(ctx context.Context, model string) error { |
| 70 | + foundModel, err := m.k8sClient.MlopsV1alpha1().Models(m.namespace).Get(ctx, model, metav1.GetOptions{}) |
| 71 | + if err != nil { |
| 72 | + return fmt.Errorf("failed getting model: %w", err) |
| 73 | + } |
| 74 | + |
| 75 | + if foundModel.Status.IsReady() { |
| 76 | + return nil |
| 77 | + } |
| 78 | + |
| 79 | + watcher, err := m.k8sClient.MlopsV1alpha1().Models(m.namespace).Watch(ctx, metav1.ListOptions{ |
| 80 | + FieldSelector: fmt.Sprintf("metadata.name=%s", model), |
| 81 | + ResourceVersion: foundModel.ResourceVersion, |
| 82 | + Watch: true, |
| 83 | + }) |
| 84 | + if err != nil { |
| 85 | + return fmt.Errorf("failed subscribed to watch model: %w", err) |
| 86 | + } |
| 87 | + defer watcher.Stop() |
| 88 | + |
| 89 | + for { |
| 90 | + select { |
| 91 | + case <-ctx.Done(): |
| 92 | + return ctx.Err() |
| 93 | + case event, ok := <-watcher.ResultChan(): |
| 94 | + if !ok { |
| 95 | + return fmt.Errorf("watch channel closed") |
| 96 | + } |
| 97 | + |
| 98 | + if event.Type == watch.Error { |
| 99 | + return fmt.Errorf("watch error: %v", event.Object) |
| 100 | + } |
| 101 | + |
| 102 | + if event.Type == watch.Added || event.Type == watch.Modified { |
| 103 | + model := event.Object.(*v1alpha1.Model) |
| 104 | + if model.Status.IsReady() { |
| 105 | + return nil |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + if event.Type == watch.Deleted { |
| 110 | + return fmt.Errorf("resource was deleted") |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments