Skip to content

Commit 048ebec

Browse files
rollouts: add condition when external sync is created (#3834)
1 parent 5e0807a commit 048ebec

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

rollouts/controllers/remoterootsync_controller.go

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ import (
2020
"context"
2121
"encoding/json"
2222
"fmt"
23+
"reflect"
2324
"sync"
2425

2526
apierrors "k8s.io/apimachinery/pkg/api/errors"
27+
"k8s.io/apimachinery/pkg/api/meta"
2628
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2729
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2830
"k8s.io/apimachinery/pkg/runtime"
@@ -60,6 +62,10 @@ var (
6062
remoteRootSyncNamespaceLabel = "gitops.kpt.dev/remoterootsync-namespace"
6163
)
6264

65+
const (
66+
externalSyncCreatedConditionType = "ExternalSyncCreated"
67+
)
68+
6369
// RemoteRootSyncReconciler reconciles a RemoteRootSync object
6470
type RemoteRootSyncReconciler struct {
6571
client.Client
@@ -115,14 +121,20 @@ func (r *RemoteRootSyncReconciler) Reconcile(ctx context.Context, req ctrl.Reque
115121
// The object is being deleted
116122
if controllerutil.ContainsFinalizer(&remoterootsync, myFinalizerName) {
117123
// our finalizer is present, so lets handle any external dependency
118-
if err := r.deleteExternalResources(ctx, &remoterootsync); err != nil {
119-
// if fail to delete the external dependency here, return with error
120-
// so that it can be retried
121-
return ctrl.Result{}, fmt.Errorf("have problem to delete external resource: %w", err)
124+
if meta.IsStatusConditionTrue(remoterootsync.Status.Conditions, externalSyncCreatedConditionType) {
125+
// Delete the external sync resource
126+
err := r.deleteExternalResources(ctx, &remoterootsync)
127+
if err != nil && !apierrors.IsNotFound(err) {
128+
// if fail to delete the external dependency here, return with error
129+
// so that it can be retried
130+
return ctrl.Result{}, fmt.Errorf("have problem to delete external resource: %w", err)
131+
}
132+
133+
// Make sure we stop any watches that are no longer needed.
134+
logger.Info("Pruning watches")
135+
r.pruneWatches(req.NamespacedName, &remoterootsync.Spec.ClusterRef)
122136
}
123-
// Make sure we stop any watches that are no longer needed.
124-
logger.Info("Pruning watches")
125-
r.pruneWatches(req.NamespacedName, &remoterootsync.Spec.ClusterRef)
137+
126138
// remove our finalizer from the list and update it.
127139
controllerutil.RemoveFinalizer(&remoterootsync, myFinalizerName)
128140
if err := r.Update(ctx, &remoterootsync); err != nil {
@@ -133,8 +145,6 @@ func (r *RemoteRootSyncReconciler) Reconcile(ctx context.Context, req ctrl.Reque
133145
return ctrl.Result{}, nil
134146
}
135147

136-
r.setupWatches(ctx, remoterootsync.Name, remoterootsync.Namespace, remoterootsync.Spec.ClusterRef)
137-
138148
clusterRef := &remoterootsync.Spec.ClusterRef
139149
dynCl, err := r.getDynamicClientForCluster(ctx, clusterRef)
140150
if err != nil {
@@ -145,6 +155,8 @@ func (r *RemoteRootSyncReconciler) Reconcile(ctx context.Context, req ctrl.Reque
145155
return ctrl.Result{}, err
146156
}
147157

158+
r.setupWatches(ctx, remoterootsync.Name, remoterootsync.Namespace, remoterootsync.Spec.ClusterRef)
159+
148160
syncStatus, err := checkSyncStatus(ctx, dynCl, req.Name)
149161
if err != nil {
150162
return ctrl.Result{}, err
@@ -162,12 +174,19 @@ func (r *RemoteRootSyncReconciler) updateStatus(ctx context.Context, rrs *gitops
162174
logger := klog.FromContext(ctx)
163175

164176
// Don't update if there are no changes.
165-
if rrs.Status.SyncStatus == syncStatus && rrs.Generation == rrs.Status.ObservedGeneration {
177+
178+
rrsPrior := rrs.DeepCopy()
179+
180+
rrs.Status.SyncStatus = syncStatus
181+
rrs.Status.ObservedGeneration = rrs.Generation
182+
183+
meta.SetStatusCondition(&rrs.Status.Conditions, metav1.Condition{Type: externalSyncCreatedConditionType, Status: metav1.ConditionTrue, Reason: "SyncCreated"})
184+
185+
if reflect.DeepEqual(rrs.Status, rrsPrior.Status) {
166186
return nil
167187
}
188+
168189
logger.Info("Updating status")
169-
rrs.Status.SyncStatus = syncStatus
170-
rrs.Status.ObservedGeneration = rrs.Generation
171190
return r.Client.Status().Update(ctx, rrs)
172191
}
173192

0 commit comments

Comments
 (0)