From 1d94cbecce0e04c58e88c0a06ee328bb6b4b6444 Mon Sep 17 00:00:00 2001 From: Akash Nayak Date: Fri, 15 Mar 2024 18:59:04 +0530 Subject: [PATCH] feat: Add support for enabling and disabling QA category to Move2Kube API Signed-off-by: Akash Nayak --- Dockerfile | 2 +- assets/openapi.json | 20 ++++++++++++ internal/filesystem/filesystem.go | 36 ++++++++++++++++------ internal/filesystem/interface.go | 4 +-- internal/move2kubeapi/handlers/handlers.go | 4 +++ internal/move2kubeapi/handlers/outputs.go | 16 +++++++++- 6 files changed, 68 insertions(+), 14 deletions(-) diff --git a/Dockerfile b/Dockerfile index d6d6b91..f69c8eb 100644 --- a/Dockerfile +++ b/Dockerfile @@ -26,7 +26,7 @@ RUN mkdir -p $GOPATH/src $GOPATH/bin && chmod -R 777 $GOPATH ENV PATH=$GOPATH/bin:/usr/local/go/bin:$PATH # Download Go. -ARG GO_VERSION=1.18 +ARG GO_VERSION=1.19 ARG TARGETARCH ARG TARGETOS ENV TARGETARCH_ENV=${TARGETARCH:-amd64} diff --git a/assets/openapi.json b/assets/openapi.json index a4ecb3d..d9b56af 100755 --- a/assets/openapi.json +++ b/assets/openapi.json @@ -1333,6 +1333,26 @@ "schema": { "type": "boolean" } + }, + { + "name": "disable-qa-category", + "in": "query", + "description": "String to disable QA categories. (comma-separated values)", + "required": false, + "example": "transformers,network", + "schema": { + "type": "string" + } + }, + { + "name": "enable-qa-category", + "in": "query", + "description": "String to enable QA categories. (comma-separated values)", + "required": false, + "example": "imageregistry,cicd", + "schema": { + "type": "string" + } } ], "requestBody": { diff --git a/internal/filesystem/filesystem.go b/internal/filesystem/filesystem.go index 57205e2..21473af 100644 --- a/internal/filesystem/filesystem.go +++ b/internal/filesystem/filesystem.go @@ -1515,18 +1515,19 @@ func (fs *FileSystem) deletePlan(t *bolt.Tx, workspaceId, projectId string) erro } // ResumeTransformation resumes a transformation that did not finish -func (fs *FileSystem) ResumeTransformation(workspaceId, projectId, projOutputId string, debugMode, skipQA bool) error { +func (fs *FileSystem) ResumeTransformation(workspaceId, projectId, projOutputId string, debugMode, skipQA bool, enableQACategories, disableQACategories []string) error { db, err := fs.GetDatabase(false) if err != nil { return err } + defer db.Close() return db.Update(func(t *bolt.Tx) error { - return fs.resumeTransformation(t, workspaceId, projectId, projOutputId, debugMode, skipQA) + return fs.resumeTransformation(t, workspaceId, projectId, projOutputId, debugMode, skipQA, enableQACategories, disableQACategories) }) } -func (fs *FileSystem) resumeTransformation(t *bolt.Tx, workspaceId, projectId, projOutputId string, debugMode, skipQA bool) error { +func (fs *FileSystem) resumeTransformation(t *bolt.Tx, workspaceId, projectId, projOutputId string, debugMode, skipQA bool, enableQACategories, disableQACategories []string) error { // check conditions project, err := fs.readProject(t, workspaceId, projectId) if err != nil { @@ -1617,23 +1618,23 @@ func (fs *FileSystem) resumeTransformation(t *bolt.Tx, workspaceId, projectId, p currentRunConfigPaths = append(commonConfigPaths, currentRunConfigPaths...) } // resume the transformation - go fs.runTransform(currentRunDir, currentRunConfigPaths, currentRunSrcDir, currentRunCustDir, currentRunOutDir, message, qaServerMeta.Port, transformCh, workspaceId, projectId, projOutput, debugMode, skipQA, true) + go fs.runTransform(currentRunDir, currentRunConfigPaths, currentRunSrcDir, currentRunCustDir, currentRunOutDir, message, qaServerMeta.Port, transformCh, workspaceId, projectId, projOutput, debugMode, skipQA, true, enableQACategories, disableQACategories) return nil } // StartTransformation starts the transformation for a project. -func (fs *FileSystem) StartTransformation(workspaceId, projectId string, projOutput types.ProjectOutput, plan io.Reader, debugMode, skipQA bool) error { +func (fs *FileSystem) StartTransformation(workspaceId, projectId string, projOutput types.ProjectOutput, plan io.Reader, debugMode, skipQA bool, enableQACategories, disableQACategories []string) error { db, err := fs.GetDatabase(false) if err != nil { return err } defer db.Close() return db.Update(func(t *bolt.Tx) error { - return fs.startTransformation(t, workspaceId, projectId, projOutput, plan, debugMode, skipQA) + return fs.startTransformation(t, workspaceId, projectId, projOutput, plan, debugMode, skipQA, enableQACategories, disableQACategories) }) } -func (fs *FileSystem) startTransformation(t *bolt.Tx, workspaceId, projectId string, projOutput types.ProjectOutput, plan io.Reader, debugMode, skipQA bool) error { +func (fs *FileSystem) startTransformation(t *bolt.Tx, workspaceId, projectId string, projOutput types.ProjectOutput, plan io.Reader, debugMode, skipQA bool, enableQACategories, disableQACategories []string) error { // check conditions project, err := fs.readProject(t, workspaceId, projectId) if err != nil { @@ -1811,7 +1812,7 @@ func (fs *FileSystem) startTransformation(t *bolt.Tx, workspaceId, projectId str currentRunConfigPaths = append(commonConfigPaths, currentRunConfigPaths...) } // start the transformation - go fs.runTransform(currentRunDir, currentRunConfigPaths, currentRunSrcDir, currentRunCustDir, currentRunOutDir, message, qaServerMeta.Port, transformCh, workspaceId, projectId, projOutput, debugMode, skipQA, false) + go fs.runTransform(currentRunDir, currentRunConfigPaths, currentRunSrcDir, currentRunCustDir, currentRunOutDir, message, qaServerMeta.Port, transformCh, workspaceId, projectId, projOutput, debugMode, skipQA, false, enableQACategories, disableQACategories) logrus.Infof("Waiting for QA engine to start for the output '%s' of the project '%s'", projOutput.Id, projectId) if err := <-transformCh; err != nil { return fmt.Errorf("failed to start the transformation and qa engine. Error: %w", err) @@ -2156,7 +2157,7 @@ func NewFileSystem() (*FileSystem, error) { } for _, project := range projects { for _, projOutput := range project.Outputs { - if err := fileSystem.ResumeTransformation(workspace.Id, project.Id, projOutput.Id, false, false); err != nil { + if err := fileSystem.ResumeTransformation(workspace.Id, project.Id, projOutput.Id, false, false, []string{}, []string{}); err != nil { logrus.Debugf("failed to resume the transformation for output with id: %s of project id: %s . Error: %q", projOutput.Id, project.Id, err) } } @@ -2375,7 +2376,7 @@ func (fs *FileSystem) runPlan(currentRunDir string, currentRunConfigPaths []stri return err } -func (fs *FileSystem) runTransform(currentRunDir string, currentRunConfigPaths []string, currentRunSrcDir, currentRunCustDir, currentRunOutDir, message string, port int, transformCh chan error, workspaceId, projectId string, projOutput types.ProjectOutput, debugMode bool, skipQA bool, overwriteOutDir bool) error { +func (fs *FileSystem) runTransform(currentRunDir string, currentRunConfigPaths []string, currentRunSrcDir, currentRunCustDir, currentRunOutDir, message string, port int, transformCh chan error, workspaceId, projectId string, projOutput types.ProjectOutput, debugMode bool, skipQA bool, overwriteOutDir bool, enableQACategories, disableQACategories []string) error { logrus.Infof("Starting transformation in %s with configs from %+v and source from %s , customizations from %s and output to %s", currentRunDir, currentRunConfigPaths, currentRunSrcDir, currentRunCustDir, currentRunOutDir) portStr, err := cast.ToStringE(port) if err != nil { @@ -2395,6 +2396,21 @@ func (fs *FileSystem) runTransform(currentRunDir string, currentRunConfigPaths [ if skipQA { cmdArgs = append(cmdArgs, "--qa-skip") } + if len(enableQACategories) > 0 && len(disableQACategories) > 0 { + logrus.Errorf("--qa-enable and --qa-disable cannot be used together.Proceeding with only --qa-disable flag\n") + enableQACategories = []string{} + } + if len(disableQACategories) > 0 { + for _, disableQACategory := range disableQACategories { + cmdArgs = append(cmdArgs, "--qa-disable", disableQACategory) + } + logrus.Infof("disable QA Categories: %v", disableQACategories) + } else if len(enableQACategories) > 0 { + for _, enableQACategory := range enableQACategories { + cmdArgs = append(cmdArgs, "--qa-enable", enableQACategory) + } + logrus.Infof("enable QA Categories: %v", enableQACategories) + } if !common.Config.EnableLocalExecution { cmdArgs = append(cmdArgs, "--disable-local-execution") } diff --git a/internal/filesystem/interface.go b/internal/filesystem/interface.go index 59a4661..52a1af2 100644 --- a/internal/filesystem/interface.go +++ b/internal/filesystem/interface.go @@ -44,8 +44,8 @@ type IFileSystem interface { ReadPlan(workspaceId, projectId string) (plan io.Reader, err error) UpdatePlan(workspaceId, projectId string, plan io.Reader) error DeletePlan(workspaceId, projectId string) error - StartTransformation(workspaceId, projectId string, projOutput types.ProjectOutput, plan io.Reader, debugMode, skipQA bool) error - ResumeTransformation(workspaceId, projectId, projOutputId string, debugMode, skipQA bool) error + StartTransformation(workspaceId, projectId string, projOutput types.ProjectOutput, plan io.Reader, debugMode, skipQA bool, enableQACategories, disableQACategories []string) error + ResumeTransformation(workspaceId, projectId, projOutputId string, debugMode, skipQA bool, enableQACategories, disableQACategories []string) error ReadProjectOutput(workspaceId, projectId, projOutputId string) (projOutput types.ProjectOutput, file io.Reader, err error) ReadProjectOutputGraph(workspaceId, projectId, projOutputId string) (projOutput types.ProjectOutput, file io.Reader, err error) DeleteProjectOutput(workspaceId, projectId, projOutputId string) error diff --git a/internal/move2kubeapi/handlers/handlers.go b/internal/move2kubeapi/handlers/handlers.go index e5f4bbe..e8ed197 100644 --- a/internal/move2kubeapi/handlers/handlers.go +++ b/internal/move2kubeapi/handlers/handlers.go @@ -32,6 +32,10 @@ import ( const ( // SKIP_QA_QUERY_PARAM is the name of the query parameter used for skipping QA SKIP_QA_QUERY_PARAM = "skip-qa" + // DISABLE_QA_CATEGORY is the name of the query parameter used for disabling QA category + DISABLE_QA_CATEGORY = "disable-qa-category" + // ENABLE_QA_CATEGORY is the name of the query parameter used for enabling QA category + ENABLE_QA_CATEGORY = "enable-qa-category" // REMOTE_SOURCE_QUERY_PARAM is the URL of the git remote to be used as source REMOTE_SOURCE_QUERY_PARAM = "remote-source" // DEBUG_QUERY_PARAM is the name of the query parameter used for debug mode diff --git a/internal/move2kubeapi/handlers/outputs.go b/internal/move2kubeapi/handlers/outputs.go index 29ee4ab..93b18f5 100644 --- a/internal/move2kubeapi/handlers/outputs.go +++ b/internal/move2kubeapi/handlers/outputs.go @@ -53,8 +53,22 @@ func HandleStartTransformation(w http.ResponseWriter, r *http.Request) { // empty body planReader = nil } + debugMode := r.URL.Query().Get(DEBUG_QUERY_PARAM) == "true" skipQA := r.URL.Query().Get(SKIP_QA_QUERY_PARAM) == "true" + var enableQACategories []string + var disableQACategories []string + if val, ok := r.URL.Query()[DISABLE_QA_CATEGORY]; ok { + disableQACategories = val + } + if val, ok := r.URL.Query()[ENABLE_QA_CATEGORY]; ok { + enableQACategories = val + } + // qa-disable and qa-enable flags are mutually exclusive + if len(enableQACategories) > 0 && len(disableQACategories) > 0 { + logrus.Errorf("--qa-enable and --qa-disable cannot be used together.Proceeding with only --qa-disable flag\n") + enableQACategories = []string{} + } timestamp, _, err := common.GetTimestamp() if err != nil { logrus.Errorf("failed to get the timestamp. Error: %q", err) @@ -66,7 +80,7 @@ func HandleStartTransformation(w http.ResponseWriter, r *http.Request) { projOutput.Timestamp = timestamp projOutput.Name = projOutput.Id // This isn't really used anywhere projOutput.Status = types.ProjectOutputStatusInProgress - if err := m2kFS.StartTransformation(workspaceId, projectId, projOutput, planReader, debugMode, skipQA); err != nil { + if err := m2kFS.StartTransformation(workspaceId, projectId, projOutput, planReader, debugMode, skipQA, enableQACategories, disableQACategories); err != nil { logrus.Errorf("failed to start the transformation. Error: %q", err) if notExErr, ok := err.(types.ErrorDoesNotExist); ok { if notExErr.Id == "plan" {