Skip to content

Commit 6126c56

Browse files
authored
Misc cleanup (#773)
1 parent bc0162f commit 6126c56

File tree

19 files changed

+194
-285
lines changed

19 files changed

+194
-285
lines changed

cli/cmd/delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func delete(apiName string, keepCache bool) {
5454
if !_flagDeleteForce {
5555
readyReplicas := getReadyReplicasOrNil(apiName)
5656
if readyReplicas != nil && *readyReplicas > 2 {
57-
prompt.YesOrExit(fmt.Sprintf("are you sure you want to delete the %s api (which has %d running replicas)?", apiName, *readyReplicas), "", "")
57+
prompt.YesOrExit(fmt.Sprintf("are you sure you want to delete the %s api (which has %d live replicas)?", apiName, *readyReplicas), "", "")
5858
}
5959
}
6060

cli/cmd/get.go

Lines changed: 21 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -109,17 +109,17 @@ func getAPI(apiName string) (string, error) {
109109

110110
var out string
111111

112-
t := apiTable([]spec.API{*apiRes.API}, []status.Status{*apiRes.Status}, []metrics.Metrics{*apiRes.Metrics})
112+
t := apiTable([]spec.API{apiRes.API}, []status.Status{apiRes.Status}, []metrics.Metrics{apiRes.Metrics})
113113
out += t.MustFormat()
114114

115115
api := apiRes.API
116116

117117
if api.Tracker != nil {
118118
switch api.Tracker.ModelType {
119119
case userconfig.ClassificationModelType:
120-
out += "\n" + classificationMetricsStr(apiRes.Metrics)
120+
out += "\n" + classificationMetricsStr(&apiRes.Metrics)
121121
case userconfig.RegressionModelType:
122-
out += "\n" + regressionMetricsStr(apiRes.Metrics)
122+
out += "\n" + regressionMetricsStr(&apiRes.Metrics)
123123
}
124124
}
125125

@@ -129,7 +129,7 @@ func getAPI(apiName string) (string, error) {
129129
out += fmt.Sprintf("\n%s curl %s?debug=true -X POST -H \"Content-Type: application/json\" -d @sample.json\n", console.Bold("curl:"), apiEndpoint)
130130

131131
if api.Predictor.Type == userconfig.TensorFlowPredictorType || api.Predictor.Type == userconfig.ONNXPredictorType {
132-
out += "\n" + describeModelInput(apiRes.Status, apiEndpoint)
132+
out += "\n" + describeModelInput(&apiRes.Status, apiEndpoint)
133133
}
134134

135135
out += titleStr("configuration") + strings.TrimSpace(api.UserStr())
@@ -190,64 +190,35 @@ func apiTable(apis []spec.API, statuses []status.Status, allMetrics []metrics.Me
190190
}
191191
}
192192

193-
func metricsStrs(metrics *metrics.Metrics) (string, string, string, string) {
194-
inferenceLatency := "-"
195-
code2XX := "-"
196-
code4XX := "-"
197-
code5XX := "-"
198-
199-
if metrics.NetworkStats != nil {
200-
if metrics.NetworkStats.Latency != nil {
201-
if *metrics.NetworkStats.Latency < 1000 {
202-
inferenceLatency = fmt.Sprintf("%.6g ms", *metrics.NetworkStats.Latency)
203-
} else {
204-
inferenceLatency = fmt.Sprintf("%.6g s", (*metrics.NetworkStats.Latency)/1000)
205-
}
206-
}
207-
208-
if metrics.NetworkStats.Code2XX != 0 {
209-
code2XX = s.Int(metrics.NetworkStats.Code2XX)
210-
}
211-
if metrics.NetworkStats.Code4XX != 0 {
212-
code4XX = s.Int(metrics.NetworkStats.Code4XX)
213-
}
214-
if metrics.NetworkStats.Code5XX != 0 {
215-
code5XX = s.Int(metrics.NetworkStats.Code5XX)
216-
}
217-
}
218-
219-
return inferenceLatency, code2XX, code4XX, code5XX
220-
}
221-
222193
func latencyStr(metrics *metrics.Metrics) string {
223-
if metrics.NetworkStats != nil && metrics.NetworkStats.Latency != nil {
224-
if *metrics.NetworkStats.Latency < 1000 {
225-
return fmt.Sprintf("%.6g ms", *metrics.NetworkStats.Latency)
226-
}
227-
return fmt.Sprintf("%.6g s", (*metrics.NetworkStats.Latency)/1000)
194+
if metrics.NetworkStats == nil || metrics.NetworkStats.Latency == nil {
195+
return "-"
196+
}
197+
if *metrics.NetworkStats.Latency < 1000 {
198+
return fmt.Sprintf("%.6g ms", *metrics.NetworkStats.Latency)
228199
}
229-
return "-"
200+
return fmt.Sprintf("%.6g s", (*metrics.NetworkStats.Latency)/1000)
230201
}
231202

232203
func code2XXStr(metrics *metrics.Metrics) string {
233-
if metrics.NetworkStats != nil && metrics.NetworkStats.Code2XX != 0 {
234-
return s.Int(metrics.NetworkStats.Code2XX)
204+
if metrics.NetworkStats == nil || metrics.NetworkStats.Code2XX == 0 {
205+
return "-"
235206
}
236-
return "-"
207+
return s.Int(metrics.NetworkStats.Code2XX)
237208
}
238209

239210
func code4XXStr(metrics *metrics.Metrics) string {
240-
if metrics.NetworkStats != nil && metrics.NetworkStats.Code4XX != 0 {
241-
return s.Int(metrics.NetworkStats.Code4XX)
211+
if metrics.NetworkStats == nil || metrics.NetworkStats.Code4XX == 0 {
212+
return "-"
242213
}
243-
return "-"
214+
return s.Int(metrics.NetworkStats.Code4XX)
244215
}
245216

246217
func code5XXStr(metrics *metrics.Metrics) string {
247-
if metrics.NetworkStats != nil && metrics.NetworkStats.Code5XX != 0 {
248-
return s.Int(metrics.NetworkStats.Code5XX)
218+
if metrics.NetworkStats == nil || metrics.NetworkStats.Code5XX == 0 {
219+
return "-"
249220
}
250-
return "-"
221+
return s.Int(metrics.NetworkStats.Code5XX)
251222
}
252223

253224
func regressionMetricsStr(metrics *metrics.Metrics) string {
@@ -282,12 +253,9 @@ func regressionMetricsStr(metrics *metrics.Metrics) string {
282253
}
283254

284255
func classificationMetricsStr(metrics *metrics.Metrics) string {
285-
classList := make([]string, len(metrics.ClassDistribution))
286-
287-
i := 0
256+
classList := make([]string, 0, len(metrics.ClassDistribution))
288257
for inputName := range metrics.ClassDistribution {
289-
classList[i] = inputName
290-
i++
258+
classList = append(classList, inputName)
291259
}
292260
sort.Strings(classList)
293261

manager/manifests/fluentd.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ spec:
193193
value: $CORTEX_REGION
194194
- name: LOG_GROUP_NAME
195195
value: $CORTEX_LOG_GROUP
196-
- name: K8S_NODE_NAME
196+
- name: K8S_NODE_NAME # used by fluentd to avoid a k8s query
197197
valueFrom:
198198
fieldRef:
199199
fieldPath: spec.nodeName

pkg/lib/cron/cron.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ func Run(f func() error, errHandler func(error), interval time.Duration) Cron {
4545
for {
4646
select {
4747
case <-cronCancel:
48-
// cleanup?
4948
return
5049
case <-cronRun:
5150
runCron()

pkg/operator/endpoints/deploy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func Deploy(w http.ResponseWriter, r *http.Request) {
8989
results := make([]schema.DeployResult, len(apiConfigs))
9090
for i, apiConfig := range apiConfigs {
9191
api, msg, err := operator.UpdateAPI(&apiConfig, projectID, force)
92-
results[i].API = api
92+
results[i].API = *api
9393
results[i].Message = msg
9494
if err != nil {
9595
results[i].Error = errors.Message(err)

pkg/operator/endpoints/get.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ func GetAPI(w http.ResponseWriter, r *http.Request) {
8787
}
8888

8989
respond(w, schema.GetAPIResponse{
90-
API: api,
91-
Status: status,
92-
Metrics: metrics,
90+
API: *api,
91+
Status: *status,
92+
Metrics: *metrics,
9393
BaseURL: baseURL,
9494
})
9595
}

pkg/operator/endpoints/info.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828
func Info(w http.ResponseWriter, r *http.Request) {
2929
response := schema.InfoResponse{
3030
MaskedAWSAccessKeyID: s.MaskString(os.Getenv("AWS_ACCESS_KEY_ID"), 4),
31-
ClusterConfig: config.Cluster,
31+
ClusterConfig: *config.Cluster,
3232
}
3333
respond(w, response)
3434
}

pkg/operator/endpoints/middleware.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,7 @@ func AuthMiddleware(next http.Handler) http.Handler {
7777
if err != nil {
7878
respondError(w, ErrorAuthAPIError())
7979
return
80-
}
81-
82-
if !isValid {
80+
} else if !isValid {
8381
respondErrorCode(w, http.StatusForbidden, ErrorAuthInvalid())
8482
return
8583
}

pkg/operator/operator/api.go

Lines changed: 18 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -49,30 +49,28 @@ func UpdateAPI(apiConfig *userconfig.API, projectID string, force bool) (*spec.A
4949
api := getAPISpec(apiConfig, projectID, deploymentID)
5050

5151
if prevDeployment == nil {
52-
err = config.AWS.UploadMsgpackToS3(api, *config.Cluster.Bucket, api.Key)
53-
if err != nil {
52+
if err := config.AWS.UploadMsgpackToS3(api, *config.Cluster.Bucket, api.Key); err != nil {
5453
return nil, "", errors.Wrap(err, "upload api spec")
5554
}
56-
if err = applyK8sResources(api, prevDeployment, prevService, prevVirtualService); err != nil {
55+
if err := applyK8sResources(api, prevDeployment, prevService, prevVirtualService); err != nil {
5756
go deleteK8sResources(api.Name)
5857
return nil, "", err
5958
}
6059
return api, fmt.Sprintf("creating %s api", api.Name), nil
6160
}
6261

63-
if !areDeploymentsEqual(prevDeployment, deploymentSpec(api, prevDeployment)) {
62+
if !areAPIsEqual(prevDeployment, deploymentSpec(api, prevDeployment)) {
6463
isUpdating, err := isAPIUpdating(prevDeployment)
6564
if err != nil {
6665
return nil, "", err
6766
}
6867
if isUpdating && !force {
6968
return nil, "", errors.New(fmt.Sprintf("%s api is updating (override with --force)", api.Name))
7069
}
71-
err = config.AWS.UploadMsgpackToS3(api, *config.Cluster.Bucket, api.Key)
72-
if err != nil {
70+
if err := config.AWS.UploadMsgpackToS3(api, *config.Cluster.Bucket, api.Key); err != nil {
7371
return nil, "", errors.Wrap(err, "upload api spec")
7472
}
75-
if err = applyK8sResources(api, prevDeployment, prevService, prevVirtualService); err != nil {
73+
if err := applyK8sResources(api, prevDeployment, prevService, prevVirtualService); err != nil {
7674
return nil, "", err
7775
}
7876
return api, fmt.Sprintf("updating %s api", api.Name), nil
@@ -101,6 +99,7 @@ func RefreshAPI(apiName string, force bool) (string, error) {
10199
if err != nil {
102100
return "", err
103101
}
102+
104103
if isUpdating && !force {
105104
return "", errors.New(fmt.Sprintf("%s api is updating (override with --force)", apiName))
106105
}
@@ -117,8 +116,7 @@ func RefreshAPI(apiName string, force bool) (string, error) {
117116

118117
api = getAPISpec(api.API, api.ProjectID, k8s.RandomName())
119118

120-
err = config.AWS.UploadMsgpackToS3(api, *config.Cluster.Bucket, api.Key)
121-
if err != nil {
119+
if err := config.AWS.UploadMsgpackToS3(api, *config.Cluster.Bucket, api.Key); err != nil {
122120
return "", errors.Wrap(err, "upload api spec")
123121
}
124122

@@ -149,11 +147,7 @@ func DeleteAPI(apiName string, keepCache bool) error {
149147
return nil
150148
}
151149

152-
func getAPISpec(
153-
apiConfig *userconfig.API,
154-
projectID string,
155-
deploymentID string,
156-
) *spec.API {
150+
func getAPISpec(apiConfig *userconfig.API, projectID string, deploymentID string) *spec.API {
157151
var buf bytes.Buffer
158152
buf.WriteString(apiConfig.Name)
159153
buf.WriteString(*apiConfig.Endpoint)
@@ -175,13 +169,7 @@ func getAPISpec(
175169
}
176170
}
177171

178-
func getK8sResources(apiConfig *userconfig.API) (
179-
*kapps.Deployment,
180-
*kcore.Service,
181-
*kunstructured.Unstructured,
182-
error,
183-
) {
184-
172+
func getK8sResources(apiConfig *userconfig.API) (*kapps.Deployment, *kcore.Service, *kunstructured.Unstructured, error) {
185173
var deployment *kapps.Deployment
186174
var service *kcore.Service
187175
var virtualService *kunstructured.Unstructured
@@ -207,13 +195,7 @@ func getK8sResources(apiConfig *userconfig.API) (
207195
return deployment, service, virtualService, err
208196
}
209197

210-
func applyK8sResources(
211-
api *spec.API,
212-
prevDeployment *kapps.Deployment,
213-
prevService *kcore.Service,
214-
prevVirtualService *kunstructured.Unstructured,
215-
) error {
216-
198+
func applyK8sResources(api *spec.API, prevDeployment *kapps.Deployment, prevService *kcore.Service, prevVirtualService *kunstructured.Unstructured) error {
217199
return parallel.RunFirstErr(
218200
func() error {
219201
return applyK8sDeployment(api, prevDeployment)
@@ -307,6 +289,7 @@ func isAPIUpdating(deployment *kapps.Deployment) (bool, error) {
307289
if err != nil {
308290
return false, err
309291
}
292+
310293
replicaCounts := getReplicaCounts(deployment, pods)
311294

312295
minReplicas, ok := s.ParseInt32(deployment.Labels["minReplicas"])
@@ -321,24 +304,16 @@ func isAPIUpdating(deployment *kapps.Deployment) (bool, error) {
321304
return false, nil
322305
}
323306

324-
func areDeploymentsEqual(d1, d2 *kapps.Deployment) bool {
325-
return deploymentsMatch(d1, d2) &&
326-
k8s.PodComputesEqual(&d1.Spec.Template.Spec, &d2.Spec.Template.Spec)
327-
}
328-
329307
func isPodSpecLatest(deployment *kapps.Deployment, pod *kcore.Pod) bool {
330-
return podLabelsEqual(deployment.Spec.Template.Labels, pod.Labels) &&
331-
k8s.PodComputesEqual(&deployment.Spec.Template.Spec, &pod.Spec)
332-
}
333-
334-
func podLabelsEqual(labels1, labels2 map[string]string) bool {
335-
return labels1["apiName"] == labels2["apiName"] &&
336-
labels1["apiID"] == labels2["apiID"] &&
337-
labels1["deploymentID"] == labels2["deploymentID"]
308+
return k8s.PodComputesEqual(&deployment.Spec.Template.Spec, &pod.Spec) &&
309+
deployment.Spec.Template.Labels["apiName"] == pod.Labels["apiName"] &&
310+
deployment.Spec.Template.Labels["apiID"] == pod.Labels["apiID"] &&
311+
deployment.Spec.Template.Labels["deploymentID"] == pod.Labels["deploymentID"]
338312
}
339313

340-
func deploymentsMatch(d1, d2 *kapps.Deployment) bool {
341-
return k8s.DeploymentStrategiesMatch(d1.Spec.Strategy, d2.Spec.Strategy) &&
314+
func areAPIsEqual(d1, d2 *kapps.Deployment) bool {
315+
return k8s.PodComputesEqual(&d1.Spec.Template.Spec, &d2.Spec.Template.Spec) &&
316+
k8s.DeploymentStrategiesMatch(d1.Spec.Strategy, d2.Spec.Strategy) &&
342317
d1.Labels["apiName"] == d2.Labels["apiName"] &&
343318
d1.Labels["apiID"] == d2.Labels["apiID"] &&
344319
d1.Labels["deploymentID"] == d2.Labels["deploymentID"] &&

0 commit comments

Comments
 (0)