-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathplugin_handler.go
More file actions
81 lines (65 loc) · 2.36 KB
/
plugin_handler.go
File metadata and controls
81 lines (65 loc) · 2.36 KB
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
package server
import (
"fmt"
"net/http"
"os"
"path/filepath"
jsonpatch "github.com/evanphx/json-patch"
"github.com/sirupsen/logrus"
)
var mlog = logrus.WithField("module", "manifest")
func manifestHandler(cfg *Config) http.HandlerFunc {
baseManifestData, err := os.ReadFile(filepath.Join(cfg.StaticPath, "plugin-manifest.json"))
if err != nil {
mlog.WithError(err).Error("cannot read base manifest file")
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusInternalServerError)
})
}
patchedManifest := patchManifest(baseManifestData, cfg)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
w.Header().Set("Expires", "0")
w.Write(patchedManifest)
})
}
func patchManifest(baseManifestData []byte, cfg *Config) []byte {
if len(cfg.Features) == 0 {
return baseManifestData
}
patchedManifest := baseManifestData
if !cfg.Features[DevConfig] {
// Don't clear the extensions when running in dev mode, so only 1 instance of the monitoring-plugin
// can be run as a development environment
patchedManifest = performPatch(baseManifestData, filepath.Join(cfg.ConfigPath, "clear-extensions.patch.json"))
}
if cfg.Features[Incidents] || cfg.Features[ClusterHealthAnalyzer] {
patchedManifest = performPatch(patchedManifest, filepath.Join(cfg.ConfigPath, "cluster-health-analyzer.patch.json"))
}
for feature := range cfg.Features {
if feature == ClusterHealthAnalyzer || feature == Incidents {
continue
}
patchedManifest = performPatch(patchedManifest, filepath.Join(cfg.ConfigPath, fmt.Sprintf("%s.patch.json", feature)))
}
return []byte(patchedManifest)
}
func performPatch(originalData []byte, patchFilePath string) []byte {
patchData, err := os.ReadFile(patchFilePath)
if err != nil {
mlog.WithField("reason", err).Warnf("cannot read patch file %s", patchFilePath)
return originalData
}
patch, err := jsonpatch.DecodePatch(patchData)
if err != nil {
mlog.WithField("reason", err).Warnf("cannot decode patch data %s", patchData)
return originalData
}
patchedManifest, err := patch.ApplyIndent(originalData, " ")
if err != nil {
mlog.WithError(err).Error("cannot patch base manifest file")
return originalData
}
return patchedManifest
}