-
Notifications
You must be signed in to change notification settings - Fork 120
/
dashboard.go
326 lines (294 loc) · 13 KB
/
dashboard.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// Package dashboard provides utility functions to config Open Data Hub Dashboard: A web dashboard that displays
// installed Open Data Hub components with easy access to component UIs and documentation
// +groupName=datasciencecluster.opendatahub.io
package dashboard
import (
"context"
"fmt"
"path/filepath"
"strings"
"github.com/go-logr/logr"
operatorv1 "github.com/openshift/api/operator/v1"
routev1 "github.com/openshift/api/route/v1"
v1 "k8s.io/api/core/v1"
apierrs "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"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/common"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/deploy"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/monitoring"
)
var (
ComponentName = "dashboard"
Path = deploy.DefaultManifestPath + "/" + ComponentName + "/base" // ODH
PathISV = deploy.DefaultManifestPath + "/" + ComponentName + "/apps" // ODH APPS
PathModelServing = deploy.DefaultManifestPath + "/" + ComponentName + "/modelserving" // ODH modelserving
PathCRDs = deploy.DefaultManifestPath + "/" + ComponentName + "/crd" // ODH + RHOAI
PathConsoleLink = deploy.DefaultManifestPath + "/" + ComponentName + "/consolelink" // ODH consolelink
ComponentNameSupported = "rhods-dashboard"
PathSupported = deploy.DefaultManifestPath + "/" + ComponentName + "/overlays/rhoai" // RHOAI
PathSupportedModelServing = deploy.DefaultManifestPath + "/" + ComponentName + "/overlays/modelserving" // RHOAI modelserving
PathISVSM = deploy.DefaultManifestPath + "/" + ComponentName + "/overlays/apps/apps-onprem" // RHOAI APPS
PathISVAddOn = deploy.DefaultManifestPath + "/" + ComponentName + "/overlays/apps/apps-addon" // RHOAI APPS
PathConsoleLinkSupported = deploy.DefaultManifestPath + "/" + ComponentName + "/overlays/consolelink" // RHOAI
PathODHDashboardConfig = deploy.DefaultManifestPath + "/" + ComponentName + "/overlays/odhdashboardconfig" // RHOAI odhdashboardconfig
NameConsoleLink = "console"
NamespaceConsoleLink = "openshift-console"
)
// Verifies that Dashboard implements ComponentInterface.
var _ components.ComponentInterface = (*Dashboard)(nil)
// Dashboard struct holds the configuration for the Dashboard component.
// +kubebuilder:object:generate=true
type Dashboard struct {
components.Component `json:""`
}
func (d *Dashboard) OverrideManifests(platform string) error {
// If devflags are set, update default manifests path
if len(d.DevFlags.Manifests) != 0 {
manifestConfig := d.DevFlags.Manifests[0]
if err := deploy.DownloadManifests(ComponentName, manifestConfig); err != nil {
return err
}
// If overlay is defined, update paths
if platform == string(deploy.ManagedRhods) || platform == string(deploy.SelfManagedRhods) {
defaultKustomizePath := "overlays/rhoai"
if manifestConfig.SourcePath != "" {
defaultKustomizePath = manifestConfig.SourcePath
}
PathSupported = filepath.Join(deploy.DefaultManifestPath, ComponentName, defaultKustomizePath)
} else {
defaultKustomizePath := "base"
if manifestConfig.SourcePath != "" {
defaultKustomizePath = manifestConfig.SourcePath
}
Path = filepath.Join(deploy.DefaultManifestPath, ComponentName, defaultKustomizePath)
}
}
return nil
}
func (d *Dashboard) GetComponentName() string {
return ComponentName
}
//nolint:gocyclo
func (d *Dashboard) ReconcileComponent(ctx context.Context,
cli client.Client,
logger logr.Logger,
owner metav1.Object,
dscispec *dsciv1.DSCInitializationSpec,
currentComponentExist bool,
) error {
var l logr.Logger
platform, err := deploy.GetPlatform(cli)
if err != nil {
return err
}
if platform == deploy.SelfManagedRhods || platform == deploy.ManagedRhods {
l = d.ConfigComponentLogger(logger, ComponentNameSupported, dscispec)
} else {
l = d.ConfigComponentLogger(logger, ComponentName, dscispec)
}
var imageParamMap = map[string]string{
"odh-dashboard-image": "RELATED_IMAGE_ODH_DASHBOARD_IMAGE",
}
enabled := d.GetManagementState() == operatorv1.Managed
monitoringEnabled := dscispec.Monitoring.ManagementState == operatorv1.Managed
// Update Default rolebinding
if enabled {
// Update Default rolebinding
// cleanup OAuth client related secret and CR if dashboard is in 'installed false' status
if err := d.cleanOauthClient(cli, dscispec, currentComponentExist, l); err != nil {
return err
}
if d.DevFlags != nil {
// Download manifests and update paths
if err := d.OverrideManifests(string(platform)); err != nil {
return err
}
}
// 1. Deploy CRDs
if err := d.deployCRDsForPlatform(cli, owner, dscispec.ApplicationsNamespace, platform); err != nil {
return fmt.Errorf("failed to deploy Dashboard CRD: %w", err)
}
// 2. platform specific RBAC
if platform == deploy.OpenDataHub || platform == "" {
err := cluster.UpdatePodSecurityRolebinding(cli, dscispec.ApplicationsNamespace, "odh-dashboard")
if err != nil {
return err
}
}
if platform == deploy.SelfManagedRhods || platform == deploy.ManagedRhods {
err := cluster.UpdatePodSecurityRolebinding(cli, dscispec.ApplicationsNamespace, "rhods-dashboard")
if err != nil {
return err
}
}
// 3. Update image parameters
if (dscispec.DevFlags == nil || dscispec.DevFlags.ManifestsUri == "") && (d.DevFlags == nil || len(d.DevFlags.Manifests) == 0) {
if err := deploy.ApplyParams(PathSupported, imageParamMap, false); err != nil {
return fmt.Errorf("failed to update image from %s : %w", PathSupported, err)
}
}
}
// common: Deploy odh-dashboard manifests
// TODO: check if we can have the same component name odh-dashboard for both, or still keep rhods-dashboard for RHOAI
switch platform {
case deploy.SelfManagedRhods, deploy.ManagedRhods:
// anaconda
if err := cluster.CreateSecret(cli, "anaconda-ce-access", dscispec.ApplicationsNamespace); err != nil {
return fmt.Errorf("failed to create access-secret for anaconda: %w", err)
}
// overlay which including ../../base + anaconda-ce-validator
if err := deploy.DeployManifestsFromPath(cli, owner, PathSupported, dscispec.ApplicationsNamespace, ComponentNameSupported, enabled); err != nil {
return fmt.Errorf("failed to apply manifests from %s: %w", PathSupported, err)
}
// modelserving
if err := deploy.DeployManifestsFromPath(cli, owner, PathSupportedModelServing, dscispec.ApplicationsNamespace, ComponentNameSupported, enabled); err != nil {
return fmt.Errorf("failed to set dashboard modelserving from %s: %w", PathSupportedModelServing, err)
}
// Apply RHOAI specific configs, e.g anaconda screct and cronjob and ISV
if err := d.applyRHOAISpecificConfigs(cli, owner, dscispec.ApplicationsNamespace, platform); err != nil {
return err
}
// consolelink
if err := d.deployConsoleLink(cli, owner, platform, dscispec.ApplicationsNamespace, ComponentNameSupported); err != nil {
return err
}
l.Info("apply manifests done")
// CloudService Monitoring handling
if platform == deploy.ManagedRhods {
if enabled {
// first check if the service is up, so prometheus won't fire alerts when it is just startup
if err := monitoring.WaitForDeploymentAvailable(ctx, cli, ComponentNameSupported, dscispec.ApplicationsNamespace, 20, 3); err != nil {
return fmt.Errorf("deployment for %s is not ready to server: %w", ComponentName, err)
}
l.Info("deployment is done, updating monitoring rules")
}
if err := d.UpdatePrometheusConfig(cli, enabled && monitoringEnabled, ComponentNameSupported); 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
}
l.Info("updating SRE monitoring done")
}
return nil
default:
// base
if err = deploy.DeployManifestsFromPath(cli, owner, Path, dscispec.ApplicationsNamespace, ComponentName, enabled); err != nil {
return err
}
// ISV
if err = deploy.DeployManifestsFromPath(cli, owner, PathISV, dscispec.ApplicationsNamespace, ComponentName, enabled); err != nil {
return err
}
// modelserving
if err := deploy.DeployManifestsFromPath(cli, owner, PathModelServing, dscispec.ApplicationsNamespace, ComponentName, enabled); err != nil {
return fmt.Errorf("failed to set dashboard modelserving from %s: %w", PathModelServing, err)
}
// consolelink
if err := d.deployConsoleLink(cli, owner, platform, dscispec.ApplicationsNamespace, ComponentName); err != nil {
return err
}
l.Info("apply manifests done")
return nil
}
}
func (d *Dashboard) deployCRDsForPlatform(cli client.Client, owner metav1.Object, namespace string, platform deploy.Platform) error {
componentName := ComponentName
if platform == deploy.SelfManagedRhods || platform == deploy.ManagedRhods {
componentName = ComponentNameSupported
}
// we only deploy CRD, we do not remove CRD
return deploy.DeployManifestsFromPath(cli, owner, PathCRDs, namespace, componentName, true)
}
func (d *Dashboard) applyRHOAISpecificConfigs(cli client.Client, owner metav1.Object, namespace string, platform deploy.Platform) error {
enabled := d.ManagementState == operatorv1.Managed
// set proper group name
dashboardConfig := filepath.Join(PathODHDashboardConfig, "odhdashboardconfig.yaml")
adminGroups := map[deploy.Platform]string{
deploy.SelfManagedRhods: "rhods-admins",
deploy.ManagedRhods: "dedicated-admins",
}[platform]
if err := common.ReplaceStringsInFile(dashboardConfig, map[string]string{"<admin_groups>": adminGroups}); err != nil {
return err
}
if err := deploy.DeployManifestsFromPath(cli, owner, PathODHDashboardConfig, namespace, ComponentNameSupported, enabled); err != nil {
return fmt.Errorf("failed to create OdhDashboardConfig from %s: %w", PathODHDashboardConfig, err)
}
// ISV
path := PathISVSM
if platform == deploy.ManagedRhods {
path = PathISVAddOn
}
if err := deploy.DeployManifestsFromPath(cli, owner, path, namespace, ComponentNameSupported, enabled); err != nil {
return fmt.Errorf("failed to set dashboard ISV from %s : %w", Path, err)
}
return nil
}
func (d *Dashboard) deployConsoleLink(cli client.Client, owner metav1.Object, platform deploy.Platform, namespace, componentName string) error {
var manifestsPath, sectionTitle, routeName string
switch platform {
case deploy.SelfManagedRhods:
sectionTitle = "OpenShift Self Managed Services"
manifestsPath = PathConsoleLinkSupported
routeName = componentName
case deploy.ManagedRhods:
sectionTitle = "OpenShift Managed Services"
manifestsPath = PathConsoleLinkSupported
routeName = componentName
default:
sectionTitle = "OpenShift Open Data Hub"
manifestsPath = PathConsoleLink
routeName = "odh-dashboard"
}
pathConsoleLink := filepath.Join(manifestsPath, "consolelink.yaml")
consoleRoute := &routev1.Route{}
if err := cli.Get(context.TODO(), client.ObjectKey{Name: NameConsoleLink, Namespace: NamespaceConsoleLink}, consoleRoute); err != nil {
return fmt.Errorf("error getting console route URL %s : %w", NameConsoleLink, err)
}
domainIndex := strings.Index(consoleRoute.Spec.Host, ".")
consoleLinkDomain := consoleRoute.Spec.Host[domainIndex+1:]
if err := common.ReplaceStringsInFile(pathConsoleLink, map[string]string{
"<dashboard-url>": "https://" + routeName + "-" + namespace + "." + consoleLinkDomain,
"<section-title>": sectionTitle,
}); err != nil {
return fmt.Errorf("error replacing with correct dashboard URL for consolelink : %w", err)
}
enabled := d.ManagementState == operatorv1.Managed
if err := deploy.DeployManifestsFromPath(cli, owner, PathConsoleLink, namespace, componentName, enabled); err != nil {
return fmt.Errorf("failed to set dashboard consolelink %s : %w", pathConsoleLink, err)
}
return nil
}
func (d *Dashboard) cleanOauthClient(cli client.Client, dscispec *dsciv1.DSCInitializationSpec, currentComponentExist bool, l logr.Logger) error {
// Remove previous oauth-client secrets
// Check if component is going from state of `Not Installed --> Installed`
// Assumption: Component is currently set to enabled
name := "dashboard-oauth-client"
if !currentComponentExist {
fmt.Println("Cleanup any left secret")
// Delete client secrets from previous installation
oauthClientSecret := &v1.Secret{}
err := cli.Get(context.TODO(), client.ObjectKey{
Namespace: dscispec.ApplicationsNamespace,
Name: name,
}, oauthClientSecret)
if err != nil {
if !apierrs.IsNotFound(err) {
return fmt.Errorf("error getting secret %s: %w", name, err)
}
} else {
if err := cli.Delete(context.TODO(), oauthClientSecret); err != nil {
return fmt.Errorf("error deleting secret %s: %w", name, err)
}
l.Info("successfully deleted secret", "secret", name)
}
}
return nil
}