-
Notifications
You must be signed in to change notification settings - Fork 368
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3406 from jnummelin/feat/standalone-autopilot-upd…
…ates Standalone autopilot update logic
- Loading branch information
Showing
25 changed files
with
1,618 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
// Copyright 2023 k0s authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package updater | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/k0sproject/k0s/inttest/common" | ||
aptest "github.com/k0sproject/k0s/inttest/common/autopilot" | ||
|
||
apconst "github.com/k0sproject/k0s/pkg/autopilot/constant" | ||
appc "github.com/k0sproject/k0s/pkg/autopilot/controller/plans/core" | ||
"github.com/k0sproject/k0s/pkg/kubernetes/watch" | ||
"github.com/stretchr/testify/suite" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes" | ||
) | ||
|
||
const ( | ||
ManifestTestDirPerms = "775" | ||
) | ||
|
||
type plansSingleControllerSuite struct { | ||
common.FootlooseSuite | ||
} | ||
|
||
var envTemplate = ` | ||
export K0S_UPDATE_SERVER={{.Address}} | ||
export K0S_UPDATE_PERIOD=1m | ||
export K0S_UPDATE_CHECK_INTERVAL=1m | ||
` | ||
|
||
// SetupTest prepares the controller and filesystem, getting it into a consistent | ||
// state which we can run tests against. | ||
func (s *plansSingleControllerSuite) SetupTest() { | ||
ctx := s.Context() | ||
s.Require().NoError(s.WaitForSSH(s.ControllerNode(0), 2*time.Minute, 1*time.Second)) | ||
|
||
// Dump some env vars for testing in /etc/conf.d/k0scontroller | ||
vars := struct { | ||
Address string | ||
}{ | ||
Address: fmt.Sprintf("http://%s", s.GetUpdateServerIPAddress()), | ||
} | ||
s.PutFileTemplate(s.ControllerNode(0), "/etc/conf.d/k0scontroller", envTemplate, vars) | ||
|
||
s.Require().NoError(s.InitController(0), "--disable-components=metrics-server") | ||
s.Require().NoError(s.WaitJoinAPI(s.ControllerNode(0))) | ||
|
||
kc, err := s.KubeClient(s.ControllerNode(0)) | ||
s.Require().NoError(err) | ||
|
||
client, err := s.ExtensionsClient(s.ControllerNode(0)) | ||
s.Require().NoError(err) | ||
|
||
s.Require().NoError(aptest.WaitForCRDByName(ctx, client, "plans")) | ||
s.Require().NoError(aptest.WaitForCRDByName(ctx, client, "controlnodes")) | ||
s.Require().NoError(aptest.WaitForCRDByName(ctx, client, "updateconfigs")) | ||
// Wait that we see an event for update before proceeding to actual update testing | ||
err = watch.Events(kc.CoreV1().Events("")). | ||
Until(s.Context(), func(e *corev1.Event) (done bool, err error) { | ||
return e.Type == "Normal" && | ||
e.Source.Component == "k0s" && | ||
e.Reason == "NewVersionAvailable", nil | ||
}) | ||
s.Require().NoError(err) | ||
// Get the first line of access logs to verify that the update headers are present | ||
ssh, err := s.SSH(s.Context(), "updateserver0") | ||
s.Require().NoError(err) | ||
defer ssh.Disconnect() | ||
logs, err := ssh.ExecWithOutput(s.Context(), "head -1 /var/log/nginx/access.log") | ||
s.Require().NoError(err) | ||
s.verifyUpdateHeaders(kc, logs) | ||
} | ||
|
||
func (s *plansSingleControllerSuite) verifyUpdateHeaders(kc kubernetes.Interface, logLine string) { | ||
// Verify that the update headers are present in the update server logs | ||
s.Require().Contains(logLine, `K0S_StorageType="etcd"`) | ||
s.Require().Contains(logLine, "K0S_ControlPlaneNodesCount=1") | ||
s.Require().Contains(logLine, fmt.Sprintf(`K0S_ClusterID="%s"`, s.getClusterID(kc))) | ||
s.Require().Contains(logLine, `K0S_CNIProvider="kuberouter"`) | ||
} | ||
|
||
func (s *plansSingleControllerSuite) getClusterID(kc kubernetes.Interface) string { | ||
ns, err := kc.CoreV1().Namespaces().Get(s.Context(), "kube-system", metav1.GetOptions{}) | ||
s.Require().NoError(err) | ||
return fmt.Sprintf("%s:%s", ns.Name, ns.UID) | ||
} | ||
|
||
// TestApply applies a well-formed `plan` yaml, and asserts that all of the correct values | ||
// across different objects are correct. | ||
func (s *plansSingleControllerSuite) TestApply() { | ||
updaterConfig := ` | ||
apiVersion: autopilot.k0sproject.io/v1beta2 | ||
kind: UpdateConfig | ||
metadata: | ||
name: autopilot | ||
spec: | ||
channel: latest | ||
updateServer: {{.Address}} | ||
upgradeStrategy: | ||
type: periodic | ||
periodic: | ||
days: [Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday] | ||
startTime: 00:00 | ||
length: 24h | ||
planSpec: | ||
commands: | ||
- k0supdate: | ||
forceupdate: true | ||
targets: | ||
controllers: | ||
discovery: | ||
selector: {} | ||
workers: | ||
discovery: | ||
selector: {} | ||
` | ||
|
||
vars := struct { | ||
Address string | ||
}{ | ||
Address: fmt.Sprintf("http://%s", s.GetUpdateServerIPAddress()), | ||
} | ||
|
||
manifestFile := "/tmp/updateconfig.yaml" | ||
s.PutFileTemplate(s.ControllerNode(0), manifestFile, updaterConfig, vars) | ||
|
||
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) | ||
|
||
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) | ||
|
||
kc, err := s.KubeClient(s.ControllerNode(0)) | ||
s.Require().NoError(err) | ||
|
||
// Verify that the update headers are present in the update server logs, | ||
// ignoring the "grab" user-agent as that's the actual k0s bin download which we don't care | ||
ssh, err := s.SSH(s.Context(), "updateserver0") | ||
s.Require().NoError(err) | ||
defer ssh.Disconnect() | ||
logs, err := ssh.ExecWithOutput(s.Context(), "grep -v grab /var/log/nginx/access.log | tail -1") | ||
s.Require().NoError(err) | ||
|
||
s.verifyUpdateHeaders(kc, logs) | ||
|
||
} | ||
|
||
// TestPlansSingleControllerSuite sets up a suite using a single controller, running various | ||
// autopilot upgrade scenarios against it. | ||
func TestPlansSingleControllerSuite(t *testing.T) { | ||
suite.Run(t, &plansSingleControllerSuite{ | ||
common.FootlooseSuite{ | ||
ControllerCount: 1, | ||
WorkerCount: 0, | ||
WithUpdateServer: true, | ||
LaunchMode: common.LaunchModeOpenRC, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
channel: latest | ||
version: v5.6.7 | ||
downloadURLs: | ||
- arch: amd64 | ||
os: linux | ||
k0s: http://localhost/dist/k0s | ||
- arch: arm64 | ||
os: linux | ||
k0s: http://localhost/dist/k0s | ||
- arch: arm | ||
os: linux | ||
k0s: http://localhost/dist/k0s |
Oops, something went wrong.