Skip to content

Commit

Permalink
Merge pull request kubernetes-sigs#107 from chuckha/defer-patch
Browse files Browse the repository at this point in the history
Defer the patch
  • Loading branch information
k8s-ci-robot authored Aug 9, 2019
2 parents 08dafc3 + 95c11ff commit 8e3a5ce
Showing 1 changed file with 11 additions and 22 deletions.
33 changes: 11 additions & 22 deletions controllers/kubeadmconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,13 @@ type KubeadmConfigReconciler struct {
// +kubebuilder:rbac:groups=cluster.x-k8s.io,resources=clusters;machines,verbs=get;list;watch

// Reconcile TODO
func (r *KubeadmConfigReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
func (r *KubeadmConfigReconciler) Reconcile(req ctrl.Request) (_ ctrl.Result, rerr error) {

ctx := context.Background()
log := r.Log.WithValues("kubeadmconfig", req.NamespacedName)

config := cabpkv1alpha2.KubeadmConfig{}
if err := r.Get(ctx, req.NamespacedName, &config); err != nil {
config := &cabpkv1alpha2.KubeadmConfig{}
if err := r.Get(ctx, req.NamespacedName, config); err != nil {
if apierrors.IsNotFound(err) {
return ctrl.Result{}, nil
}
Expand Down Expand Up @@ -131,6 +132,13 @@ func (r *KubeadmConfigReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro

// Store Config's state, pre-modifications, to allow patching
patchConfig := client.MergeFrom(config.DeepCopy())
defer func() {
err := r.patchConfig(ctx, config, patchConfig)
if err != nil {
log.Error(err, "failed to patch config")
rerr = err
}
}()

// Check for control plane ready. If it's not ready then we will requeue the machine until it is.
// The cluster-api machine controller set this value.
Expand Down Expand Up @@ -210,15 +218,7 @@ func (r *KubeadmConfigReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro

config.Status.BootstrapData = cloudInitData
config.Status.Ready = true

if err := r.patchConfig(ctx, &config, patchConfig); err != nil {
log.Error(err, "failed to patch config")
return ctrl.Result{}, err
}
log.Info("KubeadmConfig updated with BootstrapData for kubeadm init; Status.Ready=true")

return ctrl.Result{}, nil

}

// Every other case it's a join scenario
Expand Down Expand Up @@ -271,10 +271,6 @@ func (r *KubeadmConfigReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro

config.Status.BootstrapData = joinData
config.Status.Ready = true
if err := r.patchConfig(ctx, &config, patchConfig); err != nil {
log.Error(err, "failed to patch config")
return ctrl.Result{}, err
}
return ctrl.Result{}, nil
}

Expand All @@ -290,13 +286,6 @@ func (r *KubeadmConfigReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro
}
config.Status.BootstrapData = joinData
config.Status.Ready = true

if err := r.patchConfig(ctx, &config, patchConfig); err != nil {
log.Error(err, "failed to patch config")
return ctrl.Result{}, err
}

log.Info("Updated config with bootstrap data")
return ctrl.Result{}, nil
}

Expand Down

0 comments on commit 8e3a5ce

Please sign in to comment.