Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/go_modules/github.com/cert-manage…
Browse files Browse the repository at this point in the history
…r/cert-manager-1.14.5
  • Loading branch information
nesmabadr authored May 6, 2024
2 parents 482576a + 407b873 commit a723ce0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 60 deletions.
45 changes: 12 additions & 33 deletions internal/controller/kyma_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,15 @@ import (

type (
EventReasonError string
EventReasonInfo string
)

var ErrManifestsStillExist = errors.New("manifests still exist")

const (
moduleReconciliationError EventReasonError = "ModuleReconciliationError"
syncContextError EventReasonError = "SyncContextError"
metricsError EventReasonError = "MetricsError"
deletionError EventReasonError = "DeletionError"
updateStatus EventReasonInfo = "StatusUpdate"
webhookChartRemoval EventReasonInfo = "WebhookChartRemoval"
updateSpecError EventReasonError = "UpdateSpecError"
updateStatusError EventReasonError = "UpdateStatusError"
)

type KymaReconciler struct {
Expand Down Expand Up @@ -137,12 +134,11 @@ func (r *KymaReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
if apierrors.IsUnauthorized(err) {
r.deleteRemoteClientCache(ctx, kyma)
r.Metrics.RecordRequeueReason(metrics.KymaUnauthorized, queue.UnexpectedRequeue)
return ctrl.Result{Requeue: true}, r.updateStatusWithError(ctx, kyma, err)
return r.requeueWithError(ctx, kyma, err)
}

if err != nil {
r.deleteRemoteClientCache(ctx, kyma)
r.enqueueWarningEvent(kyma, syncContextError, err)
r.Metrics.RecordRequeueReason(metrics.SyncContextRetrieval, queue.UnexpectedRequeue)
return r.requeueWithError(ctx, kyma, err)
}
Expand Down Expand Up @@ -264,22 +260,13 @@ func (r *KymaReconciler) deleteRemoteKyma(ctx context.Context, kyma *v1beta2.Kym
}

func (r *KymaReconciler) requeueWithError(ctx context.Context, kyma *v1beta2.Kyma, err error) (ctrl.Result, error) {
updateErr := r.updateStatusWithError(ctx, kyma, err)
if updateErr == nil {
r.enqueueWarningEvent(kyma, moduleReconciliationError, err)
}

return ctrl.Result{Requeue: true}, updateErr
return ctrl.Result{Requeue: true}, r.updateStatusWithError(ctx, kyma, err)
}

func (r *KymaReconciler) enqueueWarningEvent(kyma *v1beta2.Kyma, reason EventReasonError, err error) {
r.Event(kyma, "Warning", string(reason), err.Error())
}

func (r *KymaReconciler) enqueueNormalEvent(kyma *v1beta2.Kyma, reason EventReasonInfo, message string) {
r.Event(kyma, "Normal", string(reason), message)
}

func (r *KymaReconciler) fetchRemoteKyma(ctx context.Context, controlPlaneKyma *v1beta2.Kyma) (*v1beta2.Kyma, error) {
syncContext, err := remote.SyncContextFromContext(ctx)
if err != nil {
Expand Down Expand Up @@ -350,12 +337,10 @@ func (r *KymaReconciler) processKymaState(ctx context.Context, kyma *v1beta2.Kym
}

func (r *KymaReconciler) handleInitialState(ctx context.Context, kyma *v1beta2.Kyma) (ctrl.Result, error) {
const msg = "started processing"
if err := r.updateStatus(ctx, kyma, shared.StateProcessing, msg); err != nil {
if err := r.updateStatus(ctx, kyma, shared.StateProcessing, "started processing"); err != nil {
r.Metrics.RecordRequeueReason(metrics.InitialStateHandling, queue.UnexpectedRequeue)
return ctrl.Result{}, err
}
r.enqueueNormalEvent(kyma, updateStatus, msg)
r.Metrics.RecordRequeueReason(metrics.InitialStateHandling, queue.IntendedRequeue)
return ctrl.Result{Requeue: true}, nil
}
Expand Down Expand Up @@ -426,35 +411,28 @@ func (r *KymaReconciler) handleDeletingState(ctx context.Context, kyma *v1beta2.

if r.WatcherEnabled(kyma) {
if err := r.SKRWebhookManager.Remove(ctx, kyma); err != nil {
// error is expected, try again
r.enqueueNormalEvent(kyma, webhookChartRemoval, err.Error())
return ctrl.Result{RequeueAfter: r.RequeueIntervals.Busy}, nil
return ctrl.Result{}, err
}
r.SKRWebhookManager.WatcherMetrics.CleanupMetrics(kyma.Name)
}

if r.SyncKymaEnabled(kyma) {
if err := remote.NewRemoteCatalogFromKyma(r.RemoteSyncNamespace).Delete(ctx); err != nil {
err = fmt.Errorf("could not delete remote module catalog: %w", err)
r.enqueueWarningEvent(kyma, deletionError, err)
r.Metrics.RecordRequeueReason(metrics.RemoteModuleCatalogDeletion, queue.UnexpectedRequeue)
return ctrl.Result{}, err
return r.requeueWithError(ctx, kyma, err)
}

r.RemoteClientCache.Delete(client.ObjectKeyFromObject(kyma))
if err := remote.RemoveFinalizersFromRemoteKyma(ctx, r.RemoteSyncNamespace); client.IgnoreNotFound(err) != nil {
err = fmt.Errorf("error while trying to remove finalizer from remote: %w", err)
r.enqueueWarningEvent(kyma, deletionError, err)
r.Metrics.RecordRequeueReason(metrics.FinalizersRemovalFromRemoteKyma, queue.UnexpectedRequeue)
return ctrl.Result{}, err
return r.requeueWithError(ctx, kyma, err)
}

logger.Info("removed remote finalizers")
}

err := r.cleanupManifestCRs(ctx, kyma)
if err != nil {
r.enqueueWarningEvent(kyma, deletionError, err)
if err := r.cleanupManifestCRs(ctx, kyma); err != nil {
r.Metrics.RecordRequeueReason(metrics.CleanupManifestCrs, queue.UnexpectedRequeue)
return ctrl.Result{}, err
}
Expand Down Expand Up @@ -520,7 +498,7 @@ func (r *KymaReconciler) removeAllFinalizers(kyma *v1beta2.Kyma) {
func (r *KymaReconciler) updateKyma(ctx context.Context, kyma *v1beta2.Kyma) error {
if err := r.Update(ctx, kyma); err != nil {
err = fmt.Errorf("error while updating kyma during deletion: %w", err)
r.enqueueWarningEvent(kyma, deletionError, err)
r.enqueueWarningEvent(kyma, updateSpecError, err)
return err
}

Expand Down Expand Up @@ -571,16 +549,17 @@ func (r *KymaReconciler) updateStatus(ctx context.Context, kyma *v1beta2.Kyma,
state shared.State, message string,
) error {
if err := status.Helper(r).UpdateStatusForExistingModules(ctx, kyma, state, message); err != nil {
r.Event(kyma, "Warning", "PatchStatus", err.Error())
return fmt.Errorf("error while updating status to %s because of %s: %w", state, message, err)
}
return nil
}

func (r *KymaReconciler) updateStatusWithError(ctx context.Context, kyma *v1beta2.Kyma, err error) error {
if err := status.Helper(r).UpdateStatusForExistingModules(ctx, kyma, shared.StateError, err.Error()); err != nil {
r.enqueueWarningEvent(kyma, updateStatusError, err)
return fmt.Errorf("error while updating status to %s: %w", shared.StateError, err)
}

return nil
}

Expand Down
24 changes: 2 additions & 22 deletions internal/declarative/v2/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
return r.removeFinalizers(ctx, obj, obj.GetFinalizers(), metrics.ManifestRemoveFinalizerWhenSecretGone, err)
}

r.Event(obj, "Warning", "ClientInitialization", err.Error())
obj.SetStatus(obj.GetStatus().WithState(shared.StateError).WithErr(err))
return r.ssaStatusIfDiffExist(ctx, obj, metrics.ManifestClientInit, currentObjStatus, err)
}
Expand Down Expand Up @@ -247,11 +246,10 @@ func (r *Reconciler) removeFinalizers(ctx context.Context, obj Object, finalizer
if finalizerRemoved {
return r.updateObject(ctx, obj, requeueReason, queue.IntendedRequeue)
}
msg := fmt.Sprintf("waiting as other finalizers are present: %s", obj.GetFinalizers())
r.Event(obj, "Normal", "FinalizerRemoval", msg)

if obj.GetStatus().State != shared.StateWarning {
obj.SetStatus(obj.GetStatus().WithState(shared.StateDeleting).WithOperation(msg))
obj.SetStatus(obj.GetStatus().WithState(shared.StateDeleting).
WithOperation(fmt.Sprintf("waiting as other finalizers are present: %s", obj.GetFinalizers())))
}

return r.ssaStatusIfDiffExist(ctx, obj, requeueReason, currentObjStatus, err)
Expand Down Expand Up @@ -295,7 +293,6 @@ func (r *Reconciler) initialize(obj Object) error {
func (r *Reconciler) Spec(ctx context.Context, obj Object) (*Spec, error) {
spec, err := r.SpecResolver.Spec(ctx, obj)
if err != nil {
r.Event(obj, "Warning", "Spec", err.Error())
obj.SetStatus(obj.GetStatus().WithState(shared.StateError).WithErr(err))
}
return spec, err
Expand All @@ -322,13 +319,11 @@ func (r *Reconciler) renderResources(

current, err = converter.ResourcesToInfos(status.Synced)
if err != nil {
r.Event(obj, "Warning", "CurrentResourceParsing", err.Error())
obj.SetStatus(status.WithState(shared.StateError).WithErr(err))
return nil, nil, err
}

if !meta.IsStatusConditionTrue(status.Conditions, resourceCondition.Type) {
r.Event(obj, "Normal", resourceCondition.Reason, resourceCondition.Message)
resourceCondition.Status = apimetav1.ConditionTrue
meta.SetStatusCondition(&status.Conditions, resourceCondition)
obj.SetStatus(status.WithOperation(resourceCondition.Message))
Expand All @@ -343,7 +338,6 @@ func (r *Reconciler) syncResources(ctx context.Context, clnt Client, obj Object,
status := obj.GetStatus()

if err := ConcurrentSSA(clnt, r.FieldOwner).Run(ctx, target); err != nil {
r.Event(obj, "Warning", "ServerSideApply", err.Error())
obj.SetStatus(status.WithState(shared.StateError).WithErr(err))
return err
}
Expand All @@ -363,7 +357,6 @@ func (r *Reconciler) syncResources(ctx context.Context, clnt Client, obj Object,

for i := range r.PostRuns {
if err := r.PostRuns[i](ctx, clnt, r.Client, obj); err != nil {
r.Event(obj, "Warning", "PostRun", err.Error())
obj.SetStatus(status.WithState(shared.StateError).WithErr(err))
return err
}
Expand Down Expand Up @@ -402,21 +395,18 @@ func (r *Reconciler) checkTargetReadiness(

crStateInfo, err := resourceReadyCheck.Run(ctx, clnt, manifest, target)
if err != nil {
r.Event(manifest, "Warning", "ResourceReadyCheck", err.Error())
manifest.SetStatus(status.WithState(shared.StateError).WithErr(err))
return err
}

if crStateInfo.State == shared.StateProcessing {
waitingMsg := "waiting for resources to become ready: " + crStateInfo.Info
r.Event(manifest, "Normal", "ResourceReadyCheck", waitingMsg)
manifest.SetStatus(status.WithState(shared.StateProcessing).WithOperation(waitingMsg))
return ErrInstallationConditionRequiresUpdate
}

installationCondition := newInstallationCondition(manifest)
if !meta.IsStatusConditionTrue(status.Conditions, installationCondition.Type) || status.State != crStateInfo.State {
r.Event(manifest, "Normal", installationCondition.Reason, installationCondition.Message)
installationCondition.Status = apimetav1.ConditionTrue
meta.SetStatusCondition(&status.Conditions, installationCondition)
manifest.SetStatus(status.WithState(crStateInfo.State).
Expand All @@ -438,7 +428,6 @@ func (r *Reconciler) removeModuleCR(ctx context.Context, clnt Client, obj Object
if !obj.GetDeletionTimestamp().IsZero() {
for _, preDelete := range r.PreDeletes {
if err := preDelete(ctx, clnt, r.Client, obj); err != nil {
r.Event(obj, "Warning", "PreDelete", err.Error())
// we do not set a status here since it will be deleting if timestamp is set.
obj.SetStatus(obj.GetStatus().WithErr(err))
return err
Expand Down Expand Up @@ -469,26 +458,19 @@ func (r *Reconciler) renderTargetResources(

targetResources, err := r.ManifestParser.Parse(spec)
if err != nil {
r.Event(obj, "Warning", "ManifestParsing", err.Error())
obj.SetStatus(status.WithState(shared.StateError).WithErr(err))
return nil, err
}

for _, transform := range r.PostRenderTransforms {
if err := transform(ctx, obj, targetResources.Items); err != nil {
r.Event(obj, "Warning", "PostRenderTransform", err.Error())
obj.SetStatus(status.WithState(shared.StateError).WithErr(err))
return nil, err
}
}

target, err := converter.UnstructuredToInfos(targetResources.Items)
if err != nil {
// Prevent ETCD load bursts during secret rotation
if !util.IsConnectionRelatedError(err) {
r.Event(obj, "Warning", "TargetResourceParsing", err.Error())
}

obj.SetStatus(status.WithState(shared.StateError).WithErr(err))
return nil, err
}
Expand All @@ -515,7 +497,6 @@ func (r *Reconciler) pruneDiff(
// This case should not happen normally, but if happens, it means the resources read from cache is incomplete,
// and we should prevent diff resources to be deleted.
// Meanwhile, evict cache to hope newly created resources back to normal.
r.Event(obj, "Warning", "PruneDiff", ErrResourceSyncDiffInSameOCILayer.Error())
obj.SetStatus(obj.GetStatus().WithState(shared.StateWarning).WithOperation(ErrResourceSyncDiffInSameOCILayer.Error()))
r.ManifestParser.EvictCache(spec)
return ErrResourceSyncDiffInSameOCILayer
Expand Down Expand Up @@ -657,7 +638,6 @@ func (r *Reconciler) handleStatusPatch(ctx context.Context, obj Object, previous
if hasStatusDiff(obj.GetStatus(), previousStatus) {
resetNonPatchableField(obj)
if err := r.Status().Patch(ctx, obj, client.Apply, client.ForceOwnership, r.FieldOwner); err != nil {
r.Event(obj, "Warning", "PatchStatus", err.Error())
return fmt.Errorf("failed to patch status: %w", err)
}
}
Expand Down
6 changes: 1 addition & 5 deletions pkg/remote/kyma_synchronization_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,9 @@ func (c *KymaSynchronizationContext) CreateOrFetchRemoteKyma(

remoteKyma, err := c.GetRemotelySyncedKyma(ctx, remoteSyncNamespace)
if meta.IsNoMatchError(err) || CRDNotFoundErr(err) {
recorder.Event(kyma, "Normal", err.Error(), "CRDs are missing in SKR and will be installed")

if err := c.CreateOrUpdateCRD(ctx, shared.KymaKind.Plural()); err != nil {
return nil, err
}

recorder.Event(kyma, "Normal", "CRDInstallation", "CRDs were installed to SKR")
}

Expand All @@ -206,11 +203,10 @@ func (c *KymaSynchronizationContext) CreateOrFetchRemoteKyma(

err = c.RuntimeClient.Create(ctx, remoteKyma)
if err != nil {
recorder.Event(kyma, "Normal", "RemoteInstallation", "Kyma was installed to SKR")
return nil, fmt.Errorf("failed to create remote kyma: %w", err)
}
recorder.Event(kyma, "Normal", "RemoteInstallation", "Kyma was installed to SKR")
} else if err != nil {
recorder.Event(kyma, "Warning", err.Error(), "Client could not fetch remote Kyma")
return nil, fmt.Errorf("failed to fetch remote kyma: %w", err)
}

Expand Down

0 comments on commit a723ce0

Please sign in to comment.