Skip to content

Commit

Permalink
Merge pull request #119 from Jont828/wait-for-control-plane
Browse files Browse the repository at this point in the history
✨ Wait for control plane to be initialized in HelmReleaseProxy controller
  • Loading branch information
k8s-ci-robot authored Oct 13, 2023
2 parents fdd10da + 546577c commit 5a8285a
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions controllers/helmreleaseproxy/helmreleaseproxy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@ import (
addonsv1alpha1 "sigs.k8s.io/cluster-api-addon-provider-helm/api/v1alpha1"
"sigs.k8s.io/cluster-api-addon-provider-helm/internal"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/conditions"
"sigs.k8s.io/cluster-api/util/patch"
"sigs.k8s.io/cluster-api/util/predicates"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/handler"
)

// HelmReleaseProxyReconciler reconciles a HelmReleaseProxy object.
Expand All @@ -51,11 +53,27 @@ type HelmReleaseProxyReconciler struct {
func (r *HelmReleaseProxyReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
log := ctrl.LoggerFrom(ctx)

clusterToHelmReleaseProxies, err := util.ClusterToTypedObjectsMapper(mgr.GetClient(), &addonsv1alpha1.HelmReleaseProxyList{}, mgr.GetScheme())
if err != nil {
return err
}

return ctrl.NewControllerManagedBy(mgr).
WithOptions(options).
For(&addonsv1alpha1.HelmReleaseProxy{}).
WithEventFilter(predicate.GenerationChangedPredicate{}).
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(log, r.WatchFilterValue)).
Watches(
&clusterv1.Cluster{},
handler.EnqueueRequestsFromMapFunc(clusterToHelmReleaseProxies),
builder.WithPredicates(
predicates.All(ctrl.LoggerFrom(ctx),
predicates.Any(ctrl.LoggerFrom(ctx),
predicates.ClusterUnpaused(ctrl.LoggerFrom(ctx)),
predicates.ClusterControlPlaneInitialized(ctrl.LoggerFrom(ctx)),
),
predicates.ResourceHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue),
),
)).
Complete(r)
}

Expand Down Expand Up @@ -177,6 +195,15 @@ func (r *HelmReleaseProxyReconciler) Reconcile(ctx context.Context, req ctrl.Req
return ctrl.Result{}, wrappedErr
}

if !conditions.IsTrue(cluster, clusterv1.ControlPlaneInitializedCondition) {
log.Info("Waiting for the control plane to be initialized")
conditions.MarkFalse(helmReleaseProxy, addonsv1alpha1.ClusterAvailableCondition, clusterv1.WaitingForControlPlaneAvailableReason, clusterv1.ConditionSeverityInfo, "")

// Return since the kubeconfig won't be available or useable until the API server is reachable.
// The controller watches the Cluster for control plane initialization in SetupWithManager, so a requeue is not necessary.
return ctrl.Result{}, nil
}

log.V(2).Info("Getting kubeconfig for cluster", "cluster", cluster.Name)
kubeconfig, err := k.GetClusterKubeconfig(ctx, cluster)
if err != nil {
Expand Down Expand Up @@ -291,8 +318,8 @@ func initalizeConditions(ctx context.Context, patchHelper *patch.Helper, helmRel
func patchHelmReleaseProxy(ctx context.Context, patchHelper *patch.Helper, helmReleaseProxy *addonsv1alpha1.HelmReleaseProxy) error {
conditions.SetSummary(helmReleaseProxy,
conditions.WithConditions(
addonsv1alpha1.HelmReleaseReadyCondition,
addonsv1alpha1.ClusterAvailableCondition,
addonsv1alpha1.HelmReleaseReadyCondition,
),
)

Expand All @@ -302,8 +329,8 @@ func patchHelmReleaseProxy(ctx context.Context, patchHelper *patch.Helper, helmR
helmReleaseProxy,
patch.WithOwnedConditions{Conditions: []clusterv1.ConditionType{
clusterv1.ReadyCondition,
addonsv1alpha1.HelmReleaseReadyCondition,
addonsv1alpha1.ClusterAvailableCondition,
addonsv1alpha1.HelmReleaseReadyCondition,
}},
patch.WithStatusObservedGeneration{},
)
Expand Down

0 comments on commit 5a8285a

Please sign in to comment.