Skip to content

Commit

Permalink
fix(gateway/rest): use cluster status instead of manifest
Browse files Browse the repository at this point in the history
check service status prior trying shell using cluster API
refs akash-network/support#87

Signed-off-by: Artur Troian <[email protected]>
  • Loading branch information
troian committed May 21, 2024
1 parent e2e5214 commit eb12cbe
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions gateway/rest/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func newRouter(log log.Logger, addr sdk.Address, pclient provider.Client, ctxCon

// POST /lease/<lease-id>/shell
lrouter.HandleFunc("/shell",
leaseShellHandler(log, pclient.Manifest(), pclient.Cluster()))
leaseShellHandler(log, pclient.Cluster()))

return router
}
Expand Down Expand Up @@ -306,24 +306,18 @@ type leaseShellResponse struct {
Message string `json:"message,omitempty"`
}

func leaseShellHandler(log log.Logger, mclient pmanifest.Client, cclient cluster.Client) http.HandlerFunc {
func leaseShellHandler(log log.Logger, cclient cluster.Client) http.HandlerFunc {
return func(rw http.ResponseWriter, req *http.Request) {
leaseID := requestLeaseID(req)

// check if deployment actually exists in the first place before querying kubernetes
active, err := mclient.IsActive(req.Context(), leaseID.DeploymentID())
// // check if deployment actually exists in the first place before querying kubernetes
status, err := cclient.LeaseStatus(req.Context(), leaseID)
if err != nil {
log.Error("failed checking deployment activity", "err", err)
rw.WriteHeader(http.StatusInternalServerError)
return
}

if !active {
log.Info("no active deployment", "lease", leaseID)
rw.WriteHeader(http.StatusNotFound)
return
}

localLog := log.With("lease", leaseID.String(), "action", "shell")

vars := req.URL.Query()
Expand Down Expand Up @@ -357,6 +351,19 @@ func leaseShellHandler(log log.Logger, mclient pmanifest.Client, cclient cluster
return
}

svc, active := status[service]
if !active {
log.Info("no active deployment", "lease", leaseID)
rw.WriteHeader(http.StatusNotFound)
return
}

if svc.Available == 0 {
log.Info("deployment does not have active replicas", "lease", leaseID)
rw.WriteHeader(http.StatusNotFound)
return
}

stdin := vars.Get("stdin")
if 0 == len(stdin) {
localLog.Error("missing parameter stdin")
Expand Down

0 comments on commit eb12cbe

Please sign in to comment.