forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatter.go
87 lines (75 loc) · 2.83 KB
/
formatter.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
package cluster
import (
"strings"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
"github.com/rancher/norman/types/values"
v3 "github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/sirupsen/logrus"
)
type Formatter struct {
KontainerDriverLister v3.KontainerDriverLister
}
func (f *Formatter) Formatter(request *types.APIContext, resource *types.RawResource) {
if convert.ToBool(resource.Values["internal"]) {
delete(resource.Links, "remove")
}
shellLink := request.URLBuilder.Link("shell", resource)
shellLink = strings.Replace(shellLink, "http", "ws", 1)
shellLink = strings.Replace(shellLink, "/shell", "?shell=true", 1)
resource.Links["shell"] = shellLink
resource.AddAction(request, "generateKubeconfig")
resource.AddAction(request, "importYaml")
resource.AddAction(request, "exportYaml")
if _, ok := resource.Values["rancherKubernetesEngineConfig"]; ok {
resource.AddAction(request, "rotateCertificates")
if enabled, ok := values.GetValue(resource.Values, "rancherKubernetesEngineConfig", "services", "etcd", "backupConfig", "enabled"); ok {
if enabled.(bool) {
resource.AddAction(request, "backupEtcd")
resource.AddAction(request, "restoreFromEtcdBackup")
}
}
}
if err := request.AccessControl.CanDo(v3.ClusterGroupVersionKind.Group, v3.ClusterResource.Name, "update", request, resource.Values, request.Schema); err == nil {
if convert.ToBool(resource.Values["enableClusterMonitoring"]) {
resource.AddAction(request, "disableMonitoring")
resource.AddAction(request, "editMonitoring")
} else {
resource.AddAction(request, "enableMonitoring")
}
}
if convert.ToBool(resource.Values["enableClusterMonitoring"]) {
resource.AddAction(request, "viewMonitoring")
}
if gkeConfig, ok := resource.Values["googleKubernetesEngineConfig"]; ok {
configMap, ok := gkeConfig.(map[string]interface{})
if !ok {
logrus.Errorf("could not convert gke config to map")
return
}
setTrueIfNil(configMap, "enableStackdriverLogging")
setTrueIfNil(configMap, "enableStackdriverMonitoring")
setTrueIfNil(configMap, "enableHorizontalPodAutoscaling")
setTrueIfNil(configMap, "enableHttpLoadBalancing")
setTrueIfNil(configMap, "enableNetworkPolicyConfig")
}
if eksConfig, ok := resource.Values["amazonElasticContainerServiceConfig"]; ok {
configMap, ok := eksConfig.(map[string]interface{})
if !ok {
logrus.Errorf("could not convert eks config to map")
return
}
setTrueIfNil(configMap, "associateWorkerNodePublicIp")
setIntIfNil(configMap, "nodeVolumeSize", 20)
}
}
func setTrueIfNil(configMap map[string]interface{}, fieldName string) {
if configMap[fieldName] == nil {
configMap[fieldName] = true
}
}
func setIntIfNil(configMap map[string]interface{}, fieldName string, replaceVal int) {
if configMap[fieldName] == nil {
configMap[fieldName] = replaceVal
}
}