@@ -20,9 +20,11 @@ import (
20
20
"context"
21
21
"encoding/json"
22
22
"fmt"
23
+ "reflect"
23
24
"sync"
24
25
25
26
apierrors "k8s.io/apimachinery/pkg/api/errors"
27
+ "k8s.io/apimachinery/pkg/api/meta"
26
28
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27
29
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
28
30
"k8s.io/apimachinery/pkg/runtime"
60
62
remoteRootSyncNamespaceLabel = "gitops.kpt.dev/remoterootsync-namespace"
61
63
)
62
64
65
+ const (
66
+ externalSyncCreatedConditionType = "ExternalSyncCreated"
67
+ )
68
+
63
69
// RemoteRootSyncReconciler reconciles a RemoteRootSync object
64
70
type RemoteRootSyncReconciler struct {
65
71
client.Client
@@ -115,14 +121,20 @@ func (r *RemoteRootSyncReconciler) Reconcile(ctx context.Context, req ctrl.Reque
115
121
// The object is being deleted
116
122
if controllerutil .ContainsFinalizer (& remoterootsync , myFinalizerName ) {
117
123
// 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 )
122
136
}
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
+
126
138
// remove our finalizer from the list and update it.
127
139
controllerutil .RemoveFinalizer (& remoterootsync , myFinalizerName )
128
140
if err := r .Update (ctx , & remoterootsync ); err != nil {
@@ -133,8 +145,6 @@ func (r *RemoteRootSyncReconciler) Reconcile(ctx context.Context, req ctrl.Reque
133
145
return ctrl.Result {}, nil
134
146
}
135
147
136
- r .setupWatches (ctx , remoterootsync .Name , remoterootsync .Namespace , remoterootsync .Spec .ClusterRef )
137
-
138
148
clusterRef := & remoterootsync .Spec .ClusterRef
139
149
dynCl , err := r .getDynamicClientForCluster (ctx , clusterRef )
140
150
if err != nil {
@@ -145,6 +155,8 @@ func (r *RemoteRootSyncReconciler) Reconcile(ctx context.Context, req ctrl.Reque
145
155
return ctrl.Result {}, err
146
156
}
147
157
158
+ r .setupWatches (ctx , remoterootsync .Name , remoterootsync .Namespace , remoterootsync .Spec .ClusterRef )
159
+
148
160
syncStatus , err := checkSyncStatus (ctx , dynCl , req .Name )
149
161
if err != nil {
150
162
return ctrl.Result {}, err
@@ -162,12 +174,19 @@ func (r *RemoteRootSyncReconciler) updateStatus(ctx context.Context, rrs *gitops
162
174
logger := klog .FromContext (ctx )
163
175
164
176
// 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 ) {
166
186
return nil
167
187
}
188
+
168
189
logger .Info ("Updating status" )
169
- rrs .Status .SyncStatus = syncStatus
170
- rrs .Status .ObservedGeneration = rrs .Generation
171
190
return r .Client .Status ().Update (ctx , rrs )
172
191
}
173
192
0 commit comments