-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Events to be a delegating event recorder
Update controllers.Events to be a delegating event recorder compatible with the upstream k8s EventRecorder interface. By default, when a MakeEvents() is used to create an instance of Events, it contains a k8s event recorder. Additional event recorders can be added to Events to forward the events to all the EventRecorder implementations. Signed-off-by: Sunny <darkowlzz@protonmail.com>
- Loading branch information
Showing
3 changed files
with
160 additions
and
49 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
Copyright 2021 The Flux 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 controller | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
corev1 "k8s.io/api/core/v1" | ||
|
||
"github.com/fluxcd/pkg/runtime/events" | ||
) | ||
|
||
func TestEvent_AnnotatedEventf(t *testing.T) { | ||
g := NewWithT(t) | ||
|
||
// Run an external server to receive the events. | ||
requestCount := 0 | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
requestCount++ | ||
b, err := io.ReadAll(r.Body) | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
|
||
var payload events.Event | ||
err = json.Unmarshal(b, &payload) | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
|
||
g.Expect(payload.InvolvedObject.Kind).To(Equal("ConfigMap")) | ||
g.Expect(payload.InvolvedObject.Name).To(Equal("test-cm")) | ||
g.Expect(payload.InvolvedObject.Namespace).To(Equal("default")) | ||
g.Expect(payload.Metadata).To(HaveKeyWithValue("test", "true")) | ||
g.Expect(payload.Reason).To(Equal("foo")) | ||
})) | ||
defer ts.Close() | ||
|
||
// Create an external event recorder. | ||
extEventRec, err := events.NewRecorder(env.GetScheme(), env.GetLogger(), ts.URL, "test-controller") | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
|
||
// Create an Events with the external event recorder. | ||
event := MakeEvents(env, "test-controller", extEventRec) | ||
obj := &corev1.ConfigMap{} | ||
obj.Namespace = "default" | ||
obj.Name = "test-cm" | ||
meta := map[string]string{ | ||
"test": "true", | ||
} | ||
|
||
event.AnnotatedEventf(obj, meta, corev1.EventTypeNormal, "foo", "foo") | ||
g.Expect(requestCount).To(BeNumerically(">", 0)) | ||
} |
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,61 @@ | ||
/* | ||
Copyright 2021 The Flux 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 controller | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
utilruntime "k8s.io/apimachinery/pkg/util/runtime" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
|
||
"github.com/fluxcd/pkg/runtime/testenv" | ||
) | ||
|
||
var ( | ||
env *testenv.Environment | ||
ctx = ctrl.SetupSignalHandler() | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
scheme := runtime.NewScheme() | ||
utilruntime.Must(corev1.AddToScheme(scheme)) | ||
|
||
env = testenv.New( | ||
testenv.WithScheme(scheme), | ||
) | ||
|
||
go func() { | ||
fmt.Println("Starting the test environment") | ||
if err := env.Start(ctx); err != nil { | ||
panic(fmt.Sprintf("Failed to start the test environment manager: %v", err)) | ||
} | ||
}() | ||
<-env.Manager.Elected() | ||
|
||
code := m.Run() | ||
|
||
fmt.Println("Stopping the test environment") | ||
if err := env.Stop(); err != nil { | ||
panic(fmt.Sprintf("Failed to stop the test environment: %v", err)) | ||
} | ||
|
||
os.Exit(code) | ||
} |