refactor(logging): Improve log formatting and error handling
- Updated log formatting in the Kubectl function for better readability. - Replaced deprecated string replacement method with `strings.ReplaceAll` in EditFile and EditKeyValue functions for improved performance. - Enhanced error handling by wrapping deferred close calls in anonymous functions to ensure proper error logging in DownloadFile, createInfrastructureViaRegisterInfra, applyInfrastructureManifest, and checkInfrastructureStatusViaGraphQL functions. - Simplified condition check in PodStatusCheck for better clarity. Signed-off-by: Sky Singh <akashsingh2210670@gmail.com>
This commit is contained in:
parent
6d3bdbde9d
commit
c289bb18fa
|
|
@ -22,7 +22,7 @@ func Kubectl(command ...string) error {
|
|||
cmd.Stderr = &stderr
|
||||
err = cmd.Run()
|
||||
if err != nil {
|
||||
log.Infof(fmt.Sprint(err) + ": " + stderr.String())
|
||||
log.Infof("%s: %s", fmt.Sprint(err), stderr.String())
|
||||
log.Infof("Error: %v", err)
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
16
pkg/file.go
16
pkg/file.go
|
|
@ -21,7 +21,7 @@ func EditFile(filepath, old, new string) error {
|
|||
|
||||
for i, line := range lines {
|
||||
if strings.Contains(line, old) {
|
||||
lines[i] = strings.Replace(lines[i], old, new, -1)
|
||||
lines[i] = strings.ReplaceAll(lines[i], old, new)
|
||||
failFlag = false
|
||||
}
|
||||
}
|
||||
|
|
@ -48,7 +48,7 @@ func EditKeyValue(filepath, key, oldvalue, newvalue string) error {
|
|||
|
||||
for i, line := range lines {
|
||||
if strings.Contains(line, key) {
|
||||
lines[i+1] = strings.Replace(lines[i+1], oldvalue, newvalue, -1)
|
||||
lines[i+1] = strings.ReplaceAll(lines[i+1], oldvalue, newvalue)
|
||||
failFlag = false
|
||||
}
|
||||
}
|
||||
|
|
@ -102,14 +102,22 @@ func DownloadFile(filepath string, url string) error {
|
|||
if err != nil {
|
||||
return fmt.Errorf("fail to get the data: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
defer func() {
|
||||
if err := resp.Body.Close(); err != nil {
|
||||
fmt.Printf("Error closing response body: %v\n", err)
|
||||
}
|
||||
}()
|
||||
|
||||
// Create the file
|
||||
out, err := os.Create(filepath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("fail to create the file: %w", err)
|
||||
}
|
||||
defer out.Close()
|
||||
defer func() {
|
||||
if err := out.Close(); err != nil {
|
||||
fmt.Printf("Error closing file: %v\n", err)
|
||||
}
|
||||
}()
|
||||
|
||||
// Write the body to file
|
||||
_, err = io.Copy(out, resp.Body)
|
||||
|
|
|
|||
|
|
@ -284,7 +284,11 @@ func createInfrastructureViaRegisterInfra(experimentsDetails *types.ExperimentDe
|
|||
if err != nil {
|
||||
return "", fmt.Errorf("failed to make GraphQL request: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
defer func() {
|
||||
if err := resp.Body.Close(); err != nil {
|
||||
fmt.Printf("Error closing response body: %v\n", err)
|
||||
}
|
||||
}()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return "", fmt.Errorf("GraphQL request failed with status: %d", resp.StatusCode)
|
||||
|
|
@ -419,7 +423,11 @@ func applyInfrastructureManifest(manifestContent []byte, experimentsDetails *typ
|
|||
if err != nil {
|
||||
return fmt.Errorf("failed to write manifest file: %v", err)
|
||||
}
|
||||
defer os.Remove(manifestFile) // Clean up temporary file
|
||||
defer func() {
|
||||
if err := os.Remove(manifestFile); err != nil {
|
||||
fmt.Printf("Error removing temporary file %s: %v\n", manifestFile, err)
|
||||
}
|
||||
}()
|
||||
|
||||
// Apply the manifest using kubectl
|
||||
command := []string{"apply", "-f", manifestFile, "--validate=false"}
|
||||
|
|
@ -533,7 +541,11 @@ func checkInfrastructureStatusViaGraphQL(experimentsDetails *types.ExperimentDet
|
|||
if err != nil {
|
||||
return false, fmt.Errorf("failed to make GraphQL request: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
defer func() {
|
||||
if err := resp.Body.Close(); err != nil {
|
||||
fmt.Printf("Error closing response body: %v\n", err)
|
||||
}
|
||||
}()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return false, fmt.Errorf("GraphQL request failed with status: %d", resp.StatusCode)
|
||||
|
|
|
|||
|
|
@ -152,8 +152,8 @@ func InstallChaosEngine(experimentsDetails *types.ExperimentDetails, chaosEngine
|
|||
chaosEngine.Spec.Components.Runner.ImagePullPolicy = corev1.PullPolicy(experimentsDetails.ImagePullPolicy)
|
||||
|
||||
// Modify the spec of engine file
|
||||
chaosEngine.ObjectMeta.Name = experimentsDetails.EngineName
|
||||
chaosEngine.ObjectMeta.Namespace = experimentsDetails.ChaosNamespace
|
||||
chaosEngine.Name = experimentsDetails.EngineName
|
||||
chaosEngine.Namespace = experimentsDetails.ChaosNamespace
|
||||
|
||||
// If ChaosEngine contain App Info then update it
|
||||
if chaosEngine.Spec.Appinfo.Appns != "" && chaosEngine.Spec.Appinfo.Applabel != "" {
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ func Infof(msg string, val ...interface{}) {
|
|||
|
||||
// Info log the General operational entries about what's going on inside the application
|
||||
func Info(msg string) {
|
||||
logrus.WithFields(logrus.Fields{}).Infof(msg)
|
||||
logrus.WithFields(logrus.Fields{}).Info(msg)
|
||||
}
|
||||
|
||||
// InfoWithValues log the General operational entries about what's going on inside the application
|
||||
|
|
|
|||
|
|
@ -86,10 +86,9 @@ func PodStatusCheck(experimentsDetails *types.ExperimentDetails, clients environ
|
|||
} else {
|
||||
flag = true
|
||||
break
|
||||
|
||||
}
|
||||
}
|
||||
if flag == true {
|
||||
if flag {
|
||||
break
|
||||
}
|
||||
if count == 19 {
|
||||
|
|
|
|||
Loading…
Reference in New Issue