-
Notifications
You must be signed in to change notification settings - Fork 368
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Standalone autopilot update logic #3406
Merged
jnummelin
merged 1 commit into
k0sproject:main
from
jnummelin:feat/standalone-autopilot-updates
Sep 25, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's 0.4.1 which has version constraints like
version.NewConstraint(">= v1.23.0+k0s.2").Check(k0sVersion)