Skip to content

Commit

Permalink
Fix workload cluster kube-vip update (#7624)
Browse files Browse the repository at this point in the history
* Fix workload cluster kube-vip update

Signed-off-by: Rahul Ganesh <[email protected]>

* Update pod spec restart policy and use remote client for delete

Signed-off-by: Rahul Ganesh <[email protected]>

---------

Signed-off-by: Rahul Ganesh <[email protected]>
Co-authored-by: Rahul Ganesh <[email protected]>
  • Loading branch information
rahulbabu95 and Rahul Ganesh authored Feb 17, 2024
1 parent 65b661c commit 7349215
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 42 deletions.
2 changes: 1 addition & 1 deletion config/crd/bases/anywhere.eks.amazonaws.com_clusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ spec:
type: string
type: object
machineHealthCheck:
description: MachineHealthCheck is a control-plane level override
description: MachineHealthCheck is a worker node level override
for the timeouts and maxUnhealthy specified in the top-level
MHC configuration. If not configured, the defaults in the
top-level MHC configuration are used.
Expand Down
44 changes: 23 additions & 21 deletions controllers/controlplaneupgrade_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,17 @@ const (

// ControlPlaneUpgradeReconciler reconciles a ControlPlaneUpgradeReconciler object.
type ControlPlaneUpgradeReconciler struct {
client client.Client
log logr.Logger
client client.Client
remoteClientRegistry RemoteClientRegistry
log logr.Logger
}

// NewControlPlaneUpgradeReconciler returns a new instance of ControlPlaneUpgradeReconciler.
func NewControlPlaneUpgradeReconciler(client client.Client) *ControlPlaneUpgradeReconciler {
func NewControlPlaneUpgradeReconciler(client client.Client, remoteClientRegistry RemoteClientRegistry) *ControlPlaneUpgradeReconciler {
return &ControlPlaneUpgradeReconciler{
client: client,
log: ctrl.Log.WithName("ControlPlaneUpgradeController"),
client: client,
remoteClientRegistry: remoteClientRegistry,
log: ctrl.Log.WithName("ControlPlaneUpgradeController"),
}
}

Expand Down Expand Up @@ -140,17 +142,25 @@ func (r *ControlPlaneUpgradeReconciler) SetupWithManager(mgr ctrl.Manager) error

func (r *ControlPlaneUpgradeReconciler) reconcile(ctx context.Context, log logr.Logger, cpUpgrade *anywherev1.ControlPlaneUpgrade) (ctrl.Result, error) {
var firstControlPlane bool
rClient, err := r.remoteClientRegistry.GetClient(ctx, GetNamespacedNameType(cpUpgrade.Spec.ControlPlane.Name, cpUpgrade.Spec.ControlPlane.Namespace))
if err != nil {
return ctrl.Result{}, err
}

// return early if controlplane upgrade is already complete
if cpUpgrade.Status.Ready {
log.Info("All Control Plane nodes are upgraded")
// check if kube-vip config map exists and clean it up
if err := cleanupKubeVipCM(ctx, log, r.client); err != nil {
if err := cleanupKubeVipCM(ctx, log, rClient); err != nil {
return ctrl.Result{}, err
}
return ctrl.Result{}, nil
}

if err := createKubeVipCMIfNotExist(ctx, r.client, cpUpgrade); err != nil {
if err := namespaceOrCreate(ctx, rClient, log, constants.EksaSystemNamespace); err != nil {
return ctrl.Result{}, nil
}
if err := createKubeVipCMIfNotExist(ctx, rClient, cpUpgrade); err != nil {
return ctrl.Result{}, err
}
log.Info("Upgrading all Control Plane nodes")
Expand Down Expand Up @@ -373,32 +383,32 @@ func getCapiMachine(ctx context.Context, client client.Client, nodeUpgrade *anyw
return machine, nil
}

func cleanupKubeVipCM(ctx context.Context, log logr.Logger, client client.Client) error {
func cleanupKubeVipCM(ctx context.Context, log logr.Logger, remoteClient client.Client) error {
cm := &corev1.ConfigMap{}
if err := client.Get(ctx, GetNamespacedNameType(constants.KubeVipConfigMapName, constants.EksaSystemNamespace), cm); err != nil {
if err := remoteClient.Get(ctx, GetNamespacedNameType(constants.KubeVipConfigMapName, constants.EksaSystemNamespace), cm); err != nil {
if apierrors.IsNotFound(err) {
log.Info("config map %s not found, skipping deletion", "ConfigMap", constants.KubeVipConfigMapName, "Namespace", constants.EksaSystemNamespace)
} else {
return fmt.Errorf("getting %s config map: %v", constants.KubeVipConfigMapName, err)
}
} else {
log.Info("Deleting kube-vip config map", "ConfigMap", constants.KubeVipConfigMapName, "Namespace", constants.EksaSystemNamespace)
if err := client.Delete(ctx, cm); err != nil {
if err := remoteClient.Delete(ctx, cm); err != nil {
return fmt.Errorf("deleting %s config map: %v", constants.KubeVipConfigMapName, err)
}
}
return nil
}

func createKubeVipCMIfNotExist(ctx context.Context, client client.Client, cpUpgrade *anywherev1.ControlPlaneUpgrade) error {
func createKubeVipCMIfNotExist(ctx context.Context, remoteClient client.Client, cpUpgrade *anywherev1.ControlPlaneUpgrade) error {
kubeVipCM := &corev1.ConfigMap{}
if err := client.Get(ctx, GetNamespacedNameType(constants.KubeVipConfigMapName, constants.EksaSystemNamespace), kubeVipCM); err != nil {
if err := remoteClient.Get(ctx, GetNamespacedNameType(constants.KubeVipConfigMapName, constants.EksaSystemNamespace), kubeVipCM); err != nil {
if apierrors.IsNotFound(err) {
kubeVipCM, err = kubeVipConfigMap(cpUpgrade)
if err != nil {
return err
}
if err := client.Create(ctx, kubeVipCM); err != nil {
if err := remoteClient.Create(ctx, kubeVipCM); err != nil {
return fmt.Errorf("failed to create %s config map: %v", constants.KubeVipConfigMapName, err)
}
} else {
Expand All @@ -425,7 +435,6 @@ func kubeVipConfigMap(cpUpgrade *anywherev1.ControlPlaneUpgrade) (*corev1.Config
return nil, errors.New("fetching kube-vip manifest from KubeadmConfigSpec")
}

blockOwnerDeletionFlag := true
return &corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
Kind: "ConfigMap",
Expand All @@ -434,13 +443,6 @@ func kubeVipConfigMap(cpUpgrade *anywherev1.ControlPlaneUpgrade) (*corev1.Config
ObjectMeta: metav1.ObjectMeta{
Name: constants.KubeVipConfigMapName,
Namespace: constants.EksaSystemNamespace,
OwnerReferences: []metav1.OwnerReference{{
APIVersion: cpUpgrade.APIVersion,
Kind: cpUpgrade.Kind,
Name: cpUpgrade.Name,
UID: cpUpgrade.UID,
BlockOwnerDeletion: &blockOwnerDeletionFlag,
}},
},
Data: map[string]string{constants.KubeVipManifestName: kubeVipConfig},
}, nil
Expand Down
Loading

0 comments on commit 7349215

Please sign in to comment.