-
Notifications
You must be signed in to change notification settings - Fork 88
/
deploy.go
89 lines (77 loc) · 2.5 KB
/
deploy.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
package handlers
import (
"encoding/json"
"net/http"
"github.com/pkg/errors"
"github.com/replicatedhq/kots/pkg/kotsutil"
"github.com/replicatedhq/kots/pkg/logger"
registrytypes "github.com/replicatedhq/kots/pkg/registry/types"
"github.com/replicatedhq/kots/pkg/upgradeservice/deploy"
)
type DeployRequest struct {
IsSkipPreflights bool `json:"isSkipPreflights"`
ContinueWithFailedPreflights bool `json:"continueWithFailedPreflights"`
}
type DeployResponse struct {
Success bool `json:"success"`
Error string `json:"error,omitempty"`
}
func (h *Handler) Deploy(w http.ResponseWriter, r *http.Request) {
response := DeployResponse{
Success: false,
}
params := GetContextParams(r)
request := DeployRequest{}
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
response.Error = "failed to decode request body"
logger.Error(errors.Wrap(err, response.Error))
JSON(w, http.StatusBadRequest, response)
return
}
kotsKinds, err := kotsutil.LoadKotsKinds(params.AppArchive)
if err != nil {
response.Error = "failed to load kots kinds from path"
logger.Error(errors.Wrap(err, response.Error))
JSON(w, http.StatusInternalServerError, response)
return
}
registrySettings := registrytypes.RegistrySettings{
Hostname: params.RegistryEndpoint,
Username: params.RegistryUsername,
Password: params.RegistryPassword,
Namespace: params.RegistryNamespace,
IsReadOnly: params.RegistryIsReadOnly,
}
canDeploy, reason, err := deploy.CanDeploy(deploy.CanDeployOptions{
Params: params,
KotsKinds: kotsKinds,
RegistrySettings: registrySettings,
})
if err != nil {
response.Error = "failed to check if app can be deployed"
logger.Error(errors.Wrap(err, response.Error))
JSON(w, http.StatusInternalServerError, response)
return
}
if !canDeploy {
response.Error = reason
logger.Error(errors.New(response.Error))
JSON(w, http.StatusBadRequest, response)
return
}
if err := deploy.Deploy(deploy.DeployOptions{
Ctx: r.Context(),
IsSkipPreflights: request.IsSkipPreflights,
ContinueWithFailedPreflights: request.ContinueWithFailedPreflights,
Params: params,
KotsKinds: kotsKinds,
RegistrySettings: registrySettings,
}); err != nil {
response.Error = "failed to deploy app"
logger.Error(errors.Wrap(err, response.Error))
JSON(w, http.StatusInternalServerError, response)
return
}
response.Success = true
JSON(w, http.StatusOK, response)
}