From 145a29208aec7370448f15dc277e2d0cd14056ff Mon Sep 17 00:00:00 2001 From: danj-replicated Date: Wed, 22 Nov 2023 17:13:33 +0000 Subject: [PATCH 1/2] fetch node object from apiserver when (un)cordoning Signed-off-by: danj-replicated (cherry picked from commit c3c94ea09115c89e3d21f7e2a2e76af5191c4255) (cherry picked from commit f9184f4e76e57c7c185f7cdf57fc7958a8519f5f) (cherry picked from commit b2daf6f2147d7d20c9a022075940511bb4ffd833) --- pkg/autopilot/controller/signal/k0s/cordon.go | 8 +++++--- pkg/autopilot/controller/signal/k0s/uncordon.go | 8 +++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/pkg/autopilot/controller/signal/k0s/cordon.go b/pkg/autopilot/controller/signal/k0s/cordon.go index d339d1254780..fb9a7530e2b1 100644 --- a/pkg/autopilot/controller/signal/k0s/cordon.go +++ b/pkg/autopilot/controller/signal/k0s/cordon.go @@ -147,9 +147,11 @@ func (r *cordoning) moveToNextState(ctx context.Context, signalNode crcli.Object // draining ignores daemonsets func (r *cordoning) drainNode(ctx context.Context, signalNode crcli.Object) error { logger := r.log.WithField("signalnode", signalNode.GetName()).WithField("phase", "drain") - node, ok := signalNode.(*corev1.Node) - if !ok { - return fmt.Errorf("failed to covert signalNode to Node") + + //get node from client + node := &corev1.Node{} + if err := r.client.Get(ctx, crcli.ObjectKey{Name: signalNode.GetName()}, node); err != nil { + return fmt.Errorf("failed to get node: %w", err) } drainer := &drain.Helper{ diff --git a/pkg/autopilot/controller/signal/k0s/uncordon.go b/pkg/autopilot/controller/signal/k0s/uncordon.go index f72cdbf35244..d3f5ee18fce6 100644 --- a/pkg/autopilot/controller/signal/k0s/uncordon.go +++ b/pkg/autopilot/controller/signal/k0s/uncordon.go @@ -146,9 +146,11 @@ func (r *uncordoning) moveToNextState(ctx context.Context, signalNode crcli.Obje // unCordonNode un-cordons a node func (r *uncordoning) unCordonNode(ctx context.Context, signalNode crcli.Object) error { logger := r.log.WithField("signalnode", signalNode.GetName()).WithField("phase", "uncordon") - node, ok := signalNode.(*corev1.Node) - if !ok { - return fmt.Errorf("failed to covert signalNode to Node") + + //get node from client + node := &corev1.Node{} + if err := r.client.Get(ctx, crcli.ObjectKey{Name: signalNode.GetName()}, node); err != nil { + return fmt.Errorf("failed to get node: %w", err) } drainer := &drain.Helper{ From 8162c97b7e49d53ead5ede179dcabdeab4b20a5c Mon Sep 17 00:00:00 2001 From: danj-replicated Date: Wed, 22 Nov 2023 19:22:07 +0000 Subject: [PATCH 2/2] Conditionally get node object from client we only want to do this if the signalClient isn't a Node type Signed-off-by: danj-replicated (cherry picked from commit 913f1c90a51f775e5273fa56618713f5ed8184e7) (cherry picked from commit daddf33d4ab3e4be88ee3fe743fc98a49121bf9a) (cherry picked from commit f86a307e3b8f7f45802b10c42327ac7ec90e6eb3) --- pkg/autopilot/controller/signal/k0s/cordon.go | 15 ++++++++++++--- pkg/autopilot/controller/signal/k0s/uncordon.go | 16 +++++++++++++--- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/pkg/autopilot/controller/signal/k0s/cordon.go b/pkg/autopilot/controller/signal/k0s/cordon.go index fb9a7530e2b1..de79bd850a83 100644 --- a/pkg/autopilot/controller/signal/k0s/cordon.go +++ b/pkg/autopilot/controller/signal/k0s/cordon.go @@ -148,10 +148,19 @@ func (r *cordoning) moveToNextState(ctx context.Context, signalNode crcli.Object func (r *cordoning) drainNode(ctx context.Context, signalNode crcli.Object) error { logger := r.log.WithField("signalnode", signalNode.GetName()).WithField("phase", "drain") - //get node from client node := &corev1.Node{} - if err := r.client.Get(ctx, crcli.ObjectKey{Name: signalNode.GetName()}, node); err != nil { - return fmt.Errorf("failed to get node: %w", err) + // if signalNode is a Node cast it to *corev1.Node + if signalNode.GetObjectKind().GroupVersionKind().Kind == "Node" { + var ok bool + node, ok = signalNode.(*corev1.Node) + if !ok { + return fmt.Errorf("failed to cast signalNode to *corev1.Node") + } + } else { + //otherwise get node from client + if err := r.client.Get(ctx, crcli.ObjectKey{Name: signalNode.GetName()}, node); err != nil { + return fmt.Errorf("failed to get node: %w", err) + } } drainer := &drain.Helper{ diff --git a/pkg/autopilot/controller/signal/k0s/uncordon.go b/pkg/autopilot/controller/signal/k0s/uncordon.go index d3f5ee18fce6..f8c39e5cee40 100644 --- a/pkg/autopilot/controller/signal/k0s/uncordon.go +++ b/pkg/autopilot/controller/signal/k0s/uncordon.go @@ -147,10 +147,20 @@ func (r *uncordoning) moveToNextState(ctx context.Context, signalNode crcli.Obje func (r *uncordoning) unCordonNode(ctx context.Context, signalNode crcli.Object) error { logger := r.log.WithField("signalnode", signalNode.GetName()).WithField("phase", "uncordon") - //get node from client node := &corev1.Node{} - if err := r.client.Get(ctx, crcli.ObjectKey{Name: signalNode.GetName()}, node); err != nil { - return fmt.Errorf("failed to get node: %w", err) + + // if signalNode is a Node cast it to *corev1.Node + if signalNode.GetObjectKind().GroupVersionKind().Kind == "Node" { + var ok bool + node, ok = signalNode.(*corev1.Node) + if !ok { + return fmt.Errorf("failed to convert signalNode to Node") + } + } else { + //otherwise get node from client + if err := r.client.Get(ctx, crcli.ObjectKey{Name: signalNode.GetName()}, node); err != nil { + return fmt.Errorf("failed to get node: %w", err) + } } drainer := &drain.Helper{