diff --git a/config/manager/kustomization.yaml b/config/manager/kustomization.yaml index 31b37ecf..e483dc4f 100644 --- a/config/manager/kustomization.yaml +++ b/config/manager/kustomization.yaml @@ -9,4 +9,4 @@ generatorOptions: images: - name: chanwit/tf-controller newName: ghcr.io/chanwit/tf-controller - newTag: v0.2.0 + newTag: v0.3.0-rc.1 diff --git a/controllers/terraform_controller.go b/controllers/terraform_controller.go index 1e4aca50..91342500 100644 --- a/controllers/terraform_controller.go +++ b/controllers/terraform_controller.go @@ -950,3 +950,42 @@ func (r *TerraformReconciler) indexBy(kind string) func(o client.Object) []strin return nil } } + +func (r *TerraformReconciler) event(ctx context.Context, terraform infrav1.Terraform, revision, severity, msg string, metadata map[string]string) { + log := logr.FromContext(ctx) + + annotations := map[string]string{ + infrav1.GroupVersion.Group + "/revision": revision, + } + + eventType := "Normal" + if severity == events.EventSeverityError { + eventType = "Warning" + } + + r.EventRecorder.AnnotatedEventf(&terraform, annotations, eventType, severity, msg) + + if r.ExternalEventRecorder != nil { + objRef, err := reference.GetReference(r.Scheme, &terraform) + if err != nil { + log.Error(err, "unable to send event") + return + } + if metadata == nil { + metadata = map[string]string{} + } + if revision != "" { + metadata["revision"] = revision + } + + reason := severity + if c := apimeta.FindStatusCondition(terraform.Status.Conditions, meta.ReadyCondition); c != nil { + reason = c.Reason + } + + if err := r.ExternalEventRecorder.Eventf(*objRef, metadata, severity, reason, msg); err != nil { + log.Error(err, "unable to send event") + return + } + } +}