forked from grafana/grafana-operator
/
kubeHelper.go
286 lines (235 loc) · 7.26 KB
/
kubeHelper.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
package common
import (
stdErrors "errors"
"fmt"
"github.com/integr8ly/grafana-operator/pkg/apis/integreatly/v1alpha1"
apps "k8s.io/api/apps/v1"
core "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
"sigs.k8s.io/controller-runtime/pkg/client/config"
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
"strings"
"time"
)
var log = logf.Log.WithName("kube_helper")
type KubeHelperImpl struct {
k8client *kubernetes.Clientset
config *ControllerConfig
}
func NewKubeHelper() *KubeHelperImpl {
config := config.GetConfigOrDie()
k8client := kubernetes.NewForConfigOrDie(config)
helper := new(KubeHelperImpl)
helper.k8client = k8client
helper.config = GetControllerConfig()
return helper
}
func (h KubeHelperImpl) getConfigMapKey(namespace, name string) string {
return fmt.Sprintf("%s_%s", namespace, strings.ToLower(name))
}
func (h KubeHelperImpl) getConfigMap(name string) (*v1.ConfigMap, error) {
namespace := h.config.GetConfigString(ConfigOperatorNamespace, "")
return h.k8client.CoreV1().ConfigMaps(namespace).Get(name, metav1.GetOptions{})
}
func (h KubeHelperImpl) updateConfigMap(c *v1.ConfigMap) error {
namespace := h.config.GetConfigString(ConfigOperatorNamespace, "")
_, err := h.k8client.CoreV1().ConfigMaps(namespace).Update(c)
return err
}
func (h KubeHelperImpl) getGrafanaDeployment() (*apps.Deployment, error) {
namespace := h.config.GetConfigString(ConfigOperatorNamespace, "")
return h.k8client.AppsV1().Deployments(namespace).Get(GrafanaDeploymentName, metav1.GetOptions{})
}
func (h KubeHelperImpl) UpdateGrafanaConfig(config string, cr *v1alpha1.Grafana) error {
configMap, err := h.getConfigMap(GrafanaConfigMapName)
if err != nil {
return err
}
configMap.Data[GrafanaConfigFileName] = config
return h.updateConfigMap(configMap)
}
func (h KubeHelperImpl) UpdateDashboard(d *v1alpha1.GrafanaDashboard) (bool, error) {
configMap, err := h.getConfigMap(GrafanaDashboardsConfigMapName)
if err != nil {
if errors.IsNotFound(err) {
return false, nil
}
return false, err
}
// Prefix the dashboard filename with the namespace to allow multiple namespaces
// to import the same dashboard
dashboardName := h.getConfigMapKey(d.Namespace, d.Spec.Name)
if configMap.Data == nil {
configMap.Data = make(map[string]string)
}
configMap.Data[dashboardName] = d.Spec.Json
err = h.updateConfigMap(configMap)
if err != nil {
return false, err
}
return true, nil
}
func (h KubeHelperImpl) isKnown(config, namespace, name string) (bool, error) {
configMap, err := h.getConfigMap(config)
if err != nil {
if errors.IsNotFound(err) {
return false, nil
}
return false, err
}
if configMap.Data == nil {
return false, nil
}
key := h.getConfigMapKey(namespace, name)
_, found := configMap.Data[key]
return found, nil
}
func (h KubeHelperImpl) IsKnown(kind string, o runtime.Object) (bool, error) {
switch kind {
case v1alpha1.GrafanaDashboardKind:
d := o.(*v1alpha1.GrafanaDashboard)
return h.isKnown(GrafanaDashboardsConfigMapName, d.Namespace, d.Spec.Name)
case v1alpha1.GrafanaDataSourceKind:
d := o.(*v1alpha1.GrafanaDataSource)
return h.isKnown(GrafanaDatasourcesConfigMapName, d.Namespace, d.Spec.Name)
default:
return false, stdErrors.New(fmt.Sprintf("unknown kind '%v'", kind))
}
}
func (h KubeHelperImpl) UpdateDataSources(name, namespace, ds string) (bool, error) {
configMap, err := h.getConfigMap(GrafanaDatasourcesConfigMapName)
if err != nil {
if errors.IsNotFound(err) {
return false, nil
}
return false, err
}
// Prefix the data source filename with the namespace to allow multiple namespaces
// to import the same dashboard
key := h.getConfigMapKey(namespace, name)
if configMap.Data == nil {
configMap.Data = make(map[string]string)
}
configMap.Data[key] = ds
err = h.updateConfigMap(configMap)
if err != nil {
return false, err
}
return true, nil
}
func (h KubeHelperImpl) DeleteDataSources(name, namespace string) error {
configMap, err := h.getConfigMap(GrafanaDatasourcesConfigMapName)
if err != nil {
// Grafana may already be uninstalled
if errors.IsNotFound(err) {
return nil
}
return err
}
// Prefix the dashboard filename with the namespace to allow multiple namespaces
// to import the same dashboard
key := h.getConfigMapKey(namespace, name)
if configMap.Data == nil {
return nil
}
if _, ok := configMap.Data[key]; !ok {
// Resource deleted but no such key in the configmap
return nil
}
delete(configMap.Data, key)
err = h.updateConfigMap(configMap)
return err
}
func (h KubeHelperImpl) DeleteDashboard(d *v1alpha1.GrafanaDashboard) error {
configMap, err := h.getConfigMap(GrafanaDashboardsConfigMapName)
if err != nil {
if errors.IsNotFound(err) {
// Grafana may already be uninstalled
return nil
}
return err
}
dashboardName := h.getConfigMapKey(d.Namespace, d.Spec.Name)
if configMap.Data == nil {
return nil
}
if _, ok := configMap.Data[dashboardName]; !ok {
// Resource deleted but no such key in the configmap
return nil
}
delete(configMap.Data, dashboardName)
err = h.updateConfigMap(configMap)
if err != nil {
return err
}
return nil
}
func (h KubeHelperImpl) getGrafanaPod(namespaceName string) (*core.Pod, error) {
podLabel := h.config.GetConfigString(ConfigPodLabelValue, PodLabelDefaultValue)
opts := metav1.ListOptions{
LabelSelector: fmt.Sprintf("app=%s", podLabel),
}
pods, err := h.k8client.CoreV1().Pods(namespaceName).List(opts)
if err != nil {
return nil, err
}
if len(pods.Items) == 0 {
return nil, errors.NewNotFound(struct {
Group string
Resource string
}{Group: "core", Resource: "pod"}, GrafanaDeploymentName)
}
return &pods.Items[0], nil
}
func (h KubeHelperImpl) UpdateGrafanaDeployment(newEnv string) error {
monitoringNamespace := h.config.GetConfigString(ConfigOperatorNamespace, "")
deployment, err := h.getGrafanaDeployment()
if err != nil {
return err
}
updated := false
// find and update the init container env var
for i, container := range deployment.Spec.Template.Spec.InitContainers {
if container.Name == InitContainerName {
for j, env := range deployment.Spec.Template.Spec.InitContainers[i].Env {
if env.Name == PluginsEnvVar {
deployment.Spec.Template.Spec.InitContainers[i].Env[j].Value = newEnv
updated = true
break
}
}
}
}
if updated {
_, err := h.k8client.AppsV1().Deployments(monitoringNamespace).Update(deployment)
return err
}
return nil
}
func (h KubeHelperImpl) RestartGrafana() error {
monitoringNamespace := h.config.GetConfigString(ConfigOperatorNamespace, "")
pod, err := h.getGrafanaPod(monitoringNamespace)
if err != nil {
if errors.IsNotFound(err) {
// No need to restart if grafana has not yet been deployed
return nil
}
return err
}
return h.k8client.CoreV1().Pods(monitoringNamespace).Delete(pod.Name, nil)
}
// Append a status message to the origin dashboard of a plugin
func AppendMessage(message string, dashboard *v1alpha1.GrafanaDashboard) {
if dashboard == nil {
return
}
status := v1alpha1.GrafanaDashboardStatusMessage{
Message: message,
Timestamp: time.Now().Format(time.RFC850),
}
dashboard.Status.Messages = append(dashboard.Status.Messages, status)
}