webhook.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. Copyright © The ESO 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. https://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 addon
  14. import (
  15. "bytes"
  16. "context"
  17. "crypto/tls"
  18. "fmt"
  19. "net/http"
  20. "time"
  21. . "github.com/onsi/ginkgo/v2"
  22. "k8s.io/apimachinery/pkg/util/wait"
  23. )
  24. const externalSecretValidationReview = `{"apiVersion":"admission.k8s.io/v1","kind":"AdmissionReview","request":{"uid":"test","kind":{"group":"external-secrets.io","version":"v1","kind":"ExternalSecret"},"resource":{"group":"external-secrets.io","version":"v1","resource":"externalsecrets"},"dryRun":true,"operation":"CREATE","userInfo":{"username":"test","uid":"test","groups":[],"extra":{}}}}`
  25. var (
  26. externalSecretWebhookURL = func(namespace string) string {
  27. return fmt.Sprintf("https://external-secrets-webhook.%s.svc.cluster.local/validate-external-secrets-io-v1-externalsecret", namespace)
  28. }
  29. webhookReadyPollInterval = time.Second
  30. webhookReadyTimeout = 5 * time.Minute
  31. webhookReadyContext = func() context.Context { return GinkgoT().Context() }
  32. )
  33. func waitForExternalSecretWebhookReady(namespace string) error {
  34. tr := &http.Transport{
  35. // nolint:gosec
  36. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  37. }
  38. client := &http.Client{Transport: tr}
  39. url := externalSecretWebhookURL(namespace)
  40. return wait.PollUntilContextTimeout(webhookReadyContext(), webhookReadyPollInterval, webhookReadyTimeout, true, func(ctx context.Context) (bool, error) {
  41. res, err := client.Post(url, "application/json", bytes.NewBufferString(externalSecretValidationReview))
  42. if err != nil {
  43. return false, nil
  44. }
  45. defer func() {
  46. _ = res.Body.Close()
  47. }()
  48. GinkgoWriter.Printf("webhook res: %d", res.StatusCode)
  49. return res.StatusCode == http.StatusOK, nil
  50. })
  51. }