-
Notifications
You must be signed in to change notification settings - Fork 88
/
dashboard.go
132 lines (110 loc) · 3.92 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
//
// Copyright (c) 2019-2021 Red Hat, Inc.
// This program and the accompanying materials are made
// available under the terms of the Eclipse Public License 2.0
// which is available at https://www.eclipse.org/legal/epl-2.0/
//
// SPDX-License-Identifier: EPL-2.0
//
// Contributors:
// Red Hat, Inc. - initial API and implementation
//
package dashboard
import (
"fmt"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"github.com/eclipse-che/che-operator/pkg/common/chetypes"
defaults "github.com/eclipse-che/che-operator/pkg/common/operator-defaults"
"github.com/eclipse-che/che-operator/pkg/deploy"
"github.com/eclipse-che/che-operator/pkg/deploy/expose"
"github.com/eclipse-che/che-operator/pkg/deploy/gateway"
"github.com/sirupsen/logrus"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/types"
)
const (
exposePath = "/dashboard/"
)
var (
log = ctrl.Log.WithName("dashboard")
)
type DashboardReconciler struct {
deploy.Reconcilable
}
func NewDashboardReconciler() *DashboardReconciler {
return &DashboardReconciler{}
}
func (d *DashboardReconciler) getComponentName(ctx *chetypes.DeployContext) string {
return defaults.GetCheFlavor() + "-dashboard"
}
func (d *DashboardReconciler) Reconcile(ctx *chetypes.DeployContext) (reconcile.Result, bool, error) {
// Create a new dashboard service
done, err := deploy.SyncServiceToCluster(ctx, d.getComponentName(ctx), []string{"http"}, []int32{8080}, d.getComponentName(ctx))
if !done {
return reconcile.Result{}, false, err
}
// Expose dashboard service with route or ingress
_, done, err = expose.ExposeWithHostPath(ctx, d.getComponentName(ctx), ctx.CheHost,
exposePath,
d.createGatewayConfig(ctx),
)
if !done {
return reconcile.Result{}, false, err
}
// we create dashboard SA in any case to keep a track on resources we access withing it
done, err = deploy.SyncServiceAccountToCluster(ctx, DashboardSA)
if !done {
return reconcile.Result{}, false, err
}
done, err = deploy.SyncClusterRoleToCluster(ctx, d.getClusterRoleName(ctx), GetPrivilegedPoliciesRulesForKubernetes())
if !done {
return reconcile.Result{}, false, err
}
done, err = deploy.SyncClusterRoleBindingToCluster(ctx, d.getClusterRoleBindingName(ctx), DashboardSA, d.getClusterRoleName(ctx))
if !done {
return reconcile.Result{}, false, err
}
err = deploy.AppendFinalizer(ctx, ClusterPermissionsDashboardFinalizer)
if err != nil {
return reconcile.Result{}, false, err
}
// Deploy dashboard
spec, err := d.getDashboardDeploymentSpec(ctx)
if err != nil {
return reconcile.Result{}, false, err
}
done, err = deploy.SyncDeploymentSpecToCluster(ctx, spec, deploy.DefaultDeploymentDiffOpts)
if !done {
return reconcile.Result{}, false, err
}
return reconcile.Result{}, true, nil
}
func (d *DashboardReconciler) Finalize(ctx *chetypes.DeployContext) bool {
done := true
if _, err := deploy.Delete(ctx, types.NamespacedName{Name: d.getClusterRoleName(ctx)}, &rbacv1.ClusterRole{}); err != nil {
done = false
logrus.Errorf("Failed to delete ClusterRole %s, cause: %v", d.getClusterRoleName(ctx), err)
}
if _, err := deploy.Delete(ctx, types.NamespacedName{Name: d.getClusterRoleBindingName(ctx)}, &rbacv1.ClusterRoleBinding{}); err != nil {
done = false
logrus.Errorf("Failed to delete ClusterRoleBinding %s, cause: %v", d.getClusterRoleBindingName(ctx), err)
}
if err := deploy.DeleteFinalizer(ctx, ClusterPermissionsDashboardFinalizer); err != nil {
done = false
logrus.Errorf("Error deleting finalizer: %v", err)
}
return done
}
func (d *DashboardReconciler) createGatewayConfig(ctx *chetypes.DeployContext) *gateway.TraefikConfig {
cfg := gateway.CreateCommonTraefikConfig(
d.getComponentName(ctx),
fmt.Sprintf("Path(`/`, `/f`) || PathPrefix(`%s`)", exposePath),
10,
"http://"+d.getComponentName(ctx)+":8080",
[]string{})
if ctx.CheCluster.IsAccessTokenConfigured() {
cfg.AddAuthHeaderRewrite(d.getComponentName(ctx))
}
return cfg
}