k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/test/e2e/windows/service.go (about)

     1  /*
     2  Copyright 2019 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 windows
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"net"
    23  	"strconv"
    24  
    25  	v1 "k8s.io/api/core/v1"
    26  	clientset "k8s.io/client-go/kubernetes"
    27  
    28  	"k8s.io/kubernetes/test/e2e/framework"
    29  	e2enode "k8s.io/kubernetes/test/e2e/framework/node"
    30  	e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
    31  	e2eservice "k8s.io/kubernetes/test/e2e/framework/service"
    32  	e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper"
    33  	imageutils "k8s.io/kubernetes/test/utils/image"
    34  	admissionapi "k8s.io/pod-security-admission/api"
    35  
    36  	"github.com/onsi/ginkgo/v2"
    37  	"github.com/onsi/gomega"
    38  )
    39  
    40  var _ = sigDescribe("Services", skipUnlessWindows(func() {
    41  	f := framework.NewDefaultFramework("services")
    42  	f.NamespacePodSecurityLevel = admissionapi.LevelPrivileged
    43  
    44  	var cs clientset.Interface
    45  
    46  	ginkgo.BeforeEach(func() {
    47  		//Only for Windows containers
    48  		e2eskipper.SkipUnlessNodeOSDistroIs("windows")
    49  		cs = f.ClientSet
    50  	})
    51  	ginkgo.It("should be able to create a functioning NodePort service for Windows", func(ctx context.Context) {
    52  		serviceName := "nodeport-test"
    53  		ns := f.Namespace.Name
    54  
    55  		jig := e2eservice.NewTestJig(cs, ns, serviceName)
    56  		nodeIP, err := e2enode.PickIP(ctx, jig.Client)
    57  		framework.ExpectNoError(err)
    58  
    59  		ginkgo.By("creating service " + serviceName + " with type=NodePort in namespace " + ns)
    60  		svc, err := jig.CreateTCPService(ctx, func(svc *v1.Service) {
    61  			svc.Spec.Type = v1.ServiceTypeNodePort
    62  		})
    63  		framework.ExpectNoError(err)
    64  
    65  		nodePort := int(svc.Spec.Ports[0].NodePort)
    66  
    67  		ginkgo.By("creating Pod to be part of service " + serviceName)
    68  		// tweak the Jig to use windows...
    69  		windowsNodeSelectorTweak := func(rc *v1.ReplicationController) {
    70  			rc.Spec.Template.Spec.NodeSelector = map[string]string{
    71  				"kubernetes.io/os": "windows",
    72  			}
    73  		}
    74  		_, err = jig.Run(ctx, windowsNodeSelectorTweak)
    75  		framework.ExpectNoError(err)
    76  
    77  		//using hybrid_network methods
    78  		ginkgo.By("creating Windows testing Pod")
    79  		testPod := createTestPod(f, imageutils.GetE2EImage(imageutils.Agnhost), windowsOS)
    80  		testPod = e2epod.NewPodClient(f).CreateSync(ctx, testPod)
    81  
    82  		ginkgo.By("verifying that pod has the correct nodeSelector")
    83  		// Admission controllers may sometimes do the wrong thing
    84  		gomega.Expect(testPod.Spec.NodeSelector).To(gomega.HaveKeyWithValue("kubernetes.io/os", "windows"), "pod.spec.nodeSelector")
    85  		ginkgo.By(fmt.Sprintf("checking connectivity Pod to curl http://%s:%d", nodeIP, nodePort))
    86  		assertConsistentConnectivity(ctx, f, testPod.ObjectMeta.Name, windowsOS, windowsCheck(fmt.Sprintf("http://%s", net.JoinHostPort(nodeIP, strconv.Itoa(nodePort)))), internalMaxTries)
    87  
    88  	})
    89  }))