Removing the event tracker dependency from "last-applied-configuration" annotation (#3380)
* removing last-applied-annotation checks Signed-off-by: rajdas98 <mail.rajdas@gmail.com> * switching to logrus Signed-off-by: rajdas98 <mail.rajdas@gmail.com>
This commit is contained in:
parent
3261e47bb9
commit
fa969734cb
|
|
@ -19,10 +19,11 @@ package controllers
|
|||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/go-logr/logr"
|
||||
eventtrackerv1 "github.com/litmuschaos/litmus/litmus-portal/cluster-agents/event-tracker/api/v1"
|
||||
"github.com/litmuschaos/litmus/litmus-portal/cluster-agents/event-tracker/pkg/utils"
|
||||
|
|
@ -60,23 +61,27 @@ func (r *EventTrackerPolicyReconciler) Reconcile(ctx context.Context, req ctrl.R
|
|||
var etp eventtrackerv1.EventTrackerPolicy
|
||||
err := r.Client.Get(context.Background(), req.NamespacedName, &etp)
|
||||
if errors.IsNotFound(err) {
|
||||
log.Print(req.NamespacedName, " not found")
|
||||
logrus.Print(req.NamespacedName, " not found")
|
||||
return ctrl.Result{}, nil
|
||||
} else if err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
|
||||
for index, status := range etp.Statuses {
|
||||
if string(status.Result) == ConditionPassed && strings.ToLower(status.IsTriggered) == "false" {
|
||||
log.Print("ResourceName: " + status.ResourceName + "WorkflowID: " + status.WorkflowID)
|
||||
if status.Result == ConditionPassed && strings.ToLower(status.IsTriggered) == "false" {
|
||||
logrus.Print("ResourceName: " + status.ResourceName + ", WorkflowID: " + status.WorkflowID)
|
||||
response, err := utils.SendRequest(status.WorkflowID)
|
||||
if err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
log.Print(response)
|
||||
|
||||
logrus.Print(response)
|
||||
|
||||
var res Response
|
||||
json.Unmarshal([]byte(response), &res)
|
||||
err = json.Unmarshal([]byte(response), &res)
|
||||
if err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
|
||||
if res.Data.GitopsNotifer == "Gitops Disabled" {
|
||||
etp.Statuses[index].IsTriggered = "false"
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"reflect"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
v1 "k8s.io/api/apps/v1"
|
||||
"k8s.io/apimachinery/pkg/util/runtime"
|
||||
"k8s.io/client-go/informers"
|
||||
|
|
@ -27,35 +27,18 @@ func RunDeploymentInformer(factory informers.SharedInformerFactory) {
|
|||
depNewObj := newObj.(*v1.Deployment)
|
||||
depOldObj := oldObj.(*v1.Deployment)
|
||||
|
||||
oldManifest := depOldObj.GetAnnotations()["kubectl.kubernetes.io/last-applied-configuration"]
|
||||
newManifest := depNewObj.GetAnnotations()["kubectl.kubernetes.io/last-applied-configuration"]
|
||||
var worflowid = depNewObj.GetAnnotations()["litmuschaos.io/workflow"]
|
||||
|
||||
if oldManifest != "" && newManifest != "" {
|
||||
var oldDep v1.Deployment
|
||||
err := json.Unmarshal([]byte(oldManifest), &oldDep)
|
||||
if depNewObj.GetResourceVersion() != depOldObj.GetResourceVersion() &&
|
||||
!reflect.DeepEqual(depNewObj, depOldObj) &&
|
||||
depNewObj.GetAnnotations()["litmuschaos.io/gitops"] == "true" &&
|
||||
worflowid != "" {
|
||||
logrus.Infof("GitOps Notification for workflowID: %s, ResourceType: %s, ResourceName: %s, ResourceNamespace: %s", worflowid, "Deployment", depNewObj.Name, depNewObj.Namespace)
|
||||
err := PolicyAuditor("Deployment", depNewObj, depOldObj, worflowid)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
logrus.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
var newDep v1.Deployment
|
||||
err = json.Unmarshal([]byte(newManifest), &newDep)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return
|
||||
}
|
||||
|
||||
if depNewObj.GetResourceVersion() != depOldObj.GetResourceVersion() && !reflect.DeepEqual(newDep, oldDep) {
|
||||
var worflowid = depNewObj.GetAnnotations()["litmuschaos.io/workflow"]
|
||||
if depNewObj.GetAnnotations()["litmuschaos.io/gitops"] == "true" && worflowid != "" {
|
||||
log.Printf("EventType: Update \n GitOps Notification for workflowID: %s, ResourceType: %s, ResourceName: %s, ResourceNamespace: %s", worflowid, "Deployment", depNewObj.Name, depNewObj.Namespace)
|
||||
err := PolicyAuditor("Deployment", depNewObj, depOldObj, worflowid)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
})
|
||||
|
|
@ -82,37 +65,20 @@ func RunStsInformer(factory informers.SharedInformerFactory) {
|
|||
stsNewObj := newObj.(*v1.StatefulSet)
|
||||
stsOldObj := oldObj.(*v1.StatefulSet)
|
||||
|
||||
oldManifest := stsOldObj.GetAnnotations()["kubectl.kubernetes.io/last-applied-configuration"]
|
||||
newManifest := stsNewObj.GetAnnotations()["kubectl.kubernetes.io/last-applied-configuration"]
|
||||
var worflowid = stsNewObj.GetAnnotations()["litmuschaos.io/workflow"]
|
||||
|
||||
if oldManifest != "" && newManifest != "" {
|
||||
var oldSts v1.StatefulSet
|
||||
err := json.Unmarshal([]byte(oldManifest), &oldSts)
|
||||
if stsNewObj.GetResourceVersion() != stsOldObj.GetResourceVersion() &&
|
||||
!reflect.DeepEqual(stsNewObj, stsOldObj) &&
|
||||
stsNewObj.GetAnnotations()["litmuschaos.io/gitops"] == "true" &&
|
||||
worflowid != "" {
|
||||
logrus.Infof("GitOps Notification for workflowID: %s, ResourceType: %s, ResourceName: %s, ResourceNamespace: %s", worflowid, "Deployment", stsNewObj.Name, stsNewObj.Namespace)
|
||||
err := PolicyAuditor("Deployment", stsNewObj, stsOldObj, worflowid)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
logrus.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
var newSts v1.StatefulSet
|
||||
err = json.Unmarshal([]byte(newManifest), &newSts)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return
|
||||
}
|
||||
|
||||
if stsNewObj.GetResourceVersion() != stsOldObj.GetResourceVersion() && !reflect.DeepEqual(newSts, oldSts) {
|
||||
var worflowid = stsNewObj.GetAnnotations()["litmuschaos.io/workflow"]
|
||||
if stsNewObj.GetAnnotations()["litmuschaos.io/gitops"] == "true" && worflowid != "" {
|
||||
log.Printf("EventType: Update \n GitOps Notification for workflowID: %s, ResourceType: %s, ResourceName: %s, ResourceNamespace: %s", worflowid, "StateFulSet", stsNewObj.Name, stsNewObj.Namespace)
|
||||
err := PolicyAuditor("StateFulSet", stsNewObj, stsOldObj, worflowid)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
},
|
||||
})
|
||||
|
||||
|
|
@ -138,36 +104,20 @@ func RunDSInformer(factory informers.SharedInformerFactory) {
|
|||
dsNewObj := newObj.(*v1.DaemonSet)
|
||||
dsOldObj := newObj.(*v1.DaemonSet)
|
||||
|
||||
oldManifest := dsOldObj.GetAnnotations()["kubectl.kubernetes.io/last-applied-configuration"]
|
||||
newManifest := dsNewObj.GetAnnotations()["kubectl.kubernetes.io/last-applied-configuration"]
|
||||
var worflowid = dsNewObj.GetAnnotations()["litmuschaos.io/workflow"]
|
||||
|
||||
if oldManifest != "" && newManifest != "" {
|
||||
var oldDm v1.StatefulSet
|
||||
err := json.Unmarshal([]byte(oldManifest), &oldDm)
|
||||
if dsNewObj.GetResourceVersion() != dsOldObj.GetResourceVersion() &&
|
||||
!reflect.DeepEqual(dsNewObj, dsOldObj) &&
|
||||
dsNewObj.GetAnnotations()["litmuschaos.io/gitops"] == "true" &&
|
||||
worflowid != "" {
|
||||
logrus.Infof("GitOps Notification for workflowID: %s, ResourceType: %s, ResourceName: %s, ResourceNamespace: %s", worflowid, "Deployment", dsNewObj.Name, dsNewObj.Namespace)
|
||||
err := PolicyAuditor("Deployment", dsNewObj, dsOldObj, worflowid)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
logrus.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
var newDm v1.StatefulSet
|
||||
err = json.Unmarshal([]byte(newManifest), &newDm)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return
|
||||
}
|
||||
|
||||
if dsNewObj.GetResourceVersion() != dsOldObj.GetResourceVersion() && !reflect.DeepEqual(newDm, oldDm) {
|
||||
var worflowid = dsNewObj.GetAnnotations()["litmuschaos.io/workflow"]
|
||||
if dsNewObj.GetAnnotations()["litmuschaos.io/gitops"] == "true" && worflowid != "" {
|
||||
log.Printf("EventType: Update \n GitOps Notification for workflowID: %s, ResourceType: %s, ResourceName: %s, ResourceNamespace: %s", worflowid, "DaemonSet", dsNewObj.Name, dsNewObj.Namespace)
|
||||
err := PolicyAuditor("DaemonSet", dsNewObj, dsOldObj, worflowid)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
},
|
||||
})
|
||||
|
||||
|
|
|
|||
|
|
@ -20,7 +20,6 @@ import (
|
|||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
|
@ -37,7 +36,7 @@ var (
|
|||
const (
|
||||
ExternAgentConfigName = "agent-config"
|
||||
ConditionPassed = "ConditionPassed"
|
||||
ConditionFailed = "ConditionFailed"
|
||||
//ConditionFailed = "ConditionFailed"
|
||||
)
|
||||
|
||||
const (
|
||||
|
|
@ -75,29 +74,26 @@ func conditionChecker(etp litmuschaosv1.EventTrackerPolicy, newData interface{},
|
|||
return false
|
||||
}
|
||||
|
||||
if condition.Operator == "Change" {
|
||||
oldDataResult, err := jmespath.Search(condition.Key, oldData)
|
||||
if err != nil {
|
||||
logrus.Error(err)
|
||||
return false
|
||||
}
|
||||
oldDataResult, err := jmespath.Search(condition.Key, oldData)
|
||||
if err != nil {
|
||||
logrus.Error(err)
|
||||
return false
|
||||
}
|
||||
|
||||
if newDataResult != oldDataResult {
|
||||
if newDataResult != oldDataResult {
|
||||
if condition.Operator == "Change" {
|
||||
final_result = true
|
||||
} else {
|
||||
final_result = false
|
||||
break
|
||||
str := fmt.Sprintf("%v", newDataResult)
|
||||
if val := cases(str, *condition.Value, condition.Operator); !val {
|
||||
final_result = val
|
||||
break
|
||||
} else if val {
|
||||
final_result = true
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
str := fmt.Sprintf("%v", newDataResult)
|
||||
if val := cases(str, *condition.Value, condition.Operator); !val {
|
||||
final_result = val
|
||||
break
|
||||
} else if val {
|
||||
final_result = true
|
||||
}
|
||||
logrus.Println("no changes in the resource")
|
||||
}
|
||||
}
|
||||
} else if etp.Spec.ConditionType == "or" {
|
||||
|
|
@ -107,31 +103,27 @@ func conditionChecker(etp litmuschaosv1.EventTrackerPolicy, newData interface{},
|
|||
logrus.Error(err)
|
||||
}
|
||||
|
||||
if condition.Operator == "Change" {
|
||||
oldDataResult, err := jmespath.Search(condition.Key, oldData)
|
||||
if err != nil {
|
||||
logrus.Error(err)
|
||||
return false
|
||||
}
|
||||
oldDataResult, err := jmespath.Search(condition.Key, oldData)
|
||||
if err != nil {
|
||||
logrus.Error(err)
|
||||
return false
|
||||
}
|
||||
|
||||
if newDataResult != oldDataResult {
|
||||
if newDataResult != oldDataResult {
|
||||
if condition.Operator == "Change" {
|
||||
final_result = true
|
||||
} else {
|
||||
str := fmt.Sprintf("%v", newDataResult)
|
||||
if val := cases(str, *condition.Value, condition.Operator); val {
|
||||
final_result = val
|
||||
}
|
||||
}
|
||||
} else {
|
||||
str := fmt.Sprintf("%v", newDataResult)
|
||||
if val := cases(str, *condition.Value, condition.Operator); val {
|
||||
final_result = val
|
||||
}
|
||||
logrus.Println("no changes in the resource")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if final_result {
|
||||
logrus.Info("condition matched")
|
||||
} else {
|
||||
logrus.Info("condition not matched")
|
||||
}
|
||||
|
||||
return final_result
|
||||
}
|
||||
|
||||
|
|
@ -153,7 +145,7 @@ func PolicyAuditor(resourceType string, newObj interface{}, oldObj interface{},
|
|||
}
|
||||
|
||||
if len(deploymentConfigList.Items) == 0 {
|
||||
log.Print("No event-tracker policy(s) found in " + AgentNamespace + " namespace")
|
||||
logrus.Print("No event-tracker policy(s) found in " + AgentNamespace + " namespace")
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
@ -262,40 +254,38 @@ func PolicyAuditor(resourceType string, newObj interface{}, oldObj interface{},
|
|||
}
|
||||
|
||||
check := conditionChecker(etp, newDataInterface, oldDataInterface)
|
||||
var result string
|
||||
|
||||
if check == true {
|
||||
result = ConditionPassed
|
||||
} else if check == false {
|
||||
result = ConditionFailed
|
||||
etp.Statuses = append(etp.Statuses, litmuschaosv1.EventTrackerPolicyStatus{
|
||||
TimeStamp: time.Now().Format(time.RFC850),
|
||||
Resource: resourceType,
|
||||
ResourceName: resourceName,
|
||||
Result: ConditionPassed,
|
||||
WorkflowID: workflowid,
|
||||
IsTriggered: "false",
|
||||
})
|
||||
|
||||
// Updating EventTrackerPolicy
|
||||
var unstruc unstructured.Unstructured
|
||||
data, err = json.Marshal(etp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = json.Unmarshal(data, &unstruc)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = clientSet.Resource(deploymentRes).Namespace(AgentNamespace).Update(context.TODO(), &unstruc, metav1.UpdateOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
logrus.Print("EventTrackerPolicy updated")
|
||||
} else {
|
||||
logrus.Println("Condition failed for resource name:", resourceName, " resource type:", resourceType)
|
||||
}
|
||||
|
||||
etp.Statuses = append(etp.Statuses, litmuschaosv1.EventTrackerPolicyStatus{
|
||||
TimeStamp: time.Now().Format(time.RFC850),
|
||||
Resource: resourceType,
|
||||
ResourceName: resourceName,
|
||||
Result: result,
|
||||
WorkflowID: workflowid,
|
||||
IsTriggered: "false",
|
||||
})
|
||||
|
||||
// Updating EventTrackerPolicy
|
||||
var unstruc unstructured.Unstructured
|
||||
data, err = json.Marshal(etp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = json.Unmarshal(data, &unstruc)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = clientSet.Resource(deploymentRes).Namespace(AgentNamespace).Update(context.TODO(), &unstruc, metav1.UpdateOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Print("EventTrackerPolicy updated")
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
|
|||
Loading…
Reference in New Issue