Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/run-tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Test and coverage

on:
pull_request:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- uses: actions/setup-go@v6
with:
go-version: 'stable'
- name: Gather dependencies
run: go mod download
- name: Run coverage
run: go test -race -coverprofile=coverage.txt -covermode=atomic ./...
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v6
with:
token: ${{ secrets.CODECOV_TOKEN }}
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
module github.com/krateoplatformops/provider-runtime

go 1.25.3
go 1.25.6

require (
github.com/go-logr/logr v1.4.3
github.com/google/go-cmp v0.7.0
github.com/krateoplatformops/plumbing v1.3.1
github.com/krateoplatformops/plumbing v1.7.1
golang.org/x/time v0.12.0
k8s.io/api v0.35.2
k8s.io/apimachinery v0.35.2
k8s.io/client-go v0.35.2
k8s.io/api v0.35.3
k8s.io/apimachinery v0.35.3
k8s.io/client-go v0.35.3
sigs.k8s.io/controller-runtime v0.23.3
sigs.k8s.io/controller-tools v0.20.1
)
Expand Down
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/krateoplatformops/plumbing v1.3.1 h1:DQc/ohBiGfI6mC3l2T+3ZvtSR0r3hOLqdxu73xjZUDM=
github.com/krateoplatformops/plumbing v1.3.1/go.mod h1:51a1TKAoCXRHH7cJB8kiYZcIV8ci3PLPtabqBdoD9Zg=
github.com/krateoplatformops/plumbing v1.7.1 h1:ZAjeAbfSNE9isrfb90k51IGtSp1hQjw55ndVnxcHtaE=
github.com/krateoplatformops/plumbing v1.7.1/go.mod h1:L8dMKmq9hO1tz9NzJPlryBj618J5w0PYt8z6fzAbBvs=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/mailru/easyjson v0.9.0 h1:PrnmzHw7262yW8sTBwxi1PdJA3Iw/EKBa8psRf7d9a4=
Expand Down Expand Up @@ -215,16 +215,16 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
k8s.io/api v0.35.2 h1:tW7mWc2RpxW7HS4CoRXhtYHSzme1PN1UjGHJ1bdrtdw=
k8s.io/api v0.35.2/go.mod h1:7AJfqGoAZcwSFhOjcGM7WV05QxMMgUaChNfLTXDRE60=
k8s.io/api v0.35.3 h1:pA2fiBc6+N9PDf7SAiluKGEBuScsTzd2uYBkA5RzNWQ=
k8s.io/api v0.35.3/go.mod h1:9Y9tkBcFwKNq2sxwZTQh1Njh9qHl81D0As56tu42GA4=
k8s.io/apiextensions-apiserver v0.35.0 h1:3xHk2rTOdWXXJM+RDQZJvdx0yEOgC0FgQ1PlJatA5T4=
k8s.io/apiextensions-apiserver v0.35.0/go.mod h1:E1Ahk9SADaLQ4qtzYFkwUqusXTcaV2uw3l14aqpL2LU=
k8s.io/apimachinery v0.35.2 h1:NqsM/mmZA7sHW02JZ9RTtk3wInRgbVxL8MPfzSANAK8=
k8s.io/apimachinery v0.35.2/go.mod h1:jQCgFZFR1F4Ik7hvr2g84RTJSZegBc8yHgFWKn//hns=
k8s.io/apimachinery v0.35.3 h1:MeaUwQCV3tjKP4bcwWGgZ/cp/vpsRnQzqO6J6tJyoF8=
k8s.io/apimachinery v0.35.3/go.mod h1:jQCgFZFR1F4Ik7hvr2g84RTJSZegBc8yHgFWKn//hns=
k8s.io/apiserver v0.35.0 h1:CUGo5o+7hW9GcAEF3x3usT3fX4f9r8xmgQeCBDaOgX4=
k8s.io/apiserver v0.35.0/go.mod h1:QUy1U4+PrzbJaM3XGu2tQ7U9A4udRRo5cyxkFX0GEds=
k8s.io/client-go v0.35.2 h1:YUfPefdGJA4aljDdayAXkc98DnPkIetMl4PrKX97W9o=
k8s.io/client-go v0.35.2/go.mod h1:4QqEwh4oQpeK8AaefZ0jwTFJw/9kIjdQi0jpKeYvz7g=
k8s.io/client-go v0.35.3 h1:s1lZbpN4uI6IxeTM2cpdtrwHcSOBML1ODNTCCfsP1pg=
k8s.io/client-go v0.35.3/go.mod h1:RzoXkc0mzpWIDvBrRnD+VlfXP+lRzqQjCmKtiwZ8Q9c=
k8s.io/code-generator v0.35.0 h1:TvrtfKYZTm9oDF2z+veFKSCcgZE3Igv0svY+ehCmjHQ=
k8s.io/code-generator v0.35.0/go.mod h1:iS1gvVf3c/T71N5DOGYO+Gt3PdJ6B9LYSvIyQ4FHzgc=
k8s.io/component-base v0.35.0 h1:+yBrOhzri2S1BVqyVSvcM3PtPyx5GUxCK2tinZz1G94=
Expand Down
42 changes: 28 additions & 14 deletions pkg/reconciler/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,9 @@ type Reconciler struct {
external mrExternal
managed mrManaged

log logging.Logger
record event.Recorder
log logging.Logger
record event.Recorder
throttledRecorder event.Recorder
}

type mrManaged struct {
Expand Down Expand Up @@ -476,6 +477,13 @@ func WithRecorder(er event.Recorder) ReconcilerOption {
}
}

// WithThrottledRecorder specifies a separate event recorder for events that are emitted when an error is encountered during reconciliation. This allows users of the Reconciler to choose to emit these events at a different level (e.g. Warning instead of Normal) without having to use the same level for all events. Defaults to the same recorder as the one set by WithRecorder if not specified.
func WithThrottledRecorder(er event.Recorder) ReconcilerOption {
return func(r *Reconciler) {
r.throttledRecorder = er
}
}

// NewReconciler returns a Reconciler that reconciles managed resources of the
// supplied ManagedKind with resources in an external system such as a cloud
// provider API. It panics if asked to reconcile a managed resource kind that is
Expand Down Expand Up @@ -509,6 +517,11 @@ func NewReconciler(m manager.Manager, of resource.ManagedKind, o ...ReconcilerOp
ro(r)
}

// Default the throttled recorder to the same recorder as the main recorder if it wasn't explicitly set by the caller.
if r.throttledRecorder == nil {
r.throttledRecorder = r.record
}

return r
}

Expand All @@ -532,6 +545,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req reconcile.Request) (reco
}

record := r.record
throttledRecorder := r.throttledRecorder
log = log.WithValues(
"uid", managed.GetUID(),
"version", managed.GetResourceVersion(),
Expand Down Expand Up @@ -582,7 +596,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req reconcile.Request) (reco
// do is to refuse to proceed.
if meta.ExternalCreateIncomplete(managed) {
log.Warn(errCreateIncomplete)
record.Event(managed, event.Warning(reasonCannotInitialize, actionProcessEvent, errors.New(errCreateIncomplete)))
throttledRecorder.Event(managed, event.Warning(reasonCannotInitialize, actionProcessEvent, errors.New(errCreateIncomplete)))
managed.SetConditions(prv1.Creating(), prv1.ReconcileError(errors.New(errCreateIncomplete)))
return reconcile.Result{Requeue: false}, errors.Wrap(r.client.Status().Update(ctx, managed), errUpdateManagedStatus)
}
Expand All @@ -598,14 +612,14 @@ func (r *Reconciler) Reconcile(ctx context.Context, req reconcile.Request) (reco
if kerrors.IsConflict(err) {
return reconcile.Result{Requeue: true}, nil
}
record.Event(managed, event.Warning(reasonCannotConnect, actionProcessEvent, err))
throttledRecorder.Event(managed, event.Warning(reasonCannotConnect, actionProcessEvent, err))
managed.SetConditions(prv1.ReconcileError(errors.Wrap(err, errReconcileConnect)))
return reconcile.Result{Requeue: true}, errors.Wrap(r.client.Status().Update(ctx, managed), errUpdateManagedStatus)
}
defer func() {
if err := r.external.Disconnect(ctx); err != nil {
log.Error(err, "Cannot disconnect from provider")
record.Event(managed, event.Warning(reasonCannotDisconnect, actionProcessEvent, err))
throttledRecorder.Event(managed, event.Warning(reasonCannotDisconnect, actionProcessEvent, err))
}
}()

Expand All @@ -621,15 +635,15 @@ func (r *Reconciler) Reconcile(ctx context.Context, req reconcile.Request) (reco
if kerrors.IsConflict(err) {
return reconcile.Result{Requeue: true}, nil
}
record.Event(managed, event.Warning(reasonCannotObserve, actionObserveExternalResource, err))
throttledRecorder.Event(managed, event.Warning(reasonCannotObserve, actionObserveExternalResource, err))
managed.SetConditions(prv1.ReconcileError(errors.Wrap(err, errReconcileObserve)))
return reconcile.Result{Requeue: true}, errors.Wrap(r.client.Status().Update(ctx, managed), errUpdateManagedStatus)
}

// In the observe-only mode, !observation.ResourceExists will be an error
// case, and we will explicitly return this information to the user.
if !observation.ResourceExists && meta.ShouldOnlyObserve(managed) {
record.Event(managed, event.Warning(reasonCannotObserve, actionObserveExternalResource, errors.New(errExternalResourceNotExist)))
throttledRecorder.Event(managed, event.Warning(reasonCannotObserve, actionObserveExternalResource, errors.New(errExternalResourceNotExist)))
managed.SetConditions(prv1.ReconcileError(errors.Wrap(errors.New(errExternalResourceNotExist), errReconcileObserve)))
return reconcile.Result{Requeue: true}, errors.Wrap(r.client.Status().Update(ctx, managed), errUpdateManagedStatus)
}
Expand Down Expand Up @@ -659,7 +673,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req reconcile.Request) (reco
// status with the new error condition. If not, we want requeue
// explicitly, which will trigger backoff.
log.Error(err, "Cannot delete external resource")
record.Event(managed, event.Warning(reasonCannotDelete, actionDeleteExternalResource, err))
throttledRecorder.Event(managed, event.Warning(reasonCannotDelete, actionDeleteExternalResource, err))
managed.SetConditions(prv1.Deleting(), prv1.ReconcileError(errors.Wrap(err, errReconcileDelete)))
return reconcile.Result{Requeue: true}, errors.Wrap(r.client.Status().Update(ctx, managed), errUpdateManagedStatus)
}
Expand Down Expand Up @@ -724,7 +738,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req reconcile.Request) (reco
if kerrors.IsConflict(err) {
return reconcile.Result{Requeue: true}, nil
}
record.Event(managed, event.Warning(reasonCannotUpdateManaged, actionUpdateExternalResource, errors.Wrap(err, errUpdateManaged)))
throttledRecorder.Event(managed, event.Warning(reasonCannotUpdateManaged, actionUpdateExternalResource, errors.Wrap(err, errUpdateManaged)))
managed.SetConditions(prv1.Creating(), prv1.ReconcileError(errors.Wrap(err, errUpdateManaged)))
return reconcile.Result{Requeue: true}, errors.Wrap(r.client.Status().Update(ctx, managed), errUpdateManagedStatus)
}
Expand All @@ -740,7 +754,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req reconcile.Request) (reco
if kerrors.IsConflict(err) {
return reconcile.Result{Requeue: true}, nil
}
record.Event(managed, event.Warning(reasonCannotCreate, actionCreateExternalResource, err))
throttledRecorder.Event(managed, event.Warning(reasonCannotCreate, actionCreateExternalResource, err))

// We handle annotations specially here because it's
// critical that they are persisted to the API server.
Expand All @@ -751,7 +765,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req reconcile.Request) (reco
meta.SetExternalCreateFailed(managed, time.Now())
if err := r.managed.UpdateCriticalAnnotations(ctx, managed); err != nil {
log.Error(err, errUpdateManagedAnnotations)
record.Event(managed, event.Warning(reasonCannotUpdateManaged, actionUpdateExternalResource, errors.Wrap(err, errUpdateManagedAnnotations)))
throttledRecorder.Event(managed, event.Warning(reasonCannotUpdateManaged, actionUpdateExternalResource, errors.Wrap(err, errUpdateManagedAnnotations)))

// We only log and emit an event here rather
// than setting a status condition and returning
Expand Down Expand Up @@ -781,7 +795,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req reconcile.Request) (reco
meta.SetExternalCreateSucceeded(managed, time.Now())
if err := r.managed.UpdateCriticalAnnotations(ctx, managed); err != nil {
log.Error(err, errUpdateManagedAnnotations)
record.Event(managed, event.Warning(reasonCannotUpdateManaged, actionUpdateExternalResource, errors.Wrap(err, errUpdateManagedAnnotations)))
throttledRecorder.Event(managed, event.Warning(reasonCannotUpdateManaged, actionUpdateExternalResource, errors.Wrap(err, errUpdateManagedAnnotations)))
managed.SetConditions(prv1.Creating(), prv1.ReconcileError(errors.Wrap(err, errUpdateManagedAnnotations)))
return reconcile.Result{Requeue: true}, errors.Wrap(r.client.Status().Update(ctx, managed), errUpdateManagedStatus)
}
Expand All @@ -806,7 +820,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req reconcile.Request) (reco
// and persist its status.
if err := r.client.Update(ctx, managed); err != nil {
log.Error(err, "Cannot update managed resource")
record.Event(managed, event.Warning(reasonCannotUpdateManaged, actionUpdateExternalResource, err))
throttledRecorder.Event(managed, event.Warning(reasonCannotUpdateManaged, actionUpdateExternalResource, err))
managed.SetConditions(prv1.ReconcileError(errors.Wrap(err, errUpdateManaged)))
return reconcile.Result{Requeue: true}, errors.Wrap(r.client.Status().Update(ctx, managed), errUpdateManagedStatus)
}
Expand Down Expand Up @@ -845,7 +859,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req reconcile.Request) (reco
// requeued implicitly when we update our status with the new error
// condition. If not, we requeue explicitly, which will trigger backoff.
log.Error(err, "Cannot update external resource")
record.Event(managed, event.Warning(reasonCannotUpdate, actionUpdateExternalResource, err))
throttledRecorder.Event(managed, event.Warning(reasonCannotUpdate, actionUpdateExternalResource, err))
managed.SetConditions(prv1.ReconcileError(errors.Wrap(err, errReconcileUpdate)))
return reconcile.Result{Requeue: true}, errors.Wrap(r.client.Status().Update(ctx, managed), errUpdateManagedStatus)
}
Expand Down