Skip to content

Commit

Permalink
feat: remove patch operation (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasepe authored Aug 5, 2024
1 parent 932e071 commit 4c44578
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 53 deletions.
7 changes: 1 addition & 6 deletions internal/router/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type pusher struct {
notifyQueue queue.Queuer
httpClient *http.Client
verbose bool
patch bool
}

func (c *pusher) Handle(evt corev1.Event) {
Expand All @@ -67,12 +68,6 @@ func (c *pusher) Handle(evt corev1.Event) {
"compositionId", compositionId)
}

err = patchWithLabels(c.objectResolver, &evt, compositionId)
if err != nil {
klog.ErrorS(err, "unable to patch with labels", "involvedObject", ref.Name)
return
}

all, err := c.getAllRegistrations(context.Background())
if err != nil {
klog.ErrorS(err, "unable to list registrations", "involvedObject", ref.Name)
Expand Down
47 changes: 0 additions & 47 deletions internal/router/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,15 @@ package router

import (
"context"
"encoding/json"
"fmt"

"github.com/krateoplatformops/eventrouter/internal/objects"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
)

const (
keyCompositionID = "krateo.io/composition-id"
keyPatchedBy = "krateo.io/patched-by"
)

func patchWithLabels(resolver *objects.ObjectResolver, evt *corev1.Event, compositionId string) error {
extras, err := createPatchData(map[string]string{
keyCompositionID: compositionId,
keyPatchedBy: "krateo",
})
if err != nil {
return fmt.Errorf("creating patch data: %w", err)
}

err = resolver.Patch(context.Background(), objects.PatchOpts{
GVK: schema.GroupVersionKind{Group: "", Version: "v1", Kind: "Event"},
Name: evt.Name,
Namespace: evt.Namespace,
PatchData: extras,
})
if err != nil {
if len(compositionId) > 0 {
return fmt.Errorf("patching event with composition id '%s': %w", compositionId, err)
} else {
return fmt.Errorf("patching event: %w", err)
}
}

return nil
}

func createPatchData(labels map[string]string) ([]byte, error) {
patch := struct {
Metadata struct {
Labels map[string]string `json:"labels"`
} `json:"metadata"`
}{}
patch.Metadata.Labels = map[string]string{}

for k, v := range labels {
if len(v) > 0 {
patch.Metadata.Labels[k] = v
}
}

return json.Marshal(patch)
}

func hasCompositionId(obj *corev1.Event) bool {
labels := obj.GetLabels()
if len(labels) == 0 {
Expand Down

0 comments on commit 4c44578

Please sign in to comment.