Skip to content

Commit

Permalink
Fix controller patching issues
Browse files Browse the repository at this point in the history
  • Loading branch information
anmazzotti committed Sep 18, 2023
1 parent 856fe91 commit 6d71009
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ docker-build: test ## Build docker image with the manager.
docker-build-agent: test build-agent ## Build docker image with the manager.
mkdir -p demo/bin
cp bin/agent demo/bin/agent
$(CONTAINER_TOOL) build -t agent:latest ./demo
$(CONTAINER_TOOL) build -t agent:latest --no-cache --progress=plain ./demo

.PHONY: docker-push
docker-push: ## Push docker image with the manager.
Expand Down
6 changes: 5 additions & 1 deletion demo/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ FROM opensuse/tumbleweed:latest

RUN zypper install -y systemd
RUN zypper install -y fuse-overlayfs
RUN zypper install -y patterns-kubernetes-kubeadm

## FIXME: Using cri-o until the kubeadm pattern is fixed
RUN systemctl disable containerd
RUN zypper install -y cri-o
COPY crio-docker-storage.conf /etc/crio/crio.conf.d/99-storage.conf
RUN zypper install -y patterns-kubernetes-kubeadm
RUN systemctl enable crio

COPY bin/agent /agent
COPY agent-config.yaml /oem/elemental/agent/config.yaml
Expand Down
13 changes: 7 additions & 6 deletions internal/controller/elementalcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (r *ElementalClusterReconciler) SetupWithManager(ctx context.Context, mgr c
//
// For more details about the reconciliation loop, check the official CAPI documentation:
// - https://cluster-api.sigs.k8s.io/developer/providers/cluster-infrastructure
func (r *ElementalClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
func (r *ElementalClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Result, rerr error) {
logger := log.FromContext(ctx).
WithValues(ilog.KeyNamespace, req.Namespace).
WithValues(ilog.KeyElementalCluster, req.Name)
Expand Down Expand Up @@ -97,6 +97,12 @@ func (r *ElementalClusterReconciler) Reconcile(ctx context.Context, req ctrl.Req
if err != nil {
return ctrl.Result{}, fmt.Errorf("initializing patch helper: %w", err)
}
defer func() {
// Reconciliation step #8: Patch the resource to persist changes
if err := patchHelper.Patch(ctx, elementalCluster); err != nil {
rerr = fmt.Errorf("patching ElementalCluster: %w", err)
}
}()

// Reconciliation step #4: Reconcile provider-specific cluster infrastructure
if elementalCluster.GetDeletionTimestamp() == nil || elementalCluster.GetDeletionTimestamp().IsZero() {
Expand All @@ -111,11 +117,6 @@ func (r *ElementalClusterReconciler) Reconcile(ctx context.Context, req ctrl.Req
}
}

// Reconciliation step #8: Patch the resource to persist changes
if err := patchHelper.Patch(ctx, elementalCluster); err != nil {
return ctrl.Result{}, fmt.Errorf("patching ElementalCluster: %w", err)
}

return ctrl.Result{}, nil
}

Expand Down
14 changes: 7 additions & 7 deletions internal/controller/elementalmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func (r *ElementalMachineReconciler) ClusterToElementalMachines(ctx context.Cont
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
// For more details about the reconciliation loop, check the official CAPI documentation:
// - https://cluster-api.sigs.k8s.io/developer/providers/machine-infrastructure
func (r *ElementalMachineReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
func (r *ElementalMachineReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Result, rerr error) {
logger := log.FromContext(ctx).
WithValues(ilog.KeyNamespace, req.Namespace).
WithValues(ilog.KeyElementalMachine, req.Name)
Expand Down Expand Up @@ -229,6 +229,12 @@ func (r *ElementalMachineReconciler) Reconcile(ctx context.Context, req ctrl.Req
if err != nil {
return ctrl.Result{}, fmt.Errorf("initializing patch helper: %w", err)
}
defer func() {
// Reconciliation step #12: Patch the resource to persist changes
if err := patchHelper.Patch(ctx, elementalMachine); err != nil {
rerr = fmt.Errorf("patching ElementalMachine: %w", err)
}
}()

if elementalMachine.GetDeletionTimestamp() == nil || elementalMachine.GetDeletionTimestamp().IsZero() {
// The object is not being deleted, so register the finalizer
Expand All @@ -254,7 +260,6 @@ func (r *ElementalMachineReconciler) Reconcile(ctx context.Context, req ctrl.Req
return ctrl.Result{}, fmt.Errorf("reconciling ElementalMachine: %w", err)
}
return result, nil

}

// The object is up for deletion
Expand All @@ -264,11 +269,6 @@ func (r *ElementalMachineReconciler) Reconcile(ctx context.Context, req ctrl.Req
}
}

// Reconciliation step #12: Patch the resource to persist changes
if err := patchHelper.Patch(ctx, elementalMachine); err != nil {
return ctrl.Result{}, fmt.Errorf("patching ElementalMachine: %w", err)
}

return ctrl.Result{}, nil
}

Expand Down
11 changes: 6 additions & 5 deletions internal/controller/elementalmachineregistration_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type ElementalMachineRegistrationReconciler struct {
//
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
func (r *ElementalMachineRegistrationReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
func (r *ElementalMachineRegistrationReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Result, rerr error) {
logger := log.FromContext(ctx).
WithValues(ilog.KeyNamespace, req.Namespace).
WithValues(ilog.KeyElementalMachineRegistration, req.Name)
Expand All @@ -67,10 +67,11 @@ func (r *ElementalMachineRegistrationReconciler) Reconcile(ctx context.Context,
if err != nil {
return ctrl.Result{}, fmt.Errorf("initializing patch helper: %w", err)
}

if err := patchHelper.Patch(ctx, elementalMachineRegistration); err != nil {
return ctrl.Result{}, fmt.Errorf("patching ElementalMachineRegistration: %w", err)
}
defer func() {
if err := patchHelper.Patch(ctx, elementalMachineRegistration); err != nil {
rerr = fmt.Errorf("patching ElementalMachineRegistration: %w", err)
}
}()

return ctrl.Result{}, nil
}
Expand Down

0 comments on commit 6d71009

Please sign in to comment.