Skip to content

Commit f790978

Browse files
authored
Merge pull request #6594 from devtron-labs/drain-node-fix
fix: validations in Drain node resthandler
2 parents d8634ad + a7f40bd commit f790978

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

api/k8s/capacity/k8sCapacityRestHandler.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/devtron-labs/devtron/pkg/cluster/rbac"
2727
"github.com/devtron-labs/devtron/pkg/cluster/read"
2828
bean3 "github.com/devtron-labs/devtron/pkg/k8s/bean"
29+
"gopkg.in/go-playground/validator.v9"
2930
"net/http"
3031
"strconv"
3132

@@ -60,6 +61,7 @@ type K8sCapacityRestHandlerImpl struct {
6061
environmentService environment.EnvironmentService
6162
clusterRbacService rbac.ClusterRbacService
6263
clusterReadService read.ClusterReadService
64+
validator *validator.Validate
6365
}
6466

6567
func NewK8sCapacityRestHandlerImpl(logger *zap.SugaredLogger,
@@ -68,7 +70,7 @@ func NewK8sCapacityRestHandlerImpl(logger *zap.SugaredLogger,
6870
clusterService cluster.ClusterService,
6971
environmentService environment.EnvironmentService,
7072
clusterRbacService rbac.ClusterRbacService,
71-
clusterReadService read.ClusterReadService) *K8sCapacityRestHandlerImpl {
73+
clusterReadService read.ClusterReadService, validator *validator.Validate) *K8sCapacityRestHandlerImpl {
7274
return &K8sCapacityRestHandlerImpl{
7375
logger: logger,
7476
k8sCapacityService: k8sCapacityService,
@@ -78,6 +80,7 @@ func NewK8sCapacityRestHandlerImpl(logger *zap.SugaredLogger,
7880
environmentService: environmentService,
7981
clusterRbacService: clusterRbacService,
8082
clusterReadService: clusterReadService,
83+
validator: validator,
8184
}
8285
}
8386

@@ -401,6 +404,15 @@ func (handler *K8sCapacityRestHandlerImpl) DrainNode(w http.ResponseWriter, r *h
401404
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
402405
return
403406
}
407+
408+
// Validate the struct using the validator
409+
err = handler.validator.Struct(nodeDrainReq)
410+
if err != nil {
411+
handler.logger.Errorw("validation error", "err", err, "payload", nodeDrainReq)
412+
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
413+
return
414+
}
415+
404416
userId, err := handler.userService.GetLoggedInUser(r)
405417
if userId == 0 || err != nil {
406418
common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized)

pkg/k8s/capacity/bean/bean.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,14 +168,14 @@ type NodeConditionObject struct {
168168
}
169169

170170
type NodeUpdateRequestDto struct {
171-
ClusterId int `json:"clusterId"`
172-
Name string `json:"name"`
171+
ClusterId int `json:"clusterId" validate:"number,required"`
172+
Name string `json:"name" validate:"required"`
173173
ManifestPatch string `json:"manifestPatch"`
174174
Version string `json:"version"`
175175
Kind string `json:"kind"`
176176
Taints []corev1.Taint `json:"taints"`
177177
NodeCordonHelper *NodeCordonHelper `json:"nodeCordonOptions"`
178-
NodeDrainHelper *NodeDrainHelper `json:"nodeDrainOptions"`
178+
NodeDrainHelper *NodeDrainHelper `json:"nodeDrainOptions" validate:"required"`
179179
}
180180

181181
type NodeCordonHelper struct {

wire_gen.go

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

0 commit comments

Comments
 (0)