Skip to content

Commit

Permalink
rollouts: add condition when external sync is created (#3834)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherFry committed Feb 22, 2023
1 parent 5e0807a commit 048ebec
Showing 1 changed file with 31 additions and 12 deletions.
43 changes: 31 additions & 12 deletions rollouts/controllers/remoterootsync_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ import (
"context"
"encoding/json"
"fmt"
"reflect"
"sync"

apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -60,6 +62,10 @@ var (
remoteRootSyncNamespaceLabel = "gitops.kpt.dev/remoterootsync-namespace"
)

const (
externalSyncCreatedConditionType = "ExternalSyncCreated"
)

// RemoteRootSyncReconciler reconciles a RemoteRootSync object
type RemoteRootSyncReconciler struct {
client.Client
Expand Down Expand Up @@ -115,14 +121,20 @@ func (r *RemoteRootSyncReconciler) Reconcile(ctx context.Context, req ctrl.Reque
// The object is being deleted
if controllerutil.ContainsFinalizer(&remoterootsync, myFinalizerName) {
// our finalizer is present, so lets handle any external dependency
if err := r.deleteExternalResources(ctx, &remoterootsync); err != nil {
// if fail to delete the external dependency here, return with error
// so that it can be retried
return ctrl.Result{}, fmt.Errorf("have problem to delete external resource: %w", err)
if meta.IsStatusConditionTrue(remoterootsync.Status.Conditions, externalSyncCreatedConditionType) {
// Delete the external sync resource
err := r.deleteExternalResources(ctx, &remoterootsync)
if err != nil && !apierrors.IsNotFound(err) {
// if fail to delete the external dependency here, return with error
// so that it can be retried
return ctrl.Result{}, fmt.Errorf("have problem to delete external resource: %w", err)
}

// Make sure we stop any watches that are no longer needed.
logger.Info("Pruning watches")
r.pruneWatches(req.NamespacedName, &remoterootsync.Spec.ClusterRef)
}
// Make sure we stop any watches that are no longer needed.
logger.Info("Pruning watches")
r.pruneWatches(req.NamespacedName, &remoterootsync.Spec.ClusterRef)

// remove our finalizer from the list and update it.
controllerutil.RemoveFinalizer(&remoterootsync, myFinalizerName)
if err := r.Update(ctx, &remoterootsync); err != nil {
Expand All @@ -133,8 +145,6 @@ func (r *RemoteRootSyncReconciler) Reconcile(ctx context.Context, req ctrl.Reque
return ctrl.Result{}, nil
}

r.setupWatches(ctx, remoterootsync.Name, remoterootsync.Namespace, remoterootsync.Spec.ClusterRef)

clusterRef := &remoterootsync.Spec.ClusterRef
dynCl, err := r.getDynamicClientForCluster(ctx, clusterRef)
if err != nil {
Expand All @@ -145,6 +155,8 @@ func (r *RemoteRootSyncReconciler) Reconcile(ctx context.Context, req ctrl.Reque
return ctrl.Result{}, err
}

r.setupWatches(ctx, remoterootsync.Name, remoterootsync.Namespace, remoterootsync.Spec.ClusterRef)

syncStatus, err := checkSyncStatus(ctx, dynCl, req.Name)
if err != nil {
return ctrl.Result{}, err
Expand All @@ -162,12 +174,19 @@ func (r *RemoteRootSyncReconciler) updateStatus(ctx context.Context, rrs *gitops
logger := klog.FromContext(ctx)

// Don't update if there are no changes.
if rrs.Status.SyncStatus == syncStatus && rrs.Generation == rrs.Status.ObservedGeneration {

rrsPrior := rrs.DeepCopy()

rrs.Status.SyncStatus = syncStatus
rrs.Status.ObservedGeneration = rrs.Generation

meta.SetStatusCondition(&rrs.Status.Conditions, metav1.Condition{Type: externalSyncCreatedConditionType, Status: metav1.ConditionTrue, Reason: "SyncCreated"})

if reflect.DeepEqual(rrs.Status, rrsPrior.Status) {
return nil
}

logger.Info("Updating status")
rrs.Status.SyncStatus = syncStatus
rrs.Status.ObservedGeneration = rrs.Generation
return r.Client.Status().Update(ctx, rrs)
}

Expand Down

0 comments on commit 048ebec

Please sign in to comment.