Skip to content

Commit

Permalink
fix(clusterruleraction): Fixed clusterruleraction controller (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
dfradehubs authored Jan 14, 2025
1 parent 57b3566 commit 2ae9841
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 17 deletions.
3 changes: 1 addition & 2 deletions internal/controller/ruleraction/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,8 @@ func (r *RulerActionReconciler) Reconcile(ctx context.Context, req ctrl.Request)

// 1.1 Try with Event resource first. If it is not an Event, then it will return an error
// but reconcile will try if it is a RulerAction resource relationated with an Event
tempCompoundRulerActionResource, err := r.GetEventRuleAction(ctx, req.Namespace, req.Name, resourceType)
err = r.GetEventRuleAction(ctx, CompoundRulerActionResource, req.Namespace, req.Name)
if err == nil {
*CompoundRulerActionResource = tempCompoundRulerActionResource
goto processEvent
}

Expand Down
57 changes: 42 additions & 15 deletions internal/controller/ruleraction/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"fmt"
"io"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"net/http"
"prosimcorp.com/SearchRuler/internal/globals"
"reflect"

corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -200,7 +204,7 @@ func (r *RulerActionReconciler) Sync(ctx context.Context, resource *CompoundRule
}

// GetRuleActionFromEvent returns the RulerAction resource associated with the event that triggered the reconcile
func (r *RulerActionReconciler) GetEventRuleAction(ctx context.Context, namespace, name, resourceType string) (ruleAction CompoundRulerActionResource, err error) {
func (r *RulerActionReconciler) GetEventRuleAction(ctx context.Context, ruleAction *CompoundRulerActionResource, namespace, name string) (err error) {

// Get event resource from the namespace and name of the event that triggered the reconcile
EventResource := &corev1.Event{}
Expand All @@ -210,7 +214,7 @@ func (r *RulerActionReconciler) GetEventRuleAction(ctx context.Context, namespac
}
err = r.Get(ctx, namespacedName, EventResource)
if err != nil {
return ruleAction, fmt.Errorf(
return fmt.Errorf(
"reconcile not triggered by event, triggered by resource %s : %v",
namespacedName,
err.Error(),
Expand All @@ -225,36 +229,59 @@ func (r *RulerActionReconciler) GetEventRuleAction(ctx context.Context, namespac
}
err = r.Get(ctx, searchRuleNamespacedName, searchRule)
if err != nil {
return ruleAction, fmt.Errorf(
return fmt.Errorf(
"error fetching SearchRule %s from event %s: %v",
searchRuleNamespacedName,
namespacedName,
err,
)
}

// Get RulerAction resource from searchRule resource
ruleActionNamespacedName := types.NamespacedName{
Namespace: searchRule.Spec.ActionRef.Namespace,
Name: searchRule.Spec.ActionRef.Name,
gvr := schema.GroupVersionResource{
Group: v1alpha1.GroupVersion.Group,
Version: v1alpha1.GroupVersion.Version,
Resource: "clusterruleractions",
}

switch resourceType {
case controller.ClusterRulerActionResourceType:
err = r.Get(ctx, ruleActionNamespacedName, ruleAction.ClusterRulerActionResource)
default:
err = r.Get(ctx, ruleActionNamespacedName, ruleAction.RulerActionResource)
rulerActionWrapper := globals.Application.KubeRawClient.Resource(gvr)
if searchRule.Spec.ActionRef.Namespace != "" {
gvr.Resource = "ruleractions"
rulerActionWrapper = globals.Application.KubeRawClient.Resource(gvr)
rulerActionWrapper.Namespace(searchRule.Spec.ActionRef.Namespace)
}

rulerActionResource, err := rulerActionWrapper.Get(ctx, searchRule.Spec.ActionRef.Name, metav1.GetOptions{})
if err != nil {
return ruleAction, fmt.Errorf(
// TODO: Improve this
return err
}

// If RulerAction is empty then error
if reflect.ValueOf(rulerActionResource).IsZero() {
return fmt.Errorf(
"error fetching RulerAction %s from searchRule %s: %v",
ruleActionNamespacedName,
searchRule.Spec.ActionRef.Name,
searchRuleNamespacedName,
err,
)
}

return ruleAction, nil
// Tricky for save RulerAction resource with RulerAction or ClusterRulerAction type
specBytes, err := json.Marshal(rulerActionResource.Object)
if err != nil {
return fmt.Errorf(controller.JSONMarshalErrorMessage, err)
}
switch searchRule.Spec.ActionRef.Namespace {
case "":
err = json.Unmarshal(specBytes, ruleAction.ClusterRulerActionResource)
default:
err = json.Unmarshal(specBytes, ruleAction.RulerActionResource)
}
if err != nil {
return fmt.Errorf(controller.JSONMarshalErrorMessage, err)
}

return nil
}

// getRulerActionAssociatedAlerts returns all alerts associated with the RulerAction
Expand Down

0 comments on commit 2ae9841

Please sign in to comment.