services.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package upgrades
  14. import (
  15. v1 "k8s.io/api/core/v1"
  16. "k8s.io/apimachinery/pkg/util/wait"
  17. "k8s.io/kubernetes/test/e2e/framework"
  18. e2eservice "k8s.io/kubernetes/test/e2e/framework/service"
  19. "github.com/onsi/ginkgo"
  20. )
  21. // ServiceUpgradeTest tests that a service is available before and
  22. // after a cluster upgrade. During a master-only upgrade, it will test
  23. // that a service remains available during the upgrade.
  24. type ServiceUpgradeTest struct {
  25. jig *e2eservice.TestJig
  26. tcpService *v1.Service
  27. tcpIngressIP string
  28. svcPort int
  29. }
  30. // Name returns the tracking name of the test.
  31. func (ServiceUpgradeTest) Name() string { return "service-upgrade" }
  32. func shouldTestPDBs() bool { return framework.ProviderIs("gce", "gke") }
  33. // Setup creates a service with a load balancer and makes sure it's reachable.
  34. func (t *ServiceUpgradeTest) Setup(f *framework.Framework) {
  35. serviceName := "service-test"
  36. jig := e2eservice.NewTestJig(f.ClientSet, f.Namespace.Name, serviceName)
  37. ns := f.Namespace
  38. cs := f.ClientSet
  39. ginkgo.By("creating a TCP service " + serviceName + " with type=LoadBalancer in namespace " + ns.Name)
  40. _, err := jig.CreateTCPService(func(s *v1.Service) {
  41. s.Spec.Type = v1.ServiceTypeLoadBalancer
  42. })
  43. framework.ExpectNoError(err)
  44. tcpService, err := jig.WaitForLoadBalancer(e2eservice.GetServiceLoadBalancerCreationTimeout(cs))
  45. framework.ExpectNoError(err)
  46. // Get info to hit it with
  47. tcpIngressIP := e2eservice.GetIngressPoint(&tcpService.Status.LoadBalancer.Ingress[0])
  48. svcPort := int(tcpService.Spec.Ports[0].Port)
  49. ginkgo.By("creating pod to be part of service " + serviceName)
  50. rc, err := jig.Run(jig.AddRCAntiAffinity)
  51. framework.ExpectNoError(err)
  52. if shouldTestPDBs() {
  53. ginkgo.By("creating a PodDisruptionBudget to cover the ReplicationController")
  54. _, err = jig.CreatePDB(rc)
  55. framework.ExpectNoError(err)
  56. }
  57. // Hit it once before considering ourselves ready
  58. ginkgo.By("hitting the pod through the service's LoadBalancer")
  59. timeout := e2eservice.LoadBalancerLagTimeoutDefault
  60. if framework.ProviderIs("aws") {
  61. timeout = e2eservice.LoadBalancerLagTimeoutAWS
  62. }
  63. e2eservice.TestReachableHTTP(tcpIngressIP, svcPort, timeout)
  64. t.jig = jig
  65. t.tcpService = tcpService
  66. t.tcpIngressIP = tcpIngressIP
  67. t.svcPort = svcPort
  68. }
  69. // Test runs a connectivity check to the service.
  70. func (t *ServiceUpgradeTest) Test(f *framework.Framework, done <-chan struct{}, upgrade UpgradeType) {
  71. switch upgrade {
  72. case MasterUpgrade, ClusterUpgrade:
  73. t.test(f, done, true, true)
  74. case NodeUpgrade:
  75. // Node upgrades should test during disruption only on GCE/GKE for now.
  76. t.test(f, done, shouldTestPDBs(), false)
  77. default:
  78. t.test(f, done, false, false)
  79. }
  80. }
  81. // Teardown cleans up any remaining resources.
  82. func (t *ServiceUpgradeTest) Teardown(f *framework.Framework) {
  83. // rely on the namespace deletion to clean up everything
  84. }
  85. func (t *ServiceUpgradeTest) test(f *framework.Framework, done <-chan struct{}, testDuringDisruption, testFinalizer bool) {
  86. if testDuringDisruption {
  87. // Continuous validation
  88. ginkgo.By("continuously hitting the pod through the service's LoadBalancer")
  89. wait.Until(func() {
  90. e2eservice.TestReachableHTTP(t.tcpIngressIP, t.svcPort, e2eservice.LoadBalancerLagTimeoutDefault)
  91. }, framework.Poll, done)
  92. } else {
  93. // Block until upgrade is done
  94. ginkgo.By("waiting for upgrade to finish without checking if service remains up")
  95. <-done
  96. }
  97. // Hit it once more
  98. ginkgo.By("hitting the pod through the service's LoadBalancer")
  99. e2eservice.TestReachableHTTP(t.tcpIngressIP, t.svcPort, e2eservice.LoadBalancerLagTimeoutDefault)
  100. if testFinalizer {
  101. defer func() {
  102. ginkgo.By("Check that service can be deleted with finalizer")
  103. e2eservice.WaitForServiceDeletedWithFinalizer(t.jig.Client, t.tcpService.Namespace, t.tcpService.Name)
  104. }()
  105. ginkgo.By("Check that finalizer is present on loadBalancer type service")
  106. e2eservice.WaitForServiceUpdatedWithFinalizer(t.jig.Client, t.tcpService.Namespace, t.tcpService.Name, true)
  107. }
  108. }