Skip to content

Commit 704b883

Browse files
authored
fix: resource terminal update api (#4641)
* handling resource not found error * adding back argocd assets
1 parent 488f955 commit 704b883

File tree

11 files changed

+53
-18
lines changed

11 files changed

+53
-18
lines changed

api/k8s/application/k8sApplicationRestHandler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ func (handler *K8sApplicationRestHandlerImpl) DeleteResource(w http.ResponseWrit
526526
errCode = apiErr.HttpStatusCode
527527
switch errCode {
528528
case http.StatusNotFound:
529-
errorMessage := "resource not found"
529+
errorMessage := k8s.ResourceNotFoundErr
530530
err = fmt.Errorf("%s: %w", errorMessage, err)
531531
}
532532
}

api/k8s/capacity/k8sCapacityRestHandler.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package capacity
33
import (
44
"encoding/json"
55
"errors"
6+
"fmt"
7+
"github.com/devtron-labs/common-lib/utils"
8+
"github.com/devtron-labs/devtron/pkg/k8s"
69
"net/http"
710
"strconv"
811

@@ -311,6 +314,15 @@ func (handler *K8sCapacityRestHandlerImpl) DeleteNode(w http.ResponseWriter, r *
311314
}
312315
updatedManifest, err := handler.k8sCapacityService.DeleteNode(r.Context(), &nodeDelReq)
313316
if err != nil {
317+
errCode := http.StatusInternalServerError
318+
if apiErr, ok := err.(*utils.ApiError); ok {
319+
errCode = apiErr.HttpStatusCode
320+
switch errCode {
321+
case http.StatusNotFound:
322+
errorMessage := k8s.ResourceNotFoundErr
323+
err = fmt.Errorf("%s: %w", errorMessage, err)
324+
}
325+
}
314326
handler.logger.Errorw("error in deleting node", "err", err, "deleteRequest", nodeDelReq)
315327
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
316328
return

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ require (
1616
github.com/davecgh/go-spew v1.1.1
1717
github.com/deckarep/golang-set v1.8.0
1818
github.com/devtron-labs/authenticator v0.4.33
19-
github.com/devtron-labs/common-lib v0.0.12
19+
github.com/devtron-labs/common-lib v0.0.14
2020
github.com/devtron-labs/protos v0.0.3-0.20240130061723-7b2e12ab0abb
2121
github.com/evanphx/json-patch v5.6.0+incompatible
2222
github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,8 @@ github.com/denisenkom/go-mssqldb v0.0.0-20190707035753-2be1aa521ff4 h1:YcpmyvADG
224224
github.com/denisenkom/go-mssqldb v0.0.0-20190707035753-2be1aa521ff4/go.mod h1:zAg7JM8CkOJ43xKXIj7eRO9kmWm/TW578qo+oDO6tuM=
225225
github.com/devtron-labs/authenticator v0.4.33 h1:FpAV3ZgFluaRFcMwPpwxr/mwSipJ16XRvgABq3BzP5Y=
226226
github.com/devtron-labs/authenticator v0.4.33/go.mod h1:ozNfT8WcruiSgnUbyp48WVfc41++W6xYXhKFp67lNTU=
227-
github.com/devtron-labs/common-lib v0.0.12 h1:HirqTWtaXWPbfGeqQurjtn26b2Az7sMFZ1JAAz2koNM=
228-
github.com/devtron-labs/common-lib v0.0.12/go.mod h1:95/DizzVXu1kHap/VwEvdxwgd+BvPVYc0bJzt8yqGDU=
227+
github.com/devtron-labs/common-lib v0.0.14 h1:7P0Z87Des8Bni+aVHhPITjzKITuCVOjwAHOgElrh/rk=
228+
github.com/devtron-labs/common-lib v0.0.14/go.mod h1:95/DizzVXu1kHap/VwEvdxwgd+BvPVYc0bJzt8yqGDU=
229229
github.com/devtron-labs/protos v0.0.3-0.20240130061723-7b2e12ab0abb h1:CkfQQgZc950/hTPqtQSiHV2RmZgkBLGCzwR02FZYjAU=
230230
github.com/devtron-labs/protos v0.0.3-0.20240130061723-7b2e12ab0abb/go.mod h1:pjLjgoa1GzbkOkvbMyP4SAKsaiK7eG6GoQCNauG03JA=
231231
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=

pkg/clusterTerminalAccess/UserTerminalAccessService.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ func (impl *UserTerminalAccessServiceImpl) DisconnectTerminalSession(ctx context
340340
namespace := metadataMap["Namespace"]
341341
err = impl.DeleteTerminalPod(ctx, terminalAccessData.ClusterId, terminalAccessData.PodName, namespace)
342342
if err != nil {
343-
if isResourceNotFoundErr(err) {
343+
if k8s.IsResourceNotFoundErr(err) {
344344
accessSessionData.terminateTriggered = true
345345
err = nil
346346
}
@@ -356,12 +356,6 @@ func getErrorDetailedMessage(err error) string {
356356
}
357357
return ""
358358
}
359-
func isResourceNotFoundErr(err error) bool {
360-
if errStatus, ok := err.(*k8sErrors.StatusError); ok && errStatus.Status().Reason == metav1.StatusReasonNotFound {
361-
return true
362-
}
363-
return false
364-
}
365359

366360
func (impl *UserTerminalAccessServiceImpl) StopTerminalSession(ctx context.Context, userTerminalAccessId int) {
367361
impl.Logger.Infow("terminal stop request received for user", "userTerminalAccessId", userTerminalAccessId)
@@ -518,7 +512,7 @@ func (impl *UserTerminalAccessServiceImpl) SyncPodStatus() {
518512
impl.deleteClusterTerminalTemplates(context.Background(), terminalAccessData.ClusterId, terminalAccessData.PodName, namespace)
519513
err = impl.DeleteTerminalPod(context.Background(), terminalAccessData.ClusterId, terminalAccessData.PodName, namespace)
520514
if err != nil {
521-
if isResourceNotFoundErr(err) {
515+
if k8s.IsResourceNotFoundErr(err) {
522516
errorDetailedMessage := getErrorDetailedMessage(err)
523517
terminalPodStatusString = fmt.Sprintf("%s/%s", string(models.TerminalPodTerminated), errorDetailedMessage)
524518
} else {
@@ -830,7 +824,7 @@ func (impl *UserTerminalAccessServiceImpl) getPodManifest(ctx context.Context, c
830824
}
831825
response, err := impl.K8sCommonService.GetResource(ctx, request)
832826
if err != nil {
833-
if isResourceNotFoundErr(err) {
827+
if k8s.IsResourceNotFoundErr(err) {
834828
errorDetailedMessage := getErrorDetailedMessage(err)
835829
terminalPodStatusString := fmt.Sprintf("%s/%s", string(models.TerminalPodTerminated), errorDetailedMessage)
836830
return nil, errors.New(terminalPodStatusString)
@@ -1146,7 +1140,7 @@ func (impl *UserTerminalAccessServiceImpl) forceDeletePod(ctx context.Context, p
11461140
}
11471141
podRequestBean.K8sRequest.ForceDelete = true
11481142
_, err = impl.K8sCommonService.DeleteResource(ctx, podRequestBean)
1149-
if err != nil && !isResourceNotFoundErr(err) {
1143+
if err != nil && !k8s.IsResourceNotFoundErr(err) {
11501144
return false
11511145
}
11521146
return true

pkg/k8s/application/k8sApplicationService.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"errors"
77
"fmt"
8+
"github.com/devtron-labs/common-lib/utils"
89
"github.com/devtron-labs/devtron/api/helm-app/gRPC"
910
client "github.com/devtron-labs/devtron/api/helm-app/service"
1011
"io"
@@ -1002,6 +1003,12 @@ func (impl *K8sApplicationServiceImpl) RecreateResource(ctx context.Context, req
10021003
func (impl *K8sApplicationServiceImpl) DeleteResourceWithAudit(ctx context.Context, request *k8s.ResourceRequestBean, userId int32) (*k8s2.ManifestResponse, error) {
10031004
resp, err := impl.k8sCommonService.DeleteResource(ctx, request)
10041005
if err != nil {
1006+
if k8s.IsResourceNotFoundErr(err) {
1007+
return nil, &utils.ApiError{Code: "404",
1008+
HttpStatusCode: http.StatusNotFound,
1009+
InternalMessage: err.Error(),
1010+
UserMessage: k8s.ResourceNotFoundErr}
1011+
}
10051012
impl.logger.Errorw("error in deleting resource", "err", err)
10061013
return nil, err
10071014
}

pkg/k8s/bean.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,7 @@ type ResourceGetResponse struct {
4545
ManifestResponse *k8s.ManifestResponse `json:"manifestResponse"`
4646
SecretViewAccess bool `json:"secretViewAccess"` // imp: only for resource browser, this is being used to check whether a user can see obscured secret values or not.
4747
}
48+
49+
var (
50+
ResourceNotFoundErr = "Unable to locate Kubernetes resource."
51+
)

pkg/k8s/capacity/k8sCapacityService.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package capacity
33
import (
44
"context"
55
"fmt"
6+
"github.com/devtron-labs/common-lib/utils"
67
k8s2 "github.com/devtron-labs/common-lib/utils/k8s"
78
"github.com/devtron-labs/devtron/pkg/cluster"
89
"github.com/devtron-labs/devtron/pkg/k8s"
@@ -569,6 +570,12 @@ func (impl *K8sCapacityServiceImpl) DeleteNode(ctx context.Context, request *bea
569570
// Here Sending userId as 0 as appIdentifier is being sent nil so user id is not used in method. Update userid if appIdentifier is used
570571
manifestResponse, err := impl.k8sCommonService.DeleteResource(ctx, resourceRequest)
571572
if err != nil {
573+
if k8s.IsResourceNotFoundErr(err) {
574+
return nil, &utils.ApiError{Code: "404",
575+
HttpStatusCode: http.StatusNotFound,
576+
InternalMessage: err.Error(),
577+
UserMessage: k8s.ResourceNotFoundErr}
578+
}
572579
impl.logger.Errorw("error in deleting node", "err", err)
573580
return nil, err
574581
}

pkg/k8s/helper.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package k8s
2+
3+
import (
4+
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
5+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
6+
)
7+
8+
func IsResourceNotFoundErr(err error) bool {
9+
if errStatus, ok := err.(*k8sErrors.StatusError); ok && errStatus.Status().Reason == metav1.StatusReasonNotFound {
10+
return true
11+
}
12+
return false
13+
}

vendor/github.com/devtron-labs/common-lib/utils/k8s/K8sUtil.go

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)