Skip to content

Commit 8b7d7c9

Browse files
author
Shawn Hurley
authored
pkg/ansible/controller: reconciler needs to get the resource (#1048)
after the ansible has finished. <!-- Before making a PR, please read our contributing guidelines https://github.com/operator-framework/operator-sdk/blob/master/CONTRIBUTING.MD Note: Make sure your branch is rebased to the latest upstream master. --> **Description of the change:** **Motivation for the change:** <!-- Note: If this PR is fixing an issue make sure to add a note saying: Closes #<ISSUE_NUMBER> -->
1 parent 7ef94d6 commit 8b7d7c9

File tree

3 files changed

+11
-15
lines changed

3 files changed

+11
-15
lines changed

pkg/ansible/controller/controller.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ func Add(mgr manager.Manager, options Options) *controller.Controller {
6565
Writer: mgr.GetClient(),
6666
StatusClient: mgr.GetClient(),
6767
},
68+
// This works because unstrucutred calls will go to the
69+
// API by default.
70+
APIReader: mgr.GetClient(),
6871
GVK: options.GVK,
6972
Runner: options.Runner,
7073
EventHandlers: eventHandlers,

pkg/ansible/controller/reconcile.go

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030
"github.com/operator-framework/operator-sdk/pkg/ansible/runner"
3131
"github.com/operator-framework/operator-sdk/pkg/ansible/runner/eventapi"
3232

33-
"k8s.io/api/core/v1"
33+
v1 "k8s.io/api/core/v1"
3434
apierrors "k8s.io/apimachinery/pkg/api/errors"
3535
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3636
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -53,6 +53,7 @@ type AnsibleOperatorReconciler struct {
5353
GVK schema.GroupVersionKind
5454
Runner runner.Runner
5555
Client client.Client
56+
APIReader client.Reader
5657
EventHandlers []events.EventHandler
5758
ReconcilePeriod time.Duration
5859
ManageStatus bool
@@ -176,6 +177,11 @@ func (r *AnsibleOperatorReconciler) Reconcile(request reconcile.Request) (reconc
176177
return reconcileResult, eventErr
177178
}
178179

180+
err = r.APIReader.Get(context.TODO(), request.NamespacedName, u)
181+
if err != nil {
182+
return reconcile.Result{}, err
183+
}
184+
179185
// We only want to update the CustomResource once, so we'll track changes and do it at the end
180186
runSuccessful := len(failureMessages) == 0
181187
// The finalizer has run successfully, time to remove it
@@ -204,10 +210,6 @@ func (r *AnsibleOperatorReconciler) Reconcile(request reconcile.Request) (reconc
204210

205211
func (r *AnsibleOperatorReconciler) markRunning(u *unstructured.Unstructured, namespacedName types.NamespacedName) error {
206212
// Get the latest resource to prevent updating a stale status
207-
err := r.Client.Get(context.TODO(), namespacedName, u)
208-
if err != nil {
209-
return err
210-
}
211213
statusInterface := u.Object["status"]
212214
statusMap, _ := statusInterface.(map[string]interface{})
213215
crStatus := ansiblestatus.CreateFromMap(statusMap)
@@ -238,16 +240,6 @@ func (r *AnsibleOperatorReconciler) markRunning(u *unstructured.Unstructured, na
238240
}
239241

240242
func (r *AnsibleOperatorReconciler) markDone(u *unstructured.Unstructured, namespacedName types.NamespacedName, statusEvent eventapi.StatusJobEvent, failureMessages eventapi.FailureMessages) error {
241-
logger := logf.Log.WithName("markDone")
242-
// Get the latest resource to prevent updating a stale status
243-
err := r.Client.Get(context.TODO(), namespacedName, u)
244-
if apierrors.IsNotFound(err) {
245-
logger.Info("Resource not found, assuming it was deleted", err)
246-
return nil
247-
}
248-
if err != nil {
249-
return err
250-
}
251243
statusInterface := u.Object["status"]
252244
statusMap, _ := statusInterface.(map[string]interface{})
253245
crStatus := ansiblestatus.CreateFromMap(statusMap)

pkg/ansible/controller/reconcile_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,7 @@ func TestReconcile(t *testing.T) {
487487
GVK: tc.GVK,
488488
Runner: tc.Runner,
489489
Client: tc.Client,
490+
APIReader: tc.Client,
490491
EventHandlers: tc.EventHandlers,
491492
ReconcilePeriod: tc.ReconcilePeriod,
492493
ManageStatus: tc.ManageStatus,

0 commit comments

Comments
 (0)