Skip to content

Commit

Permalink
chore: Use JSON merge patch to reconcile Camel K custom resource stat…
Browse files Browse the repository at this point in the history
…uses
  • Loading branch information
astefanutti authored and nicolaferraro committed Nov 29, 2019
1 parent 75dd983 commit adab1ca
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 51 deletions.
20 changes: 4 additions & 16 deletions pkg/controller/build/build_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"

k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -39,7 +38,6 @@ import (
"github.com/apache/camel-k/pkg/builder"
"github.com/apache/camel-k/pkg/client"
"github.com/apache/camel-k/pkg/platform"
"github.com/apache/camel-k/pkg/util/log"
)

// Add creates a new Build Controller and adds it to the Manager. The Manager will set fields on the Controller
Expand Down Expand Up @@ -169,7 +167,7 @@ func (r *ReconcileBuild) Reconcile(request reconcile.Request) (reconcile.Result,
target.SetIntegrationPlatform(pl)
}

return r.update(ctx, targetLog, target)
return r.update(ctx, &instance, target)
}

return reconcile.Result{}, err
Expand Down Expand Up @@ -199,7 +197,7 @@ func (r *ReconcileBuild) Reconcile(request reconcile.Request) (reconcile.Result,
}

if newTarget != nil {
if r, err := r.update(ctx, targetLog, newTarget); err != nil {
if r, err := r.update(ctx, &instance, newTarget); err != nil {
return r, err
}

Expand Down Expand Up @@ -230,18 +228,8 @@ func (r *ReconcileBuild) Reconcile(request reconcile.Request) (reconcile.Result,
return reconcile.Result{}, nil
}

// Update --
func (r *ReconcileBuild) update(ctx context.Context, log log.Logger, target *v1alpha1.Build) (reconcile.Result, error) {
err := r.client.Status().Update(ctx, target)
if err != nil {
if k8serrors.IsConflict(err) {
log.Error(err, "conflict")

return reconcile.Result{
Requeue: true,
}, nil
}
}
func (r *ReconcileBuild) update(ctx context.Context, base *v1alpha1.Build, target *v1alpha1.Build) (reconcile.Result, error) {
err := r.client.Status().Patch(ctx, target, k8sclient.MergeFrom(base))

return reconcile.Result{}, err
}
19 changes: 4 additions & 15 deletions pkg/controller/integrationkit/integrationkit_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/apache/camel-k/pkg/util/log"

"k8s.io/apimachinery/pkg/api/errors"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -202,7 +201,7 @@ func (r *ReconcileIntegrationKit) Reconcile(request reconcile.Request) (reconcil
target.SetIntegrationPlatform(pl)
}

return r.update(ctx, targetLog, target)
return r.update(ctx, &instance, target)
}

return reconcile.Result{}, err
Expand All @@ -228,7 +227,7 @@ func (r *ReconcileIntegrationKit) Reconcile(request reconcile.Request) (reconcil
}

if newTarget != nil {
if r, err := r.update(ctx, targetLog, newTarget); err != nil {
if r, err := r.update(ctx, &instance, newTarget); err != nil {
return r, err
}

Expand All @@ -250,25 +249,15 @@ func (r *ReconcileIntegrationKit) Reconcile(request reconcile.Request) (reconcil
return reconcile.Result{}, nil
}

// Update --
func (r *ReconcileIntegrationKit) update(ctx context.Context, log log.Logger, target *v1alpha1.IntegrationKit) (reconcile.Result, error) {
func (r *ReconcileIntegrationKit) update(ctx context.Context, base *v1alpha1.IntegrationKit, target *v1alpha1.IntegrationKit) (reconcile.Result, error) {
dgst, err := digest.ComputeForIntegrationKit(target)
if err != nil {
return reconcile.Result{}, err
}

target.Status.Digest = dgst

err = r.client.Status().Update(ctx, target)
if err != nil {
if k8serrors.IsConflict(err) {
log.Error(err, "conflict")

return reconcile.Result{
Requeue: true,
}, nil
}
}
err = r.client.Status().Patch(ctx, target, k8sclient.MergeFrom(base))

return reconcile.Result{}, err
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,20 @@ import (
"context"
"time"

camelv1alpha1 "github.com/apache/camel-k/pkg/apis/camel/v1alpha1"

"github.com/apache/camel-k/pkg/client"

"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"

k8serrors "k8s.io/apimachinery/pkg/api/errors"

k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"

"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
"github.com/apache/camel-k/pkg/client"
)

// Add creates a new IntegrationPlatform Controller and adds it to the Manager. The Manager will set fields on the Controller
Expand Down Expand Up @@ -66,10 +64,10 @@ func add(mgr manager.Manager, r reconcile.Reconciler) error {
}

// Watch for changes to primary resource IntegrationPlatform
err = c.Watch(&source.Kind{Type: &camelv1alpha1.IntegrationPlatform{}}, &handler.EnqueueRequestForObject{}, predicate.Funcs{
err = c.Watch(&source.Kind{Type: &v1alpha1.IntegrationPlatform{}}, &handler.EnqueueRequestForObject{}, predicate.Funcs{
UpdateFunc: func(e event.UpdateEvent) bool {
oldIntegrationPlatform := e.ObjectOld.(*camelv1alpha1.IntegrationPlatform)
newIntegrationPlatform := e.ObjectNew.(*camelv1alpha1.IntegrationPlatform)
oldIntegrationPlatform := e.ObjectOld.(*v1alpha1.IntegrationPlatform)
newIntegrationPlatform := e.ObjectNew.(*v1alpha1.IntegrationPlatform)
// Ignore updates to the integration platform status in which case metadata.Generation
// does not change, or except when the integration platform phase changes as it's used
// to transition from one phase to another
Expand Down Expand Up @@ -110,7 +108,7 @@ func (r *ReconcileIntegrationPlatform) Reconcile(request reconcile.Request) (rec
ctx := context.TODO()

// Fetch the IntegrationPlatform instance
var instance camelv1alpha1.IntegrationPlatform
var instance v1alpha1.IntegrationPlatform

if err := r.client.Get(ctx, request.NamespacedName, &instance); err != nil {
if errors.IsNotFound(err) {
Expand All @@ -132,7 +130,7 @@ func (r *ReconcileIntegrationPlatform) Reconcile(request reconcile.Request) (rec
NewMonitorAction(),
}

var targetPhase camelv1alpha1.IntegrationPlatformPhase
var targetPhase v1alpha1.IntegrationPlatformPhase
var err error

target := instance.DeepCopy()
Expand All @@ -153,14 +151,7 @@ func (r *ReconcileIntegrationPlatform) Reconcile(request reconcile.Request) (rec
}

if target != nil {
if err := r.client.Status().Update(ctx, target); err != nil {
if k8serrors.IsConflict(err) {
targetLog.Error(err, "conflict")
return reconcile.Result{
Requeue: true,
}, nil
}

if err := r.client.Status().Patch(ctx, target, k8sclient.MergeFrom(&instance)); err != nil {
return reconcile.Result{}, err
}

Expand All @@ -181,7 +172,7 @@ func (r *ReconcileIntegrationPlatform) Reconcile(request reconcile.Request) (rec
}
}

if targetPhase == camelv1alpha1.IntegrationPlatformPhaseReady {
if targetPhase == v1alpha1.IntegrationPlatformPhaseReady {
return reconcile.Result{}, nil
}

Expand Down

0 comments on commit adab1ca

Please sign in to comment.