Skip to content

Commit

Permalink
Create the autopilot client before applying the plan
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
twz123 committed Sep 23, 2024
1 parent 32a3061 commit 6a44da3
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 45 deletions.
8 changes: 4 additions & 4 deletions inttest/ap-airgap/airgap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
Expand Down
7 changes: 3 additions & 4 deletions inttest/ap-controllerworker/controllerworker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions inttest/ap-platformselect/platformselect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.

Expand Down
8 changes: 4 additions & 4 deletions inttest/ap-quorum/quorum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions inttest/ap-quorumsafety/quorumsafety_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions inttest/ap-removedapis/removedapis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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) {
Expand Down
9 changes: 5 additions & 4 deletions inttest/ap-selector/selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,17 +126,18 @@ 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)

out, err := s.RunCommandController(0, fmt.Sprintf("/usr/local/bin/k0s kubectl apply -f %s", manifestFile))
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)
Expand Down
8 changes: 4 additions & 4 deletions inttest/ap-single/single_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down
8 changes: 4 additions & 4 deletions inttest/ap-updater-periodic/updater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions inttest/ap-updater/updater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions inttest/kubeletcertrotate/kubeletcertrotate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 6a44da3

Please sign in to comment.