-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Emit CloudEvents for Runs. This is achieved by: - add a new read-only controller for Runs - emit CloudEvents only (no k8s events) on every reconcile of a Run - use an ephemeral cache to store sent events across reconcile runs. This is required because since the Runs controller only observes Runs, it does not have the context to know what was changed in the Run and though if a new event is required. The ephemeral cache logic is largely taken from the same functionality implemented in tektoncd/experimental/cloudevents Fixes #3862 Signed-off-by: Andrea Frittoli <[email protected]>
- Loading branch information
Showing
19 changed files
with
922 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
Copyright 2022 The Tekton Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package cache | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
|
||
cloudevents "github.com/cloudevents/sdk-go/v2" | ||
lru "github.com/hashicorp/golang-lru" | ||
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" | ||
) | ||
|
||
// Struct to unmarshal the event data | ||
type eventData struct { | ||
Run *v1alpha1.Run `json:"run,omitempty"` | ||
} | ||
|
||
// AddEventSentToCache adds the particular object to cache marking it as sent | ||
func AddEventSentToCache(cacheClient *lru.Cache, event *cloudevents.Event) error { | ||
if cacheClient == nil { | ||
return errors.New("cache client is nil") | ||
} | ||
eventKey, err := EventKey(event) | ||
if err != nil { | ||
return err | ||
} | ||
cacheClient.Add(eventKey, nil) | ||
return nil | ||
} | ||
|
||
// IsCloudEventSent checks if the event exists in the cache | ||
func IsCloudEventSent(cacheClient *lru.Cache, event *cloudevents.Event) (bool, error) { | ||
if cacheClient == nil { | ||
return false, errors.New("cache client is nil") | ||
} | ||
eventKey, err := EventKey(event) | ||
if err != nil { | ||
return false, err | ||
} | ||
return cacheClient.Contains(eventKey), nil | ||
} | ||
|
||
// EventKey defines whether an event is considered different from another | ||
// in future we might want to let specific event types override this | ||
func EventKey(event *cloudevents.Event) (string, error) { | ||
var ( | ||
data eventData | ||
resourceName string | ||
resourceNamespace string | ||
) | ||
err := json.Unmarshal(event.Data(), &data) | ||
if err != nil { | ||
return "", err | ||
} | ||
if data.Run == nil { | ||
return "", fmt.Errorf("Invalid Run data in %v", event) | ||
} | ||
resourceName = data.Run.Name | ||
resourceNamespace = data.Run.Namespace | ||
eventType := event.Type() | ||
return fmt.Sprintf("%s/run/%s/%s", eventType, resourceNamespace, resourceName), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
/* | ||
Copyright 2022 The Tekton Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package cache | ||
|
||
import ( | ||
"net/url" | ||
"testing" | ||
"time" | ||
|
||
cloudevents "github.com/cloudevents/sdk-go/v2" | ||
lru "github.com/hashicorp/golang-lru" | ||
|
||
"github.com/cloudevents/sdk-go/v2/event" | ||
cetypes "github.com/cloudevents/sdk-go/v2/types" | ||
"github.com/google/go-cmp/cmp" | ||
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" | ||
"github.com/tektoncd/pipeline/test/diff" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func strptr(s string) *string { return &s } | ||
|
||
func getEventData(run interface{}) map[string]interface{} { | ||
cloudEventData := map[string]interface{}{} | ||
if v, ok := run.(*v1alpha1.Run); ok { | ||
cloudEventData["run"] = v | ||
} | ||
return cloudEventData | ||
} | ||
|
||
func getEventToTest(eventtype string, run interface{}) *event.Event { | ||
e := event.Event{ | ||
Context: event.EventContextV1{ | ||
Type: eventtype, | ||
Source: cetypes.URIRef{URL: url.URL{Path: "/foo/bar/source"}}, | ||
ID: "test-event", | ||
Time: &cetypes.Timestamp{Time: time.Now()}, | ||
Subject: strptr("topic"), | ||
}.AsV1(), | ||
} | ||
if err := e.SetData(cloudevents.ApplicationJSON, getEventData(run)); err != nil { | ||
panic(err) | ||
} | ||
return &e | ||
} | ||
|
||
func getRunByMeta(name string, namespace string) *v1alpha1.Run { | ||
return &v1alpha1.Run{ | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: "Run", | ||
APIVersion: "v1alpha1", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: name, | ||
Namespace: namespace, | ||
}, | ||
Spec: v1alpha1.RunSpec{}, | ||
Status: v1alpha1.RunStatus{}, | ||
} | ||
} | ||
|
||
// TestEventsKey verifies that keys are extracted correctly from events | ||
func TestEventsKey(t *testing.T) { | ||
testcases := []struct { | ||
name string | ||
eventtype string | ||
run interface{} | ||
wantKey string | ||
wantErr bool | ||
}{{ | ||
name: "run event", | ||
eventtype: "my.test.run.event", | ||
run: getRunByMeta("myrun", "mynamespace"), | ||
wantKey: "my.test.run.event/run/mynamespace/myrun", | ||
wantErr: false, | ||
}, { | ||
name: "run event missing data", | ||
eventtype: "my.test.run.event", | ||
run: nil, | ||
wantKey: "", | ||
wantErr: true, | ||
}} | ||
|
||
for _, tc := range testcases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
gotEvent := getEventToTest(tc.eventtype, tc.run) | ||
gotKey, err := EventKey(gotEvent) | ||
if err != nil { | ||
if !tc.wantErr { | ||
t.Fatalf("Expecting an error, got none") | ||
} | ||
} | ||
if d := cmp.Diff(tc.wantKey, gotKey); d != "" { | ||
t.Errorf("Wrong Event key %s", diff.PrintWantGot(d)) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestAddCheckEvent(t *testing.T) { | ||
run := getRunByMeta("arun", "anamespace") | ||
runb := getRunByMeta("arun", "bnamespace") | ||
baseEvent := getEventToTest("some.event.type", run) | ||
|
||
testcases := []struct { | ||
name string | ||
firstEvent *event.Event | ||
secondEvent *event.Event | ||
wantFound bool | ||
}{{ | ||
name: "identical events", | ||
firstEvent: baseEvent, | ||
secondEvent: baseEvent, | ||
wantFound: true, | ||
}, { | ||
name: "new timestamp event", | ||
firstEvent: baseEvent, | ||
secondEvent: getEventToTest("some.event.type", run), | ||
wantFound: true, | ||
}, { | ||
name: "different namespace", | ||
firstEvent: baseEvent, | ||
secondEvent: getEventToTest("some.event.type", runb), | ||
wantFound: false, | ||
}, { | ||
name: "different event type", | ||
firstEvent: baseEvent, | ||
secondEvent: getEventToTest("some.other.event.type", run), | ||
wantFound: false, | ||
}} | ||
|
||
for _, tc := range testcases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
testCache, _ := lru.New(10) | ||
AddEventSentToCache(testCache, tc.firstEvent) | ||
found, _ := IsCloudEventSent(testCache, tc.secondEvent) | ||
if d := cmp.Diff(tc.wantFound, found); d != "" { | ||
t.Errorf("Cache check failure %s", diff.PrintWantGot(d)) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.