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 <amakhov@mirantis.com>
  • Loading branch information
makhov committed Nov 22, 2024
1 parent 07ad8ac commit abc978e
Show file tree
Hide file tree
Showing 4 changed files with 252 additions and 93 deletions.
45 changes: 44 additions & 1 deletion internal/controller/controlplane/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ import (
cpv1beta1 "github.com/k0sproject/k0smotron/api/controlplane/v1beta1"
)

const (
etcdMemberConditionTypeJoined = "Joined"
)

func (c *K0sController) createMachine(ctx context.Context, name string, cluster *clusterv1.Cluster, kcp *cpv1beta1.K0sControlPlane, infraRef corev1.ObjectReference, failureDomain *string) (*clusterv1.Machine, error) {
machine, err := c.generateMachine(ctx, name, cluster, kcp, infraRef, failureDomain)
if err != nil {
Expand Down Expand Up @@ -243,15 +247,54 @@ func matchesTemplateClonedFrom(infraMachines map[string]*unstructured.Unstructur
clonedFromGroupKind == kcp.Spec.MachineTemplate.InfrastructureRef.GroupVersionKind().GroupKind().String()
}

func (c *K0sController) checkMachineLeft(ctx context.Context, name string, clientset *kubernetes.Clientset) (bool, error) {
var etcdMember unstructured.Unstructured
err := clientset.RESTClient().
Get().
AbsPath("/apis/etcd.k0sproject.io/v1beta1/etcdmembers/" + name).
Do(ctx).
Into(&etcdMember)

if err != nil {
if apierrors.IsNotFound(err) {
return true, nil
}
return false, fmt.Errorf("error getting etcd member: %w", err)
}

conditions, _, err := unstructured.NestedSlice(etcdMember.Object, "status", "conditions")
if err != nil {
return false, fmt.Errorf("error getting etcd member conditions: %w", err)
}

for _, condition := range conditions {
conditionMap := condition.(map[string]interface{})
if conditionMap["type"] == etcdMemberConditionTypeJoined && conditionMap["status"] == "False" {
return true, nil
}
}
return false, nil
}

func (c *K0sController) markChildControlNodeToLeave(ctx context.Context, name string, clientset *kubernetes.Clientset) error {
if clientset == nil {
return nil
}

logger := log.FromContext(ctx).WithValues("controlNode", name)
err := clientset.RESTClient().
Delete().
AbsPath("/apis/coordination.k8s.io/v1/leases/namespaces/kube-node-lease/k0s-ctrl-" + name).
Do(ctx).
Error()
if err != nil && !apierrors.IsNotFound(err) {
logger.Error(err, "error deleting lease. trying to mark etcd member to leave")
}

err = clientset.RESTClient().
Patch(types.MergePatchType).
AbsPath("/apis/etcd.k0sproject.io/v1beta1/etcdmembers/" + name).
Body([]byte(`{"spec":{"leave":true}}`)).
Body([]byte(`{"spec":{"leave":true}, "metadata": {"annotations": {"k0smotron.io/marked-to-leave-at": "` + time.Now().String() + `"}}}`)).
Do(ctx).
Error()
if err != nil {
Expand Down
Loading

0 comments on commit abc978e

Please sign in to comment.