Skip to content

Commit

Permalink
Implement AfterControlPlaneInitialized, AfterControlPlaneUpgrade and …
Browse files Browse the repository at this point in the history
…AfterClusterUpgrade hooks
  • Loading branch information
Yuvaraj Kakaraparthi authored and sbueringer committed Jun 14, 2022
1 parent ef27237 commit 4c55b03
Show file tree
Hide file tree
Showing 9 changed files with 1,735 additions and 260 deletions.
3 changes: 3 additions & 0 deletions exp/runtime/hooks/api/v1alpha1/common_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import (
"k8s.io/apimachinery/pkg/runtime"
)

// PendingHooksAnnotation is the annotation used to keep a track of pending runtime hooks.
const PendingHooksAnnotation = "hooks.x-cluster.k8s.io/pending-hooks"

// ResponseObject is a runtime object extended with methods to handle response-specific fields.
// +kubebuilder:object:generate=false
type ResponseObject interface {
Expand Down
36 changes: 35 additions & 1 deletion internal/controllers/topology/cluster/desired_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"sigs.k8s.io/cluster-api/feature"
"sigs.k8s.io/cluster-api/internal/contract"
"sigs.k8s.io/cluster-api/internal/controllers/topology/cluster/scope"
"sigs.k8s.io/cluster-api/internal/hooks"
tlog "sigs.k8s.io/cluster-api/internal/log"
runtimecatalog "sigs.k8s.io/cluster-api/internal/runtime/catalog"
)
Expand Down Expand Up @@ -313,6 +314,33 @@ func (r *Reconciler) computeControlPlaneVersion(ctx context.Context, s *scope.Sc
// Nb. We do not return early in the function if the control plane is already at the desired version so as
// to know if the control plane is being upgraded. This information
// is required when updating the TopologyReconciled condition on the cluster.

// Let's call the AfterControlPlaneUpgrade now that the control plane is upgraded.
if feature.Gates.Enabled(feature.RuntimeSDK) {
// Call the hook only if it is marked. If it is not marked it means we don't need ot call the
// hook because we didn't go through an upgrade or we already called the hook after the upgrade.
if hooks.IsPending(runtimehooksv1.AfterControlPlaneUpgrade, s.Current.Cluster) {
hookRequest := &runtimehooksv1.AfterControlPlaneUpgradeRequest{
Cluster: *s.Current.Cluster,
KubernetesVersion: desiredVersion,
}
hookResponse := &runtimehooksv1.AfterControlPlaneUpgradeResponse{}
if err := r.RuntimeClient.CallAllExtensions(ctx, runtimehooksv1.AfterControlPlaneUpgrade, s.Current.Cluster, hookRequest, hookResponse); err != nil {
return "", errors.Wrapf(err, "error calling the %s hook", runtimecatalog.HookName(runtimehooksv1.AfterControlPlaneUpgrade))
}
s.HookResponseTracker.Add(runtimehooksv1.AfterControlPlaneUpgrade, hookResponse)
if hookResponse.RetryAfterSeconds != 0 {
// We have to block the upgrade of the Machine deployments.
s.UpgradeTracker.MachineDeployments.HoldUpgrades(true)
} else {
// We are done with the hook for now. We don't need to call it anymore. Unmark it.
if err := hooks.MarkAsDone(ctx, r.Client, s.Current.Cluster, runtimehooksv1.AfterControlPlaneUpgrade); err != nil {
return "", errors.Wrapf(err, "failed to unmark the %s hook", runtimecatalog.HookName(runtimehooksv1.AfterControlPlaneUpgrade))
}
}
}
}

return *currentVersion, nil
}

Expand Down Expand Up @@ -354,9 +382,15 @@ func (r *Reconciler) computeControlPlaneVersion(ctx context.Context, s *scope.Sc
// Cannot pickup the new version right now. Need to try again later.
return *currentVersion, nil
}

// We are picking up the new version here.
// Mark the AfterControlPlaneUpgrade and the AfterClusterUpgrade hooks so that we call them once we are done with the upgrade.
if err := hooks.MarkAsPending(ctx, r.Client, s.Current.Cluster, runtimehooksv1.AfterControlPlaneUpgrade, runtimehooksv1.AfterClusterUpgrade); err != nil {
return "", errors.Wrapf(err, "failed to mark the %s hook", []string{runtimecatalog.HookName(runtimehooksv1.AfterControlPlaneUpgrade), runtimecatalog.HookName(runtimehooksv1.AfterClusterUpgrade)})
}
}

// Control plane and machine deployments are stable.
// Control plane and machine deployments are stable. All the required hook are called.
// Ready to pick up the topology version.
return desiredVersion, nil
}
Expand Down
Loading

0 comments on commit 4c55b03

Please sign in to comment.