From 06d4bf2c2b310ddc338018d8473e6dc556897ffe Mon Sep 17 00:00:00 2001 From: Shubham Chaudhary Date: Sat, 1 Jun 2024 12:03:41 +0530 Subject: [PATCH] fix(logs): Improve the litmus-checker logs (#505) Signed-off-by: Shubham Chaudhary --- custom/litmus-checker/go.mod | 3 +- custom/litmus-checker/go.sum | 5 +- custom/litmus-checker/main.go | 46 ++++++++++++------- .../litmus-checker/pkg/chaos-checker/check.go | 23 +++++----- custom/litmus-checker/pkg/k8s/client.go | 17 +++---- custom/litmus-checker/pkg/util/utils.go | 4 +- 6 files changed, 55 insertions(+), 43 deletions(-) diff --git a/custom/litmus-checker/go.mod b/custom/litmus-checker/go.mod index 33526561..ae1c0845 100644 --- a/custom/litmus-checker/go.mod +++ b/custom/litmus-checker/go.mod @@ -1,8 +1,9 @@ -module github.com/gdsoumya/resourceChecker +module github.com/litmuschaos/test-tools/custom/litmus-checker go 1.20 require ( + github.com/sirupsen/logrus v1.9.3 k8s.io/apimachinery v0.21.2 k8s.io/client-go v0.21.2 ) diff --git a/custom/litmus-checker/go.sum b/custom/litmus-checker/go.sum index 5bebbb8a..9aa6da70 100644 --- a/custom/litmus-checker/go.sum +++ b/custom/litmus-checker/go.sum @@ -162,6 +162,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= +github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= +github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk= github.com/spf13/pflag v0.0.0-20170130214245-9ff6c6923cff/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= @@ -170,8 +172,9 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= -github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= diff --git a/custom/litmus-checker/main.go b/custom/litmus-checker/main.go index dc44617e..13935de2 100644 --- a/custom/litmus-checker/main.go +++ b/custom/litmus-checker/main.go @@ -2,17 +2,21 @@ package main import ( "flag" + "github.com/sirupsen/logrus" "io/ioutil" - "log" "os" "os/signal" "syscall" - chaos_checker "github.com/gdsoumya/resourceChecker/pkg/chaos-checker" - "github.com/gdsoumya/resourceChecker/pkg/k8s" - "github.com/gdsoumya/resourceChecker/pkg/util" + chaos_checker "github.com/litmuschaos/test-tools/custom/litmus-checker/pkg/chaos-checker" + "github.com/litmuschaos/test-tools/custom/litmus-checker/pkg/k8s" + "github.com/litmuschaos/test-tools/custom/litmus-checker/pkg/util" ) +func init() { + logrus.SetFormatter(&logrus.JSONFormatter{}) +} + func main() { signalChannel := make(chan os.Signal) signal.Notify(signalChannel, os.Interrupt, syscall.SIGTERM) @@ -20,23 +24,33 @@ func main() { file := flag.String("file", "", "absolute path to the chaosengine yaml") engineFile := flag.String("saveName", "", "absolute path to the output file") flag.Parse() - if *file == "" { - log.Fatal("Error Engine Artefact path not specified") + + if file == nil || *file == "" { + logrus.Fatal("Error Engine Artefact path not specified") } + data, err := ioutil.ReadFile(*file) if err != nil { - log.Fatal("Error Reading Artefact : ", err) + logrus.Fatalf("Error Reading Artefact : %v", err) } - resp, err := k8s.CreateChaosDeployment(data, kubeconfig) + + dc, dyn, err := k8s.GetDynamicClient(kubeconfig) if err != nil { - log.Fatal("Error Creating Resource : ", err) + logrus.Fatalf("Error Getting Dynamic Client : %v", err) } - engineName := resp.GetName() - log.Print("\n\nChaosEngine Name : ", engineName, "\n\n") - err = util.WriteToFile(*engineFile, engineName) + + resp, err := k8s.CreateChaosDeployment(data, dc, dyn) if err != nil { - log.Print("ERROR : cannot write engine-name - ", err) + logrus.Fatalf("Error Creating Resource : %v", err) + } + + engineName := resp.GetName() + logrus.Infof("ChaosEngine Name : %s", engineName) + + if err = util.WriteToFile(*engineFile, engineName); err != nil { + logrus.Infof("ERROR: cannot write engine-name %v", err) } + gvk := resp.GroupVersionKind() resDef := k8s.ResourceDef{ Name: engineName, @@ -50,10 +64,10 @@ func main() { // Required, While aborting a Chaos Experiment, wait-container (argo-exec) sends SIGTERM signal to other (main) containers for aborting Argo-Workflow Pod go func() { <-signalChannel - log.Print("SIGTERM SIGNAL RECEIVED, Shutting down litmus-checker...") + logrus.Info("SIGTERM SIGNAL RECEIVED, Shutting down litmus-checker...") os.Exit(0) }() - log.Print("Created Resource Details: \n", resDef) - chaos_checker.CheckChaos(kubeconfig, resDef) + logrus.Infof("Created Resource Details: %v", resDef) + chaos_checker.CheckChaos(resDef, dc, dyn) } diff --git a/custom/litmus-checker/pkg/chaos-checker/check.go b/custom/litmus-checker/pkg/chaos-checker/check.go index 03faa753..1e99ab76 100644 --- a/custom/litmus-checker/pkg/chaos-checker/check.go +++ b/custom/litmus-checker/pkg/chaos-checker/check.go @@ -1,25 +1,23 @@ package chaos_checker import ( - "log" + "github.com/sirupsen/logrus" + "k8s.io/client-go/discovery" + "k8s.io/client-go/dynamic" "strings" "time" - "github.com/gdsoumya/resourceChecker/pkg/k8s" + "github.com/litmuschaos/test-tools/custom/litmus-checker/pkg/k8s" ) -func CheckChaos(kubeconfig *string, res k8s.ResourceDef) { - dc, dyn, err := k8s.GetDynamicClient(kubeconfig) - if err != nil { - log.Fatal("ERROR : ", err) - } - log.Print("Starting Chaos Checker in 1min") +func CheckChaos(res k8s.ResourceDef, dc discovery.DiscoveryInterface, dyn dynamic.Interface) { + logrus.Info("Starting Chaos Checker in 1min") + for { time.Sleep(time.Minute * 1) - log.Print("Checking if Engine Completed or Stopped") data, err := k8s.GetResourceDetails(dc, dyn, res) if err != nil { - log.Fatal("ERROR : ", err) + logrus.Fatalf("Failed to get resource details: %v", err) } status, ok := data.Object["status"].(map[string]interface{}) if !ok { @@ -27,11 +25,12 @@ func CheckChaos(kubeconfig *string, res k8s.ResourceDef) { } engStat, ok := status["engineStatus"].(string) if ok { + logrus.Infof("Engine Status :%s", engStat) if strings.ToLower(engStat) == "completed" { - log.Print("[*] ENGINE COMPLETED") + logrus.Info("[*] ENGINE COMPLETED") return } else if strings.ToLower(engStat) == "stopped" { - log.Print("[!] ERROR : ENGINE STATUS STOPPED") + logrus.Info("[!] ERROR : ENGINE STATUS STOPPED") return } } diff --git a/custom/litmus-checker/pkg/k8s/client.go b/custom/litmus-checker/pkg/k8s/client.go index 4e32967e..996edc54 100644 --- a/custom/litmus-checker/pkg/k8s/client.go +++ b/custom/litmus-checker/pkg/k8s/client.go @@ -28,20 +28,18 @@ type ResourceDef struct { var decUnstructured = yaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme) -func CreateChaosDeployment(manifest []byte, kubeconfig *string) (*unstructured.Unstructured, error) { - dc, dyn, err := GetDynamicClient(kubeconfig) - if err != nil { - return nil, err - } +func CreateChaosDeployment(manifest []byte, dc discovery.DiscoveryInterface, dyn dynamic.Interface) (*unstructured.Unstructured, error) { + mapper := restmapper.NewDeferredDiscoveryRESTMapper(memory.NewMemCacheClient(dc)) // Decode YAML manifest into unstructured.Unstructured obj := &unstructured.Unstructured{} - _, gvk, err := decUnstructured.Decode([]byte(manifest), nil, obj) + _, gvk, err := decUnstructured.Decode(manifest, nil, obj) if err != nil { return nil, err } + if obj.GetKind() != "ChaosEngine" { - return nil, errors.New("Not A ChaosEngine Spec") + return nil, errors.New("not a ChaosEngine Spec") } // Find GVR mapping, err := mapper.RESTMapping(gvk.GroupKind(), gvk.Version) @@ -84,10 +82,7 @@ func GetResourceDetails(dc discovery.DiscoveryInterface, dyn dynamic.Interface, func GetKubeConfig(kubeconfig *string) (*rest.Config, error) { // Use in-cluster config if kubeconfig path is specified if *kubeconfig == "" { - config, err := rest.InClusterConfig() - if err != nil { - return config, err - } + return rest.InClusterConfig() } config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig) if err != nil { diff --git a/custom/litmus-checker/pkg/util/utils.go b/custom/litmus-checker/pkg/util/utils.go index 24e73df4..da524c8b 100644 --- a/custom/litmus-checker/pkg/util/utils.go +++ b/custom/litmus-checker/pkg/util/utils.go @@ -1,13 +1,13 @@ package util import ( - "log" + "github.com/sirupsen/logrus" "os" ) func WriteToFile(engine, engineName string) error { if engine == "" { - log.Print("No output file provided skipping write") + logrus.Info("No output file provided skipping write") return nil } f, err := os.Create(engine)