Skip to content

Commit ad7b9b5

Browse files
test/e2e_node: add e2e test for Kubeletconfig drop-in dir
Signed-off-by: Sohan Kunkerkar <[email protected]> Co-authored-by: Peter Hunt <[email protected]>
1 parent ee5578b commit ad7b9b5

File tree

5 files changed

+157
-10
lines changed

5 files changed

+157
-10
lines changed

test/e2e/framework/test_context.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,14 @@ var (
9999
// Test suite authors can use framework/viper to make all command line
100100
// parameters also configurable via a configuration file.
101101
type TestContextType struct {
102-
KubeConfig string
103-
KubeContext string
104-
KubeAPIContentType string
105-
KubeletRootDir string
106-
CertDir string
107-
Host string
108-
BearerToken string `datapolicy:"token"`
102+
KubeConfig string
103+
KubeContext string
104+
KubeAPIContentType string
105+
KubeletRootDir string
106+
KubeletConfigDropinDir string
107+
CertDir string
108+
Host string
109+
BearerToken string `datapolicy:"token"`
109110
// TODO: Deprecating this over time... instead just use gobindata_util.go , see #23987.
110111
RepoRoot string
111112
// ListImages will list off all images that are used then quit

test/e2e/nodefeature/nodefeature.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ var (
3737
GracefulNodeShutdownBasedOnPodPriority = framework.WithNodeFeature(framework.ValidNodeFeatures.Add("GracefulNodeShutdownBasedOnPodPriority"))
3838
HostAccess = framework.WithNodeFeature(framework.ValidNodeFeatures.Add("HostAccess"))
3939
ImageID = framework.WithNodeFeature(framework.ValidNodeFeatures.Add(" ImageID"))
40+
KubeletConfigDropInDir = framework.WithNodeFeature(framework.ValidNodeFeatures.Add("KubeletConfigDropInDir"))
4041
LSCIQuotaMonitoring = framework.WithNodeFeature(framework.ValidNodeFeatures.Add("LSCIQuotaMonitoring"))
4142
NodeAllocatable = framework.WithNodeFeature(framework.ValidNodeFeatures.Add("NodeAllocatable"))
4243
NodeProblemDetector = framework.WithNodeFeature(framework.ValidNodeFeatures.Add("NodeProblemDetector"))

test/e2e_node/e2e_node_suite_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ func registerNodeFlags(flags *flag.FlagSet) {
9090
framework.TestContext.NodeE2E = true
9191
flags.StringVar(&framework.TestContext.BearerToken, "bearer-token", "", "The bearer token to authenticate with. If not specified, it would be a random token. Currently this token is only used in node e2e tests.")
9292
flags.StringVar(&framework.TestContext.NodeName, "node-name", "", "Name of the node to run tests on.")
93+
flags.StringVar(&framework.TestContext.KubeletConfigDropinDir, "config-dir", "", "Path to a directory containing drop-in configurations for the kubelet.")
9394
// TODO(random-liu): Move kubelet start logic out of the test.
9495
// TODO(random-liu): Move log fetch logic out of the test.
9596
// There are different ways to start kubelet (systemd, initd, docker, manually started etc.)
@@ -200,6 +201,14 @@ func TestE2eNode(t *testing.T) {
200201

201202
// We're not running in a special mode so lets run tests.
202203
gomega.RegisterFailHandler(ginkgo.Fail)
204+
// Initialize the KubeletConfigDropinDir again if the test doesn't run in run-kubelet-mode.
205+
if framework.TestContext.KubeletConfigDropinDir == "" {
206+
var err error
207+
framework.TestContext.KubeletConfigDropinDir, err = services.KubeletConfigDirCWDDir()
208+
if err != nil {
209+
klog.Errorf("failed to create kubelet config directory: %v", err)
210+
}
211+
}
203212
reportDir := framework.TestContext.ReportDir
204213
if reportDir != "" {
205214
// Create the directory if it doesn't already exist
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package e2enode
18+
19+
import (
20+
"context"
21+
"os"
22+
"path/filepath"
23+
"time"
24+
25+
"github.com/onsi/ginkgo/v2"
26+
"github.com/onsi/gomega"
27+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28+
kubeletconfig "k8s.io/kubernetes/pkg/kubelet/apis/config"
29+
"k8s.io/kubernetes/test/e2e/framework"
30+
"k8s.io/kubernetes/test/e2e/nodefeature"
31+
)
32+
33+
var _ = SIGDescribe("Kubelet Config", framework.WithSlow(), framework.WithSerial(), framework.WithDisruptive(), nodefeature.KubeletConfigDropInDir, func() {
34+
f := framework.NewDefaultFramework("kubelet-config-drop-in-dir-test")
35+
ginkgo.Context("when merging drop-in configs", func() {
36+
var oldcfg *kubeletconfig.KubeletConfiguration
37+
ginkgo.BeforeEach(func(ctx context.Context) {
38+
var err error
39+
oldcfg, err = getCurrentKubeletConfig(ctx)
40+
framework.ExpectNoError(err)
41+
})
42+
ginkgo.AfterEach(func(ctx context.Context) {
43+
files, err := filepath.Glob(filepath.Join(framework.TestContext.KubeletConfigDropinDir, "*"+".conf"))
44+
framework.ExpectNoError(err)
45+
for _, file := range files {
46+
err := os.Remove(file)
47+
framework.ExpectNoError(err)
48+
}
49+
updateKubeletConfig(ctx, f, oldcfg, true)
50+
})
51+
ginkgo.It("should merge kubelet configs correctly", func(ctx context.Context) {
52+
// Get the initial kubelet configuration
53+
initialConfig, err := getCurrentKubeletConfig(ctx)
54+
framework.ExpectNoError(err)
55+
56+
ginkgo.By("Stopping the kubelet")
57+
restartKubelet := stopKubelet()
58+
59+
// wait until the kubelet health check will fail
60+
gomega.Eventually(ctx, func() bool {
61+
return kubeletHealthCheck(kubeletHealthCheckURL)
62+
}, f.Timeouts.PodStart, f.Timeouts.Poll).Should(gomega.BeFalse())
63+
64+
configDir := framework.TestContext.KubeletConfigDropinDir
65+
66+
contents := []byte(`apiVersion: kubelet.config.k8s.io/v1beta1
67+
kind: KubeletConfiguration
68+
port: 10255
69+
readOnlyPort: 10257
70+
clusterDNS:
71+
- 192.168.1.10
72+
systemReserved:
73+
memory: 1Gi`)
74+
framework.ExpectNoError(os.WriteFile(filepath.Join(configDir, "10-kubelet.conf"), contents, 0755))
75+
contents = []byte(`apiVersion: kubelet.config.k8s.io/v1beta1
76+
kind: KubeletConfiguration
77+
clusterDNS:
78+
- 192.168.1.1
79+
- 192.168.1.5
80+
- 192.168.1.8
81+
port: 8080
82+
cpuManagerReconcilePeriod: 0s
83+
systemReserved:
84+
memory: 2Gi`)
85+
framework.ExpectNoError(os.WriteFile(filepath.Join(configDir, "20-kubelet.conf"), contents, 0755))
86+
ginkgo.By("Restarting the kubelet")
87+
restartKubelet()
88+
// wait until the kubelet health check will succeed
89+
gomega.Eventually(ctx, func() bool {
90+
return kubeletHealthCheck(kubeletHealthCheckURL)
91+
}, f.Timeouts.PodStart, f.Timeouts.Poll).Should(gomega.BeTrue())
92+
93+
mergedConfig, err := getCurrentKubeletConfig(ctx)
94+
framework.ExpectNoError(err)
95+
96+
// Replace specific fields in the initial configuration with expectedConfig values
97+
initialConfig.Port = int32(8080) // not overridden by second file, should be retained.
98+
initialConfig.ReadOnlyPort = int32(10257) // overridden by second file.
99+
initialConfig.SystemReserved = map[string]string{ // overridden by map in second file.
100+
"memory": "2Gi",
101+
}
102+
initialConfig.ClusterDNS = []string{"192.168.1.1", "192.168.1.5", "192.168.1.8"} // overridden by slice in second file.
103+
// This value was explicitly set in the drop-in, make sure it is retained
104+
initialConfig.CPUManagerReconcilePeriod = metav1.Duration{Duration: time.Duration(0)}
105+
// Meanwhile, this value was not explicitly set, but could have been overridden by a "default" of 0 for the type.
106+
// Ensure the true default persists.
107+
initialConfig.CPUCFSQuotaPeriod = metav1.Duration{Duration: time.Duration(100000000)}
108+
// Compare the expected config with the merged config
109+
gomega.Expect(initialConfig).To(gomega.BeComparableTo(mergedConfig), "Merged kubelet config does not match the expected configuration.")
110+
})
111+
})
112+
113+
})

test/e2e_node/services/kubelet.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,12 @@ func (e *E2EServices) startKubelet(featureGates map[string]bool) (*server, error
174174
return nil, err
175175
}
176176

177+
// KubeletDropInConfiguration directory path
178+
framework.TestContext.KubeletConfigDropinDir, err = KubeletConfigDirCWDDir()
179+
if err != nil {
180+
return nil, err
181+
}
182+
177183
// Create pod directory
178184
podPath, err := createPodDirectory()
179185
if err != nil {
@@ -243,6 +249,8 @@ func (e *E2EServices) startKubelet(featureGates map[string]bool) (*server, error
243249
unitName = fmt.Sprintf("kubelet-%s.service", unitTimestamp)
244250
cmdArgs = append(cmdArgs,
245251
systemdRun,
252+
// Set the environment variable to enable kubelet config drop-in directory.
253+
"-E", "KUBELET_CONFIG_DROPIN_DIR_ALPHA=yes",
246254
"-p", "Delegate=true",
247255
"-p", logLocation+framework.TestContext.ReportDir+"/kubelet.log",
248256
"--unit="+unitName,
@@ -282,6 +290,9 @@ func (e *E2EServices) startKubelet(featureGates map[string]bool) (*server, error
282290
kc.FeatureGates = featureGates
283291
}
284292

293+
// Add the KubeletDropinConfigDirectory flag if set.
294+
cmdArgs = append(cmdArgs, "--config-dir", framework.TestContext.KubeletConfigDropinDir)
295+
285296
// Keep hostname override for convenience.
286297
if framework.TestContext.NodeName != "" { // If node name is specified, set hostname override.
287298
cmdArgs = append(cmdArgs, "--hostname-override", framework.TestContext.NodeName)
@@ -295,7 +306,7 @@ func (e *E2EServices) startKubelet(featureGates map[string]bool) (*server, error
295306
cmdArgs = append(cmdArgs, "--image-service-endpoint", framework.TestContext.ImageServiceEndpoint)
296307
}
297308

298-
if err := writeKubeletConfigFile(kc, kubeletConfigPath); err != nil {
309+
if err := WriteKubeletConfigFile(kc, kubeletConfigPath); err != nil {
299310
return nil, err
300311
}
301312
// add the flag to load config from a file
@@ -324,8 +335,8 @@ func (e *E2EServices) startKubelet(featureGates map[string]bool) (*server, error
324335
return server, server.start()
325336
}
326337

327-
// writeKubeletConfigFile writes the kubelet config file based on the args and returns the filename
328-
func writeKubeletConfigFile(internal *kubeletconfig.KubeletConfiguration, path string) error {
338+
// WriteKubeletConfigFile writes the kubelet config file based on the args and returns the filename
339+
func WriteKubeletConfigFile(internal *kubeletconfig.KubeletConfiguration, path string) error {
329340
data, err := kubeletconfigcodec.EncodeKubeletConfig(internal, kubeletconfigv1beta1.SchemeGroupVersion)
330341
if err != nil {
331342
return err
@@ -408,6 +419,18 @@ func kubeletConfigCWDPath() (string, error) {
408419
return filepath.Join(cwd, "kubelet-config"), nil
409420
}
410421

422+
func KubeletConfigDirCWDDir() (string, error) {
423+
cwd, err := os.Getwd()
424+
if err != nil {
425+
return "", fmt.Errorf("failed to get current working directory: %w", err)
426+
}
427+
dir := filepath.Join(cwd, "kubelet.conf.d")
428+
if err := os.MkdirAll(dir, 0755); err != nil {
429+
return "", err
430+
}
431+
return dir, nil
432+
}
433+
411434
// like createKubeconfig, but creates kubeconfig at current-working-directory/kubeconfig
412435
// returns a fully-qualified path to the kubeconfig file
413436
func createKubeconfigCWD() (string, error) {

0 commit comments

Comments
 (0)