This repository has been archived by the owner on Jan 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
desire_app_handler.go
210 lines (180 loc) · 5.71 KB
/
desire_app_handler.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
package handlers
import (
"encoding/json"
"net/http"
"code.cloudfoundry.org/bbs"
"code.cloudfoundry.org/bbs/models"
"code.cloudfoundry.org/lager"
"code.cloudfoundry.org/nsync/helpers"
"code.cloudfoundry.org/nsync/recipebuilder"
"code.cloudfoundry.org/runtimeschema/cc_messages"
"code.cloudfoundry.org/runtimeschema/metric"
"github.com/cloudfoundry-incubator/routing-info/cfroutes"
"github.com/cloudfoundry-incubator/routing-info/tcp_routes"
)
const (
desiredLRPCounter = metric.Counter("LRPsDesired")
)
type DesireAppHandler struct {
recipeBuilders map[string]recipebuilder.RecipeBuilder
bbsClient bbs.Client
logger lager.Logger
}
func NewDesireAppHandler(logger lager.Logger, bbsClient bbs.Client, builders map[string]recipebuilder.RecipeBuilder) DesireAppHandler {
return DesireAppHandler{
recipeBuilders: builders,
bbsClient: bbsClient,
logger: logger,
}
}
func (h *DesireAppHandler) DesireApp(resp http.ResponseWriter, req *http.Request) {
processGuid := req.FormValue(":process_guid")
logger := h.logger.Session("desire-app", lager.Data{
"process_guid": processGuid,
"method": req.Method,
"request": req.URL.String(),
})
logger.Info("serving")
defer logger.Info("complete")
desiredApp := cc_messages.DesireAppRequestFromCC{}
err := json.NewDecoder(req.Body).Decode(&desiredApp)
if err != nil {
logger.Error("parse-desired-app-request-failed", err)
resp.WriteHeader(http.StatusBadRequest)
return
}
logger.Info("request-from-cc", lager.Data{"routing_info": desiredApp.RoutingInfo})
envNames := []string{}
for _, envVar := range desiredApp.Environment {
envNames = append(envNames, envVar.Name)
}
logger.Debug("environment", lager.Data{"keys": envNames})
if processGuid != desiredApp.ProcessGuid {
logger.Error("process-guid-mismatch", err, lager.Data{"body-process-guid": desiredApp.ProcessGuid})
resp.WriteHeader(http.StatusBadRequest)
return
}
statusCode := http.StatusConflict
for tries := 2; tries > 0 && statusCode == http.StatusConflict; tries-- {
existingLRP, err := h.getDesiredLRP(logger, processGuid)
if err != nil {
statusCode = http.StatusServiceUnavailable
break
}
if existingLRP != nil {
err = h.updateDesiredApp(logger, existingLRP, desiredApp)
} else {
err = h.createDesiredApp(logger, desiredApp)
}
if err != nil {
mErr := models.ConvertError(err)
switch mErr.Type {
case models.Error_ResourceConflict:
fallthrough
case models.Error_ResourceExists:
statusCode = http.StatusConflict
default:
if _, ok := err.(recipebuilder.Error); ok {
statusCode = http.StatusBadRequest
} else {
statusCode = http.StatusServiceUnavailable
}
}
} else {
statusCode = http.StatusAccepted
desiredLRPCounter.Increment()
}
}
resp.WriteHeader(statusCode)
}
func (h *DesireAppHandler) getDesiredLRP(logger lager.Logger, processGuid string) (*models.DesiredLRP, error) {
logger = logger.Session("fetching-desired-lrp")
lrp, err := h.bbsClient.DesiredLRPByProcessGuid(logger, processGuid)
logger.Debug("fetched-desired-lrp")
if err == nil {
logger.Debug("desired-lrp-already-present")
return lrp, nil
}
bbsError := models.ConvertError(err)
if bbsError.Type == models.Error_ResourceNotFound {
logger.Error("desired-lrp-not-found", err)
return nil, nil
}
logger.Error("failed-fetching-desired-lrp", err)
return nil, err
}
func (h *DesireAppHandler) createDesiredApp(
logger lager.Logger,
desireAppMessage cc_messages.DesireAppRequestFromCC,
) error {
var builder recipebuilder.RecipeBuilder = h.recipeBuilders["buildpack"]
if desireAppMessage.DockerImageUrl != "" {
builder = h.recipeBuilders["docker"]
}
desiredLRP, err := builder.Build(&desireAppMessage)
if err != nil {
logger.Error("failed-to-build-recipe", err)
return err
}
logger.Debug("creating-desired-lrp", lager.Data{"routes": sanitizeRoutes(desiredLRP.Routes)})
err = h.bbsClient.DesireLRP(logger, desiredLRP)
if err != nil {
logger.Error("failed-to-create-lrp", err)
return err
}
logger.Debug("created-desired-lrp")
return nil
}
func (h *DesireAppHandler) updateDesiredApp(
logger lager.Logger,
existingLRP *models.DesiredLRP,
desireAppMessage cc_messages.DesireAppRequestFromCC,
) error {
var builder recipebuilder.RecipeBuilder = h.recipeBuilders["buildpack"]
if desireAppMessage.DockerImageUrl != "" {
builder = h.recipeBuilders["docker"]
}
ports, err := builder.ExtractExposedPorts(&desireAppMessage)
if err != nil {
logger.Error("failed to-get-exposed-port", err)
return err
}
updateRoutes, err := helpers.CCRouteInfoToRoutes(desireAppMessage.RoutingInfo, ports)
if err != nil {
logger.Error("failed-to-marshal-routes", err)
return err
}
routes := existingLRP.Routes
if routes == nil {
routes = &models.Routes{}
}
if value, ok := updateRoutes[cfroutes.CF_ROUTER]; ok {
(*routes)[cfroutes.CF_ROUTER] = value
}
if value, ok := updateRoutes[tcp_routes.TCP_ROUTER]; ok {
(*routes)[tcp_routes.TCP_ROUTER] = value
}
instances := int32(desireAppMessage.NumInstances)
updateRequest := &models.DesiredLRPUpdate{
Annotation: &desireAppMessage.ETag,
Instances: &instances,
Routes: routes,
}
logger.Debug("updating-desired-lrp", lager.Data{"routes": sanitizeRoutes(existingLRP.Routes)})
err = h.bbsClient.UpdateDesiredLRP(logger, desireAppMessage.ProcessGuid, updateRequest)
if err != nil {
logger.Error("failed-to-update-lrp", err)
return err
}
logger.Debug("updated-desired-lrp")
return nil
}
func sanitizeRoutes(routes *models.Routes) *models.Routes {
newRoutes := make(models.Routes)
if routes != nil {
cfRoutes := *routes
newRoutes[cfroutes.CF_ROUTER] = cfRoutes[cfroutes.CF_ROUTER]
newRoutes[tcp_routes.TCP_ROUTER] = cfRoutes[tcp_routes.TCP_ROUTER]
}
return &newRoutes
}