-
Notifications
You must be signed in to change notification settings - Fork 113
/
modelmeshserving.go
176 lines (158 loc) · 6.16 KB
/
modelmeshserving.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Package modelmeshserving provides utility functions to config MoModelMesh, a general-purpose model serving management/routing layer
package modelmeshserving
import (
"context"
"fmt"
"path/filepath"
"strings"
operatorv1 "github.com/openshift/api/operator/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
dsciv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/dscinitialization/v1"
"github.com/opendatahub-io/opendatahub-operator/v2/components"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/cluster"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/deploy"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/monitoring"
)
var (
ComponentName = "model-mesh"
Path = deploy.DefaultManifestPath + "/" + ComponentName + "/overlays/odh"
DependentComponentName = "odh-model-controller"
DependentPath = deploy.DefaultManifestPath + "/" + DependentComponentName + "/base"
)
// Verifies that Dashboard implements ComponentInterface.
var _ components.ComponentInterface = (*ModelMeshServing)(nil)
// ModelMeshServing struct holds the configuration for the ModelMeshServing component.
// +kubebuilder:object:generate=true
type ModelMeshServing struct {
components.Component `json:""`
}
func (m *ModelMeshServing) OverrideManifests(_ string) error {
// Go through each manifest and set the overlays if defined
for _, subcomponent := range m.DevFlags.Manifests {
if strings.Contains(subcomponent.URI, DependentComponentName) {
// Download subcomponent
if err := deploy.DownloadManifests(DependentComponentName, subcomponent); err != nil {
return err
}
// If overlay is defined, update paths
defaultKustomizePath := "base"
if subcomponent.SourcePath != "" {
defaultKustomizePath = subcomponent.SourcePath
}
DependentPath = filepath.Join(deploy.DefaultManifestPath, DependentComponentName, defaultKustomizePath)
}
if strings.Contains(subcomponent.URI, ComponentName) {
// Download subcomponent
if err := deploy.DownloadManifests(ComponentName, subcomponent); err != nil {
return err
}
// If overlay is defined, update paths
defaultKustomizePath := "overlays/odh"
if subcomponent.SourcePath != "" {
defaultKustomizePath = subcomponent.SourcePath
}
Path = filepath.Join(deploy.DefaultManifestPath, ComponentName, defaultKustomizePath)
}
}
return nil
}
func (m *ModelMeshServing) GetComponentName() string {
return ComponentName
}
func (m *ModelMeshServing) ReconcileComponent(ctx context.Context,
cli client.Client,
resConf *rest.Config,
owner metav1.Object,
dscispec *dsciv1.DSCInitializationSpec,
_ bool,
) error {
var imageParamMap = map[string]string{
"odh-mm-rest-proxy": "RELATED_IMAGE_ODH_MM_REST_PROXY_IMAGE",
"odh-modelmesh-runtime-adapter": "RELATED_IMAGE_ODH_MODELMESH_RUNTIME_ADAPTER_IMAGE",
"odh-modelmesh": "RELATED_IMAGE_ODH_MODELMESH_IMAGE",
"odh-modelmesh-controller": "RELATED_IMAGE_ODH_MODELMESH_CONTROLLER_IMAGE",
"odh-model-controller": "RELATED_IMAGE_ODH_MODEL_CONTROLLER_IMAGE",
}
// odh-model-controller to use
var dependentImageParamMap = map[string]string{
"odh-model-controller": "RELATED_IMAGE_ODH_MODEL_CONTROLLER_IMAGE",
}
enabled := m.GetManagementState() == operatorv1.Managed
monitoringEnabled := dscispec.Monitoring.ManagementState == operatorv1.Managed
platform, err := deploy.GetPlatform(cli)
if err != nil {
return err
}
// Update Default rolebinding
if enabled {
if m.DevFlags != nil {
// Download manifests and update paths
if err = m.OverrideManifests(string(platform)); err != nil {
return err
}
}
if err := cluster.UpdatePodSecurityRolebinding(cli, dscispec.ApplicationsNamespace,
"modelmesh",
"modelmesh-controller",
"odh-prometheus-operator",
"prometheus-custom"); err != nil {
return err
}
// Update image parameters
if (dscispec.DevFlags == nil || dscispec.DevFlags.ManifestsUri == "") && (m.DevFlags == nil || len(m.DevFlags.Manifests) == 0) {
if err := deploy.ApplyParams(Path, m.SetImageParamsMap(imageParamMap), false); err != nil {
return err
}
}
}
err = deploy.DeployManifestsFromPath(cli, owner, Path, dscispec.ApplicationsNamespace, ComponentName, enabled)
if err != nil {
return err
}
// For odh-model-controller
if enabled {
if err := cluster.UpdatePodSecurityRolebinding(cli, dscispec.ApplicationsNamespace,
"odh-model-controller"); err != nil {
return err
}
// Update image parameters for odh-model-controller
if dscispec.DevFlags == nil || dscispec.DevFlags.ManifestsUri == "" {
if err := deploy.ApplyParams(DependentPath, m.SetImageParamsMap(dependentImageParamMap), false); err != nil {
return err
}
}
}
if err := deploy.DeployManifestsFromPath(cli, owner, DependentPath, dscispec.ApplicationsNamespace, m.GetComponentName(), enabled); err != nil {
// explicitly ignore error if error contains keywords "spec.selector" and "field is immutable" and return all other error.
if !strings.Contains(err.Error(), "spec.selector") || !strings.Contains(err.Error(), "field is immutable") {
return err
}
}
// CloudService Monitoring handling
if platform == deploy.ManagedRhods {
if enabled {
// first check if service is up, so prometheus won't fire alerts when it is just startup
if err := monitoring.WaitForDeploymentAvailable(ctx, resConf, ComponentName, dscispec.ApplicationsNamespace, 20, 2); err != nil {
return fmt.Errorf("deployment for %s is not ready to server: %w", ComponentName, err)
}
fmt.Printf("deployment for %s is done, updating monitoring rules\n", ComponentName)
}
// first model-mesh rules
if err := m.UpdatePrometheusConfig(cli, enabled && monitoringEnabled, ComponentName); err != nil {
return err
}
// then odh-model-controller rules
if err := m.UpdatePrometheusConfig(cli, enabled && monitoringEnabled, DependentComponentName); err != nil {
return err
}
if err = deploy.DeployManifestsFromPath(cli, owner,
filepath.Join(deploy.DefaultManifestPath, "monitoring", "prometheus", "apps"),
dscispec.Monitoring.Namespace,
"prometheus", true); err != nil {
return err
}
}
return nil
}