kubernetes/test/e2e_node_windows/standalone_test.go
zylxjtu 312d00189c Add Windows node-level e2e tests to a dedicated directory
Introduce test/e2e_node_windows/ as a self-contained Windows node e2e
test suite, fully isolated from the Linux-focused test/e2e_node/ package.
All new files are gated with //go:build windows, and the tree includes
a scoped OWNERS file so it lands under an agreed governance model.

In hack/lib/golang.sh, skip building test/e2e_node/e2e_node.test when
KUBE_BUILD_PLATFORMS targets Windows. Windows has a separate e2e_node
test binary which does not currently need to be bundled in an archive.

Document the Windows feature label in test/e2e/feature/feature.go.
2026-05-26 18:04:09 +00:00

218 lines
6.4 KiB
Go

//go:build windows
/*
Copyright The Kubernetes 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 e2enodewindows
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"time"
v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/uuid"
"k8s.io/cli-runtime/pkg/printers"
"k8s.io/kubernetes/pkg/cluster/ports"
"k8s.io/kubernetes/test/e2e/feature"
"k8s.io/kubernetes/test/e2e/framework"
testutils "k8s.io/kubernetes/test/utils"
imageutils "k8s.io/kubernetes/test/utils/image"
admissionapi "k8s.io/pod-security-admission/api"
"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
)
var _ = SIGWindowsDescribe(feature.Windows, feature.StandaloneMode, func() {
f := framework.NewDefaultFramework("static-pod")
f.NamespacePodSecurityLevel = admissionapi.LevelBaseline
ginkgo.Context("when creating a windows static pod", func() {
var ns, podPath, staticPodName string
ginkgo.It("the pod should be running", func(ctx context.Context) {
ns = f.Namespace.Name
staticPodName = "static-pod-" + string(uuid.NewUUID())
podPath = kubeletCfg.StaticPodPath
err := createWindowsBasicStaticPod(podPath, staticPodName, ns)
framework.ExpectNoError(err)
gomega.Eventually(ctx, func(ctx context.Context) error {
pod, err := getPodFromStandaloneKubelet(ctx, ns, staticPodName)
if err != nil {
return fmt.Errorf("error getting pod(%v/%v) from standalone kubelet: %v", ns, staticPodName, err)
}
isReady, err := testutils.PodRunningReady(pod)
if err != nil {
return fmt.Errorf("error checking if pod (%v/%v) is running ready: %v", ns, staticPodName, err)
}
if !isReady {
return fmt.Errorf("pod (%v/%v) is not running", ns, staticPodName)
}
return nil
}, f.Timeouts.PodStart, time.Second*5).Should(gomega.BeNil())
})
ginkgo.AfterEach(func(ctx context.Context) {
ginkgo.By(fmt.Sprintf("delete the static pod (%v/%v)", ns, staticPodName))
err := deleteStaticPod(podPath, staticPodName, ns)
framework.ExpectNoError(err)
ginkgo.By(fmt.Sprintf("wait for pod to disappear (%v/%v)", ns, staticPodName))
gomega.Eventually(ctx, func(ctx context.Context) error {
_, err := getPodFromStandaloneKubelet(ctx, ns, staticPodName)
if apierrors.IsNotFound(err) {
return nil
}
return fmt.Errorf("pod (%v/%v) still exists", ns, staticPodName)
}).Should(gomega.Succeed())
})
})
})
func createWindowsBasicStaticPod(dir, name, namespace string) error {
podSpec := &v1.Pod{
TypeMeta: metav1.TypeMeta{
Kind: "Pod",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Spec: v1.PodSpec{
RestartPolicy: v1.RestartPolicyAlways,
Containers: []v1.Container{
{
Name: "regular1",
Image: imageutils.GetE2EImage(imageutils.BusyBox),
Command: []string{
"powershell", "-c", "touch /tmp/healthy; sleep 10000",
},
Resources: v1.ResourceRequirements{
Requests: v1.ResourceList{
v1.ResourceMemory: resource.MustParse("100Mi"),
},
Limits: v1.ResourceList{
v1.ResourceMemory: resource.MustParse("512Mi"),
},
},
ReadinessProbe: &v1.Probe{
InitialDelaySeconds: 2,
TimeoutSeconds: 2,
ProbeHandler: v1.ProbeHandler{
Exec: &v1.ExecAction{
Command: []string{"powershell", "-c", "cat /tmp/healthy"},
},
},
},
},
},
},
}
file := staticPodPath(dir, name, namespace)
f, err := os.OpenFile(file, os.O_RDWR|os.O_TRUNC|os.O_CREATE, 0666)
if err != nil {
return err
}
defer f.Close()
y := printers.YAMLPrinter{}
if err := y.PrintObj(podSpec, f); err != nil {
return err
}
return nil
}
// staticPodPath returns the path for a static pod manifest file.
func staticPodPath(dir, name, namespace string) string {
return filepath.Join(dir, namespace+"-"+name+".yaml")
}
// deleteStaticPod removes the static pod manifest file.
func deleteStaticPod(dir, name, namespace string) error {
file := staticPodPath(dir, name, namespace)
return os.Remove(file)
}
// getPodFromStandaloneKubelet queries the kubelet's read-only port to find a pod by name.
func getPodFromStandaloneKubelet(ctx context.Context, podNamespace string, podName string) (*v1.Pod, error) {
endpoint := fmt.Sprintf("http://127.0.0.1:%d/pods", ports.KubeletReadOnlyPort)
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
req, err := http.NewRequest("GET", endpoint, nil)
framework.ExpectNoError(err)
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", framework.TestContext.BearerToken))
req.Header.Add("Accept", "application/json")
resp, err := client.Do(req)
if err != nil {
framework.Logf("Failed to get /pods: %v", err)
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
framework.Logf("/pods response status not 200. Response was: %+v", resp)
return nil, fmt.Errorf("/pods response was not 200: %v", err)
}
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("/pods response was unable to be read: %v", err)
}
pods, err := decodePods(respBody)
if err != nil {
return nil, fmt.Errorf("unable to decode /pods: %v", err)
}
for _, p := range pods.Items {
// Static pods have a node name suffix so comparing as substring
if strings.Contains(p.Name, podName) && strings.Contains(p.Namespace, podNamespace) {
return &p, nil
}
}
return nil, apierrors.NewNotFound(schema.GroupResource{Resource: "pods"}, podName)
}
// decodePods decodes the JSON response from the kubelet /pods endpoint.
func decodePods(respBody []byte) (*v1.PodList, error) {
var pods v1.PodList
err := json.Unmarshal(respBody, &pods)
if err != nil {
return nil, err
}
return &pods, nil
}