This commit is contained in:
François Rigaut 2025-05-22 12:39:34 +03:00 committed by GitHub
commit abee9dff30
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 37 additions and 0 deletions

View File

@ -69,6 +69,8 @@ type Options struct {
// ChangeLogOptions for recording change logs.
ChangeLogOptions *ChangeLogOptions
NamespacedEvents bool
}
// ForControllerRuntime extracts options for controller-runtime.

View File

@ -18,6 +18,7 @@ limitations under the License.
package event
import (
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
)
@ -101,6 +102,40 @@ func (r *APIRecorder) WithAnnotations(keysAndValues ...string) Recorder {
return ar
}
// A NamespacedAPIRecorder records Kubernetes events to an API server for and only for
// namespaced resources.
type NamespacedAPIRecorder struct {
APIRecorder
}
// NewNamespacedAPIRecorder returns an NamespacedAPIRecorder that records Kubernetes events to an
// APIServer using the supplied EventRecorder, only for namespaced resources.
func NewNamespacedAPIRecorder(r record.EventRecorder) *NamespacedAPIRecorder {
return &NamespacedAPIRecorder{
APIRecorder{kube: r, annotations: map[string]string{}},
}
}
// Event records the supplied event.
func (r *NamespacedAPIRecorder) Event(obj runtime.Object, e Event) {
m, err := meta.Accessor(obj)
// If we cannot determine if the object is namespace (i.e. err is not nil), don't do anything
if err == nil && m.GetNamespace() != "" && m.GetNamespace() != "default" {
r.APIRecorder.Event(obj, e)
}
}
// WithAnnotations returns a new *NamespacedAPIRecorder that includes the supplied
// annotations with all recorded events.
func (r *NamespacedAPIRecorder) WithAnnotations(keysAndValues ...string) Recorder {
nar := NewNamespacedAPIRecorder(r.kube)
for k, v := range r.annotations {
nar.annotations[k] = v
}
sliceMap(keysAndValues, nar.annotations)
return nar
}
func sliceMap(from []string, to map[string]string) {
for i := 0; i+1 < len(from); i += 2 {
k, v := from[i], from[i+1]