From c2d00140b1d73267201e6f3c31546f6499bc64a3 Mon Sep 17 00:00:00 2001 From: vishal Date: Mon, 29 Jul 2019 15:59:28 -0400 Subject: [PATCH 1/5] List active Cortex deployments --- cli/cmd/get.go | 41 ++++++++++ cli/cmd/lib_config_reader.go | 4 + cli/cmd/root.go | 5 ++ docs/cluster/cli.md | 3 +- pkg/operator/api/context/context.go | 1 + .../api/resource/deployment_status.go | 80 +++++++++++++++++++ pkg/operator/api/schema/schema.go | 12 +++ pkg/operator/context/context.go | 2 + pkg/operator/endpoints/deployments.go | 43 ++++++++++ pkg/operator/operator.go | 1 + pkg/operator/workloads/workflow.go | 33 ++++++++ 11 files changed, 224 insertions(+), 1 deletion(-) create mode 100644 pkg/operator/api/resource/deployment_status.go create mode 100644 pkg/operator/endpoints/deployments.go diff --git a/cli/cmd/get.go b/cli/cmd/get.go index 691d849b5b..0055c826f4 100644 --- a/cli/cmd/get.go +++ b/cli/cmd/get.go @@ -47,6 +47,7 @@ func init() { addWatchFlag(getCmd) addSummaryFlag(getCmd) addVerboseFlag(getCmd) + addActiveDeploymentsFlag(getCmd) // addResourceTypesToHelp(getCmd) } @@ -63,6 +64,10 @@ var getCmd = &cobra.Command{ } func runGet(cmd *cobra.Command, args []string) (string, error) { + if flagActiveDeployments || !IsAppNameSpecified() { + return getDeploymentsResponse() + } + resourcesRes, err := getResourcesResponse() if err != nil { return "", err @@ -109,6 +114,42 @@ func runGet(cmd *cobra.Command, args []string) (string, error) { return "", errors.New("too many args") // unexpected } +func getDeploymentsResponse() (string, error) { + httpResponse, err := HTTPGet("/deployments", map[string]string{}) + if err != nil { + return "", err + } + + var resourcesRes schema.GetDeploymentsResponse + if err = json.Unmarshal(httpResponse, &resourcesRes); err != nil { + return "", err + } + + if len(resourcesRes.Deployments) == 0 { + return "No active Cortex deployments found", nil + } + + rows := make([][]interface{}, len(resourcesRes.Deployments)) + for idx, deployment := range resourcesRes.Deployments { + rows[idx] = []interface{}{ + deployment.Name, + deployment.Status.String(), + libtime.Since(&deployment.LastUpdated) + " ago", + } + } + + t := table.Table{ + Headers: []table.Header{ + {Title: "NAME", MaxWidth: 32}, + {Title: "STATUS", MaxWidth: 21}, + {Title: "LAST UPDATED"}, + }, + Rows: rows, + } + + return table.MustFormat(t), nil +} + func getResourcesResponse() (*schema.GetResourcesResponse, error) { appName, err := AppNameFromFlagOrConfig() if err != nil { diff --git a/cli/cmd/lib_config_reader.go b/cli/cmd/lib_config_reader.go index 1eba1df173..efa8a08457 100644 --- a/cli/cmd/lib_config_reader.go +++ b/cli/cmd/lib_config_reader.go @@ -106,3 +106,7 @@ func AppNameFromFlagOrConfig() (string, error) { return appName, nil } + +func IsAppNameSpecified() bool { + return flagAppName != "" || appRootOrBlank() != "" +} diff --git a/cli/cmd/root.go b/cli/cmd/root.go index d5d0d7b26e..6dc2161e35 100644 --- a/cli/cmd/root.go +++ b/cli/cmd/root.go @@ -40,6 +40,7 @@ var flagWatch bool var flagAppName string var flagVerbose bool var flagSummary bool +var flagActiveDeployments bool var configFileExts = []string{"yaml", "yml"} @@ -106,6 +107,10 @@ func addSummaryFlag(cmd *cobra.Command) { cmd.PersistentFlags().BoolVarP(&flagSummary, "summary", "s", false, "show summarized output") } +func addActiveDeploymentsFlag(cmd *cobra.Command) { + getCmd.PersistentFlags().BoolVarP(&flagActiveDeployments, "active-deployments", "a", false, "list active Cortex deployments") +} + var resourceTypesHelp = fmt.Sprintf("\nResource Types:\n %s\n", strings.Join(resource.VisibleTypes.StringList(), "\n ")) func addResourceTypesToHelp(cmd *cobra.Command) { diff --git a/docs/cluster/cli.md b/docs/cluster/cli.md index 347920b8b4..57aa9cd545 100644 --- a/docs/cluster/cli.md +++ b/docs/cluster/cli.md @@ -25,6 +25,7 @@ Usage: cortex get [RESOURCE_TYPE] [RESOURCE_NAME] [flags] Flags: + -a, --active-deployments list active Cortex deployments -d, --deployment string deployment name -e, --env string environment (default "dev") -h, --help help for get @@ -33,7 +34,7 @@ Flags: -w, --watch re-run the command every 2 seconds ``` -The `get` command displays the current state of all resources on the cluster. Specifying a resource name provides the state of the particular resource. A detailed view of the configuration and additional metdata of a specific resource can be retrieved by adding the `-v` or `--verbose` flag. Using the `-s` or `--summary` flag will show a summarized view of all resource statuses. +The `get` command displays the current state of all resources on the cluster. Specifying a resource name provides the state of the particular resource. A detailed view of the configuration and additional metdata of a specific resource can be retrieved by adding the `-v` or `--verbose` flag. Using the `-s` or `--summary` flag will show a summarized view of all resource statuses. A list of active Cortex deployments can be displayed by specifying the `-a` or `--active-deployments` flag. ## logs diff --git a/pkg/operator/api/context/context.go b/pkg/operator/api/context/context.go index 0aef70158b..76f76873eb 100644 --- a/pkg/operator/api/context/context.go +++ b/pkg/operator/api/context/context.go @@ -29,6 +29,7 @@ import ( type Context struct { ID string `json:"id"` Key string `json:"key"` + CreatedEpoch int64 `json:"created_epoch"` CortexConfig *config.CortexConfig `json:"cortex_config"` DatasetVersion string `json:"dataset_version"` Root string `json:"root"` diff --git a/pkg/operator/api/resource/deployment_status.go b/pkg/operator/api/resource/deployment_status.go new file mode 100644 index 0000000000..ae9bc624dd --- /dev/null +++ b/pkg/operator/api/resource/deployment_status.go @@ -0,0 +1,80 @@ +/* +Copyright 2019 Cortex Labs, Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package resource + +type DeploymentStatus int + +const ( + UnknownDeploymentStatus DeploymentStatus = iota + UpdatedDeploymentStatus + UpdatingDeploymentStatus + FailedDeploymentStatus +) + +var deploymentStatuses = []string{ + "unknown", + "updated", + "updating", + "failed", +} + +func DeploymentStatusFromString(s string) DeploymentStatus { + for i := 0; i < len(deploymentStatuses); i++ { + if s == deploymentStatuses[i] { + return DeploymentStatus(i) + } + } + return UnknownDeploymentStatus +} + +func DeploymentStatusStrings() []string { + return deploymentStatuses[1:] +} + +func (t DeploymentStatus) String() string { + return deploymentStatuses[t] +} + +// MarshalText satisfies TextMarshaler +func (t DeploymentStatus) MarshalText() ([]byte, error) { + return []byte(t.String()), nil +} + +// UnmarshalText satisfies TextUnmarshaler +func (t *DeploymentStatus) UnmarshalText(text []byte) error { + enum := string(text) + for i := 0; i < len(deploymentStatuses); i++ { + if enum == deploymentStatuses[i] { + *t = DeploymentStatus(i) + return nil + } + } + + *t = UnknownDeploymentStatus + return nil +} + +// UnmarshalBinary satisfies BinaryUnmarshaler +// Needed for msgpack +func (t *DeploymentStatus) UnmarshalBinary(data []byte) error { + return t.UnmarshalText(data) +} + +// MarshalBinary satisfies BinaryMarshaler +func (t DeploymentStatus) MarshalBinary() ([]byte, error) { + return []byte(t.String()), nil +} diff --git a/pkg/operator/api/schema/schema.go b/pkg/operator/api/schema/schema.go index 7823363cc5..2ced0d9362 100644 --- a/pkg/operator/api/schema/schema.go +++ b/pkg/operator/api/schema/schema.go @@ -17,6 +17,8 @@ limitations under the License. package schema import ( + "time" + "github.com/cortexlabs/cortex/pkg/operator/api/context" "github.com/cortexlabs/cortex/pkg/operator/api/resource" ) @@ -41,6 +43,16 @@ type GetResourcesResponse struct { APIsBaseURL string `json:"apis_base_url"` } +type Deployment struct { + Name string `json:"name"` + Status resource.DeploymentStatus `json:"status"` + LastUpdated time.Time `json:"last_updated"` +} + +type GetDeploymentsResponse struct { + Deployments []Deployment `json:"deployments"` +} + type GetAggregateResponse struct { Value []byte `json:"value"` } diff --git a/pkg/operator/context/context.go b/pkg/operator/context/context.go index 58794f401d..c867b24e7c 100644 --- a/pkg/operator/context/context.go +++ b/pkg/operator/context/context.go @@ -20,6 +20,7 @@ import ( "path/filepath" "sort" "strings" + "time" "github.com/cortexlabs/cortex/pkg/consts" "github.com/cortexlabs/cortex/pkg/lib/configreader" @@ -122,6 +123,7 @@ func New( ignoreCache bool, ) (*context.Context, error) { ctx := &context.Context{} + ctx.CreatedEpoch = time.Now().Unix() ctx.CortexConfig = config.Cortex diff --git a/pkg/operator/endpoints/deployments.go b/pkg/operator/endpoints/deployments.go new file mode 100644 index 0000000000..eec583d950 --- /dev/null +++ b/pkg/operator/endpoints/deployments.go @@ -0,0 +1,43 @@ +/* +Copyright 2019 Cortex Labs, Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package endpoints + +import ( + "net/http" + "time" + + "github.com/cortexlabs/cortex/pkg/operator/api/schema" + "github.com/cortexlabs/cortex/pkg/operator/workloads" +) + +func GetDeployments(w http.ResponseWriter, r *http.Request) { + + currentContexts := workloads.CurrentContexts() + deployments := make([]schema.Deployment, len(currentContexts)) + for idx, ctx := range currentContexts { + deployments[idx].Name = ctx.App.Name + status, _ := workloads.GetDeploymentStatus(ctx.App.Name) + deployments[idx].Status = status + deployments[idx].LastUpdated = time.Unix(ctx.CreatedEpoch, 0) + } + + response := schema.GetDeploymentsResponse{ + Deployments: deployments, + } + + Respond(w, response) +} diff --git a/pkg/operator/operator.go b/pkg/operator/operator.go index be56591c7e..ae99110baa 100644 --- a/pkg/operator/operator.go +++ b/pkg/operator/operator.go @@ -58,6 +58,7 @@ func main() { router.HandleFunc("/deploy", endpoints.Deploy).Methods("POST") router.HandleFunc("/delete", endpoints.Delete).Methods("POST") + router.HandleFunc("/deployments", endpoints.GetDeployments).Methods("GET") router.HandleFunc("/resources", endpoints.GetResources).Methods("GET") router.HandleFunc("/aggregate/{id}", endpoints.GetAggregate).Methods("GET") router.HandleFunc("/logs/read", endpoints.ReadLogs) diff --git a/pkg/operator/workloads/workflow.go b/pkg/operator/workloads/workflow.go index c3374ed366..128db9c69c 100644 --- a/pkg/operator/workloads/workflow.go +++ b/pkg/operator/workloads/workflow.go @@ -24,6 +24,7 @@ import ( "github.com/cortexlabs/cortex/pkg/lib/errors" "github.com/cortexlabs/cortex/pkg/lib/sets/strset" "github.com/cortexlabs/cortex/pkg/operator/api/context" + "github.com/cortexlabs/cortex/pkg/operator/api/resource" "github.com/cortexlabs/cortex/pkg/operator/api/userconfig" "github.com/cortexlabs/cortex/pkg/operator/config" ) @@ -280,6 +281,38 @@ func IsWorkloadEnded(appName string, workloadID string) (bool, error) { return false, errors.New("workload not found in the current context") } +func GetDeploymentStatus(appName string) (resource.DeploymentStatus, error) { + ctx := CurrentContext(appName) + if ctx == nil { + return resource.UnknownDeploymentStatus, nil + } + + allSuccess := true + for _, workload := range extractWorkloads(ctx) { + isFailed, err := workload.IsFailed(ctx) + if err != nil { + return resource.UnknownDeploymentStatus, err + } + if isFailed { + return resource.FailedDeploymentStatus, err + } + + isSucceeded, err := workload.IsSucceeded(ctx) + if err != nil { + return resource.UnknownDeploymentStatus, err + } + if !isSucceeded { + allSuccess = false + } + } + + if allSuccess { + return resource.UpdatedDeploymentStatus, nil + } + + return resource.UpdatingDeploymentStatus, nil +} + func IsDeploymentUpdating(appName string) (bool, error) { ctx := CurrentContext(appName) if ctx == nil { From 6b85c2c82bf9f13d46ae2c7a766d10ddcc561647 Mon Sep 17 00:00:00 2001 From: vishal Date: Mon, 29 Jul 2019 20:44:08 +0000 Subject: [PATCH 2/5] Adjust comments --- cli/cmd/root.go | 2 +- docs/cluster/cli.md | 16 ++++++++-------- pkg/operator/workloads/workflow.go | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/cli/cmd/root.go b/cli/cmd/root.go index 6dc2161e35..a409bf5410 100644 --- a/cli/cmd/root.go +++ b/cli/cmd/root.go @@ -108,7 +108,7 @@ func addSummaryFlag(cmd *cobra.Command) { } func addActiveDeploymentsFlag(cmd *cobra.Command) { - getCmd.PersistentFlags().BoolVarP(&flagActiveDeployments, "active-deployments", "a", false, "list active Cortex deployments") + getCmd.PersistentFlags().BoolVarP(&flagActiveDeployments, "active-deployments", "a", false, "list active deployments") } var resourceTypesHelp = fmt.Sprintf("\nResource Types:\n %s\n", strings.Join(resource.VisibleTypes.StringList(), "\n ")) diff --git a/docs/cluster/cli.md b/docs/cluster/cli.md index 57aa9cd545..5424b7a3ef 100644 --- a/docs/cluster/cli.md +++ b/docs/cluster/cli.md @@ -25,16 +25,16 @@ Usage: cortex get [RESOURCE_TYPE] [RESOURCE_NAME] [flags] Flags: - -a, --active-deployments list active Cortex deployments - -d, --deployment string deployment name - -e, --env string environment (default "dev") - -h, --help help for get - -s, --summary show summarized output - -v, --verbose show verbose output - -w, --watch re-run the command every 2 seconds + -a, --active-deployments list active deployments + -d, --deployment string deployment name + -e, --env string environment (default "dev") + -h, --help help for get + -s, --summary show summarized output + -v, --verbose show verbose output + -w, --watch re-run the command every 2 seconds ``` -The `get` command displays the current state of all resources on the cluster. Specifying a resource name provides the state of the particular resource. A detailed view of the configuration and additional metdata of a specific resource can be retrieved by adding the `-v` or `--verbose` flag. Using the `-s` or `--summary` flag will show a summarized view of all resource statuses. A list of active Cortex deployments can be displayed by specifying the `-a` or `--active-deployments` flag. +The `get` command displays the current state of all resources on the cluster. Specifying a resource name provides the state of the particular resource. A detailed view of the configuration and additional metdata of a specific resource can be retrieved by adding the `-v` or `--verbose` flag. Using the `-s` or `--summary` flag will show a summarized view of all resource statuses. A list of active deployments can be displayed by specifying the `-a` or `--active-deployments` flag. ## logs diff --git a/pkg/operator/workloads/workflow.go b/pkg/operator/workloads/workflow.go index 128db9c69c..c83b0eb77f 100644 --- a/pkg/operator/workloads/workflow.go +++ b/pkg/operator/workloads/workflow.go @@ -294,7 +294,7 @@ func GetDeploymentStatus(appName string) (resource.DeploymentStatus, error) { return resource.UnknownDeploymentStatus, err } if isFailed { - return resource.FailedDeploymentStatus, err + return resource.FailedDeploymentStatus, nil } isSucceeded, err := workload.IsSucceeded(ctx) From 62880273e537ae2706667ca3f35944d92af9f9a6 Mon Sep 17 00:00:00 2001 From: vishal Date: Tue, 30 Jul 2019 16:11:55 +0000 Subject: [PATCH 3/5] Address PR comments --- cli/cmd/get.go | 8 +-- cli/cmd/root.go | 6 +-- docs/cluster/cli.md | 16 +++--- examples/iris/pytorch/handler.py | 2 + .../api/resource/deployment_status.go | 4 +- pkg/operator/endpoints/deploy.go | 5 +- pkg/operator/endpoints/deployments.go | 9 ++-- pkg/operator/workloads/workflow.go | 53 +++++-------------- 8 files changed, 41 insertions(+), 62 deletions(-) diff --git a/cli/cmd/get.go b/cli/cmd/get.go index 0055c826f4..2149a39635 100644 --- a/cli/cmd/get.go +++ b/cli/cmd/get.go @@ -47,7 +47,7 @@ func init() { addWatchFlag(getCmd) addSummaryFlag(getCmd) addVerboseFlag(getCmd) - addActiveDeploymentsFlag(getCmd) + addAllDeploymentsFlag(getCmd) // addResourceTypesToHelp(getCmd) } @@ -64,7 +64,7 @@ var getCmd = &cobra.Command{ } func runGet(cmd *cobra.Command, args []string) (string, error) { - if flagActiveDeployments || !IsAppNameSpecified() { + if flagAllDeployments || !IsAppNameSpecified() { return getDeploymentsResponse() } @@ -126,7 +126,7 @@ func getDeploymentsResponse() (string, error) { } if len(resourcesRes.Deployments) == 0 { - return "No active Cortex deployments found", nil + return "No deployments found", nil } rows := make([][]interface{}, len(resourcesRes.Deployments)) @@ -134,7 +134,7 @@ func getDeploymentsResponse() (string, error) { rows[idx] = []interface{}{ deployment.Name, deployment.Status.String(), - libtime.Since(&deployment.LastUpdated) + " ago", + libtime.Since(&deployment.LastUpdated), } } diff --git a/cli/cmd/root.go b/cli/cmd/root.go index a409bf5410..185166b040 100644 --- a/cli/cmd/root.go +++ b/cli/cmd/root.go @@ -40,7 +40,7 @@ var flagWatch bool var flagAppName string var flagVerbose bool var flagSummary bool -var flagActiveDeployments bool +var flagAllDeployments bool var configFileExts = []string{"yaml", "yml"} @@ -107,8 +107,8 @@ func addSummaryFlag(cmd *cobra.Command) { cmd.PersistentFlags().BoolVarP(&flagSummary, "summary", "s", false, "show summarized output") } -func addActiveDeploymentsFlag(cmd *cobra.Command) { - getCmd.PersistentFlags().BoolVarP(&flagActiveDeployments, "active-deployments", "a", false, "list active deployments") +func addAllDeploymentsFlag(cmd *cobra.Command) { + getCmd.PersistentFlags().BoolVarP(&flagAllDeployments, "all-deployments", "a", false, "list all deployments") } var resourceTypesHelp = fmt.Sprintf("\nResource Types:\n %s\n", strings.Join(resource.VisibleTypes.StringList(), "\n ")) diff --git a/docs/cluster/cli.md b/docs/cluster/cli.md index 5424b7a3ef..75054d005d 100644 --- a/docs/cluster/cli.md +++ b/docs/cluster/cli.md @@ -25,16 +25,16 @@ Usage: cortex get [RESOURCE_TYPE] [RESOURCE_NAME] [flags] Flags: - -a, --active-deployments list active deployments - -d, --deployment string deployment name - -e, --env string environment (default "dev") - -h, --help help for get - -s, --summary show summarized output - -v, --verbose show verbose output - -w, --watch re-run the command every 2 seconds + -a, --all-deployments list all deployments + -d, --deployment string deployment name + -e, --env string environment (default "dev") + -h, --help help for get + -s, --summary show summarized output + -v, --verbose show verbose output + -w, --watch re-run the command every 2 seconds ``` -The `get` command displays the current state of all resources on the cluster. Specifying a resource name provides the state of the particular resource. A detailed view of the configuration and additional metdata of a specific resource can be retrieved by adding the `-v` or `--verbose` flag. Using the `-s` or `--summary` flag will show a summarized view of all resource statuses. A list of active deployments can be displayed by specifying the `-a` or `--active-deployments` flag. +The `get` command displays the current state of all resources on the cluster. Specifying a resource name provides the state of the particular resource. A detailed view of the configuration and additional metdata of a specific resource can be retrieved by adding the `-v` or `--verbose` flag. Using the `-s` or `--summary` flag will show a summarized view of all resource statuses. A list of deployments can be displayed by specifying the `-a` or `--all-deployments` flag. ## logs diff --git a/examples/iris/pytorch/handler.py b/examples/iris/pytorch/handler.py index cc0e7369e4..57376ae2a2 100644 --- a/examples/iris/pytorch/handler.py +++ b/examples/iris/pytorch/handler.py @@ -2,6 +2,8 @@ iris_labels = ["Iris-setosa", "Iris-versicolor", "Iris-virginica"] +raise Exception("hi") + def pre_inference(sample, metadata): return { diff --git a/pkg/operator/api/resource/deployment_status.go b/pkg/operator/api/resource/deployment_status.go index ae9bc624dd..f3a156c954 100644 --- a/pkg/operator/api/resource/deployment_status.go +++ b/pkg/operator/api/resource/deployment_status.go @@ -22,14 +22,14 @@ const ( UnknownDeploymentStatus DeploymentStatus = iota UpdatedDeploymentStatus UpdatingDeploymentStatus - FailedDeploymentStatus + ErrorDeploymentStatus ) var deploymentStatuses = []string{ "unknown", "updated", "updating", - "failed", + "error", } func DeploymentStatusFromString(s string) DeploymentStatus { diff --git a/pkg/operator/endpoints/deploy.go b/pkg/operator/endpoints/deploy.go index 197782793a..26426a272d 100644 --- a/pkg/operator/endpoints/deploy.go +++ b/pkg/operator/endpoints/deploy.go @@ -23,6 +23,7 @@ import ( "github.com/cortexlabs/cortex/pkg/lib/files" "github.com/cortexlabs/cortex/pkg/lib/zip" "github.com/cortexlabs/cortex/pkg/operator/api/context" + "github.com/cortexlabs/cortex/pkg/operator/api/resource" "github.com/cortexlabs/cortex/pkg/operator/api/schema" "github.com/cortexlabs/cortex/pkg/operator/api/userconfig" "github.com/cortexlabs/cortex/pkg/operator/config" @@ -61,12 +62,14 @@ func Deploy(w http.ResponseWriter, r *http.Request) { fullCtxMatch = true } - isUpdating, err := workloads.IsDeploymentUpdating(ctx.App.Name) + deploymentStatus, err := workloads.GetDeploymentStatus(ctx.App.Name) if err != nil { RespondError(w, err) return } + isUpdating := deploymentStatus == resource.UpdatingDeploymentStatus + if isUpdating { if fullCtxMatch { respondDeploy(w, ResDeploymentUpToDateUpdating) diff --git a/pkg/operator/endpoints/deployments.go b/pkg/operator/endpoints/deployments.go index eec583d950..0cd7914234 100644 --- a/pkg/operator/endpoints/deployments.go +++ b/pkg/operator/endpoints/deployments.go @@ -25,14 +25,13 @@ import ( ) func GetDeployments(w http.ResponseWriter, r *http.Request) { - currentContexts := workloads.CurrentContexts() deployments := make([]schema.Deployment, len(currentContexts)) - for idx, ctx := range currentContexts { - deployments[idx].Name = ctx.App.Name + for i, ctx := range currentContexts { + deployments[i].Name = ctx.App.Name status, _ := workloads.GetDeploymentStatus(ctx.App.Name) - deployments[idx].Status = status - deployments[idx].LastUpdated = time.Unix(ctx.CreatedEpoch, 0) + deployments[i].Status = status + deployments[i].LastUpdated = time.Unix(ctx.CreatedEpoch, 0) } response := schema.GetDeploymentsResponse{ diff --git a/pkg/operator/workloads/workflow.go b/pkg/operator/workloads/workflow.go index c83b0eb77f..685476b37e 100644 --- a/pkg/operator/workloads/workflow.go +++ b/pkg/operator/workloads/workflow.go @@ -287,38 +287,8 @@ func GetDeploymentStatus(appName string) (resource.DeploymentStatus, error) { return resource.UnknownDeploymentStatus, nil } - allSuccess := true - for _, workload := range extractWorkloads(ctx) { - isFailed, err := workload.IsFailed(ctx) - if err != nil { - return resource.UnknownDeploymentStatus, err - } - if isFailed { - return resource.FailedDeploymentStatus, nil - } - - isSucceeded, err := workload.IsSucceeded(ctx) - if err != nil { - return resource.UnknownDeploymentStatus, err - } - if !isSucceeded { - allSuccess = false - } - } - - if allSuccess { - return resource.UpdatedDeploymentStatus, nil - } - - return resource.UpdatingDeploymentStatus, nil -} - -func IsDeploymentUpdating(appName string) (bool, error) { - ctx := CurrentContext(appName) - if ctx == nil { - return false, nil - } - + failedCount := 0 + updatingCount := 0 for _, workload := range extractWorkloads(ctx) { // Pending HPA workloads shouldn't block new deployments @@ -328,7 +298,7 @@ func IsDeploymentUpdating(appName string) (bool, error) { isSucceeded, err := workload.IsSucceeded(ctx) if err != nil { - return false, err + return resource.UnknownDeploymentStatus, err } if isSucceeded { continue @@ -336,23 +306,28 @@ func IsDeploymentUpdating(appName string) (bool, error) { isFailed, err := workload.IsFailed(ctx) if err != nil { - return false, err + return resource.UnknownDeploymentStatus, err } if isFailed { + failedCount++ continue } canRun, err := workload.CanRun(ctx) if err != nil { - return false, err + return resource.UnknownDeploymentStatus, err } if !canRun { continue } - - // It's either running or can run - return true, nil + updatingCount++ } - return false, nil + if failedCount > 0 { + return resource.ErrorDeploymentStatus, nil + } + if updatingCount > 0 { + return resource.UpdatingDeploymentStatus, nil + } + return resource.UpdatedDeploymentStatus, nil } From 9b59e83120fc058d3bfd6377721d247bd43b2724 Mon Sep 17 00:00:00 2001 From: vishal Date: Tue, 30 Jul 2019 16:14:28 +0000 Subject: [PATCH 4/5] Remove unnecessary variable --- pkg/operator/workloads/workflow.go | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/pkg/operator/workloads/workflow.go b/pkg/operator/workloads/workflow.go index 685476b37e..e0a81ca134 100644 --- a/pkg/operator/workloads/workflow.go +++ b/pkg/operator/workloads/workflow.go @@ -287,7 +287,6 @@ func GetDeploymentStatus(appName string) (resource.DeploymentStatus, error) { return resource.UnknownDeploymentStatus, nil } - failedCount := 0 updatingCount := 0 for _, workload := range extractWorkloads(ctx) { @@ -309,8 +308,7 @@ func GetDeploymentStatus(appName string) (resource.DeploymentStatus, error) { return resource.UnknownDeploymentStatus, err } if isFailed { - failedCount++ - continue + return resource.ErrorDeploymentStatus, nil } canRun, err := workload.CanRun(ctx) @@ -323,9 +321,6 @@ func GetDeploymentStatus(appName string) (resource.DeploymentStatus, error) { updatingCount++ } - if failedCount > 0 { - return resource.ErrorDeploymentStatus, nil - } if updatingCount > 0 { return resource.UpdatingDeploymentStatus, nil } From 290660985be8ecca4002c4470894219067bfa178 Mon Sep 17 00:00:00 2001 From: vishal Date: Tue, 30 Jul 2019 18:00:10 +0000 Subject: [PATCH 5/5] Address PR comments --- examples/iris/pytorch/handler.py | 2 -- pkg/operator/workloads/workflow.go | 8 ++++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/examples/iris/pytorch/handler.py b/examples/iris/pytorch/handler.py index 57376ae2a2..cc0e7369e4 100644 --- a/examples/iris/pytorch/handler.py +++ b/examples/iris/pytorch/handler.py @@ -2,8 +2,6 @@ iris_labels = ["Iris-setosa", "Iris-versicolor", "Iris-virginica"] -raise Exception("hi") - def pre_inference(sample, metadata): return { diff --git a/pkg/operator/workloads/workflow.go b/pkg/operator/workloads/workflow.go index 4b5f9364c2..ab174bb7fd 100644 --- a/pkg/operator/workloads/workflow.go +++ b/pkg/operator/workloads/workflow.go @@ -287,10 +287,10 @@ func GetDeploymentStatus(appName string) (resource.DeploymentStatus, error) { return resource.UnknownDeploymentStatus, nil } - updatingCount := 0 + isUpdating := false for _, workload := range extractWorkloads(ctx) { - // Pending HPA workloads shouldn't block new deployments + // HPA workloads don't really count if workload.GetWorkloadType() == workloadTypeHPA { continue } @@ -318,10 +318,10 @@ func GetDeploymentStatus(appName string) (resource.DeploymentStatus, error) { if !canRun { continue } - updatingCount++ + isUpdating = true } - if updatingCount > 0 { + if isUpdating { return resource.UpdatingDeploymentStatus, nil } return resource.UpdatedDeploymentStatus, nil