forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
actionhandler.go
93 lines (83 loc) · 3.47 KB
/
actionhandler.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
package cluster
import (
"fmt"
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/types"
"github.com/rancher/rancher/pkg/clustermanager"
v3 "github.com/rancher/types/apis/management.cattle.io/v3"
mgmtclient "github.com/rancher/types/client/management/v3"
"github.com/rancher/types/user"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)
type ActionHandler struct {
NodepoolGetter v3.NodePoolsGetter
ClusterClient v3.ClusterInterface
NodeTemplateGetter v3.NodeTemplatesGetter
UserMgr user.Manager
ClusterManager *clustermanager.Manager
BackupClient v3.EtcdBackupInterface
}
func (a ActionHandler) ClusterActionHandler(actionName string, action *types.Action, apiContext *types.APIContext) error {
canUpdateCluster := func() bool {
cluster := map[string]interface{}{
"id": apiContext.ID,
}
return apiContext.AccessControl.CanDo(v3.ClusterGroupVersionKind.Group, v3.ClusterResource.Name, "update", apiContext, cluster, apiContext.Schema) == nil
}
switch actionName {
case v3.ClusterActionGenerateKubeconfig:
return a.GenerateKubeconfigActionHandler(actionName, action, apiContext)
case v3.ClusterActionImportYaml:
return a.ImportYamlHandler(actionName, action, apiContext)
case v3.ClusterActionExportYaml:
return a.ExportYamlHandler(actionName, action, apiContext)
case v3.ClusterActionViewMonitoring:
return a.viewMonitoring(actionName, action, apiContext)
case v3.ClusterActionEditMonitoring:
if !canUpdateCluster() {
return httperror.NewAPIError(httperror.Unauthorized, "can not access")
}
return a.editMonitoring(actionName, action, apiContext)
case v3.ClusterActionEnableMonitoring:
if !canUpdateCluster() {
return httperror.NewAPIError(httperror.Unauthorized, "can not access")
}
return a.enableMonitoring(actionName, action, apiContext)
case v3.ClusterActionDisableMonitoring:
if !canUpdateCluster() {
return httperror.NewAPIError(httperror.Unauthorized, "can not access")
}
return a.disableMonitoring(actionName, action, apiContext)
case v3.ClusterActionBackupEtcd:
if !canUpdateCluster() {
return httperror.NewAPIError(httperror.Unauthorized, "can not backup etcd")
}
return a.BackupEtcdHandler(actionName, action, apiContext)
case v3.ClusterActionRestoreFromEtcdBackup:
if !canUpdateCluster() {
return httperror.NewAPIError(httperror.Unauthorized, "can not restore etcd backup")
}
return a.RestoreFromEtcdBackupHandler(actionName, action, apiContext)
case v3.ClusterActionRotateCertificates:
if !canUpdateCluster() {
return httperror.NewAPIError(httperror.Unauthorized, "can not rotate certificates")
}
return a.RotateCertificates(actionName, action, apiContext)
}
return httperror.NewAPIError(httperror.NotFound, "not found")
}
func (a ActionHandler) getClusterToken(clusterID string, apiContext *types.APIContext) (string, error) {
userName := a.UserMgr.GetUser(apiContext)
return a.UserMgr.EnsureClusterToken(clusterID, fmt.Sprintf("kubeconfig-%s.%s", userName, clusterID), "Kubeconfig token", userName)
}
func (a ActionHandler) getToken(apiContext *types.APIContext) (string, error) {
userName := a.UserMgr.GetUser(apiContext)
return a.UserMgr.EnsureToken("kubeconfig-"+userName, "Kubeconfig token", userName)
}
func (a ActionHandler) getKubeConfig(apiContext *types.APIContext, cluster *mgmtclient.Cluster) (*clientcmdapi.Config, error) {
token, err := a.getToken(apiContext)
if err != nil {
return nil, err
}
return a.ClusterManager.KubeConfig(cluster.ID, token), nil
}