utils.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. Copyright 2014 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 integration
  14. import (
  15. "context"
  16. "testing"
  17. "time"
  18. "google.golang.org/grpc"
  19. apierrors "k8s.io/apimachinery/pkg/api/errors"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/apimachinery/pkg/util/wait"
  22. "k8s.io/apiserver/pkg/storage/storagebackend"
  23. clientset "k8s.io/client-go/kubernetes"
  24. coreclient "k8s.io/client-go/kubernetes/typed/core/v1"
  25. "go.etcd.io/etcd/clientv3"
  26. "go.etcd.io/etcd/pkg/transport"
  27. )
  28. // DeletePodOrErrorf deletes a pod or fails with a call to t.Errorf.
  29. func DeletePodOrErrorf(t *testing.T, c clientset.Interface, ns, name string) {
  30. if err := c.CoreV1().Pods(ns).Delete(context.TODO(), name, nil); err != nil {
  31. t.Errorf("unable to delete pod %v: %v", name, err)
  32. }
  33. }
  34. // Requests to try. Each one should be forbidden or not forbidden
  35. // depending on the authentication and authorization setup of the master.
  36. var (
  37. Code200 = map[int]bool{200: true}
  38. Code201 = map[int]bool{201: true}
  39. Code400 = map[int]bool{400: true}
  40. Code401 = map[int]bool{401: true}
  41. Code403 = map[int]bool{403: true}
  42. Code404 = map[int]bool{404: true}
  43. Code405 = map[int]bool{405: true}
  44. Code409 = map[int]bool{409: true}
  45. Code422 = map[int]bool{422: true}
  46. Code500 = map[int]bool{500: true}
  47. Code503 = map[int]bool{503: true}
  48. )
  49. // WaitForPodToDisappear polls the API server if the pod has been deleted.
  50. func WaitForPodToDisappear(podClient coreclient.PodInterface, podName string, interval, timeout time.Duration) error {
  51. return wait.PollImmediate(interval, timeout, func() (bool, error) {
  52. _, err := podClient.Get(context.TODO(), podName, metav1.GetOptions{})
  53. if err == nil {
  54. return false, nil
  55. }
  56. if apierrors.IsNotFound(err) {
  57. return true, nil
  58. }
  59. return false, err
  60. })
  61. }
  62. // GetEtcdClients returns an initialized clientv3.Client and clientv3.KV.
  63. func GetEtcdClients(config storagebackend.TransportConfig) (*clientv3.Client, clientv3.KV, error) {
  64. tlsInfo := transport.TLSInfo{
  65. CertFile: config.CertFile,
  66. KeyFile: config.KeyFile,
  67. TrustedCAFile: config.TrustedCAFile,
  68. }
  69. tlsConfig, err := tlsInfo.ClientConfig()
  70. if err != nil {
  71. return nil, nil, err
  72. }
  73. cfg := clientv3.Config{
  74. Endpoints: config.ServerList,
  75. DialTimeout: 20 * time.Second,
  76. DialOptions: []grpc.DialOption{
  77. grpc.WithBlock(), // block until the underlying connection is up
  78. },
  79. TLS: tlsConfig,
  80. }
  81. c, err := clientv3.New(cfg)
  82. if err != nil {
  83. return nil, nil, err
  84. }
  85. return c, clientv3.NewKV(c), nil
  86. }