Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions api/restHandler/PipelineConfigRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"fmt"
"io"
"net/http"
"os"

"strconv"
"strings"

Expand Down Expand Up @@ -1290,8 +1292,17 @@ func (handler PipelineConfigRestHandlerImpl) UpdateAppOverride(w http.ResponseWr
writeJsonResp(w, err, nil, http.StatusBadRequest)
}
ChartVersion := dat.RefChartTemplate
validate, error := validator2.DeploymentTemplateValidate(dat.ValuesOverride, ChartVersion)
if validate {

if _, err := os.Stat(fmt.Sprintf("schema/%s.json",ChartVersion)); err == nil {
validate, error := validator2.DeploymentTemplateValidate(dat.ValuesOverride, ChartVersion)
if !validate {
fmt.Println("Values are incorrect", error)
writeJsonResp(w, error, nil, http.StatusBadRequest)
return
}


}
err = handler.validator.Struct(templateRequest)
if err != nil {
handler.Logger.Errorw("validation err, UpdateAppOverride", "err", err, "payload", templateRequest)
Expand Down Expand Up @@ -1320,11 +1331,9 @@ func (handler PipelineConfigRestHandlerImpl) UpdateAppOverride(w http.ResponseWr
return
}
writeJsonResp(w, err, createResp, http.StatusOK)
} else {
fmt.Println("Values are incorrect", error)
writeJsonResp(w, error, nil, http.StatusBadRequest)
return
}




}
func (handler PipelineConfigRestHandlerImpl) FetchArtifactForRollback(w http.ResponseWriter, r *http.Request) {
Expand Down
59 changes: 34 additions & 25 deletions internal/validator/DeploymentTemplateValidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"encoding/json"
"errors"
"fmt"

"github.com/devtron-labs/devtron/internal/util"
"io/ioutil"
"log"

"os"
"regexp"

Expand Down Expand Up @@ -70,6 +72,9 @@ const cpu = "cpu"
const memory = "memory"

func DeploymentTemplateValidate(templatejson interface{}, schemafile string) (bool, error) {

sugaredLogger := util.NewSugardLogger()

gojsonschema.FormatCheckers.Add("cpu", CpuChecker{})
gojsonschema.FormatCheckers.Add("memory", MemoryChecker{})

Expand All @@ -81,47 +86,51 @@ func DeploymentTemplateValidate(templatejson interface{}, schemafile string) (bo
documentLoader := gojsonschema.NewGoLoader(templatejson)
buff, err := json.Marshal(templatejson)
if err != nil {
log.Fatal(err)
sugaredLogger.Error(err)
return false, err
}
fmt.Println(string(buff))
result, err := gojsonschema.Validate(schemaLoader, documentLoader)
if err != nil {
log.Fatal(err)
sugaredLogger.Error(err)
return false, err
}
if result.Valid() {
var dat map[string]interface{}

if err := json.Unmarshal(buff, &dat); err != nil {
log.Fatal(err)
sugaredLogger.Error(err)
return false, err
}
//limits and requests are mandatory fields in schema
autoscaleEnabled := dat["autoscaling"]
if autoscaleEnabled == nil {
fmt.Println(autoscaleEnabled)
}else if autoscaleEnabled.(map[string]interface{})["enabled"] == nil {
fmt.Println("hello")
}else{
if autoscaleEnabled.(map[string]interface{})["enabled"].(bool) {
limit := dat["resources"].(map[string]interface{})["limits"].(map[string]interface{})
request := dat["resources"].(map[string]interface{})["requests"].(map[string]interface{})

cpu_limit, _ := util2.CpuToNumber(limit["cpu"].(string))
memory_limit, _ := util2.MemoryToNumber(limit["memory"].(string))
cpu_request, _ := util2.CpuToNumber(request["cpu"].(string))
memory_request, _ := util2.MemoryToNumber(request["memory"].(string))

envoproxy_limit := dat["envoyproxy"].(map[string]interface{})["resources"].(map[string]interface{})["limits"].(map[string]interface{})
envoproxy_request := dat["envoyproxy"].(map[string]interface{})["resources"].(map[string]interface{})["requests"].(map[string]interface{})

envoproxy_cpu_limit, _ := util2.CpuToNumber(envoproxy_limit["cpu"].(string))
envoproxy_memory_limit, _ := util2.MemoryToNumber(envoproxy_limit["memory"].(string))
envoproxy_cpu_request, _ := util2.CpuToNumber(envoproxy_request["cpu"].(string))
envoproxy_memory_request, _ := util2.MemoryToNumber(envoproxy_request["memory"].(string))
if (envoproxy_cpu_limit < envoproxy_cpu_request) || (envoproxy_memory_limit < envoproxy_memory_request) || (cpu_limit < cpu_request) || (memory_limit < memory_request) {
return false, errors.New("requests is greater than limits")
}

autoscaleEnabled := dat["autoscaling"].(map[string]interface{})
if autoscaleEnabled["enabled"].(bool) {
limit := dat["resources"].(map[string]interface{})["limits"].(map[string]interface{})
request := dat["resources"].(map[string]interface{})["requests"].(map[string]interface{})

cpu_limit, _ := util2.CpuToNumber(limit["cpu"].(string))
memory_limit, _ := util2.MemoryToNumber(limit["memory"].(string))
cpu_request, _ := util2.CpuToNumber(request["cpu"].(string))
memory_request, _ := util2.MemoryToNumber(request["memory"].(string))

envoproxy_limit := dat["envoyproxy"].(map[string]interface{})["resources"].(map[string]interface{})["limits"].(map[string]interface{})
envoproxy_request := dat["envoyproxy"].(map[string]interface{})["resources"].(map[string]interface{})["requests"].(map[string]interface{})

envoproxy_cpu_limit, _ := util2.CpuToNumber(envoproxy_limit["cpu"].(string))
envoproxy_memory_limit, _ := util2.MemoryToNumber(envoproxy_limit["memory"].(string))
envoproxy_cpu_request, _ := util2.CpuToNumber(envoproxy_request["cpu"].(string))
envoproxy_memory_request, _ := util2.MemoryToNumber(envoproxy_request["memory"].(string))
if (envoproxy_cpu_limit < envoproxy_cpu_request) || (envoproxy_memory_limit < envoproxy_memory_request) || (cpu_limit < cpu_request) || (memory_limit < memory_request) {
return false, errors.New("requests is greater than limits")
}

}

fmt.Println("ok")
return true, nil
} else {
Expand Down
Loading