forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactions_monitoring.go
135 lines (113 loc) · 4.64 KB
/
actions_monitoring.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
package cluster
import (
"encoding/json"
"io/ioutil"
"net/http"
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
"github.com/rancher/rancher/pkg/monitoring"
v3 "github.com/rancher/types/apis/management.cattle.io/v3"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func (a ActionHandler) viewMonitoring(actionName string, action *types.Action, apiContext *types.APIContext) error {
cluster, err := a.ClusterClient.Get(apiContext.ID, v1.GetOptions{})
if err != nil {
return httperror.WrapAPIError(err, httperror.NotFound, "none existent Cluster")
}
if cluster.DeletionTimestamp != nil {
return httperror.NewAPIError(httperror.InvalidType, "deleting Cluster")
}
if !cluster.Spec.EnableClusterMonitoring {
return httperror.NewAPIError(httperror.InvalidState, "disabling Monitoring")
}
// need to support `map[string]string` as entry value type in norman Builder.convertMap
answers, err := convert.EncodeToMap(monitoring.GetOverwroteAppAnswers(cluster.Annotations))
if err != nil {
return httperror.WrapAPIError(err, httperror.ServerError, "failed to parse response")
}
apiContext.WriteResponse(http.StatusOK, map[string]interface{}{
"answers": answers,
"type": "monitoringOutput",
})
return nil
}
func (a ActionHandler) editMonitoring(actionName string, action *types.Action, apiContext *types.APIContext) error {
cluster, err := a.ClusterClient.Get(apiContext.ID, v1.GetOptions{})
if err != nil {
return httperror.WrapAPIError(err, httperror.NotFound, "none existent Cluster")
}
if cluster.DeletionTimestamp != nil {
return httperror.NewAPIError(httperror.InvalidType, "deleting Cluster")
}
if !cluster.Spec.EnableClusterMonitoring {
return httperror.NewAPIError(httperror.InvalidState, "disabling Monitoring")
}
data, err := ioutil.ReadAll(apiContext.Request.Body)
if err != nil {
return httperror.WrapAPIError(err, httperror.InvalidBodyContent, "unable to read request content")
}
var input v3.MonitoringInput
if err = json.Unmarshal(data, &input); err != nil {
return httperror.WrapAPIError(err, httperror.InvalidBodyContent, "failed to parse request content")
}
cluster = cluster.DeepCopy()
cluster.Annotations = monitoring.AppendAppOverwritingAnswers(cluster.Annotations, string(data))
_, err = a.ClusterClient.Update(cluster)
if err != nil {
return httperror.WrapAPIError(err, httperror.ServerError, "failed to upgrade monitoring")
}
apiContext.WriteResponse(http.StatusNoContent, map[string]interface{}{})
return nil
}
func (a ActionHandler) enableMonitoring(actionName string, action *types.Action, apiContext *types.APIContext) error {
cluster, err := a.ClusterClient.Get(apiContext.ID, v1.GetOptions{})
if err != nil {
return httperror.WrapAPIError(err, httperror.NotFound, "none existent Cluster")
}
if cluster.DeletionTimestamp != nil {
return httperror.NewAPIError(httperror.InvalidType, "deleting Cluster")
}
if cluster.Spec.EnableClusterMonitoring {
apiContext.WriteResponse(http.StatusNoContent, map[string]interface{}{})
return nil
}
data, err := ioutil.ReadAll(apiContext.Request.Body)
if err != nil {
return httperror.WrapAPIError(err, httperror.InvalidBodyContent, "unable to read request content")
}
var input v3.MonitoringInput
if err = json.Unmarshal(data, &input); err != nil {
return httperror.WrapAPIError(err, httperror.InvalidBodyContent, "failed to parse request content")
}
cluster = cluster.DeepCopy()
cluster.Spec.EnableClusterMonitoring = true
cluster.Annotations = monitoring.AppendAppOverwritingAnswers(cluster.Annotations, string(data))
_, err = a.ClusterClient.Update(cluster)
if err != nil {
return httperror.WrapAPIError(err, httperror.ServerError, "failed to enable monitoring")
}
apiContext.WriteResponse(http.StatusNoContent, map[string]interface{}{})
return nil
}
func (a ActionHandler) disableMonitoring(actionName string, action *types.Action, apiContext *types.APIContext) error {
cluster, err := a.ClusterClient.Get(apiContext.ID, v1.GetOptions{})
if err != nil {
return httperror.WrapAPIError(err, httperror.NotFound, "none existent Cluster")
}
if cluster.DeletionTimestamp != nil {
return httperror.NewAPIError(httperror.InvalidType, "deleting Cluster")
}
if !cluster.Spec.EnableClusterMonitoring {
apiContext.WriteResponse(http.StatusNoContent, map[string]interface{}{})
return nil
}
cluster = cluster.DeepCopy()
cluster.Spec.EnableClusterMonitoring = false
_, err = a.ClusterClient.Update(cluster)
if err != nil {
return httperror.WrapAPIError(err, httperror.ServerError, "failed to disable monitoring")
}
apiContext.WriteResponse(http.StatusNoContent, map[string]interface{}{})
return nil
}