This repository has been archived by the owner on Mar 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 70
/
routes_navcycle.go
221 lines (189 loc) · 6.48 KB
/
routes_navcycle.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
package daemon
import (
"context"
"fmt"
"net/http"
"sync"
"github.com/gin-gonic/gin"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/pkg/errors"
"github.com/replicatedhq/ship/pkg/api"
"github.com/replicatedhq/ship/pkg/filetree"
"github.com/replicatedhq/ship/pkg/lifecycle"
"github.com/replicatedhq/ship/pkg/lifecycle/daemon/daemontypes"
"github.com/replicatedhq/ship/pkg/lifecycle/render/config/resolve"
"github.com/replicatedhq/ship/pkg/lifecycle/render/planner"
"github.com/replicatedhq/ship/pkg/patch"
"github.com/replicatedhq/ship/pkg/state"
"github.com/replicatedhq/ship/pkg/templates"
"github.com/spf13/afero"
)
// NavcycleRoutes provide workflow execution with standard browser navigation
type NavcycleRoutes struct {
Logger log.Logger
TreeLoader filetree.Loader
StateManager state.Manager
StepExecutor V2Executor
Fs afero.Afero
Shutdown chan interface{}
StepProgress *daemontypes.ProgressMap
Messenger lifecycle.Messenger
HelmIntro lifecycle.HelmIntro
HelmValues lifecycle.HelmValues
Kustomizer lifecycle.Kustomizer
KustomizeIntro lifecycle.KustomizeIntro
Renderer lifecycle.Renderer
Terraformer lifecycle.Terraformer
Planner planner.Planner
BuilderBuilder *templates.BuilderBuilder
Patcher patch.Patcher
ConfigRenderer *resolve.APIConfigRenderer
KubectlApply lifecycle.KubectlApply
ConfigSaved chan interface{}
TerraformConfirmed chan bool
CurrentConfig map[string]interface{}
PreExecuteFuncMap map[string]preExecuteFunc
KubectlConfirmed chan bool
// This isn't known at injection time, so we have to set in Register
Release *api.Release
// Prevent data races when registering preexecute functions
mut sync.Mutex
// Prevent a race when registering completed steps and starting the next step
completeMut sync.Mutex
}
// Register registers routes
func (d *NavcycleRoutes) Register(group *gin.RouterGroup, release *api.Release) {
d.Release = release
v1 := group.Group("/api/v1")
v1.GET("/metadata", d.getMetadata(release))
v1.GET("/navcycle", d.getNavcycle)
v1.GET("/navcycle/step/:step", d.getStep)
v1.POST("/navcycle/step/:step", d.completeStep)
v1.POST("/shutdown", d.shutdown)
kustom := v1.Group("/kustomize")
kustom.POST("file", d.kustomizeGetFile)
kustom.POST("save", d.kustomizeSaveOverlay)
kustom.POST("finalize", d.kustomizeFinalize)
kustom.POST("patch", d.createOrMergePatch)
kustom.POST("include", d.includeBase)
kustom.DELETE("patch", d.deletePatch)
kustom.DELETE("resource", d.deleteResource)
kustom.DELETE("base", d.deleteBase)
kustom.POST("apply", d.applyPatch)
conf := v1.Group("/config")
conf.POST("live", d.postAppConfigLive(release))
conf.PUT("", d.putAppConfig(release))
conf.PUT("finalize", d.finalizeAppConfig(release))
terr := v1.Group("/terraform")
terr.POST("apply", d.terraformApply)
terr.POST("skip", d.terraformApply)
kube := v1.Group("/kubectl")
kube.POST("confirm", d.kubectlConfirm)
d.registerPreExecuteFuncs()
}
type preExecuteFunc func(context.Context, api.Step) error
func (d *NavcycleRoutes) registerPreExecuteFuncs() {
d.mut.Lock()
preExecuteFuncMap := make(map[string]preExecuteFunc)
for _, step := range d.Release.Spec.Lifecycle.V1 {
if step.Kustomize != nil {
preExecuteFuncMap[step.Shared().ID] = d.Kustomizer.PreExecute
}
}
d.PreExecuteFuncMap = preExecuteFuncMap
d.mut.Unlock()
}
func (d *NavcycleRoutes) shutdown(c *gin.Context) {
debug := level.Debug(log.With(d.Logger, "method", "shutdown"))
debug.Log("event", "shutdownFromUI")
d.Shutdown <- nil
c.JSON(http.StatusOK, map[string]interface{}{
"status": "shutdown",
})
}
// returns false if aborted
func (d *NavcycleRoutes) maybeAbortDueToMissingRequirement(requires []string, c *gin.Context, requestedStepID string) (ok bool) {
required, err := d.getRequiredButIncompleteStepFor(requires, false)
if err != nil {
c.AbortWithError(500, errors.Wrapf(err, "check requirements for step %s", requestedStepID))
return false
}
if required != "" {
// if there is a required step that has not been completed, wait for any currently running steps to complete before checking again
required, err = d.getRequiredButIncompleteStepFor(requires, true)
if err != nil {
c.AbortWithError(500, errors.Wrapf(err, "check requirements for step %s", requestedStepID))
return false
}
if required != "" {
d.errRequired(required, c)
return false
}
}
return true
}
// this will return an incomplete step that is present in the list of required steps.
// if there are multiple required but incomplete steps, this will return the first one,
// although from a UI perspective the order is probably not strictly defined
func (d *NavcycleRoutes) getRequiredButIncompleteStepFor(requires []string, withLock bool) (string, error) {
debug := level.Debug(log.With(d.Logger, "method", "getRequiredButIncompleteStepFor"))
if len(requires) == 0 {
return "", nil
}
if withLock {
debug.Log("event", "completeMut.lock")
d.completeMut.Lock()
defer d.completeMut.Unlock()
}
stepsCompleted := map[string]interface{}{}
currentState, err := d.StateManager.CachedState()
if err != nil {
return "", errors.Wrap(err, "load state")
}
if currentState.Versioned().V1 != nil &&
currentState.Versioned().V1.Lifecycle != nil &&
currentState.Versioned().V1.Lifecycle.StepsCompleted != nil {
stepsCompleted = currentState.Versioned().V1.Lifecycle.StepsCompleted
debug.Log("event", "steps.notEmpty", "completed", fmt.Sprintf("%v", stepsCompleted))
}
for _, requiredStep := range requires {
if _, ok := stepsCompleted[requiredStep]; ok {
continue
}
debug.Log("event", "requiredStep.incomplete", "completed", stepsCompleted, "required", requiredStep)
return requiredStep, nil
}
return "", nil
}
func (d *NavcycleRoutes) hydrateAndSend(step api.Step, c *gin.Context) {
step, err := d.buildStepContents(step)
if err != nil {
c.AbortWithError(500, err)
return
}
result, err := d.hydrateStep(daemontypes.NewStep(step))
if err != nil {
c.AbortWithError(500, err)
return
}
c.JSON(200, result)
}
func (d *NavcycleRoutes) errRequired(required string, c *gin.Context) {
c.JSON(400, map[string]interface{}{
"currentStep": map[string]interface{}{
"requirementNotMet": map[string]interface{}{
"required": required,
},
},
"phase": "requirementNotMet",
})
}
func (d *NavcycleRoutes) errNotFound(c *gin.Context) {
c.JSON(404, map[string]interface{}{
"currentStep": map[string]interface{}{
"notFound": map[string]interface{}{},
},
"phase": "notFound",
})
}