Skip to content

Commit

Permalink
fix(logs): Improve the litmus-checker logs (#505)
Browse files Browse the repository at this point in the history
Signed-off-by: Shubham Chaudhary <[email protected]>
  • Loading branch information
ispeakc0de committed Jun 1, 2024
1 parent a3f5998 commit 06d4bf2
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 43 deletions.
3 changes: 2 additions & 1 deletion custom/litmus-checker/go.mod
Original file line number Diff line number Diff line change
@@ -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
)
Expand Down
5 changes: 4 additions & 1 deletion custom/litmus-checker/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
Expand Down
46 changes: 30 additions & 16 deletions custom/litmus-checker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,55 @@ 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)
kubeconfig := flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
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,
Expand All @@ -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)
}
23 changes: 11 additions & 12 deletions custom/litmus-checker/pkg/chaos-checker/check.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,36 @@
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 {
continue
}
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
}
}
Expand Down
17 changes: 6 additions & 11 deletions custom/litmus-checker/pkg/k8s/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions custom/litmus-checker/pkg/util/utils.go
Original file line number Diff line number Diff line change
@@ -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)
Expand Down

0 comments on commit 06d4bf2

Please sign in to comment.