forked from kubemod/kubemod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webapp_handlers.go
134 lines (102 loc) · 3.19 KB
/
webapp_handlers.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
package app
import (
"net/http"
"encoding/json"
"github.com/gin-gonic/gin"
"github.com/kubemod/kubemod/api/v1beta1"
"github.com/kubemod/kubemod/core"
"github.com/kubemod/kubemod/util"
"sigs.k8s.io/yaml"
)
// DryRunRequest represents a /v1/dryrun request payload.
type DryRunRequest struct {
ResourceManifest interface{} `json:"resourceManifest" binding:"required"`
ModRules []*v1beta1.ModRule `json:"modRules" binding:"required"`
}
// DryRunResponse represents the resonse of a successful /v1/dryrun
type DryRunResponse struct {
Patch interface{} `json:"patch"`
Diff string `json:"diff"`
Rejections []string `json:"rejections"`
}
const (
dryRunNamespace string = "dryrun-namespace"
)
// Set up the web app HTTP routes.
func (app *KubeModWebApp) setupRoutes(router *gin.Engine) {
v1 := router.Group("/v1")
v1.PUT("/dryrun", app.dryRunHandler)
}
// Dry-runs a set of ModRules against a resource.
func (app *KubeModWebApp) dryRunHandler(c *gin.Context) {
var payload DryRunRequest
var err error
if err = c.ShouldBind(&payload); err != nil {
app.reportBadRequest(c, err)
return
}
app.log.V(1).Info("processing request", "route", c.Request.URL, "payload", payload)
// Instantiate a ModRuleStore for this request and populate it with the modrules.
store := core.NewModRuleStore(app.modRuleStoreItemFactory, app.clusterModRulesNamespace, app.log)
for _, modRule := range payload.ModRules {
// Populate the modrule with its default values if missing.
modRule.Default()
// Fix the namespace of each modrule - we want to try them as if they all were created in the same namespace.
modRule.Namespace = dryRunNamespace
// Validate the modrule
if err = modRule.ValidateCreate(); err != nil {
app.reportBadRequest(c, err)
return
}
// Add the modrule to the store.
if err := store.Put(modRule); err != nil {
app.reportBadRequest(c, err)
return
}
}
originalJSON, err := json.Marshal(payload.ResourceManifest)
if err != nil {
app.reportBadRequest(c, err)
return
}
// First run the patch operations.
patched, patch, err := store.CalculatePatch(dryRunNamespace, originalJSON, app.log)
if err != nil {
app.reportBadRequest(c, err)
return
}
// Try rejections against the after-patch manifest.
rejections := store.DetermineRejections(dryRunNamespace, patched, app.log)
// If there is a valid patch, calculate the diff in unified diff format.
var diff string
if len(patch) > 0 {
originalYAML, err := yaml.JSONToYAML(originalJSON)
if err != nil {
app.reportBadRequest(c, err)
return
}
patchedYAML, err := yaml.Marshal(patched)
if err != nil {
app.reportBadRequest(c, err)
return
}
diffBytes, err := util.Diff(originalYAML, patchedYAML)
if err != nil {
app.reportBadRequest(c, err)
return
}
diff = string(diffBytes)
}
response := DryRunResponse{
Patch: patch,
Diff: diff,
Rejections: rejections,
}
c.JSON(http.StatusOK, response)
}
// Common method to return an HTTP 400 bad request
func (app *KubeModWebApp) reportBadRequest(c *gin.Context, err error) {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
app.log.Error(err, "request failed", "route", c.Request.URL)
return
}