util.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package util
  13. import (
  14. "bytes"
  15. "context"
  16. "fmt"
  17. "net/http"
  18. "os"
  19. "time"
  20. fluxhelm "github.com/fluxcd/helm-controller/api/v2beta1"
  21. fluxsrc "github.com/fluxcd/source-controller/api/v1beta2"
  22. // nolint
  23. . "github.com/onsi/ginkgo/v2"
  24. v1 "k8s.io/api/core/v1"
  25. apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
  26. apierrors "k8s.io/apimachinery/pkg/api/errors"
  27. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  28. "k8s.io/apimachinery/pkg/runtime"
  29. "k8s.io/apimachinery/pkg/util/wait"
  30. "k8s.io/client-go/kubernetes"
  31. "k8s.io/client-go/kubernetes/scheme"
  32. restclient "k8s.io/client-go/rest"
  33. "k8s.io/client-go/tools/clientcmd"
  34. "k8s.io/client-go/tools/remotecommand"
  35. crclient "sigs.k8s.io/controller-runtime/pkg/client"
  36. esv1alpha1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  37. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  38. genv1alpha1 "github.com/external-secrets/external-secrets/apis/generators/v1alpha1"
  39. )
  40. var Scheme = runtime.NewScheme()
  41. func init() {
  42. _ = scheme.AddToScheme(Scheme)
  43. _ = esv1beta1.AddToScheme(Scheme)
  44. _ = esv1alpha1.AddToScheme(Scheme)
  45. _ = genv1alpha1.AddToScheme(Scheme)
  46. _ = fluxhelm.AddToScheme(Scheme)
  47. _ = fluxsrc.AddToScheme(Scheme)
  48. _ = apiextensionsv1.AddToScheme(Scheme)
  49. }
  50. const (
  51. // How often to poll for conditions.
  52. Poll = 2 * time.Second
  53. )
  54. // CreateKubeNamespace creates a new Kubernetes Namespace for a test.
  55. func CreateKubeNamespace(baseName string, kubeClientSet kubernetes.Interface) (*v1.Namespace, error) {
  56. ns := &v1.Namespace{
  57. ObjectMeta: metav1.ObjectMeta{
  58. GenerateName: fmt.Sprintf("e2e-tests-%v-", baseName),
  59. },
  60. }
  61. return kubeClientSet.CoreV1().Namespaces().Create(context.TODO(), ns, metav1.CreateOptions{})
  62. }
  63. // DeleteKubeNamespace will delete a namespace resource.
  64. func DeleteKubeNamespace(namespace string, kubeClientSet kubernetes.Interface) error {
  65. return kubeClientSet.CoreV1().Namespaces().Delete(context.TODO(), namespace, metav1.DeleteOptions{})
  66. }
  67. // WaitForKubeNamespaceNotExist will wait for the namespace with the given name
  68. // to not exist for up to 2 minutes.
  69. func WaitForKubeNamespaceNotExist(namespace string, kubeClientSet kubernetes.Interface) error {
  70. return wait.PollImmediate(Poll, time.Minute*2, namespaceNotExist(kubeClientSet, namespace))
  71. }
  72. func namespaceNotExist(c kubernetes.Interface, namespace string) wait.ConditionFunc {
  73. return func() (bool, error) {
  74. _, err := c.CoreV1().Namespaces().Get(context.TODO(), namespace, metav1.GetOptions{})
  75. if apierrors.IsNotFound(err) {
  76. return true, nil
  77. }
  78. if err != nil {
  79. return false, err
  80. }
  81. return false, nil
  82. }
  83. }
  84. // ExecCmd exec command on specific pod and wait the command's output.
  85. func ExecCmd(client kubernetes.Interface, config *restclient.Config, podName, namespace string,
  86. command string) (string, error) {
  87. cmd := []string{
  88. "sh",
  89. "-c",
  90. command,
  91. }
  92. req := client.CoreV1().RESTClient().Post().Resource("pods").Name(podName).
  93. Namespace(namespace).SubResource("exec")
  94. option := &v1.PodExecOptions{
  95. Command: cmd,
  96. Stdin: false,
  97. Stdout: true,
  98. Stderr: true,
  99. TTY: false,
  100. }
  101. req.VersionedParams(
  102. option,
  103. scheme.ParameterCodec,
  104. )
  105. exec, err := remotecommand.NewSPDYExecutor(config, "POST", req.URL())
  106. if err != nil {
  107. return "", err
  108. }
  109. var stdout, stderr bytes.Buffer
  110. err = exec.Stream(remotecommand.StreamOptions{
  111. Stdout: &stdout,
  112. Stderr: &stderr,
  113. Tty: false,
  114. })
  115. if err != nil {
  116. return "", fmt.Errorf("unable to exec stream: %w: \n%s\n%s", err, stdout.String(), stderr.String())
  117. }
  118. return stdout.String() + stderr.String(), nil
  119. }
  120. // WaitForPodsRunning waits for a given amount of time until a group of Pods is running in the given namespace.
  121. func WaitForPodsRunning(kubeClientSet kubernetes.Interface, expectedReplicas int, namespace string, opts metav1.ListOptions) (*v1.PodList, error) {
  122. var pods *v1.PodList
  123. err := wait.PollImmediate(1*time.Second, time.Minute*5, func() (bool, error) {
  124. pl, err := kubeClientSet.CoreV1().Pods(namespace).List(context.TODO(), opts)
  125. if err != nil {
  126. return false, nil
  127. }
  128. r := 0
  129. for i := range pl.Items {
  130. if pl.Items[i].Status.Phase == v1.PodRunning {
  131. r++
  132. }
  133. }
  134. if r == expectedReplicas {
  135. pods = pl
  136. return true, nil
  137. }
  138. return false, nil
  139. })
  140. return pods, err
  141. }
  142. // WaitForPodsReady waits for a given amount of time until a group of Pods is running in the given namespace.
  143. func WaitForPodsReady(kubeClientSet kubernetes.Interface, expectedReplicas int, namespace string, opts metav1.ListOptions) error {
  144. return wait.PollImmediate(1*time.Second, time.Minute*5, func() (bool, error) {
  145. pl, err := kubeClientSet.CoreV1().Pods(namespace).List(context.TODO(), opts)
  146. if err != nil {
  147. return false, nil
  148. }
  149. r := 0
  150. for i := range pl.Items {
  151. if isRunning, _ := podRunningReady(&pl.Items[i]); isRunning {
  152. r++
  153. }
  154. }
  155. if r == expectedReplicas {
  156. return true, nil
  157. }
  158. return false, nil
  159. })
  160. }
  161. // podRunningReady checks whether pod p's phase is running and it has a ready
  162. // condition of status true.
  163. func podRunningReady(p *v1.Pod) (bool, error) {
  164. // Check the phase is running.
  165. if p.Status.Phase != v1.PodRunning {
  166. return false, fmt.Errorf("want pod '%s' on '%s' to be '%v' but was '%v'",
  167. p.ObjectMeta.Name, p.Spec.NodeName, v1.PodRunning, p.Status.Phase)
  168. }
  169. // Check the ready condition is true.
  170. if !isPodReady(p) {
  171. return false, fmt.Errorf("pod '%s' on '%s' didn't have condition {%v %v}; conditions: %v",
  172. p.ObjectMeta.Name, p.Spec.NodeName, v1.PodReady, v1.ConditionTrue, p.Status.Conditions)
  173. }
  174. return true, nil
  175. }
  176. func isPodReady(p *v1.Pod) bool {
  177. for _, condition := range p.Status.Conditions {
  178. if condition.Type != v1.ContainersReady {
  179. continue
  180. }
  181. return condition.Status == v1.ConditionTrue
  182. }
  183. return false
  184. }
  185. // WaitForURL tests the provided url. Once a http 200 is returned the func returns with no error.
  186. // Timeout is 5min.
  187. func WaitForURL(url string) error {
  188. return wait.PollImmediate(2*time.Second, time.Minute*5, func() (bool, error) {
  189. req, err := http.NewRequest(http.MethodGet, url, http.NoBody)
  190. if err != nil {
  191. return false, nil
  192. }
  193. res, err := http.DefaultClient.Do(req)
  194. if err != nil {
  195. return false, nil
  196. }
  197. defer res.Body.Close()
  198. if res.StatusCode == http.StatusOK {
  199. return true, nil
  200. }
  201. return false, err
  202. })
  203. }
  204. // UpdateKubeSA updates a new Kubernetes Service Account for a test.
  205. func UpdateKubeSA(baseName string, kubeClientSet kubernetes.Interface, ns string, annotations map[string]string) (*v1.ServiceAccount, error) {
  206. sa := &v1.ServiceAccount{
  207. ObjectMeta: metav1.ObjectMeta{
  208. Name: baseName,
  209. Annotations: annotations,
  210. },
  211. }
  212. return kubeClientSet.CoreV1().ServiceAccounts(ns).Update(context.TODO(), sa, metav1.UpdateOptions{})
  213. }
  214. // UpdateKubeSA updates a new Kubernetes Service Account for a test.
  215. func GetKubeSA(baseName string, kubeClientSet kubernetes.Interface, ns string) (*v1.ServiceAccount, error) {
  216. return kubeClientSet.CoreV1().ServiceAccounts(ns).Get(context.TODO(), baseName, metav1.GetOptions{})
  217. }
  218. // NewConfig loads and returns the kubernetes credentials from the environment.
  219. // KUBECONFIG env var takes precedence and falls back to in-cluster config.
  220. func NewConfig() (*restclient.Config, *kubernetes.Clientset, crclient.Client) {
  221. var kubeConfig *restclient.Config
  222. var err error
  223. kcPath := os.Getenv("KUBECONFIG")
  224. if kcPath != "" {
  225. kubeConfig, err = clientcmd.BuildConfigFromFlags("", kcPath)
  226. if err != nil {
  227. Fail(err.Error())
  228. }
  229. } else {
  230. kubeConfig, err = restclient.InClusterConfig()
  231. if err != nil {
  232. Fail(err.Error())
  233. }
  234. }
  235. kubeClientSet, err := kubernetes.NewForConfig(kubeConfig)
  236. if err != nil {
  237. Fail(err.Error())
  238. }
  239. CRClient, err := crclient.New(kubeConfig, crclient.Options{Scheme: Scheme})
  240. if err != nil {
  241. Fail(err.Error())
  242. }
  243. return kubeConfig, kubeClientSet, CRClient
  244. }