Skip to content

Commit

Permalink
Merge pull request #144 from hasheddan/ownoam
Browse files Browse the repository at this point in the history
Update OAM trait and workload reconcilers to use MustBeControllableBy
  • Loading branch information
negz authored Apr 9, 2020
2 parents f9d4e85 + 294db22 commit 82a0ff5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
11 changes: 5 additions & 6 deletions pkg/reconciler/oam/trait/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,11 @@ func (r *Reconciler) Reconcile(req reconcile.Request) (reconcile.Result, error)
}

// The trait's referenced workload should always be translated in an
// object(s) that is controlled by the workload. In the case where an
// object(s) already exists in the same namespace and with the same name
// before it is created, this wll guard against modifying it.

// TODO(negz): Migrate to resource.ControlledBy
if err := r.applicator.Apply(ctx, translation, resource.ControllersMustMatch()); err != nil { // nolint:staticcheck
// object(s) that is controllable by the workload. In the case where an
// object(s) already exists in the same namespace, with the same name, and
// with a different controller before it is created, this wll guard against
// modifying it.
if err := r.applicator.Apply(ctx, translation, resource.MustBeControllableBy(trait.GetWorkloadReference().UID)); err != nil { // nolint:staticcheck
log.Debug("Cannot apply workload translation", "error", err, "requeue-after", time.Now().Add(shortWait))
r.record.Event(trait, event.Warning(reasonCannotApplyModification, err))
trait.SetConditions(v1alpha1.ReconcileError(errors.Wrap(err, errApplyTraitModification)))
Expand Down
14 changes: 8 additions & 6 deletions pkg/reconciler/oam/workload/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ func WithApplicator(a resource.Applicator) ReconcilerOption {
}
}

// WithApplyOptions specifies options to pass to the applicator.
// WithApplyOptions specifies options to pass to the applicator. These options
// are applied in addition to MustBeControllableBy, which is always applied and
// cannot be overridden.
func WithApplyOptions(a ...resource.ApplyOption) ReconcilerOption {
return func(r *Reconciler) {
r.applyOpts = a
Expand Down Expand Up @@ -121,10 +123,9 @@ func NewReconciler(m ctrl.Manager, workload resource.WorkloadKind, o ...Reconcil
newWorkload: nw,
workload: TranslateFn(NoopTranslate),
applicator: resource.NewAPIPatchingApplicator(m.GetClient()),
// TODO(negz): Migrate to resource.ControlledBy
applyOpts: []resource.ApplyOption{resource.ControllersMustMatch()}, // nolint:staticcheck
log: logging.NewNopLogger(),
record: event.NewNopRecorder(),
applyOpts: []resource.ApplyOption{},
log: logging.NewNopLogger(),
record: event.NewNopRecorder(),
}

for _, ro := range o {
Expand Down Expand Up @@ -180,7 +181,8 @@ func (r *Reconciler) Reconcile(req reconcile.Request) (reconcile.Result, error)
// so this label is not being used.
meta.AddLabels(o, map[string]string{lowerGroupKind(workload.GetObjectKind()): string(workload.GetUID())})

if err := r.applicator.Apply(ctx, o, r.applyOpts...); err != nil {
opts := append([]resource.ApplyOption{resource.MustBeControllableBy(workload.GetUID())}, r.applyOpts...)
if err := r.applicator.Apply(ctx, o, opts...); err != nil {
log.Debug("Cannot apply workload translation", "error", err, "requeue-after", time.Now().Add(shortWait))
r.record.Event(workload, event.Warning(reasonCannotApplyWorkloadTranslation, err))
workload.SetConditions(v1alpha1.ReconcileError(errors.Wrap(err, errApplyWorkloadTranslation)))
Expand Down

0 comments on commit 82a0ff5

Please sign in to comment.