Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch CAPI cluster after reconciling it #68

Merged
merged 1 commit into from
Aug 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion internal/controllers/import_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
errorutils "k8s.io/apimachinery/pkg/util/errors"
yamlDecoder "k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -142,14 +143,36 @@ func (r *CAPIImportReconciler) Reconcile(ctx context.Context, req ctrl.Request)
return ctrl.Result{Requeue: true}, err
}

patchBase := client.MergeFromWithOptions(capiCluster.DeepCopy(), client.MergeFromWithOptimisticLock{})

log = log.WithValues("cluster", capiCluster.Name)

// Wait for controlplane to be ready
if !capiCluster.Status.ControlPlaneReady {
log.Info("Clusters control plane is not ready, requeue")
log.Info("clusters control plane is not ready, requeue")
return ctrl.Result{RequeueAfter: defaultRequeueDuration}, nil
}

// Collect errors as an aggregate to return together after all patches have been performed.
var errs []error

result, err := r.reconcile(ctx, capiCluster)
if err != nil {
errs = append(errs, fmt.Errorf("error reconciling cluster: %w", err))
}

if err := r.Patch(ctx, capiCluster, patchBase); err != nil {
errs = append(errs, fmt.Errorf("failed to patch cluster: %w", err))
}

if len(errs) > 0 {
return ctrl.Result{}, errorutils.NewAggregate(errs)
}

return result, nil
}

func (r *CAPIImportReconciler) reconcile(ctx context.Context, capiCluster *clusterv1.Cluster) (ctrl.Result, error) {
// fetch the rancher clusters
rancherClusterHandler := rancher.NewClusterHandler(ctx, r.Client)
rancherClusterName := rancherClusterNameFromCAPICluster(capiCluster.Name)
Expand Down