From 23a088198d1a42545a367ec2b41b247277dfd69b Mon Sep 17 00:00:00 2001 From: Stefan Prodan Date: Tue, 8 Nov 2022 14:36:56 +0200 Subject: [PATCH] Migrate runtime to Event API v1beta1 Signed-off-by: Stefan Prodan --- runtime/events/event.go | 78 ------------------------- runtime/events/recorder.go | 13 +++-- runtime/events/recorder_fuzzer_test.go | 4 +- runtime/events/recorder_test.go | 10 ++-- runtime/events/zz_generated.deepcopy.go | 48 --------------- runtime/go.mod | 10 ++-- runtime/go.sum | 18 +++--- 7 files changed, 31 insertions(+), 150 deletions(-) delete mode 100644 runtime/events/event.go delete mode 100644 runtime/events/zz_generated.deepcopy.go diff --git a/runtime/events/event.go b/runtime/events/event.go deleted file mode 100644 index 419dbe923..000000000 --- a/runtime/events/event.go +++ /dev/null @@ -1,78 +0,0 @@ -/* -Copyright 2020 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 events - -import ( - corev1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -// These constants define valid event severity values. -const ( - // EventSeverityTrace represents a trace event, usually - // informing about actions taken during reconciliation. - EventSeverityTrace string = "trace" - // EventSeverityInfo represents an informational event, usually - // informing about changes. - EventSeverityInfo string = "info" - // EventSeverityError represent an error event, usually a warning - // that something goes wrong. - EventSeverityError string = "error" -) - -// EventTypeTrace represents a trace event. -const EventTypeTrace string = "Trace" - -// Event is a report of an event issued by a controller. -// +kubebuilder:object:generate=true -type Event struct { - // The object that this event is about. - // +required - InvolvedObject corev1.ObjectReference `json:"involvedObject"` - - // Severity type of this event (trace, info, error) - // +kubebuilder:validation:Enum=trace;info;error - // +required - Severity string `json:"severity"` - - // The time at which this event was recorded. - // +required - Timestamp metav1.Time `json:"timestamp"` - - // A human-readable description of this event. - // Maximum length 39,000 characters. - // +kubebuilder:validation:MaxLength=39000 - // +required - Message string `json:"message"` - - // A machine understandable string that gives the reason - // for the transition into the object's current status. - // +required - Reason string `json:"reason"` - - // Metadata of this event, e.g. apply change set. - // +optional - Metadata map[string]string `json:"metadata,omitempty"` - - // Name of the controller that emitted this event, e.g. `source-controller`. - // +required - ReportingController string `json:"reportingController"` - - // ID of the controller instance, e.g. `source-controller-xyzf`. - // +optional - ReportingInstance string `json:"reportingInstance,omitempty"` -} diff --git a/runtime/events/recorder.go b/runtime/events/recorder.go index 6f0717566..5bc11fb68 100644 --- a/runtime/events/recorder.go +++ b/runtime/events/recorder.go @@ -35,6 +35,7 @@ import ( "k8s.io/client-go/tools/reference" ctrl "sigs.k8s.io/controller-runtime" + eventv1 "github.com/fluxcd/pkg/apis/event/v1beta1" "github.com/fluxcd/pkg/runtime/logger" ) @@ -144,7 +145,7 @@ func (r *Recorder) AnnotatedEventf( // Do not send trace events to notification controller, // traces are persisted as Kubernetes events only as normal events. - if severity == EventSeverityTrace { + if severity == eventv1.EventSeverityTrace { r.EventRecorder.AnnotatedEventf(object, annotations, corev1.EventTypeNormal, reason, messageFmt, args...) return } @@ -190,7 +191,7 @@ func (r *Recorder) AnnotatedEventf( return } - event := Event{ + event := eventv1.Event{ InvolvedObject: *ref, Severity: severity, Timestamp: metav1.Now(), @@ -224,10 +225,10 @@ func (r *Recorder) AnnotatedEventf( func eventTypeToSeverity(eventType string) string { switch eventType { case corev1.EventTypeWarning: - return EventSeverityError - case EventTypeTrace: - return EventSeverityTrace + return eventv1.EventSeverityError + case eventv1.EventTypeTrace: + return eventv1.EventSeverityTrace default: - return EventSeverityInfo + return eventv1.EventSeverityInfo } } diff --git a/runtime/events/recorder_fuzzer_test.go b/runtime/events/recorder_fuzzer_test.go index 26add1d8f..bd133e017 100644 --- a/runtime/events/recorder_fuzzer_test.go +++ b/runtime/events/recorder_fuzzer_test.go @@ -35,6 +35,8 @@ import ( "k8s.io/apimachinery/pkg/runtime" utilruntime "k8s.io/apimachinery/pkg/util/runtime" ctrl "sigs.k8s.io/controller-runtime" + + eventv1 "github.com/fluxcd/pkg/apis/event/v1beta1" ) var ( @@ -65,7 +67,7 @@ func Fuzz_Eventf(f *testing.F) { return } - var payload Event + var payload eventv1.Event err = json.Unmarshal(b, &payload) if err != nil { return diff --git a/runtime/events/recorder_test.go b/runtime/events/recorder_test.go index e11c33c56..582462984 100644 --- a/runtime/events/recorder_test.go +++ b/runtime/events/recorder_test.go @@ -26,6 +26,8 @@ import ( "github.com/stretchr/testify/require" corev1 "k8s.io/api/core/v1" ctrl "sigs.k8s.io/controller-runtime" + + eventv1 "github.com/fluxcd/pkg/apis/event/v1beta1" ) func TestEventRecorder_AnnotatedEventf(t *testing.T) { @@ -35,7 +37,7 @@ func TestEventRecorder_AnnotatedEventf(t *testing.T) { b, err := io.ReadAll(r.Body) require.NoError(t, err) - var payload Event + var payload eventv1.Event err = json.Unmarshal(b, &payload) require.NoError(t, err) @@ -63,7 +65,7 @@ func TestEventRecorder_AnnotatedEventf(t *testing.T) { require.Equal(t, 2, requestCount) // When a trace event is sent, it's dropped, no new request. - eventRecorder.AnnotatedEventf(obj, meta, EventTypeTrace, "sync", "sync %s", obj.Name) + eventRecorder.AnnotatedEventf(obj, meta, eventv1.EventTypeTrace, "sync", "sync %s", obj.Name) require.Equal(t, 2, requestCount) } @@ -74,7 +76,7 @@ func TestEventRecorder_AnnotatedEventf_Retry(t *testing.T) { b, err := io.ReadAll(r.Body) require.NoError(t, err) - var payload Event + var payload eventv1.Event err = json.Unmarshal(b, &payload) require.NoError(t, err) @@ -101,7 +103,7 @@ func TestEventRecorder_AnnotatedEventf_RateLimited(t *testing.T) { b, err := io.ReadAll(r.Body) require.NoError(t, err) - var payload Event + var payload eventv1.Event err = json.Unmarshal(b, &payload) require.NoError(t, err) diff --git a/runtime/events/zz_generated.deepcopy.go b/runtime/events/zz_generated.deepcopy.go deleted file mode 100644 index be879ed24..000000000 --- a/runtime/events/zz_generated.deepcopy.go +++ /dev/null @@ -1,48 +0,0 @@ -//go:build !ignore_autogenerated -// +build !ignore_autogenerated - -/* -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. -*/ - -// Code generated by controller-gen. DO NOT EDIT. - -package events - -import () - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *Event) DeepCopyInto(out *Event) { - *out = *in - out.InvolvedObject = in.InvolvedObject - in.Timestamp.DeepCopyInto(&out.Timestamp) - if in.Metadata != nil { - in, out := &in.Metadata, &out.Metadata - *out = make(map[string]string, len(*in)) - for key, val := range *in { - (*out)[key] = val - } - } -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Event. -func (in *Event) DeepCopy() *Event { - if in == nil { - return nil - } - out := new(Event) - in.DeepCopyInto(out) - return out -} diff --git a/runtime/go.mod b/runtime/go.mod index 22e8fd83f..2a0242757 100644 --- a/runtime/go.mod +++ b/runtime/go.mod @@ -4,12 +4,14 @@ go 1.18 replace ( github.com/fluxcd/pkg/apis/acl => ../apis/acl + github.com/fluxcd/pkg/apis/event => ../apis/event github.com/fluxcd/pkg/apis/meta => ../apis/meta ) require ( github.com/AdaLogics/go-fuzz-headers v0.0.0-20220903154154-e8044f6e4c72 github.com/fluxcd/pkg/apis/acl v0.1.0 + github.com/fluxcd/pkg/apis/event v0.1.0 github.com/fluxcd/pkg/apis/meta v0.17.0 github.com/go-logr/logr v1.2.3 github.com/google/go-cmp v0.5.9 @@ -21,10 +23,10 @@ require ( github.com/spf13/pflag v1.0.5 github.com/stretchr/testify v1.8.0 go.uber.org/zap v1.23.0 - k8s.io/api v0.25.2 - k8s.io/apimachinery v0.25.2 - k8s.io/client-go v0.25.2 - k8s.io/component-base v0.25.2 + k8s.io/api v0.25.3 + k8s.io/apimachinery v0.25.3 + k8s.io/client-go v0.25.3 + k8s.io/component-base v0.25.3 k8s.io/klog/v2 v2.80.1 sigs.k8s.io/cli-utils v0.33.0 sigs.k8s.io/controller-runtime v0.13.0 diff --git a/runtime/go.sum b/runtime/go.sum index 21af2e913..98fc7ab44 100644 --- a/runtime/go.sum +++ b/runtime/go.sum @@ -509,7 +509,7 @@ go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5/go.mod h1:nmDLcffg48OtT/PSW0H go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw= go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/goleak v1.1.10/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A= -go.uber.org/goleak v1.1.12 h1:gZAh5/EyT/HQwlpkCy6wTpqfH9H8Lz8zbm3dZh+OyzA= +go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk= go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4= go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU= go.uber.org/zap v1.17.0/go.mod h1:MXVU+bhUf/A7Xi2HNOnopQOrmycQ5Ih87HtOu4q5SSo= @@ -930,23 +930,23 @@ honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k= honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k= k8s.io/api v0.24.0/go.mod h1:5Jl90IUrJHUJYEMANRURMiVvJ0g7Ax7r3R1bqO8zx8I= -k8s.io/api v0.25.2 h1:v6G8RyFcwf0HR5jQGIAYlvtRNrxMJQG1xJzaSeVnIS8= -k8s.io/api v0.25.2/go.mod h1:qP1Rn4sCVFwx/xIhe+we2cwBLTXNcheRyYXwajonhy0= +k8s.io/api v0.25.3 h1:Q1v5UFfYe87vi5H7NU0p4RXC26PPMT8KOpr1TLQbCMQ= +k8s.io/api v0.25.3/go.mod h1:o42gKscFrEVjHdQnyRenACrMtbuJsVdP+WVjqejfzmI= k8s.io/apiextensions-apiserver v0.25.0 h1:CJ9zlyXAbq0FIW8CD7HHyozCMBpDSiH7EdrSTCZcZFY= k8s.io/apiextensions-apiserver v0.25.0/go.mod h1:3pAjZiN4zw7R8aZC5gR0y3/vCkGlAjCazcg1me8iB/E= k8s.io/apimachinery v0.24.0/go.mod h1:82Bi4sCzVBdpYjyI4jY6aHX+YCUchUIrZrXKedjd2UM= -k8s.io/apimachinery v0.25.2 h1:WbxfAjCx+AeN8Ilp9joWnyJ6xu9OMeS/fsfjK/5zaQs= -k8s.io/apimachinery v0.25.2/go.mod h1:hqqA1X0bsgsxI6dXsJ4HnNTBOmJNxyPp8dw3u2fSHwA= +k8s.io/apimachinery v0.25.3 h1:7o9ium4uyUOM76t6aunP0nZuex7gDf8VGwkR5RcJnQc= +k8s.io/apimachinery v0.25.3/go.mod h1:jaF9C/iPNM1FuLl7Zuy5b9v+n35HGSh6AQ4HYRkCqwo= k8s.io/cli-runtime v0.24.0/go.mod h1:9XxoZDsEkRFUThnwqNviqzljtT/LdHtNWvcNFrAXl0A= k8s.io/cli-runtime v0.25.2 h1:XOx+SKRjBpYMLY/J292BHTkmyDffl/qOx3YSuFZkTuc= k8s.io/cli-runtime v0.25.2/go.mod h1:OQx3+/0st6x5YpkkJQlEWLC73V0wHsOFMC1/roxV8Oc= k8s.io/client-go v0.24.0/go.mod h1:VFPQET+cAFpYxh6Bq6f4xyMY80G6jKKktU6G0m00VDw= -k8s.io/client-go v0.25.2 h1:SUPp9p5CwM0yXGQrwYurw9LWz+YtMwhWd0GqOsSiefo= -k8s.io/client-go v0.25.2/go.mod h1:i7cNU7N+yGQmJkewcRD2+Vuj4iz7b30kI8OcL3horQ4= +k8s.io/client-go v0.25.3 h1:oB4Dyl8d6UbfDHD8Bv8evKylzs3BXzzufLiO27xuPs0= +k8s.io/client-go v0.25.3/go.mod h1:t39LPczAIMwycjcXkVc+CB+PZV69jQuNx4um5ORDjQA= k8s.io/code-generator v0.24.0/go.mod h1:dpVhs00hTuTdTY6jvVxvTFCk6gSMrtfRydbhZwHI15w= k8s.io/component-base v0.24.0/go.mod h1:Dgazgon0i7KYUsS8krG8muGiMVtUZxG037l1MKyXgrA= -k8s.io/component-base v0.25.2 h1:Nve/ZyHLUBHz1rqwkjXm/Re6IniNa5k7KgzxZpTfSQY= -k8s.io/component-base v0.25.2/go.mod h1:90W21YMr+Yjg7MX+DohmZLzjsBtaxQDDwaX4YxDkl60= +k8s.io/component-base v0.25.3 h1:UrsxciGdrCY03ULT1h/S/gXFCOPnLhUVwSyx+hM/zq4= +k8s.io/component-base v0.25.3/go.mod h1:WYoS8L+IlTZgU7rhAl5Ctpw0WdMxDfCC5dkxcEFa/TI= k8s.io/component-helpers v0.24.0/go.mod h1:Q2SlLm4h6g6lPTC9GMMfzdywfLSvJT2f1hOnnjaWD8c= k8s.io/gengo v0.0.0-20200413195148-3a45101e95ac/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0= k8s.io/gengo v0.0.0-20210813121822-485abfe95c7c/go.mod h1:FiNAH4ZV3gBg2Kwh89tzAEV2be7d5xI0vBa/VySYy3E=