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