From 6a44da3ae1a9c3c093663ea761b0172f3c77cbec Mon Sep 17 00:00:00 2001 From: Tom Wieczorek Date: Fri, 20 Sep 2024 16:36:16 +0200 Subject: [PATCH] Create the autopilot client before applying the plan If it's done the other way round, it might happen that the k0s binary is being replaced by the autopilot update process, and cannot be executed. This prevents errors like this: > Process exited with status 126: "sh: /usr/local/bin/k0s: Permission denied" Signed-off-by: Tom Wieczorek --- inttest/ap-airgap/airgap_test.go | 8 ++++---- inttest/ap-controllerworker/controllerworker_test.go | 7 +++---- inttest/ap-platformselect/platformselect_test.go | 8 ++++---- inttest/ap-quorum/quorum_test.go | 8 ++++---- inttest/ap-quorumsafety/quorumsafety_test.go | 8 ++++---- inttest/ap-removedapis/removedapis_test.go | 8 ++++---- inttest/ap-selector/selector_test.go | 9 +++++---- inttest/ap-single/single_test.go | 8 ++++---- inttest/ap-updater-periodic/updater_test.go | 8 ++++---- inttest/ap-updater/updater_test.go | 8 ++++---- inttest/kubeletcertrotate/kubeletcertrotate_test.go | 10 +++++----- 11 files changed, 45 insertions(+), 45 deletions(-) diff --git a/inttest/ap-airgap/airgap_test.go b/inttest/ap-airgap/airgap_test.go index 44d31686a46f..cc852d8d99ba 100644 --- a/inttest/ap-airgap/airgap_test.go +++ b/inttest/ap-airgap/airgap_test.go @@ -142,6 +142,10 @@ spec: ) s.Require().NoError(err) + client, err := s.AutopilotClient(s.ControllerNode(0)) + s.Require().NoError(err) + s.NotEmpty(client) + manifestFile := "/tmp/happy.yaml" s.PutFileTemplate(s.ControllerNode(0), manifestFile, planTemplate, nil) @@ -151,10 +155,6 @@ spec: s.T().Logf("kubectl apply output: '%s'", out) s.Require().NoError(err) - client, err := s.AutopilotClient(s.ControllerNode(0)) - s.Require().NoError(err) - s.NotEmpty(client) - // The plan has enough information to perform a successful update of k0s, so wait for it. _, err = aptest.WaitForPlanState(ctx, client, apconst.AutopilotName, appc.PlanCompleted) s.Require().NoError(err) diff --git a/inttest/ap-controllerworker/controllerworker_test.go b/inttest/ap-controllerworker/controllerworker_test.go index ec56b3ac5682..555dd30510d4 100644 --- a/inttest/ap-controllerworker/controllerworker_test.go +++ b/inttest/ap-controllerworker/controllerworker_test.go @@ -114,6 +114,9 @@ func (s *controllerworkerSuite) SetupTest() { // TestApply applies a well-formed `plan` yaml, and asserts that // all of the correct values across different objects + controllers are correct. func (s *controllerworkerSuite) TestApply() { + client, err := s.AutopilotClient(s.ControllerNode(0)) + s.Require().NoError(err) + s.NotEmpty(client) planTemplate := ` apiVersion: autopilot.k0sproject.io/v1beta2 @@ -149,10 +152,6 @@ spec: s.T().Logf("kubectl apply output: '%s'", out) s.Require().NoError(err) - client, err := s.AutopilotClient(s.ControllerNode(0)) - s.Require().NoError(err) - s.NotEmpty(client) - // The plan has enough information to perform a successful update of k0s, so wait for it. plan, err := aptest.WaitForPlanState(s.Context(), client, apconst.AutopilotName, appc.PlanCompleted) s.Require().NoError(err) diff --git a/inttest/ap-platformselect/platformselect_test.go b/inttest/ap-platformselect/platformselect_test.go index a527a983e76e..a74a3243e7ac 100644 --- a/inttest/ap-platformselect/platformselect_test.go +++ b/inttest/ap-platformselect/platformselect_test.go @@ -51,6 +51,10 @@ func (s *platformSelectSuite) SetupTest() { // TestApply applies a well-formed `plan` yaml that includes multiple // platform definitions, and asserts that the proper binary is downloaded. func (s *platformSelectSuite) TestApply() { + client, err := s.AutopilotClient(s.ControllerNode(0)) + s.Require().NoError(err) + s.NotEmpty(client) + planTemplate := ` apiVersion: autopilot.k0sproject.io/v1beta2 kind: Plan @@ -85,10 +89,6 @@ spec: s.T().Logf("kubectl apply output: '%s'", out) s.Require().NoError(err) - client, err := s.AutopilotClient(s.ControllerNode(0)) - s.Require().NoError(err) - s.NotEmpty(client) - // Its expected that if the wrong platform were to be downloaded, the update wouldn't be successful, // as the binary would fail to run. diff --git a/inttest/ap-quorum/quorum_test.go b/inttest/ap-quorum/quorum_test.go index 6e0ea3961ac4..796858a76729 100644 --- a/inttest/ap-quorum/quorum_test.go +++ b/inttest/ap-quorum/quorum_test.go @@ -77,6 +77,10 @@ func (s *quorumSuite) SetupTest() { // TestApply applies a well-formed `plan` yaml, and asserts that // all of the correct values across different objects + controllers are correct. func (s *quorumSuite) TestApply() { + client, err := s.AutopilotClient(s.ControllerNode(0)) + s.Require().NoError(err) + s.NotEmpty(client) + planTemplate := ` apiVersion: autopilot.k0sproject.io/v1beta2 kind: Plan @@ -109,10 +113,6 @@ spec: s.T().Logf("kubectl apply output: '%s'", out) s.Require().NoError(err) - client, err := s.AutopilotClient(s.ControllerNode(0)) - s.Require().NoError(err) - s.NotEmpty(client) - // The plan has enough information to perform a successful update of k0s, so wait for it. plan, err := aptest.WaitForPlanState(s.Context(), client, apconst.AutopilotName, appc.PlanCompleted) s.Require().NoError(err) diff --git a/inttest/ap-quorumsafety/quorumsafety_test.go b/inttest/ap-quorumsafety/quorumsafety_test.go index 17e18b6fbc51..0763e8b6f73a 100644 --- a/inttest/ap-quorumsafety/quorumsafety_test.go +++ b/inttest/ap-quorumsafety/quorumsafety_test.go @@ -99,6 +99,10 @@ metadata: // Create + populate the plan + client, err := s.AutopilotClient(s.ControllerNode(0)) + s.Require().NoError(err) + s.NotEmpty(client) + planTemplate := ` apiVersion: autopilot.k0sproject.io/v1beta2 kind: Plan @@ -131,10 +135,6 @@ spec: s.T().Logf("kubectl apply output (plan): '%s'", out) s.Require().NoError(err) - client, err := s.AutopilotClient(s.ControllerNode(0)) - s.Require().NoError(err) - s.NotEmpty(client) - // The plan should fail with "InconsistentTargets" due to autopilot detecting that `controller2` // despite existing as a `ControlNode`, does not resolve. _, err = aptest.WaitForPlanState(s.Context(), client, apconst.AutopilotName, appc.PlanInconsistentTargets) diff --git a/inttest/ap-removedapis/removedapis_test.go b/inttest/ap-removedapis/removedapis_test.go index de7d522de9a7..d4e448f7f1a9 100644 --- a/inttest/ap-removedapis/removedapis_test.go +++ b/inttest/ap-removedapis/removedapis_test.go @@ -79,6 +79,10 @@ func (s *plansRemovedAPIsSuite) SetupTest() { // TestApply applies a well-formed `plan` yaml, and asserts that all of the correct values // across different objects are correct. func (s *plansRemovedAPIsSuite) TestApply() { + client, err := s.AutopilotClient(s.ControllerNode(0)) + s.Require().NoError(err) + s.NotEmpty(client) + ctx := s.Context() manifestFile := "/tmp/plan.yaml" @@ -88,10 +92,6 @@ func (s *plansRemovedAPIsSuite) TestApply() { s.T().Logf("kubectl apply output: '%s'", out) s.Require().NoError(err) - client, err := s.AutopilotClient(s.ControllerNode(0)) - s.Require().NoError(err) - s.NotEmpty(client) - // The plan has enough information to perform a successful update of k0s, so wait for it. plan, err := aptest.WaitForPlanState(ctx, client, apconst.AutopilotName, appc.PlanWarning) if s.NoError(err) && s.Len(plan.Status.Commands, 1) { diff --git a/inttest/ap-selector/selector_test.go b/inttest/ap-selector/selector_test.go index a707ea1ca551..7a9026c034dc 100644 --- a/inttest/ap-selector/selector_test.go +++ b/inttest/ap-selector/selector_test.go @@ -126,6 +126,11 @@ spec: s.Require().NoError(err) // Save + apply the plan + + apc, err := s.AutopilotClient(s.ControllerNode(0)) + s.Require().NoError(err) + s.NotEmpty(apc) + manifestFile := "/tmp/plan.yaml" s.PutFileTemplate(s.ControllerNode(0), manifestFile, planTemplate, nil) @@ -133,10 +138,6 @@ spec: s.T().Logf("kubectl apply output: '%s'", out) s.Require().NoError(err) - apc, err := s.AutopilotClient(s.ControllerNode(0)) - s.Require().NoError(err) - s.NotEmpty(apc) - // The plan has enough information to perform a successful update of k0s, so wait for it. plan, err := aptest.WaitForPlanState(s.Context(), apc, apconst.AutopilotName, appc.PlanCompleted) s.Require().NoError(err) diff --git a/inttest/ap-single/single_test.go b/inttest/ap-single/single_test.go index 80092b391a07..27a78cee5e6c 100644 --- a/inttest/ap-single/single_test.go +++ b/inttest/ap-single/single_test.go @@ -61,6 +61,10 @@ func (s *plansSingleControllerSuite) SetupTest() { // TestApply applies a well-formed `plan` yaml, and asserts that all of the correct values // across different objects are correct. func (s *plansSingleControllerSuite) TestApply() { + client, err := s.AutopilotClient(s.ControllerNode(0)) + s.Require().NoError(err) + s.NotEmpty(client) + planTemplate := ` apiVersion: autopilot.k0sproject.io/v1beta2 kind: Plan @@ -91,10 +95,6 @@ spec: s.T().Logf("kubectl apply output: '%s'", out) s.Require().NoError(err) - client, err := s.AutopilotClient(s.ControllerNode(0)) - s.Require().NoError(err) - s.NotEmpty(client) - // The plan has enough information to perform a successful update of k0s, so wait for it. plan, err := aptest.WaitForPlanState(s.Context(), client, apconst.AutopilotName, appc.PlanCompleted) s.Require().NoError(err, "While waiting for plan to complete") diff --git a/inttest/ap-updater-periodic/updater_test.go b/inttest/ap-updater-periodic/updater_test.go index b69c15609b17..9b918333bbe3 100644 --- a/inttest/ap-updater-periodic/updater_test.go +++ b/inttest/ap-updater-periodic/updater_test.go @@ -105,6 +105,10 @@ func (s *plansSingleControllerSuite) getClusterID(kc kubernetes.Interface) strin // TestApply applies a well-formed `plan` yaml, and asserts that all of the correct values // across different objects are correct. func (s *plansSingleControllerSuite) TestApply() { + client, err := s.AutopilotClient(s.ControllerNode(0)) + s.Require().NoError(err) + s.NotEmpty(client) + updaterConfig := ` apiVersion: autopilot.k0sproject.io/v1beta2 kind: UpdateConfig @@ -145,10 +149,6 @@ spec: s.T().Logf("kubectl apply output: '%s'", out) s.Require().NoError(err) - client, err := s.AutopilotClient(s.ControllerNode(0)) - s.Require().NoError(err) - s.NotEmpty(client) - // The plan has enough information to perform a successful update of k0s, so wait for it. _, err = aptest.WaitForPlanState(s.Context(), client, apconst.AutopilotName, appc.PlanCompleted) s.Require().NoError(err) diff --git a/inttest/ap-updater/updater_test.go b/inttest/ap-updater/updater_test.go index c6c28e09b365..bd0664df27cd 100644 --- a/inttest/ap-updater/updater_test.go +++ b/inttest/ap-updater/updater_test.go @@ -56,6 +56,10 @@ func (s *plansSingleControllerSuite) SetupTest() { // TestApply applies a well-formed `plan` yaml, and asserts that all of the correct values // across different objects are correct. func (s *plansSingleControllerSuite) TestApply() { + client, err := s.AutopilotClient(s.ControllerNode(0)) + s.Require().NoError(err) + s.NotEmpty(client) + updaterConfig := ` apiVersion: autopilot.k0sproject.io/v1beta2 kind: UpdateConfig @@ -90,10 +94,6 @@ spec: s.T().Logf("kubectl apply output: '%s'", out) s.Require().NoError(err) - client, err := s.AutopilotClient(s.ControllerNode(0)) - s.Require().NoError(err) - s.NotEmpty(client) - // The plan has enough information to perform a successful update of k0s, so wait for it. _, err = aptest.WaitForPlanState(s.Context(), client, apconst.AutopilotName, appc.PlanCompleted) s.Require().NoError(err) diff --git a/inttest/kubeletcertrotate/kubeletcertrotate_test.go b/inttest/kubeletcertrotate/kubeletcertrotate_test.go index 9e3a968c9f37..d9576cd37017 100644 --- a/inttest/kubeletcertrotate/kubeletcertrotate_test.go +++ b/inttest/kubeletcertrotate/kubeletcertrotate_test.go @@ -84,8 +84,12 @@ func (s *kubeletCertRotateSuite) SetupTest() { } func (s *kubeletCertRotateSuite) applyPlan(id string) { + client, err := s.AutopilotClient(s.ControllerNode(0)) + s.Require().NoError(err) + s.NotEmpty(client) + // Ensure that a plan and yaml do not exist (safely) - _, err := s.RunCommandController(0, "/usr/local/bin/k0s kubectl delete plan autopilot | true") + _, err = s.RunCommandController(0, "/usr/local/bin/k0s kubectl delete plan autopilot | true") s.Require().NoError(err) _, err = s.RunCommandController(0, "rm -f /tmp/happy.yaml") s.Require().NoError(err) @@ -129,10 +133,6 @@ spec: s.T().Logf("kubectl apply output: '%s'", out) s.Require().NoError(err) - client, err := s.AutopilotClient(s.ControllerNode(0)) - s.Require().NoError(err) - s.NotEmpty(client) - // The plan has enough information to perform a successful update of k0s, so wait for it. plan, err := aptest.WaitForPlanState(s.Context(), client, apconst.AutopilotName, appc.PlanCompleted) s.Require().NoError(err)