From e43d513e3b276f8abbe955631a203d63422f0215 Mon Sep 17 00:00:00 2001 From: Amit Kumar Das Date: Fri, 15 Mar 2024 12:48:14 +0530 Subject: [PATCH] Revert "Replace deprecated package (#4475)" (#4533) This reverts commit da8ae756998bfb388972d9536d223d57a6e5ce47. --- .github/workflows/codeql-analysis.yml | 6 +- .../api/handlers/rest/user_handlers_test.go | 8 +- .../pkg/chaos_infrastructure/infra_utils.go | 18 ++--- .../server/pkg/chaoshub/handler/handler.go | 74 ++++++------------- .../pkg/chaoshub/handler/handler_test.go | 14 ++-- .../server/pkg/chaoshub/ops/gitops_test.go | 4 +- .../graphql/server/pkg/chaoshub/service.go | 33 +++++---- .../graphql/server/pkg/gitops/gitops.go | 13 ++-- .../graphql/server/pkg/gitops/service.go | 48 ++++++------ chaoscenter/subscriber/go.sum | 1 - .../subscriber/pkg/graphql/operations.go | 13 +--- 11 files changed, 99 insertions(+), 133 deletions(-) diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index b36af186eda..fa8946a4009 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -43,7 +43,7 @@ jobs: # Initializes the CodeQL tools for scanning. - name: Initialize CodeQL - uses: github/codeql-action/init@v3 + uses: github/codeql-action/init@v1 with: languages: ${{ matrix.language }} # If you wish to specify custom queries, you can do so here or in a config file. @@ -54,7 +54,7 @@ jobs: # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). # If this step fails, then you should remove it and run the build manually (see below) - name: Autobuild - uses: github/codeql-action/autobuild@v3 + uses: github/codeql-action/autobuild@v1 # ℹī¸ Command-line programs to run using the OS shell. # 📚 https://git.io/JvXDl @@ -68,4 +68,4 @@ jobs: # make release - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v3 + uses: github/codeql-action/analyze@v1 diff --git a/chaoscenter/authentication/api/handlers/rest/user_handlers_test.go b/chaoscenter/authentication/api/handlers/rest/user_handlers_test.go index 157b101c3a3..e06e4b29645 100644 --- a/chaoscenter/authentication/api/handlers/rest/user_handlers_test.go +++ b/chaoscenter/authentication/api/handlers/rest/user_handlers_test.go @@ -4,7 +4,7 @@ import ( "bytes" "encoding/json" "errors" - "io" + "io/ioutil" "log" "net/http" "net/http/httptest" @@ -26,7 +26,7 @@ import ( // TestMain is the entry point for testing func TestMain(m *testing.M) { gin.SetMode(gin.TestMode) - log.SetOutput(io.Discard) + log.SetOutput(ioutil.Discard) os.Exit(m.Run()) } @@ -516,7 +516,7 @@ func TestResetPassword(t *testing.T) { c := GetTestGinContext(w) c.Request.Method = http.MethodPost bodyBytes, _ := json.Marshal(tt.inputBody) - c.Request.Body = io.NopCloser(bytes.NewReader(bodyBytes)) + c.Request.Body = ioutil.NopCloser(bytes.NewReader([]byte(bodyBytes))) c.Set("role", tt.mockRole) c.Set("uid", tt.mockUID) c.Set("username", tt.mockUsername) @@ -592,7 +592,7 @@ func TestUpdateUserState(t *testing.T) { c := GetTestGinContext(w) c.Request.Method = http.MethodPost bodyBytes, _ := json.Marshal(tc.inputBody) - c.Request.Body = io.NopCloser(bytes.NewReader([]byte(bodyBytes))) + c.Request.Body = ioutil.NopCloser(bytes.NewReader([]byte(bodyBytes))) c.Set("role", tc.mockRole) c.Set("uid", tc.mockUID) c.Set("username", tc.mockUsername) diff --git a/chaoscenter/graphql/server/pkg/chaos_infrastructure/infra_utils.go b/chaoscenter/graphql/server/pkg/chaos_infrastructure/infra_utils.go index 0a780186311..d28acc65ac9 100644 --- a/chaoscenter/graphql/server/pkg/chaos_infrastructure/infra_utils.go +++ b/chaoscenter/graphql/server/pkg/chaos_infrastructure/infra_utils.go @@ -2,8 +2,6 @@ package chaos_infrastructure import ( "fmt" - "os" - "strings" "github.com/ghodss/yaml" "github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model" @@ -11,8 +9,12 @@ import ( dbChaosInfra "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/chaos_infrastructure" "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/k8s" "github.com/litmuschaos/litmus/chaoscenter/graphql/server/utils" - log "github.com/sirupsen/logrus" + "github.com/sirupsen/logrus" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + + "io/ioutil" + "os" + "strings" ) type SubscriberConfigurations struct { @@ -63,7 +65,7 @@ func GetK8sInfraYaml(infra dbChaosInfra.ChaosInfra) ([]byte, error) { } else if infra.InfraScope == NamespaceScope { respData, err = ManifestParser(infra, "manifests/namespace", &config) } else { - log.Error("INFRA_SCOPE env is empty!") + logrus.Error("INFRA_SCOPE env is empty!") } if err != nil { return nil, err @@ -127,11 +129,7 @@ func ManifestParser(infra dbChaosInfra.ChaosInfra, rootPath string, config *Subs return nil, fmt.Errorf("failed to open the file %v", err) } - defer func() { - if err := file.Close(); err != nil { - log.Warnf("failed to close file: %v", err) - } - }() + defer file.Close() list, err := file.Readdirnames(0) // 0 to read all files and folders if err != nil { @@ -176,7 +174,7 @@ func ManifestParser(infra dbChaosInfra.ChaosInfra, rootPath string, config *Subs } for _, fileName := range list { - fileContent, err := os.ReadFile(rootPath + "/" + fileName) + fileContent, err := ioutil.ReadFile(rootPath + "/" + fileName) if err != nil { return nil, fmt.Errorf("failed to read the file %v", err) } diff --git a/chaoscenter/graphql/server/pkg/chaoshub/handler/handler.go b/chaoscenter/graphql/server/pkg/chaoshub/handler/handler.go index 63e027ce58d..a9af8cf6f97 100644 --- a/chaoscenter/graphql/server/pkg/chaoshub/handler/handler.go +++ b/chaoscenter/graphql/server/pkg/chaoshub/handler/handler.go @@ -5,9 +5,9 @@ import ( "encoding/json" "fmt" "io" + "io/ioutil" "net/http" "os" - "path" "path/filepath" "strconv" "strings" @@ -17,7 +17,9 @@ import ( chaoshubops "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaoshub/ops" "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/chaos_hub" "github.com/litmuschaos/litmus/chaoscenter/graphql/server/utils" + log "github.com/sirupsen/logrus" + "gopkg.in/yaml.v2" ) @@ -35,9 +37,9 @@ func GetChartsPath(chartsInput model.CloningInput, projectID string, isDefault b } // GetChartsData is used to get details of charts like experiments. -func GetChartsData(chartsPath string) ([]*model.Chart, error) { +func GetChartsData(ChartsPath string) ([]*model.Chart, error) { var allChartsDetails []ChaosChart - Charts, err := os.ReadDir(path.Clean(chartsPath)) + Charts, err := ioutil.ReadDir(ChartsPath) if err != nil { log.Error("file reading error", err) return nil, err @@ -46,7 +48,7 @@ func GetChartsData(chartsPath string) ([]*model.Chart, error) { if chart.Name() == "icons" { continue } - chartDetails, _ := ReadExperimentFile(chartsPath + chart.Name() + "/" + chart.Name() + ".chartserviceversion.yaml") + chartDetails, _ := ReadExperimentFile(ChartsPath + chart.Name() + "/" + chart.Name() + ".chartserviceversion.yaml") allChartsDetails = append(allChartsDetails, chartDetails) } @@ -75,17 +77,14 @@ func GetExperimentData(experimentFilePath string) (*model.Chart, error) { return nil, err } var chartData *model.Chart - err = json.Unmarshal(e, &chartData) - if err != nil { - return nil, err - } + json.Unmarshal(e, &chartData) return chartData, nil } // ReadExperimentFile is used for reading experiment file from given path -func ReadExperimentFile(givenPath string) (ChaosChart, error) { +func ReadExperimentFile(path string) (ChaosChart, error) { var experiment ChaosChart - experimentFile, err := os.ReadFile(path.Clean(givenPath)) + experimentFile, err := ioutil.ReadFile(path) if err != nil { return experiment, fmt.Errorf("file path of the, err: %+v", err) } @@ -98,7 +97,7 @@ func ReadExperimentFile(givenPath string) (ChaosChart, error) { // ReadExperimentYAMLFile is used for reading experiment/engine file from given path func ReadExperimentYAMLFile(path string) (string, error) { var s string - YAMLData, err := os.ReadFile(path) + YAMLData, err := ioutil.ReadFile(path) if err != nil { return s, fmt.Errorf("file path of the, err: %+v", err) } @@ -111,7 +110,7 @@ func ReadExperimentYAMLFile(path string) (string, error) { func ListPredefinedWorkflowDetails(name string, projectID string) ([]*model.PredefinedExperimentList, error) { experimentsPath := DefaultPath + projectID + "/" + name + "/workflows" var predefinedWorkflows []*model.PredefinedExperimentList - files, err := os.ReadDir(experimentsPath) + files, err := ioutil.ReadDir(experimentsPath) if err != nil { return nil, err } @@ -161,17 +160,13 @@ func DownloadRemoteHub(hubDetails model.CreateRemoteChaosHub, projectID string) return err } //create the destination directory where the hub will be downloaded - hubPath := dirPath + "/" + hubDetails.Name + ".zip" - destDir, err := os.Create(path.Clean(hubPath)) + hubpath := dirPath + "/" + hubDetails.Name + ".zip" + destDir, err := os.Create(hubpath) if err != nil { log.Error(err) return err } - defer func() { - if err := destDir.Close(); err != nil { - log.Warnf("failed to close dir: %v", err) - } - }() + defer destDir.Close() //download the zip file from the provided url download, err := http.Get(hubDetails.RepoURL) @@ -180,11 +175,7 @@ func DownloadRemoteHub(hubDetails model.CreateRemoteChaosHub, projectID string) return err } - defer func() { - if err := download.Body.Close(); err != nil { - log.Warnf("failed to close body: %v", err) - } - }() + defer download.Body.Close() if download.StatusCode != http.StatusOK { return fmt.Errorf("err: " + download.Status) @@ -198,14 +189,14 @@ func DownloadRemoteHub(hubDetails model.CreateRemoteChaosHub, projectID string) contentLength := download.Header.Get("content-length") length, err := strconv.Atoi(contentLength) if length > maxSize { - _ = os.Remove(path.Clean(hubPath)) + _ = os.Remove(hubpath) return fmt.Errorf("err: File size exceeded the threshold %d", length) } //validate the content-type contentType := download.Header.Get("content-type") if contentType != "application/zip" { - _ = os.Remove(path.Clean(hubPath)) + _ = os.Remove(hubpath) return fmt.Errorf("err: Invalid file type %s", contentType) } @@ -217,13 +208,13 @@ func DownloadRemoteHub(hubDetails model.CreateRemoteChaosHub, projectID string) } //unzip the ChaosHub to the default hub directory - err = UnzipRemoteHub(hubPath, projectID) + err = UnzipRemoteHub(hubpath, hubDetails, projectID) if err != nil { return err } //remove the redundant zip file - err = os.Remove(path.Clean(hubPath)) + err = os.Remove(hubpath) if err != nil { return err } @@ -231,24 +222,16 @@ func DownloadRemoteHub(hubDetails model.CreateRemoteChaosHub, projectID string) } // UnzipRemoteHub is used to unzip the zip file -func UnzipRemoteHub(zipPath string, projectID string) error { +func UnzipRemoteHub(zipPath string, hubDetails model.CreateRemoteChaosHub, projectID string) error { extractPath := DefaultPath + projectID zipReader, err := zip.OpenReader(zipPath) if err != nil { log.Error(err) return err } - defer func() { - if err := zipReader.Close(); err != nil { - log.Warnf("failed to close reader: %v", err) - } - }() - + defer zipReader.Close() for _, file := range zipReader.File { - err := CopyZipItems(file, extractPath, file.Name) - if err != nil { - return err - } + CopyZipItems(file, extractPath, file.Name) } return nil } @@ -277,18 +260,9 @@ func CopyZipItems(file *zip.File, extractPath string, chartsPath string) error { if err != nil { log.Error(err) } - defer func() { - if err := fileCopy.Close(); err != nil { - log.Warnf("failed to close file: %v", err) - } - }() - + fileCopy.Close() } - defer func() { - if err := fileReader.Close(); err != nil { - log.Warnf("failed to close file: %v", err) - } - }() + fileReader.Close() return nil } diff --git a/chaoscenter/graphql/server/pkg/chaoshub/handler/handler_test.go b/chaoscenter/graphql/server/pkg/chaoshub/handler/handler_test.go index ffcd2106ac3..54ad7b63a0a 100644 --- a/chaoscenter/graphql/server/pkg/chaoshub/handler/handler_test.go +++ b/chaoscenter/graphql/server/pkg/chaoshub/handler/handler_test.go @@ -1,16 +1,18 @@ package handler_test import ( - "io" + "github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model" + "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaoshub/handler" + chaosHubOps "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaoshub/ops" + "github.com/litmuschaos/litmus/chaoscenter/graphql/server/utils" + + "io/ioutil" "os" "testing" "github.com/gin-gonic/gin" "github.com/google/uuid" - "github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model" - "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaoshub/handler" - chaosHubOps "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaoshub/ops" - "github.com/litmuschaos/litmus/chaoscenter/graphql/server/utils" + log "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" ) @@ -18,7 +20,7 @@ import ( // TestMain is the entry point for testing func TestMain(m *testing.M) { gin.SetMode(gin.TestMode) - log.SetOutput(io.Discard) + log.SetOutput(ioutil.Discard) os.Exit(m.Run()) } diff --git a/chaoscenter/graphql/server/pkg/chaoshub/ops/gitops_test.go b/chaoscenter/graphql/server/pkg/chaoshub/ops/gitops_test.go index 3494fb17970..b4e34a85f75 100644 --- a/chaoscenter/graphql/server/pkg/chaoshub/ops/gitops_test.go +++ b/chaoscenter/graphql/server/pkg/chaoshub/ops/gitops_test.go @@ -2,7 +2,7 @@ package chaoshubops_test import ( "fmt" - "io" + "io/ioutil" "os" "testing" "time" @@ -25,7 +25,7 @@ var ( // TestMain is the entry point for testing func TestMain(m *testing.M) { gin.SetMode(gin.TestMode) - log.SetOutput(io.Discard) + log.SetOutput(ioutil.Discard) os.Exit(m.Run()) } diff --git a/chaoscenter/graphql/server/pkg/chaoshub/service.go b/chaoscenter/graphql/server/pkg/chaoshub/service.go index 5d0a78255e9..278b14dec41 100644 --- a/chaoscenter/graphql/server/pkg/chaoshub/service.go +++ b/chaoscenter/graphql/server/pkg/chaoshub/service.go @@ -4,13 +4,11 @@ import ( "context" "errors" "fmt" + "io/ioutil" "os" - "path" "strconv" "time" - "github.com/google/uuid" - "github.com/jinzhu/copier" "github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model" "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/authorization" "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaoshub/handler" @@ -18,9 +16,13 @@ import ( "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb" dbSchemaChaosHub "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/chaos_hub" "github.com/litmuschaos/litmus/chaoscenter/graphql/server/utils" + "go.mongodb.org/mongo-driver/mongo" + + "github.com/google/uuid" + "github.com/jinzhu/copier" + log "github.com/sirupsen/logrus" "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/mongo" ) const ( @@ -478,22 +480,22 @@ func (c *chaosHubService) GetChaosFault(ctx context.Context, request model.Exper } //Get fault chartserviceversion.yaml data - csvPath := path.Clean(basePath + "/" + request.ExperimentName + ".chartserviceversion.yaml") - csvYaml, err := os.ReadFile(csvPath) + csvPath := basePath + "/" + request.ExperimentName + ".chartserviceversion.yaml" + csvYaml, err := ioutil.ReadFile(csvPath) if err != nil { csvYaml = []byte("") } //Get engine.yaml data - enginePath := path.Clean(basePath + "/" + "engine.yaml") - engineYaml, err := os.ReadFile(enginePath) + enginePath := basePath + "/" + "engine.yaml" + engineYaml, err := ioutil.ReadFile(enginePath) if err != nil { engineYaml = []byte("") } //Get fault.yaml data - faultPath := path.Clean(basePath + "/" + "fault.yaml") - faultYaml, err := os.ReadFile(faultPath) + faultPath := basePath + "/" + "fault.yaml" + faultYaml, err := ioutil.ReadFile(faultPath) if err != nil { faultYaml = []byte("") } @@ -727,9 +729,8 @@ func (c *chaosHubService) ListPredefinedExperiments(ctx context.Context, hubID s } else { hubPath = DefaultPath + projectID + "/" + hub.Name + "/experiments/" } - hubPath = path.Clean(hubPath) var predefinedWorkflows []*model.PredefinedExperimentList - files, err := os.ReadDir(hubPath) + files, err := ioutil.ReadDir(hubPath) if err != nil { return nil, err } @@ -802,24 +803,24 @@ func (c *chaosHubService) getPredefinedExperimentDetails(experimentsPath string, var ( csvManifest = "" workflowManifest = "" - predefinedPath = experimentsPath + experiment + "/" + experiment + ".chartserviceversion.yaml" + path = experimentsPath + experiment + "/" + experiment + ".chartserviceversion.yaml" isExist = true preDefinedWorkflow = &model.PredefinedExperimentList{} ) - _, err := os.Stat(predefinedPath) + _, err := os.Stat(path) if err != nil { isExist = false } if isExist { - yamlData, err := os.ReadFile(path.Clean(experimentsPath + experiment + "/" + experiment + ".chartserviceversion.yaml")) + yamlData, err := ioutil.ReadFile(experimentsPath + experiment + "/" + experiment + ".chartserviceversion.yaml") if err != nil { csvManifest = "" } csvManifest = string(yamlData) - yamlData, err = os.ReadFile(path.Clean(experimentsPath + experiment + "/" + "experiment.yaml")) + yamlData, err = ioutil.ReadFile(experimentsPath + experiment + "/" + "experiment.yaml") if err != nil { workflowManifest = "" } diff --git a/chaoscenter/graphql/server/pkg/gitops/gitops.go b/chaoscenter/graphql/server/pkg/gitops/gitops.go index 0c4d6b6973b..925cb0577c8 100644 --- a/chaoscenter/graphql/server/pkg/gitops/gitops.go +++ b/chaoscenter/graphql/server/pkg/gitops/gitops.go @@ -6,12 +6,16 @@ import ( "errors" "fmt" "io" + "io/ioutil" "os" - "path" "strconv" "strings" "time" + "github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model" + "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/authorization" + "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/gitops" + "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/object" @@ -19,9 +23,6 @@ import ( "github.com/go-git/go-git/v5/plumbing/transport/http" "github.com/go-git/go-git/v5/plumbing/transport/ssh" "github.com/golang-jwt/jwt" - "github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model" - "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/authorization" - "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/gitops" log "github.com/sirupsen/logrus" ssh2 "golang.org/x/crypto/ssh" ) @@ -112,7 +113,7 @@ func (c GitConfig) setupGitRepo(user GitUser) error { gitInfo := map[string]string{"projectID": c.ProjectID, "revision": "1"} if exists { - data, err := os.ReadFile(path.Clean(projectPath + "/.info")) + data, err := ioutil.ReadFile(projectPath + "/.info") if err != nil { return errors.New("can't read existing git info file " + err.Error()) } @@ -136,7 +137,7 @@ func (c GitConfig) setupGitRepo(user GitUser) error { if err != nil { return err } - err = os.WriteFile(path.Clean(projectPath+"/.info"), data, 0644) + err = ioutil.WriteFile(projectPath+"/.info", data, 0644) if err != nil { return err } diff --git a/chaoscenter/graphql/server/pkg/gitops/service.go b/chaoscenter/graphql/server/pkg/gitops/service.go index 0f8cd1e108c..9fbb662fecd 100644 --- a/chaoscenter/graphql/server/pkg/gitops/service.go +++ b/chaoscenter/graphql/server/pkg/gitops/service.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "io/ioutil" "os" "path/filepath" "strconv" @@ -11,18 +12,19 @@ import ( "sync" "time" + chaosExperimentOps "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaos_experiment/ops" + "github.com/ghodss/yaml" "github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model" - chaosExperimentOps "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaos_experiment/ops" - chaosInfra "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaos_infrastructure" - dataStore "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/data-store" + chaos_infra "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaos_infrastructure" + data_store "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/data-store" store "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/data-store" "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/chaos_experiment" "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/chaos_infrastructure" "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/gitops" "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/grpc" + "github.com/sirupsen/logrus" - log "github.com/sirupsen/logrus" "github.com/tidwall/gjson" "github.com/tidwall/sjson" "go.mongodb.org/mongo-driver/bson" @@ -66,7 +68,7 @@ func NewGitOpsService(gitOpsOperator *gitops.Operator, chaosExperimentService ch } } -// GitOpsNotificationHandler sends experiment run request(single run experiment only) to agent on GitOps notification +// GitOpsNotificationHandler sends experiment run request(single run experiment only) to agent on gitops notification func (g *gitOpsService) GitOpsNotificationHandler(ctx context.Context, infra chaos_infrastructure.ChaosInfra, experimentID string) (string, error) { gitLock.Lock(infra.ProjectID, nil) defer gitLock.Unlock(infra.ProjectID, nil) @@ -75,7 +77,7 @@ func (g *gitOpsService) GitOpsNotificationHandler(ctx context.Context, infra cha return "", errors.New("Cannot get Git Config from DB : " + err.Error()) } if config == nil { - return "GitOps Disabled", nil + return "Gitops Disabled", nil } query := bson.D{{"infra_id", infra.InfraID}, {"experiment_id", experimentID}, {"is_removed", false}} experiments, err := g.chaosExperimentOps.GetExperiments(query) @@ -97,7 +99,7 @@ func (g *gitOpsService) GitOpsNotificationHandler(ctx context.Context, infra cha } username := "git-ops" - chaosInfra.SendExperimentToSubscriber(experiments[0].ProjectID, &model.ChaosExperimentRequest{ + chaos_infra.SendExperimentToSubscriber(experiments[0].ProjectID, &model.ChaosExperimentRequest{ ExperimentManifest: experiments[0].Revision[len(experiments[0].Revision)-1].ExperimentManifest, InfraID: experiments[0].InfraID, }, &username, nil, "create", store.Store) @@ -105,7 +107,7 @@ func (g *gitOpsService) GitOpsNotificationHandler(ctx context.Context, infra cha return "Request Acknowledged for experimentID: " + experimentID, nil } -// EnableGitOpsHandler enables GitOps for a particular project +// EnableGitOpsHandler enables gitops for a particular project func (g *gitOpsService) EnableGitOpsHandler(ctx context.Context, projectID string, config model.GitConfig) (bool, error) { gitLock.Lock(projectID, nil) defer gitLock.Unlock(projectID, nil) @@ -115,18 +117,14 @@ func (g *gitOpsService) EnableGitOpsHandler(ctx context.Context, projectID strin var conn *grpc2.ClientConn client, conn := grpc.GetAuthGRPCSvcClient(conn) - defer func() { - if err := conn.Close(); err != nil { - log.Warnf("failed to close connection: %v", err) - } - }() + defer conn.Close() _, err := grpc.GetProjectById(client, projectID) if err != nil { return false, errors.New("Failed to setup GitOps : " + err.Error()) } - logrus.Info("Enabling GitOps") + logrus.Info("Enabling Gitops") gitDB := gitops.GetGitConfigDB(projectID, config) commit, err := SetupGitOps(GitUserFromContext(ctx), GetGitOpsConfig(gitDB)) @@ -143,12 +141,12 @@ func (g *gitOpsService) EnableGitOpsHandler(ctx context.Context, projectID strin return true, nil } -// DisableGitOpsHandler disables GitOps for a specific project +// DisableGitOpsHandler disables gitops for a specific project func (g *gitOpsService) DisableGitOpsHandler(ctx context.Context, projectID string) (bool, error) { gitLock.Lock(projectID, nil) defer gitLock.Unlock(projectID, nil) - logrus.Info("Disabling GitOps") + logrus.Info("Disabling Gitops") err := g.gitOpsOperator.DeleteGitConfig(ctx, projectID) if err != nil { return false, errors.New("Failed to delete git config from DB : " + err.Error()) @@ -162,7 +160,7 @@ func (g *gitOpsService) DisableGitOpsHandler(ctx context.Context, projectID stri return true, nil } -// UpdateGitOpsDetailsHandler updates an exiting GitOps config for a project +// UpdateGitOpsDetailsHandler updates an exiting gitops config for a project func (g *gitOpsService) UpdateGitOpsDetailsHandler(ctx context.Context, projectID string, config model.GitConfig) (bool, error) { gitLock.Lock(projectID, nil) defer gitLock.Unlock(projectID, nil) @@ -178,7 +176,7 @@ func (g *gitOpsService) UpdateGitOpsDetailsHandler(ctx context.Context, projectI return false, errors.New("GitOps Disabled ") } - logrus.Info("Enabling GitOps") + logrus.Info("Enabling Gitops") gitDB := gitops.GetGitConfigDB(projectID, config) gitConfig := GetGitOpsConfig(gitDB) @@ -207,7 +205,7 @@ func (g *gitOpsService) UpdateGitOpsDetailsHandler(ctx context.Context, projectI return true, nil } -// GetGitOpsDetails returns the current GitOps config for the requested project +// GetGitOpsDetails returns the current gitops config for the requested project func (g *gitOpsService) GetGitOpsDetails(ctx context.Context, projectID string) (*model.GitConfigResponse, error) { gitLock.Lock(projectID, nil) defer gitLock.Unlock(projectID, nil) @@ -271,7 +269,7 @@ func (g *gitOpsService) UpsertExperimentToGit(ctx context.Context, projectID str return errors.New("Cannot convert manifest to yaml : " + err.Error()) } - err = os.WriteFile(experimentPath, data, 0644) + err = ioutil.WriteFile(experimentPath, data, 0644) if err != nil { return errors.New("Cannot write experiment to git : " + err.Error()) } @@ -470,7 +468,7 @@ func (g *gitOpsService) SyncDBToGit(ctx context.Context, config GitConfig) error continue } // read changes [new additions/updates] - data, err := os.ReadFile(config.LocalPath + "/" + file) + data, err := ioutil.ReadFile(config.LocalPath + "/" + file) if err != nil { logrus.Error("Error reading data from git file : " + file + " | " + err.Error()) continue @@ -575,7 +573,7 @@ func (g *gitOpsService) createExperiment(ctx context.Context, data, file string, return false, errors.New("Cannot convert manifest to yaml : " + err.Error()) } - err = os.WriteFile(experimentPath, yamlData, 0644) + err = ioutil.WriteFile(experimentPath, yamlData, 0644) if err != nil { return false, errors.New("Cannot write experiment to git : " + err.Error()) } @@ -626,10 +624,10 @@ func (g *gitOpsService) updateExperiment(ctx context.Context, data, wfID, file s if err != nil { return err } - return g.chaosExperimentService.ProcessExperimentUpdate(input, "git-ops", wfType, revID, updateRevision, config.ProjectID, dataStore.Store) + return g.chaosExperimentService.ProcessExperimentUpdate(input, "git-ops", wfType, revID, updateRevision, config.ProjectID, data_store.Store) } -// deleteExperiment helps in deleting an experiment from DB during the SyncDBToGit operation +// deleteExperiment helps in deleting a experiment from DB during the SyncDBToGit operation func (g *gitOpsService) deleteExperiment(file string, config GitConfig) error { _, fileName := filepath.Split(file) fileName = strings.Replace(fileName, ".yaml", "", -1) @@ -640,5 +638,5 @@ func (g *gitOpsService) deleteExperiment(file string, config GitConfig) error { return err } - return g.chaosExperimentService.ProcessExperimentDelete(query, experiment, "git-ops", dataStore.Store) + return g.chaosExperimentService.ProcessExperimentDelete(query, experiment, "git-ops", data_store.Store) } diff --git a/chaoscenter/subscriber/go.sum b/chaoscenter/subscriber/go.sum index ca3e2a24677..14102d88d20 100644 --- a/chaoscenter/subscriber/go.sum +++ b/chaoscenter/subscriber/go.sum @@ -238,7 +238,6 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= diff --git a/chaoscenter/subscriber/pkg/graphql/operations.go b/chaoscenter/subscriber/pkg/graphql/operations.go index b2ea6372454..510bdf546af 100644 --- a/chaoscenter/subscriber/pkg/graphql/operations.go +++ b/chaoscenter/subscriber/pkg/graphql/operations.go @@ -3,12 +3,10 @@ package graphql import ( "bytes" "encoding/json" - "io" + "io/ioutil" "net/http" "strconv" "strings" - - log "github.com/sirupsen/logrus" ) func (gql *subscriberGql) SendRequest(server string, payload []byte) (string, error) { @@ -22,13 +20,8 @@ func (gql *subscriberGql) SendRequest(server string, payload []byte) (string, er return "", err } - body, err := io.ReadAll(resp.Body) - defer func() { - if err := resp.Body.Close(); err != nil { - log.Warnf("failed to close body: %v", err) - } - }() - + body, err := ioutil.ReadAll(resp.Body) + resp.Body.Close() if err != nil { return "", err }