Replace deprecated package (#4559)

* upgrade: upgrade codeql version

Signed-off-by: namkyu1999 <lak9348@konkuk.ac.kr>

* feat: replace deprecated pkg in auth

Signed-off-by: namkyu1999 <lak9348@konkuk.ac.kr>

* feat: replace unused pkg to subscriber

Signed-off-by: namkyu1999 <lak9348@konkuk.ac.kr>

* fix: replace unused pkg to graphql

Signed-off-by: namkyu1999 <lak9348@konkuk.ac.kr>

* fix: resolve conflict

Signed-off-by: namkyu1999 <lak9348@konkuk.ac.kr>

---------

Signed-off-by: namkyu1999 <lak9348@konkuk.ac.kr>
Co-authored-by: Saranya Jena <saranya.jena@harness.io>
This commit is contained in:
Namkyu Park 2024-05-13 03:12:51 +09:00 committed by GitHub
parent 53c1165144
commit 87f06850ab
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 160 additions and 202 deletions

View File

@ -43,7 +43,7 @@ jobs:
# Initializes the CodeQL tools for scanning. # Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL - name: Initialize CodeQL
uses: github/codeql-action/init@v1 uses: github/codeql-action/init@v3
with: with:
languages: ${{ matrix.language }} languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file. # 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). # 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) # If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild - name: Autobuild
uses: github/codeql-action/autobuild@v1 uses: github/codeql-action/autobuild@v3
# Command-line programs to run using the OS shell. # Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl # 📚 https://git.io/JvXDl
@ -68,4 +68,4 @@ jobs:
# make release # make release
- name: Perform CodeQL Analysis - name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1 uses: github/codeql-action/analyze@v3

View File

@ -4,7 +4,7 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"errors" "errors"
"io/ioutil" "io"
"log" "log"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
@ -26,7 +26,7 @@ import (
// TestMain is the entry point for testing // TestMain is the entry point for testing
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
gin.SetMode(gin.TestMode) gin.SetMode(gin.TestMode)
log.SetOutput(ioutil.Discard) log.SetOutput(io.Discard)
os.Exit(m.Run()) os.Exit(m.Run())
} }
@ -519,7 +519,7 @@ func TestResetPassword(t *testing.T) {
c := GetTestGinContext(w) c := GetTestGinContext(w)
c.Request.Method = http.MethodPost c.Request.Method = http.MethodPost
bodyBytes, _ := json.Marshal(tt.inputBody) bodyBytes, _ := json.Marshal(tt.inputBody)
c.Request.Body = ioutil.NopCloser(bytes.NewReader([]byte(bodyBytes))) c.Request.Body = io.NopCloser(bytes.NewReader([]byte(bodyBytes)))
c.Set("role", tt.mockRole) c.Set("role", tt.mockRole)
c.Set("uid", tt.mockUID) c.Set("uid", tt.mockUID)
c.Set("username", tt.mockUsername) c.Set("username", tt.mockUsername)
@ -595,7 +595,7 @@ func TestUpdateUserState(t *testing.T) {
c := GetTestGinContext(w) c := GetTestGinContext(w)
c.Request.Method = http.MethodPost c.Request.Method = http.MethodPost
bodyBytes, _ := json.Marshal(tc.inputBody) bodyBytes, _ := json.Marshal(tc.inputBody)
c.Request.Body = ioutil.NopCloser(bytes.NewReader([]byte(bodyBytes))) c.Request.Body = io.NopCloser(bytes.NewReader([]byte(bodyBytes)))
c.Set("role", tc.mockRole) c.Set("role", tc.mockRole)
c.Set("uid", tc.mockUID) c.Set("uid", tc.mockUID)
c.Set("username", tc.mockUsername) c.Set("username", tc.mockUsername)

View File

@ -2,6 +2,8 @@ package chaos_infrastructure
import ( import (
"fmt" "fmt"
"os"
"strings"
"github.com/ghodss/yaml" "github.com/ghodss/yaml"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model" "github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model"
@ -9,12 +11,8 @@ import (
dbChaosInfra "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/chaos_infrastructure" 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/pkg/k8s"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/utils" "github.com/litmuschaos/litmus/chaoscenter/graphql/server/utils"
"github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"io/ioutil"
"os"
"strings"
) )
type SubscriberConfigurations struct { type SubscriberConfigurations struct {
@ -65,7 +63,7 @@ func GetK8sInfraYaml(infra dbChaosInfra.ChaosInfra) ([]byte, error) {
} else if infra.InfraScope == NamespaceScope { } else if infra.InfraScope == NamespaceScope {
respData, err = ManifestParser(infra, "manifests/namespace", &config) respData, err = ManifestParser(infra, "manifests/namespace", &config)
} else { } else {
logrus.Error("INFRA_SCOPE env is empty!") log.Error("INFRA_SCOPE env is empty!")
} }
if err != nil { if err != nil {
return nil, err return nil, err
@ -129,14 +127,19 @@ func ManifestParser(infra dbChaosInfra.ChaosInfra, rootPath string, config *Subs
return nil, fmt.Errorf("failed to open the file %v", err) return nil, fmt.Errorf("failed to open the file %v", err)
} }
defer file.Close() defer func(file *os.File) {
err := file.Close()
if err != nil {
log.Errorf("failed to close the file %v", err)
}
}(file)
list, err := file.Readdirnames(0) // 0 to read all files and folders list, err := file.Readdirnames(0) // 0 to read all files and folders
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to read the file %v", err) return nil, fmt.Errorf("failed to read the file %v", err)
} }
var nodeselector string var nodeSelector string
if infra.NodeSelector != nil { if infra.NodeSelector != nil {
selector := strings.Split(*infra.NodeSelector, ",") selector := strings.Split(*infra.NodeSelector, ",")
selectorList := make(map[string]string) selectorList := make(map[string]string)
@ -145,18 +148,17 @@ func ManifestParser(infra dbChaosInfra.ChaosInfra, rootPath string, config *Subs
selectorList[kv[0]] = kv[1] selectorList[kv[0]] = kv[1]
} }
nodeSelector := struct { byt, err := yaml.Marshal(
NodeSelector map[string]string `yaml:"nodeSelector" json:"nodeSelector"` struct {
}{ NodeSelector map[string]string `yaml:"nodeSelector" json:"nodeSelector"`
NodeSelector: selectorList, }{
} NodeSelector: selectorList,
})
byt, err := yaml.Marshal(nodeSelector)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to marshal the node selector %v", err) return nil, fmt.Errorf("failed to marshal the node selector %v", err)
} }
nodeselector = string(utils.AddRootIndent(byt, 6)) nodeSelector = string(utils.AddRootIndent(byt, 6))
} }
var tolerations string var tolerations string
@ -174,7 +176,7 @@ func ManifestParser(infra dbChaosInfra.ChaosInfra, rootPath string, config *Subs
} }
for _, fileName := range list { for _, fileName := range list {
fileContent, err := ioutil.ReadFile(rootPath + "/" + fileName) fileContent, err := os.ReadFile(rootPath + "/" + fileName)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to read the file %v", err) return nil, fmt.Errorf("failed to read the file %v", err)
} }
@ -209,7 +211,7 @@ func ManifestParser(infra dbChaosInfra.ChaosInfra, rootPath string, config *Subs
} }
if infra.NodeSelector != nil { if infra.NodeSelector != nil {
newContent = strings.Replace(newContent, "#{NODE_SELECTOR}", nodeselector, -1) newContent = strings.Replace(newContent, "#{NODE_SELECTOR}", nodeSelector, -1)
} }
generatedYAML = append(generatedYAML, newContent) generatedYAML = append(generatedYAML, newContent)
} }
@ -250,7 +252,8 @@ func SendExperimentToSubscriber(projectID string, workflow *model.ChaosExperimen
var workflowObj unstructured.Unstructured var workflowObj unstructured.Unstructured
err := yaml.Unmarshal([]byte(workflow.ExperimentManifest), &workflowObj) err := yaml.Unmarshal([]byte(workflow.ExperimentManifest), &workflowObj)
if err != nil { if err != nil {
fmt.Errorf("error while parsing experiment manifest %v", err) log.Errorf("error while parsing experiment manifest %v", err)
return
} }
SendRequestToSubscriber(SubscriberRequests{ SendRequestToSubscriber(SubscriberRequests{

View File

@ -5,7 +5,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
@ -19,9 +18,7 @@ import (
chaoshubops "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaoshub/ops" 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/pkg/database/mongodb/chaos_hub"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/utils" "github.com/litmuschaos/litmus/chaoscenter/graphql/server/utils"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
) )
@ -39,9 +36,9 @@ func GetChartsPath(chartsInput model.CloningInput, projectID string, isDefault b
} }
// GetChartsData is used to get details of charts like experiments. // 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 var allChartsDetails []ChaosChart
Charts, err := ioutil.ReadDir(ChartsPath) Charts, err := os.ReadDir(chartsPath)
if err != nil { if err != nil {
log.Error("file reading error", err) log.Error("file reading error", err)
return nil, err return nil, err
@ -50,7 +47,7 @@ func GetChartsData(ChartsPath string) ([]*model.Chart, error) {
if chart.Name() == "icons" { if chart.Name() == "icons" {
continue continue
} }
chartDetails, _ := ReadExperimentFile(ChartsPath + chart.Name() + "/" + chart.Name() + ".chartserviceversion.yaml") chartDetails, _ := ReadExperimentFile(chartsPath + chart.Name() + "/" + chart.Name() + ".chartserviceversion.yaml")
allChartsDetails = append(allChartsDetails, chartDetails) allChartsDetails = append(allChartsDetails, chartDetails)
} }
@ -79,14 +76,16 @@ func GetExperimentData(experimentFilePath string) (*model.Chart, error) {
return nil, err return nil, err
} }
var chartData *model.Chart var chartData *model.Chart
json.Unmarshal(e, &chartData) if err = json.Unmarshal(e, &chartData); err != nil {
return nil, err
}
return chartData, nil return chartData, nil
} }
// ReadExperimentFile is used for reading experiment file from given path // ReadExperimentFile is used for reading experiment file from given path
func ReadExperimentFile(path string) (ChaosChart, error) { func ReadExperimentFile(path string) (ChaosChart, error) {
var experiment ChaosChart var experiment ChaosChart
experimentFile, err := ioutil.ReadFile(path) experimentFile, err := os.ReadFile(path)
if err != nil { if err != nil {
return experiment, fmt.Errorf("file path of the, err: %+v", err) return experiment, fmt.Errorf("file path of the, err: %+v", err)
} }
@ -99,7 +98,7 @@ func ReadExperimentFile(path string) (ChaosChart, error) {
// ReadExperimentYAMLFile is used for reading experiment/engine file from given path // ReadExperimentYAMLFile is used for reading experiment/engine file from given path
func ReadExperimentYAMLFile(path string) (string, error) { func ReadExperimentYAMLFile(path string) (string, error) {
var s string var s string
YAMLData, err := ioutil.ReadFile(path) YAMLData, err := os.ReadFile(path)
if err != nil { if err != nil {
return s, fmt.Errorf("file path of the, err: %+v", err) return s, fmt.Errorf("file path of the, err: %+v", err)
} }
@ -112,7 +111,7 @@ func ReadExperimentYAMLFile(path string) (string, error) {
func ListPredefinedWorkflowDetails(name string, projectID string) ([]*model.PredefinedExperimentList, error) { func ListPredefinedWorkflowDetails(name string, projectID string) ([]*model.PredefinedExperimentList, error) {
experimentsPath := DefaultPath + projectID + "/" + name + "/workflows" experimentsPath := DefaultPath + projectID + "/" + name + "/workflows"
var predefinedWorkflows []*model.PredefinedExperimentList var predefinedWorkflows []*model.PredefinedExperimentList
files, err := ioutil.ReadDir(experimentsPath) files, err := os.ReadDir(experimentsPath)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -162,8 +161,8 @@ func DownloadRemoteHub(hubDetails model.CreateRemoteChaosHub, projectID string)
return err return err
} }
//create the destination directory where the hub will be downloaded //create the destination directory where the hub will be downloaded
hubpath := dirPath + "/" + hubDetails.Name + ".zip" hubPath := dirPath + "/" + hubDetails.Name + ".zip"
destDir, err := os.Create(hubpath) destDir, err := os.Create(hubPath)
if err != nil { if err != nil {
log.Error(err) log.Error(err)
return err return err
@ -191,14 +190,14 @@ func DownloadRemoteHub(hubDetails model.CreateRemoteChaosHub, projectID string)
contentLength := download.Header.Get("content-length") contentLength := download.Header.Get("content-length")
length, err := strconv.Atoi(contentLength) length, err := strconv.Atoi(contentLength)
if length > maxSize { if length > maxSize {
_ = os.Remove(hubpath) _ = os.Remove(hubPath)
return fmt.Errorf("err: File size exceeded the threshold %d", length) return fmt.Errorf("err: File size exceeded the threshold %d", length)
} }
//validate the content-type //validate the content-type
contentType := download.Header.Get("content-type") contentType := download.Header.Get("content-type")
if contentType != "application/zip" { if contentType != "application/zip" {
_ = os.Remove(hubpath) _ = os.Remove(hubPath)
return fmt.Errorf("err: Invalid file type %s", contentType) return fmt.Errorf("err: Invalid file type %s", contentType)
} }
@ -210,13 +209,13 @@ func DownloadRemoteHub(hubDetails model.CreateRemoteChaosHub, projectID string)
} }
//unzip the ChaosHub to the default hub directory //unzip the ChaosHub to the default hub directory
err = UnzipRemoteHub(hubpath, hubDetails, projectID) err = UnzipRemoteHub(hubPath, projectID)
if err != nil { if err != nil {
return err return err
} }
//remove the redundant zip file //remove the redundant zip file
err = os.Remove(hubpath) err = os.Remove(hubPath)
if err != nil { if err != nil {
return err return err
} }
@ -224,16 +223,24 @@ func DownloadRemoteHub(hubDetails model.CreateRemoteChaosHub, projectID string)
} }
// UnzipRemoteHub is used to unzip the zip file // UnzipRemoteHub is used to unzip the zip file
func UnzipRemoteHub(zipPath string, hubDetails model.CreateRemoteChaosHub, projectID string) error { func UnzipRemoteHub(zipPath string, projectID string) error {
extractPath := DefaultPath + projectID extractPath := DefaultPath + projectID
zipReader, err := zip.OpenReader(zipPath) zipReader, err := zip.OpenReader(zipPath)
if err != nil { if err != nil {
log.Error(err) log.Error(err)
return err return err
} }
defer zipReader.Close() defer func(zipReader *zip.ReadCloser) {
err := zipReader.Close()
if err != nil {
log.Error(err)
}
}(zipReader)
for _, file := range zipReader.File { for _, file := range zipReader.File {
CopyZipItems(file, extractPath, file.Name) err := CopyZipItems(file, extractPath, file.Name)
if err != nil {
return err
}
} }
return nil return nil
} }
@ -365,7 +372,12 @@ func DefaultChaosHubIconHandler() gin.HandlerFunc {
} }
} }
defer img.Close() defer func(img *os.File) {
err := img.Close()
if err != nil {
log.WithError(err).Error("error while closing the file")
}
}(img)
c.Writer.Header().Set("Content-Type", "image/png") c.Writer.Header().Set("Content-Type", "image/png")
c.Writer.WriteHeader(responseStatusCode) c.Writer.WriteHeader(responseStatusCode)

View File

@ -1,18 +1,16 @@
package handler_test package handler_test
import ( import (
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model" "io"
"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" "os"
"testing" "testing"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/google/uuid" "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" log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -20,7 +18,7 @@ import (
// TestMain is the entry point for testing // TestMain is the entry point for testing
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
gin.SetMode(gin.TestMode) gin.SetMode(gin.TestMode)
log.SetOutput(ioutil.Discard) log.SetOutput(io.Discard)
os.Exit(m.Run()) os.Exit(m.Run())
} }

View File

@ -2,18 +2,17 @@ package chaoshubops_test
import ( import (
"fmt" "fmt"
"io/ioutil" "io"
"os" "os"
"testing" "testing"
"time" "time"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model"
chaosHubOps "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaoshub/ops"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object" "github.com/go-git/go-git/v5/plumbing/object"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model"
chaosHubOps "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaoshub/ops"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -25,7 +24,7 @@ var (
// TestMain is the entry point for testing // TestMain is the entry point for testing
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
gin.SetMode(gin.TestMode) gin.SetMode(gin.TestMode)
log.SetOutput(ioutil.Discard) log.SetOutput(io.Discard)
os.Exit(m.Run()) os.Exit(m.Run())
} }

View File

@ -4,11 +4,12 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"strconv" "strconv"
"time" "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/graph/model"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/authorization" "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/authorization"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaoshub/handler" "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaoshub/handler"
@ -16,13 +17,9 @@ import (
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb" "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb"
dbSchemaChaosHub "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/chaos_hub" dbSchemaChaosHub "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/chaos_hub"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/utils" "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" log "github.com/sirupsen/logrus"
"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
) )
const ( const (
@ -67,8 +64,8 @@ func NewService(chaosHubOperator *dbSchemaChaosHub.Operator) Service {
func (c *chaosHubService) AddChaosHub(ctx context.Context, chaosHub model.CreateChaosHubRequest, projectID string) (*model.ChaosHub, error) { func (c *chaosHubService) AddChaosHub(ctx context.Context, chaosHub model.CreateChaosHubRequest, projectID string) (*model.ChaosHub, error) {
if IsExist, err := c.IsChaosHubAvailable(ctx, chaosHub.Name, projectID); err != nil { if IsExist, err := c.IsChaosHubAvailable(ctx, chaosHub.Name, projectID); err != nil {
return nil, err return nil, err
} else if IsExist { } else if IsExist == true {
return nil, errors.New("Name Already exists") return nil, errors.New("name already exists")
} }
currentTime := time.Now() currentTime := time.Now()
cloneHub := NewCloningInputFrom(chaosHub) cloneHub := NewCloningInputFrom(chaosHub)
@ -123,7 +120,7 @@ func (c *chaosHubService) AddChaosHub(ctx context.Context, chaosHub model.Create
return nil, err return nil, err
} }
// Cloning the repository at a path from chaoshub link structure. // Cloning the repository at a path from ChaosHub link structure.
if err := chaosHubOps.GitClone(cloneHub, projectID); err != nil { if err := chaosHubOps.GitClone(cloneHub, projectID); err != nil {
log.Error(err) log.Error(err)
} }
@ -136,8 +133,8 @@ func (c *chaosHubService) AddRemoteChaosHub(ctx context.Context, chaosHub model.
if err != nil { if err != nil {
return nil, err return nil, err
} }
if IsExist { if IsExist == true {
return nil, errors.New("Name Already exists") return nil, errors.New("name already exists")
} }
description := "" description := ""
if chaosHub.Description != nil { if chaosHub.Description != nil {
@ -205,8 +202,8 @@ func (c *chaosHubService) SaveChaosHub(ctx context.Context, chaosHub model.Creat
if err != nil { if err != nil {
return nil, err return nil, err
} }
if IsExist { if IsExist == true {
return nil, errors.New("Name Already exists") return nil, errors.New("name already exists")
} }
// Initialize a UID for new Hub. // Initialize a UID for new Hub.
@ -345,7 +342,7 @@ func (c *chaosHubService) UpdateChaosHub(ctx context.Context, chaosHub model.Upd
} }
} }
} else { } else {
// Syncing/Cloning the repository at a path from chaoshub link structure. // Syncing/Cloning the repository at a path from ChaosHub link structure.
if prevChaosHub.Name != chaosHub.Name || prevChaosHub.RepoURL != chaosHub.RepoURL || prevChaosHub.RepoBranch != chaosHub.RepoBranch || prevChaosHub.IsPrivate != chaosHub.IsPrivate || prevChaosHub.AuthType != chaosHub.AuthType.String() { if prevChaosHub.Name != chaosHub.Name || prevChaosHub.RepoURL != chaosHub.RepoURL || prevChaosHub.RepoBranch != chaosHub.RepoBranch || prevChaosHub.IsPrivate != chaosHub.IsPrivate || prevChaosHub.AuthType != chaosHub.AuthType.String() {
err = os.RemoveAll(clonePath) err = os.RemoveAll(clonePath)
if err != nil { if err != nil {
@ -481,21 +478,21 @@ func (c *chaosHubService) GetChaosFault(ctx context.Context, request model.Exper
//Get fault chartserviceversion.yaml data //Get fault chartserviceversion.yaml data
csvPath := basePath + "/" + request.ExperimentName + ".chartserviceversion.yaml" csvPath := basePath + "/" + request.ExperimentName + ".chartserviceversion.yaml"
csvYaml, err := ioutil.ReadFile(csvPath) csvYaml, err := os.ReadFile(csvPath)
if err != nil { if err != nil {
csvYaml = []byte("") csvYaml = []byte("")
} }
//Get engine.yaml data //Get engine.yaml data
enginePath := basePath + "/" + "engine.yaml" enginePath := basePath + "/" + "engine.yaml"
engineYaml, err := ioutil.ReadFile(enginePath) engineYaml, err := os.ReadFile(enginePath)
if err != nil { if err != nil {
engineYaml = []byte("") engineYaml = []byte("")
} }
//Get fault.yaml data //Get fault.yaml data
faultPath := basePath + "/" + "fault.yaml" faultPath := basePath + "/" + "fault.yaml"
faultYaml, err := ioutil.ReadFile(faultPath) faultYaml, err := os.ReadFile(faultPath)
if err != nil { if err != nil {
faultYaml = []byte("") faultYaml = []byte("")
} }
@ -507,7 +504,7 @@ func (c *chaosHubService) GetChaosFault(ctx context.Context, request model.Exper
}, nil }, nil
} }
// ListChaosHubs returns the array of hubdetails with their current status. // ListChaosHubs returns the array of hub details with their current status.
func (c *chaosHubService) ListChaosHubs(ctx context.Context, projectID string, request *model.ListChaosHubRequest) ([]*model.ChaosHubStatus, error) { func (c *chaosHubService) ListChaosHubs(ctx context.Context, projectID string, request *model.ListChaosHubRequest) ([]*model.ChaosHubStatus, error) {
defaultHub := c.listDefaultHubs() defaultHub := c.listDefaultHubs()
updatedDefaultHub := dbSchemaChaosHub.ChaosHub{ updatedDefaultHub := dbSchemaChaosHub.ChaosHub{
@ -730,7 +727,7 @@ func (c *chaosHubService) ListPredefinedExperiments(ctx context.Context, hubID s
hubPath = DefaultPath + projectID + "/" + hub.Name + "/experiments/" hubPath = DefaultPath + projectID + "/" + hub.Name + "/experiments/"
} }
var predefinedWorkflows []*model.PredefinedExperimentList var predefinedWorkflows []*model.PredefinedExperimentList
files, err := ioutil.ReadDir(hubPath) files, err := os.ReadDir(hubPath)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -813,14 +810,14 @@ func (c *chaosHubService) getPredefinedExperimentDetails(experimentsPath string,
} }
if isExist { if isExist {
yamlData, err := ioutil.ReadFile(experimentsPath + experiment + "/" + experiment + ".chartserviceversion.yaml") yamlData, err := os.ReadFile(experimentsPath + experiment + "/" + experiment + ".chartserviceversion.yaml")
if err != nil { if err != nil {
csvManifest = "" csvManifest = ""
} }
csvManifest = string(yamlData) csvManifest = string(yamlData)
yamlData, err = ioutil.ReadFile(experimentsPath + experiment + "/" + "experiment.yaml") yamlData, err = os.ReadFile(experimentsPath + experiment + "/" + "experiment.yaml")
if err != nil { if err != nil {
workflowManifest = "" workflowManifest = ""
} }
@ -837,64 +834,6 @@ func (c *chaosHubService) getPredefinedExperimentDetails(experimentsPath string,
return preDefinedWorkflow return preDefinedWorkflow
} }
// GetExperimentManifestDetails is used to send the ChaosEngine and ChaosExperiment YAMLs
//func (c *chaosHubService) GetExperimentManifestDetails(ctx context.Context, request model.ExperimentRequest, projectID string) (*model.ExperimentDetails, error) {
//
// engineType := model.FileTypeEngine
// experimentType := model.FileTypeExperiment
//
// engineData, err := c.GetYAMLData(model.ExperimentRequest{
// ChartName: request.ChartName,
// ExperimentName: request.ExperimentName,
// Name: request.Name,
// FileType: (*string)(&engineType),
// }, projectID)
// if err != nil {
// engineData = ""
// }
// experimentData, err := c.GetYAMLData(model.ExperimentRequest{
// ChartName: request.ChartName,
// ExperimentName: request.ExperimentName,
// Name: request.Name,
// FileType: (*string)(&experimentType),
// }, projectID)
// if err != nil {
// experimentData = ""
// }
// experimentDetails := &model.ExperimentDetails{
// EngineDetails: engineData,
// ExperimentDetails: experimentData,
// }
// return experimentDetails, nil
//}
//func (c *chaosHubService) ListPredefinedWorkflows(name string, projectID string) ([]*model.PredefinedWorkflowList, error) {
// workflowsList, err := handler.ListPredefinedWorkflowDetails(name, projectID)
// if err != nil {
// return nil, err
// }
// return workflowsList, nil
//}
// GetPredefinedExperimentYAMLData is responsible for sending the workflow.yaml for a given pre-defined workflow.
//func (c *chaosHubService) GetPredefinedExperimentYAMLData(request model.ExperimentRequest, projectID string) (string, error) {
// var YAMLPath string
// if request.FileType == nil {
// return "", errors.New("provide a valid filetype")
// }
// if strings.ToLower(*request.FileType) != "workflow" {
// return "", errors.New("invalid file type")
// }
// if strings.ToLower(request.ChartName) == "predefined" && strings.ToLower(*request.FileType) == "workflow" {
// YAMLPath = handler.GetPredefinedExperimentManifest(request, projectID)
// }
// YAMLData, err := handler.ReadExperimentYAMLFile(YAMLPath)
// if err != nil {
// return "", err
// }
// return YAMLData, nil
//}
// IsChaosHubAvailable is used for checking if hub already exist or not // IsChaosHubAvailable is used for checking if hub already exist or not
func (c *chaosHubService) IsChaosHubAvailable(ctx context.Context, name string, projectID string) (bool, error) { func (c *chaosHubService) IsChaosHubAvailable(ctx context.Context, name string, projectID string) (bool, error) {
chaosHubs, err := c.chaosHubOperator.GetChaosHubByProjectID(ctx, projectID) chaosHubs, err := c.chaosHubOperator.GetChaosHubByProjectID(ctx, projectID)

View File

@ -6,16 +6,11 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"os" "os"
"strconv" "strconv"
"strings" "strings"
"time" "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"
"github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object" "github.com/go-git/go-git/v5/plumbing/object"
@ -23,6 +18,9 @@ import (
"github.com/go-git/go-git/v5/plumbing/transport/http" "github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/go-git/go-git/v5/plumbing/transport/ssh" "github.com/go-git/go-git/v5/plumbing/transport/ssh"
"github.com/golang-jwt/jwt" "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" log "github.com/sirupsen/logrus"
ssh2 "golang.org/x/crypto/ssh" ssh2 "golang.org/x/crypto/ssh"
) )
@ -87,7 +85,7 @@ func GetGitOpsConfig(repoData gitops.GitConfigDB) GitConfig {
LatestCommit: repoData.LatestCommit, LatestCommit: repoData.LatestCommit,
UserName: repoData.UserName, UserName: repoData.UserName,
Password: repoData.Password, Password: repoData.Password,
AuthType: model.AuthType(repoData.AuthType), AuthType: repoData.AuthType,
Token: repoData.Token, Token: repoData.Token,
SSHPrivateKey: repoData.SSHPrivateKey, SSHPrivateKey: repoData.SSHPrivateKey,
} }
@ -95,7 +93,7 @@ func GetGitOpsConfig(repoData gitops.GitConfigDB) GitConfig {
return gitConfig return gitConfig
} }
// setupGitRepo helps clones and sets up the repo for gitops // setupGitRepo helps clones and sets up the repo for GitOps
func (c GitConfig) setupGitRepo(user GitUser) error { func (c GitConfig) setupGitRepo(user GitUser) error {
projectPath := c.LocalPath + "/" + ProjectDataPath + "/" + c.ProjectID projectPath := c.LocalPath + "/" + ProjectDataPath + "/" + c.ProjectID
@ -113,7 +111,7 @@ func (c GitConfig) setupGitRepo(user GitUser) error {
gitInfo := map[string]string{"projectID": c.ProjectID, "revision": "1"} gitInfo := map[string]string{"projectID": c.ProjectID, "revision": "1"}
if exists { if exists {
data, err := ioutil.ReadFile(projectPath + "/.info") data, err := os.ReadFile(projectPath + "/.info")
if err != nil { if err != nil {
return errors.New("can't read existing git info file " + err.Error()) return errors.New("can't read existing git info file " + err.Error())
} }
@ -137,7 +135,7 @@ func (c GitConfig) setupGitRepo(user GitUser) error {
if err != nil { if err != nil {
return err return err
} }
err = ioutil.WriteFile(projectPath+"/.info", data, 0644) err = os.WriteFile(projectPath+"/.info", data, 0644)
if err != nil { if err != nil {
return err return err
} }
@ -204,7 +202,7 @@ func (c GitConfig) getAuthMethod() (transport.AuthMethod, error) {
} }
} }
// UnsafeGitPull executes git pull after a hard reset when uncommited changes are present in repo. Not safe. // UnsafeGitPull executes git pull after a hard reset when uncommitted changes are present in repo. Not safe.
func (c GitConfig) UnsafeGitPull() error { func (c GitConfig) UnsafeGitPull() error {
cleanStatus, err := c.GitGetStatus() cleanStatus, err := c.GitGetStatus()
if err != nil { if err != nil {
@ -282,7 +280,7 @@ func (c GitConfig) GitPull() error {
ReferenceName: plumbing.NewBranchReferenceName(c.Branch), ReferenceName: plumbing.NewBranchReferenceName(c.Branch),
SingleBranch: true, SingleBranch: true,
}) })
if err != nil && err != git.NoErrAlreadyUpToDate { if err != nil && !errors.Is(err, git.NoErrAlreadyUpToDate) {
return err return err
} }
return nil return nil
@ -325,7 +323,7 @@ func (c GitConfig) GitPush() error {
Auth: auth, Auth: auth,
Progress: nil, Progress: nil,
}) })
if err == git.NoErrAlreadyUpToDate { if errors.Is(err, git.NoErrAlreadyUpToDate) {
return nil return nil
} }
return err return err
@ -445,7 +443,7 @@ func (c GitConfig) GetLatestCommitHash() (string, error) {
return commit.Hash.String(), nil return commit.Hash.String(), nil
} }
// SetupGitOps clones and sets up the repo for gitops and returns the LatestCommit // SetupGitOps clones and sets up the repo for git ops and returns the LatestCommit
func SetupGitOps(user GitUser, gitConfig GitConfig) (string, error) { func SetupGitOps(user GitUser, gitConfig GitConfig) (string, error) {
err := gitConfig.setupGitRepo(user) err := gitConfig.setupGitRepo(user)
if err != nil { if err != nil {

View File

@ -4,7 +4,6 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
@ -12,19 +11,17 @@ import (
"sync" "sync"
"time" "time"
chaosExperimentOps "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaos_experiment/ops"
"github.com/ghodss/yaml" "github.com/ghodss/yaml"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model" "github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model"
chaos_infra "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaos_infrastructure" chaosExperimentOps "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaos_experiment/ops"
data_store "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/data-store" chaosInfra "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaos_infrastructure"
dataStore "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/data-store"
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_experiment"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/chaos_infrastructure" "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/database/mongodb/gitops"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/grpc" "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/grpc"
log "github.com/sirupsen/logrus"
"github.com/sirupsen/logrus"
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
"github.com/tidwall/sjson" "github.com/tidwall/sjson"
"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson"
@ -68,7 +65,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) { func (g *gitOpsService) GitOpsNotificationHandler(ctx context.Context, infra chaos_infrastructure.ChaosInfra, experimentID string) (string, error) {
gitLock.Lock(infra.ProjectID, nil) gitLock.Lock(infra.ProjectID, nil)
defer gitLock.Unlock(infra.ProjectID, nil) defer gitLock.Unlock(infra.ProjectID, nil)
@ -77,12 +74,12 @@ func (g *gitOpsService) GitOpsNotificationHandler(ctx context.Context, infra cha
return "", errors.New("Cannot get Git Config from DB : " + err.Error()) return "", errors.New("Cannot get Git Config from DB : " + err.Error())
} }
if config == nil { if config == nil {
return "Gitops Disabled", nil return "GitOps Disabled", nil
} }
query := bson.D{{"infra_id", infra.InfraID}, {"experiment_id", experimentID}, {"is_removed", false}} query := bson.D{{"infra_id", infra.InfraID}, {"experiment_id", experimentID}, {"is_removed", false}}
experiments, err := g.chaosExperimentOps.GetExperiments(query) experiments, err := g.chaosExperimentOps.GetExperiments(query)
if err != nil { if err != nil {
logrus.Error("Could not get experiment :", err) log.Error("Could not get experiment :", err)
return "could not get experiment", err return "could not get experiment", err
} }
if len(experiments) == 0 { if len(experiments) == 0 {
@ -94,12 +91,12 @@ func (g *gitOpsService) GitOpsNotificationHandler(ctx context.Context, infra cha
} }
experiments[0].Revision[len(experiments[0].Revision)-1].ExperimentManifest, err = sjson.Set(experiments[0].Revision[len(experiments[0].Revision)-1].ExperimentManifest, "metadata.name", experiments[0].Name+"-"+strconv.FormatInt(time.Now().UnixMilli(), 10)) experiments[0].Revision[len(experiments[0].Revision)-1].ExperimentManifest, err = sjson.Set(experiments[0].Revision[len(experiments[0].Revision)-1].ExperimentManifest, "metadata.name", experiments[0].Name+"-"+strconv.FormatInt(time.Now().UnixMilli(), 10))
if err != nil { if err != nil {
logrus.Error("Failed to updated experiment name :", err) log.Error("Failed to updated experiment name :", err)
return "", errors.New("Failed to updated experiment name " + err.Error()) return "", errors.New("Failed to updated experiment name " + err.Error())
} }
username := "git-ops" username := "git-ops"
chaos_infra.SendExperimentToSubscriber(experiments[0].ProjectID, &model.ChaosExperimentRequest{ chaosInfra.SendExperimentToSubscriber(experiments[0].ProjectID, &model.ChaosExperimentRequest{
ExperimentManifest: experiments[0].Revision[len(experiments[0].Revision)-1].ExperimentManifest, ExperimentManifest: experiments[0].Revision[len(experiments[0].Revision)-1].ExperimentManifest,
InfraID: experiments[0].InfraID, InfraID: experiments[0].InfraID,
}, &username, nil, "create", store.Store) }, &username, nil, "create", store.Store)
@ -107,7 +104,7 @@ func (g *gitOpsService) GitOpsNotificationHandler(ctx context.Context, infra cha
return "Request Acknowledged for experimentID: " + experimentID, nil 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) { func (g *gitOpsService) EnableGitOpsHandler(ctx context.Context, projectID string, config model.GitConfig) (bool, error) {
gitLock.Lock(projectID, nil) gitLock.Lock(projectID, nil)
defer gitLock.Unlock(projectID, nil) defer gitLock.Unlock(projectID, nil)
@ -117,14 +114,19 @@ func (g *gitOpsService) EnableGitOpsHandler(ctx context.Context, projectID strin
var conn *grpc2.ClientConn var conn *grpc2.ClientConn
client, conn := grpc.GetAuthGRPCSvcClient(conn) client, conn := grpc.GetAuthGRPCSvcClient(conn)
defer conn.Close() defer func(conn *grpc2.ClientConn) {
err := conn.Close()
if err != nil {
log.Error("Failed to close connection : ", err)
}
}(conn)
_, err := grpc.GetProjectById(client, projectID) _, err := grpc.GetProjectById(client, projectID)
if err != nil { if err != nil {
return false, errors.New("Failed to setup GitOps : " + err.Error()) return false, errors.New("Failed to setup GitOps : " + err.Error())
} }
logrus.Info("Enabling Gitops") log.Info("Enabling GitOps")
gitDB := gitops.GetGitConfigDB(projectID, config) gitDB := gitops.GetGitConfigDB(projectID, config)
commit, err := SetupGitOps(GitUserFromContext(ctx), GetGitOpsConfig(gitDB)) commit, err := SetupGitOps(GitUserFromContext(ctx), GetGitOpsConfig(gitDB))
@ -141,12 +143,12 @@ func (g *gitOpsService) EnableGitOpsHandler(ctx context.Context, projectID strin
return true, nil 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) { func (g *gitOpsService) DisableGitOpsHandler(ctx context.Context, projectID string) (bool, error) {
gitLock.Lock(projectID, nil) gitLock.Lock(projectID, nil)
defer gitLock.Unlock(projectID, nil) defer gitLock.Unlock(projectID, nil)
logrus.Info("Disabling Gitops") log.Info("Disabling GitOps")
err := g.gitOpsOperator.DeleteGitConfig(ctx, projectID) err := g.gitOpsOperator.DeleteGitConfig(ctx, projectID)
if err != nil { if err != nil {
return false, errors.New("Failed to delete git config from DB : " + err.Error()) return false, errors.New("Failed to delete git config from DB : " + err.Error())
@ -160,7 +162,7 @@ func (g *gitOpsService) DisableGitOpsHandler(ctx context.Context, projectID stri
return true, nil 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) { func (g *gitOpsService) UpdateGitOpsDetailsHandler(ctx context.Context, projectID string, config model.GitConfig) (bool, error) {
gitLock.Lock(projectID, nil) gitLock.Lock(projectID, nil)
defer gitLock.Unlock(projectID, nil) defer gitLock.Unlock(projectID, nil)
@ -176,7 +178,7 @@ func (g *gitOpsService) UpdateGitOpsDetailsHandler(ctx context.Context, projectI
return false, errors.New("GitOps Disabled ") return false, errors.New("GitOps Disabled ")
} }
logrus.Info("Enabling Gitops") log.Info("Enabling GitOps")
gitDB := gitops.GetGitConfigDB(projectID, config) gitDB := gitops.GetGitConfigDB(projectID, config)
gitConfig := GetGitOpsConfig(gitDB) gitConfig := GetGitOpsConfig(gitDB)
@ -205,7 +207,7 @@ func (g *gitOpsService) UpdateGitOpsDetailsHandler(ctx context.Context, projectI
return true, nil 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) { func (g *gitOpsService) GetGitOpsDetails(ctx context.Context, projectID string) (*model.GitConfigResponse, error) {
gitLock.Lock(projectID, nil) gitLock.Lock(projectID, nil)
defer gitLock.Unlock(projectID, nil) defer gitLock.Unlock(projectID, nil)
@ -269,7 +271,7 @@ func (g *gitOpsService) UpsertExperimentToGit(ctx context.Context, projectID str
return errors.New("Cannot convert manifest to yaml : " + err.Error()) return errors.New("Cannot convert manifest to yaml : " + err.Error())
} }
err = ioutil.WriteFile(experimentPath, data, 0644) err = os.WriteFile(experimentPath, data, 0644)
if err != nil { if err != nil {
return errors.New("Cannot write experiment to git : " + err.Error()) return errors.New("Cannot write experiment to git : " + err.Error())
} }
@ -296,7 +298,7 @@ func (g *gitOpsService) UpsertExperimentToGit(ctx context.Context, projectID str
// DeleteExperimentFromGit deletes experiment from git // DeleteExperimentFromGit deletes experiment from git
func (g *gitOpsService) DeleteExperimentFromGit(ctx context.Context, projectID string, experiment *model.ChaosExperimentRequest) error { func (g *gitOpsService) DeleteExperimentFromGit(ctx context.Context, projectID string, experiment *model.ChaosExperimentRequest) error {
logrus.Info("Deleting Experiment...") log.Info("Deleting Experiment...")
gitLock.Lock(projectID, nil) gitLock.Lock(projectID, nil)
defer gitLock.Unlock(projectID, nil) defer gitLock.Unlock(projectID, nil)
@ -323,7 +325,7 @@ func (g *gitOpsService) DeleteExperimentFromGit(ctx context.Context, projectID s
return errors.New("Cannot delete experiment from git : " + err.Error()) return errors.New("Cannot delete experiment from git : " + err.Error())
} }
if !exists { if !exists {
logrus.Error("File not found in git : ", gitConfig.LocalPath+"/"+experimentPath) log.Error("File not found in git : ", gitConfig.LocalPath+"/"+experimentPath)
return nil return nil
} }
err = os.RemoveAll(gitConfig.LocalPath + "/" + experimentPath) err = os.RemoveAll(gitConfig.LocalPath + "/" + experimentPath)
@ -333,13 +335,13 @@ func (g *gitOpsService) DeleteExperimentFromGit(ctx context.Context, projectID s
commit, err := gitConfig.GitCommit(GitUserFromContext(ctx), "Deleted Experiment : "+experiment.ExperimentName, &experimentPath) commit, err := gitConfig.GitCommit(GitUserFromContext(ctx), "Deleted Experiment : "+experiment.ExperimentName, &experimentPath)
if err != nil { if err != nil {
logrus.Error("Error", err) log.Error("Error", err)
return errors.New("Cannot commit experiment[delete] to git : " + err.Error()) return errors.New("Cannot commit experiment[delete] to git : " + err.Error())
} }
err = gitConfig.GitPush() err = gitConfig.GitPush()
if err != nil { if err != nil {
logrus.Error("Error", err) log.Error("Error", err)
return errors.New("Cannot push experiment[delete] to git : " + err.Error()) return errors.New("Cannot push experiment[delete] to git : " + err.Error())
} }
@ -370,7 +372,7 @@ func (g *gitOpsService) gitSyncHelper(config gitops.GitConfigDB, wg *sync.WaitGr
// get most recent data from db after acquiring lock // get most recent data from db after acquiring lock
conf, err := g.gitOpsOperator.GetGitConfig(ctx, config.ProjectID) conf, err := g.gitOpsOperator.GetGitConfig(ctx, config.ProjectID)
if err != nil { if err != nil {
logrus.Error("Repo Sync ERROR: ", config.ProjectID, err.Error()) log.Error("Repo Sync ERROR: ", config.ProjectID, err.Error())
} }
if conf == nil { if conf == nil {
return return
@ -380,7 +382,7 @@ func (g *gitOpsService) gitSyncHelper(config gitops.GitConfigDB, wg *sync.WaitGr
err = g.SyncDBToGit(nil, gitConfig) err = g.SyncDBToGit(nil, gitConfig)
if err != nil { if err != nil {
logrus.Error("Repo Sync ERROR: ", conf.ProjectID, err.Error()) log.Error("Repo Sync ERROR: ", conf.ProjectID, err.Error())
} }
} }
@ -391,17 +393,17 @@ func (g *gitOpsService) GitOpsSyncHandler(singleRun bool) {
for { for {
ctx, cancel := context.WithTimeout(backgroundContext, timeout) ctx, cancel := context.WithTimeout(backgroundContext, timeout)
logrus.Info("Running GitOps DB Sync...") log.Info("Running GitOps DB Sync...")
configs, err := g.gitOpsOperator.GetAllGitConfig(ctx) configs, err := g.gitOpsOperator.GetAllGitConfig(ctx)
cancel() cancel()
if err != nil { if err != nil {
logrus.Error("Failed to get git configs from db : ", err) //condition log.Error("Failed to get git configs from db : ", err) //condition
} }
count := len(configs) count := len(configs)
if count > 0 { if count > 0 {
logrus.Info("Updating : ", configs) // condition log.Info("Updating : ", configs) // condition
count = count - 1 count = count - 1
for count >= 0 { for count >= 0 {
@ -418,7 +420,7 @@ func (g *gitOpsService) GitOpsSyncHandler(singleRun bool) {
wg.Wait() wg.Wait()
} }
logrus.Info("GitOps DB Sync Complete") //condition log.Info("GitOps DB Sync Complete") //condition
} }
if singleRun { if singleRun {
break break
@ -448,7 +450,7 @@ func (g *gitOpsService) SyncDBToGit(ctx context.Context, config GitConfig) error
if latestCommit == config.LatestCommit { if latestCommit == config.LatestCommit {
return nil return nil
} }
logrus.Info(latestCommit, " ", config.LatestCommit, "File Changes: ", files) log.Info(latestCommit, " ", config.LatestCommit, "File Changes: ", files)
newExperiments := false newExperiments := false
for file := range files { for file := range files {
if !strings.HasSuffix(file, ".yaml") { if !strings.HasSuffix(file, ".yaml") {
@ -462,20 +464,20 @@ func (g *gitOpsService) SyncDBToGit(ctx context.Context, config GitConfig) error
if !exists { if !exists {
err = g.deleteExperiment(file, config) err = g.deleteExperiment(file, config)
if err != nil { if err != nil {
logrus.Error("Error while deleting experiment db entry : " + file + " | " + err.Error()) log.Error("Error while deleting experiment db entry : " + file + " | " + err.Error())
continue continue
} }
continue continue
} }
// read changes [new additions/updates] // read changes [new additions/updates]
data, err := ioutil.ReadFile(config.LocalPath + "/" + file) data, err := os.ReadFile(config.LocalPath + "/" + file)
if err != nil { if err != nil {
logrus.Error("Error reading data from git file : " + file + " | " + err.Error()) log.Error("Error reading data from git file : " + file + " | " + err.Error())
continue continue
} }
data, err = yaml.YAMLToJSON(data) data, err = yaml.YAMLToJSON(data)
if err != nil { if err != nil {
logrus.Error("Error unmarshalling data from git file : " + file + " | " + err.Error()) log.Error("Error unmarshalling data from git file : " + file + " | " + err.Error())
continue continue
} }
wfID := gjson.Get(string(data), "metadata.labels.experiment_id").String() wfID := gjson.Get(string(data), "metadata.labels.experiment_id").String()
@ -484,12 +486,12 @@ func (g *gitOpsService) SyncDBToGit(ctx context.Context, config GitConfig) error
continue continue
} }
logrus.Info("WFID in changed File :", wfID) log.Info("WFID in changed File :", wfID)
if wfID == "" { if wfID == "" {
logrus.Info("New Experiment pushed to git : " + file) log.Info("New Experiment pushed to git : " + file)
flag, err := g.createExperiment(ctx, string(data), file, config) flag, err := g.createExperiment(ctx, string(data), file, config)
if err != nil { if err != nil {
logrus.Error("Error while creating new experiment db entry : " + file + " | " + err.Error()) log.Error("Error while creating new experiment db entry : " + file + " | " + err.Error())
continue continue
} }
if flag { if flag {
@ -498,7 +500,7 @@ func (g *gitOpsService) SyncDBToGit(ctx context.Context, config GitConfig) error
} else { } else {
err = g.updateExperiment(ctx, string(data), wfID, file, config) err = g.updateExperiment(ctx, string(data), wfID, file, config)
if err != nil { if err != nil {
logrus.Error("Error while updating experiment db entry : " + file + " | " + err.Error()) log.Error("Error while updating experiment db entry : " + file + " | " + err.Error())
continue continue
} }
} }
@ -539,7 +541,7 @@ func (g *gitOpsService) createExperiment(ctx context.Context, data, file string,
fileName = strings.Replace(fileName, ".yaml", "", -1) fileName = strings.Replace(fileName, ".yaml", "", -1)
wfName := gjson.Get(data, "metadata.name").String() wfName := gjson.Get(data, "metadata.name").String()
infraID := gjson.Get(data, "metadata.labels.infra_id").String() infraID := gjson.Get(data, "metadata.labels.infra_id").String()
logrus.Info("Experiment Details | wf_name: ", wfName, " infra_id: ", infraID) log.Info("Experiment Details | wf_name: ", wfName, " infra_id: ", infraID)
if wfName == "" || infraID == "" { if wfName == "" || infraID == "" {
return false, nil return false, nil
} }
@ -573,7 +575,7 @@ func (g *gitOpsService) createExperiment(ctx context.Context, data, file string,
return false, errors.New("Cannot convert manifest to yaml : " + err.Error()) return false, errors.New("Cannot convert manifest to yaml : " + err.Error())
} }
err = ioutil.WriteFile(experimentPath, yamlData, 0644) err = os.WriteFile(experimentPath, yamlData, 0644)
if err != nil { if err != nil {
return false, errors.New("Cannot write experiment to git : " + err.Error()) return false, errors.New("Cannot write experiment to git : " + err.Error())
} }
@ -587,9 +589,9 @@ func (g *gitOpsService) updateExperiment(ctx context.Context, data, wfID, file s
fileName = strings.Replace(fileName, ".yaml", "", -1) fileName = strings.Replace(fileName, ".yaml", "", -1)
wfName := gjson.Get(data, "metadata.name").String() wfName := gjson.Get(data, "metadata.name").String()
infraID := gjson.Get(data, "metadata.labels.infra_id").String() infraID := gjson.Get(data, "metadata.labels.infra_id").String()
logrus.Info("Experiment Details | wf_name: ", wfName, " infra_id: ", infraID) log.Info("Experiment Details | wf_name: ", wfName, " infra_id: ", infraID)
if wfName == "" || infraID == "" { if wfName == "" || infraID == "" {
logrus.Error("Cannot Update experiment missing experiment name or infra id") log.Error("Cannot Update experiment missing experiment name or infra id")
return nil return nil
} }
@ -603,7 +605,7 @@ func (g *gitOpsService) updateExperiment(ctx context.Context, data, wfID, file s
} }
if infraID != experiment[0].InfraID { if infraID != experiment[0].InfraID {
logrus.Error("Cannot change infra id for existing experiment") log.Error("Cannot change infra id for existing experiment")
return nil return nil
} }
@ -624,10 +626,10 @@ func (g *gitOpsService) updateExperiment(ctx context.Context, data, wfID, file s
if err != nil { if err != nil {
return err return err
} }
return g.chaosExperimentService.ProcessExperimentUpdate(input, "git-ops", wfType, revID, updateRevision, config.ProjectID, data_store.Store) return g.chaosExperimentService.ProcessExperimentUpdate(input, "git-ops", wfType, revID, updateRevision, config.ProjectID, dataStore.Store)
} }
// deleteExperiment helps in deleting a experiment from DB during the SyncDBToGit operation // deleteExperiment helps in deleting experiment from DB during the SyncDBToGit operation
func (g *gitOpsService) deleteExperiment(file string, config GitConfig) error { func (g *gitOpsService) deleteExperiment(file string, config GitConfig) error {
_, fileName := filepath.Split(file) _, fileName := filepath.Split(file)
fileName = strings.Replace(fileName, ".yaml", "", -1) fileName = strings.Replace(fileName, ".yaml", "", -1)
@ -638,5 +640,5 @@ func (g *gitOpsService) deleteExperiment(file string, config GitConfig) error {
return err return err
} }
return g.chaosExperimentService.ProcessExperimentDelete(query, experiment, "git-ops", data_store.Store) return g.chaosExperimentService.ProcessExperimentDelete(query, experiment, "git-ops", dataStore.Store)
} }

View File

@ -3,10 +3,12 @@ package graphql
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"io/ioutil" "io"
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
log "github.com/sirupsen/logrus"
) )
func (gql *subscriberGql) SendRequest(server string, payload []byte) (string, error) { func (gql *subscriberGql) SendRequest(server string, payload []byte) (string, error) {
@ -20,8 +22,13 @@ func (gql *subscriberGql) SendRequest(server string, payload []byte) (string, er
return "", err return "", err
} }
body, err := ioutil.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
resp.Body.Close() defer func() {
if err := resp.Body.Close(); err != nil {
log.Warnf("failed to close body: %v", err)
}
}()
if err != nil { if err != nil {
return "", err return "", err
} }