Add revision to events metadata

This commit is contained in:
stefanprodan 2020-07-02 08:01:06 +03:00
parent 2ebd5b6450
commit 406ce977a4
2 changed files with 18 additions and 17 deletions

View File

@ -134,7 +134,7 @@ func (r *KustomizationReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro
// instead we requeue on a fix interval. // instead we requeue on a fix interval.
msg := fmt.Sprintf("Dependencies do not meet ready condition, retrying in %s", r.requeueDependency.String()) msg := fmt.Sprintf("Dependencies do not meet ready condition, retrying in %s", r.requeueDependency.String())
log.Error(err, msg) log.Error(err, msg)
r.event(kustomization, msg, "info") r.event(kustomization, source.GetArtifact().Revision, recorder.EventSeverityInfo, msg)
return ctrl.Result{RequeueAfter: r.requeueDependency}, nil return ctrl.Result{RequeueAfter: r.requeueDependency}, nil
} }
log.Info("All dependencies area ready, proceeding with apply") log.Info("All dependencies area ready, proceeding with apply")
@ -144,7 +144,7 @@ func (r *KustomizationReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro
syncedKustomization, err := r.sync(*kustomization.DeepCopy(), source) syncedKustomization, err := r.sync(*kustomization.DeepCopy(), source)
if err != nil { if err != nil {
log.Error(err, "Kustomization apply failed", "revision", source.GetArtifact().Revision) log.Error(err, "Kustomization apply failed", "revision", source.GetArtifact().Revision)
r.event(kustomization, err.Error(), "error") r.event(kustomization, source.GetArtifact().Revision, recorder.EventSeverityError, err.Error())
} }
// update status // update status
@ -249,7 +249,7 @@ func (r *KustomizationReconciler) sync(
} }
// apply // apply
err = r.applyWithRetry(kustomization, dirPath, 5*time.Second) err = r.applyWithRetry(kustomization, source.GetArtifact().Revision, dirPath, 5*time.Second)
if err != nil { if err != nil {
return kustomizev1.KustomizationNotReady( return kustomizev1.KustomizationNotReady(
kustomization, kustomization,
@ -269,7 +269,7 @@ func (r *KustomizationReconciler) sync(
} }
// health assessment // health assessment
err = r.checkHealth(kustomization) err = r.checkHealth(kustomization, source.GetArtifact().Revision)
if err != nil { if err != nil {
return kustomizev1.KustomizationNotReadySnapshot( return kustomizev1.KustomizationNotReadySnapshot(
kustomization, kustomization,
@ -284,7 +284,7 @@ func (r *KustomizationReconciler) sync(
snapshot, snapshot,
source.GetArtifact().Revision, source.GetArtifact().Revision,
kustomizev1.ApplySucceedReason, kustomizev1.ApplySucceedReason,
"kustomization was successfully applied", "Applied revision: "+source.GetArtifact().Revision,
), nil ), nil
} }
@ -451,7 +451,7 @@ func (r *KustomizationReconciler) validate(kustomization kustomizev1.Kustomizati
return nil return nil
} }
func (r *KustomizationReconciler) apply(kustomization kustomizev1.Kustomization, dirPath string) error { func (r *KustomizationReconciler) apply(kustomization kustomizev1.Kustomization, revision, dirPath string) error {
start := time.Now() start := time.Now()
timeout := kustomization.GetTimeout() + (time.Second * 1) timeout := kustomization.GetTimeout() + (time.Second * 1)
ctx, cancel := context.WithTimeout(context.Background(), timeout) ctx, cancel := context.WithTimeout(context.Background(), timeout)
@ -485,20 +485,21 @@ func (r *KustomizationReconciler) apply(kustomization kustomizev1.Kustomization,
) )
var diff bool var diff bool
for _, action := range resources { changeSet := ""
if action != "unchanged" { for obj, action := range resources {
if action != "" && action != "unchanged" {
diff = true diff = true
break changeSet += obj + " " + action + "\n"
} }
} }
if diff { if diff {
r.event(kustomization, string(output), "info") r.event(kustomization, revision, recorder.EventSeverityInfo, changeSet)
} }
return nil return nil
} }
func (r *KustomizationReconciler) applyWithRetry(kustomization kustomizev1.Kustomization, dirPath string, delay time.Duration) error { func (r *KustomizationReconciler) applyWithRetry(kustomization kustomizev1.Kustomization, revision, dirPath string, delay time.Duration) error {
err := r.apply(kustomization, dirPath) err := r.apply(kustomization, revision, dirPath)
if err != nil { if err != nil {
// retry apply due to CRD/CR race // retry apply due to CRD/CR race
if strings.Contains(err.Error(), "could not find the requested resource") || if strings.Contains(err.Error(), "could not find the requested resource") ||
@ -507,7 +508,7 @@ func (r *KustomizationReconciler) applyWithRetry(kustomization kustomizev1.Kusto
"error", err.Error(), "error", err.Error(),
"kustomization", fmt.Sprintf("%s/%s", kustomization.GetNamespace(), kustomization.GetName())) "kustomization", fmt.Sprintf("%s/%s", kustomization.GetNamespace(), kustomization.GetName()))
time.Sleep(delay) time.Sleep(delay)
if err := r.apply(kustomization, dirPath); err != nil { if err := r.apply(kustomization, revision, dirPath); err != nil {
return err return err
} }
} else { } else {
@ -536,7 +537,7 @@ func (r *KustomizationReconciler) prune(kustomization kustomizev1.Kustomization,
return nil return nil
} }
func (r *KustomizationReconciler) checkHealth(kustomization kustomizev1.Kustomization) error { func (r *KustomizationReconciler) checkHealth(kustomization kustomizev1.Kustomization, revision string) error {
timeout := kustomization.GetTimeout() + (time.Second * 1) timeout := kustomization.GetTimeout() + (time.Second * 1)
ctx, cancel := context.WithTimeout(context.Background(), timeout) ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel() defer cancel()
@ -574,7 +575,7 @@ func (r *KustomizationReconciler) checkHealth(kustomization kustomizev1.Kustomiz
} }
if alerts != "" { if alerts != "" {
r.event(kustomization, alerts, "info") r.event(kustomization, revision, recorder.EventSeverityInfo, alerts)
} }
return nil return nil
} }
@ -632,7 +633,7 @@ func (r *KustomizationReconciler) checkDependencies(kustomization kustomizev1.Ku
return nil return nil
} }
func (r *KustomizationReconciler) event(kustomization kustomizev1.Kustomization, msg string, severity string) { func (r *KustomizationReconciler) event(kustomization kustomizev1.Kustomization, revision, severity, msg string) {
r.EventRecorder.Event(&kustomization, "Normal", severity, msg) r.EventRecorder.Event(&kustomization, "Normal", severity, msg)
objRef, err := reference.GetReference(r.Scheme, &kustomization) objRef, err := reference.GetReference(r.Scheme, &kustomization)
if err != nil { if err != nil {
@ -644,7 +645,7 @@ func (r *KustomizationReconciler) event(kustomization kustomizev1.Kustomization,
} }
if r.ExternalEventRecorder != nil { if r.ExternalEventRecorder != nil {
if err := r.ExternalEventRecorder.Eventf(*objRef, nil, severity, severity, msg); err != nil { if err := r.ExternalEventRecorder.Eventf(*objRef, map[string]string{"revision": revision}, severity, severity, msg); err != nil {
r.Log.WithValues( r.Log.WithValues(
strings.ToLower(kustomization.Kind), strings.ToLower(kustomization.Kind),
fmt.Sprintf("%s/%s", kustomization.GetNamespace(), kustomization.GetName()), fmt.Sprintf("%s/%s", kustomization.GetNamespace(), kustomization.GetName()),

Binary file not shown.

Before

Width:  |  Height:  |  Size: 26 KiB

After

Width:  |  Height:  |  Size: 67 KiB