Skip to content

Commit

Permalink
Initiate update on machine template ref change
Browse files Browse the repository at this point in the history
Signed-off-by: Alexey Makhov <[email protected]>
  • Loading branch information
makhov committed Nov 14, 2024
1 parent f2eef3d commit 331642b
Show file tree
Hide file tree
Showing 4 changed files with 400 additions and 2 deletions.
32 changes: 32 additions & 0 deletions internal/controller/controlplane/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/controllers/external"
"sigs.k8s.io/cluster-api/util/collections"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -97,6 +98,21 @@ func (c *K0sController) generateMachine(_ context.Context, name string, cluster
}, nil
}

func (c *K0sController) getInfraMachines(ctx context.Context, machines collections.Machines) (map[string]*unstructured.Unstructured, error) {
result := map[string]*unstructured.Unstructured{}
for _, m := range machines {
infraMachine, err := external.Get(ctx, c.Client, &m.Spec.InfrastructureRef, m.Namespace)
if err != nil {
if apierrors.IsNotFound(err) {
continue
}
return nil, fmt.Errorf("failed to retrieve infra machine for machine object %s: %w", m.Name, err)
}
result[m.Name] = infraMachine
}
return result, nil
}

func (c *K0sController) createMachineFromTemplate(ctx context.Context, name string, cluster *clusterv1.Cluster, kcp *cpv1beta1.K0sControlPlane) (*unstructured.Unstructured, error) {
machineFromTemplate, err := c.generateMachineFromTemplate(ctx, name, cluster, kcp)
if err != nil {
Expand Down Expand Up @@ -208,6 +224,22 @@ func (c *K0sController) generateMachineFromTemplate(ctx context.Context, name st
return machine, nil
}

func matchesTemplateClonedFrom(infraMachines map[string]*unstructured.Unstructured, kcp *cpv1beta1.K0sControlPlane, machine *clusterv1.Machine) bool {
if machine == nil {
return false
}
infraMachine, found := infraMachines[machine.Name]
if !found {
return false
}

clonedFromName := infraMachine.GetAnnotations()[clusterv1.TemplateClonedFromNameAnnotation]
clonedFromGroupKind := infraMachine.GetAnnotations()[clusterv1.TemplateClonedFromGroupKindAnnotation]

return clonedFromName == kcp.Spec.MachineTemplate.InfrastructureRef.Name &&
clonedFromGroupKind == kcp.Spec.MachineTemplate.InfrastructureRef.GroupVersionKind().GroupKind().String()
}

func (c *K0sController) markChildControlNodeToLeave(ctx context.Context, name string, clientset *kubernetes.Clientset) error {
if clientset == nil {
return nil
Expand Down
13 changes: 11 additions & 2 deletions internal/controller/controlplane/k0s_controlplane_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,18 @@ func (c *K0sController) reconcileMachines(ctx context.Context, cluster *clusterv
}
}

infraMachines, err := c.getInfraMachines(ctx, machines)
if err != nil {
return replicasToReport, fmt.Errorf("error getting infra machines: %w", err)
}

machineNames := make(map[string]bool)
for _, m := range machines.Names() {
machineNames[m] = true
for _, m := range machines {
machineNames[m.Name] = true
if !matchesTemplateClonedFrom(infraMachines, kcp, m) {
desiredReplicas++
machinesToDelete++
}
}

if len(machineNames) < int(desiredReplicas) {
Expand Down
1 change: 1 addition & 0 deletions inttest/Makefile.variables
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ smoketests := \
check-capi-remote-machine-job-provision \
check-capi-remote-machine-template \
check-capi-remote-machine-template-update \
check-capi-docker-machine-change-template \
check-capi-docker-machine-template-update \
check-capi-docker-machine-template-update-recreate \
check-capi-docker-machine-template-update-recreate-single \
Loading

0 comments on commit 331642b

Please sign in to comment.