webhook.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. "strings"
  21. "time"
  22. "k8s.io/apimachinery/pkg/util/wait"
  23. . "github.com/onsi/ginkgo/v2"
  24. )
  25. 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":{}}}}`
  26. const externalSecretsChartName = "external-secrets"
  27. var (
  28. externalSecretWebhookURL = func(serviceName, namespace string) string {
  29. return fmt.Sprintf("https://%s.%s.svc.cluster.local/validate-external-secrets-io-v1-externalsecret", serviceName, namespace)
  30. }
  31. webhookReadyPollInterval = time.Second
  32. webhookReadyTimeout = 5 * time.Minute
  33. webhookReadyContext = func() context.Context { return GinkgoT().Context() }
  34. )
  35. func webhookServiceName(releaseName string) string {
  36. return fmt.Sprintf("%s-webhook", chartFullName(releaseName))
  37. }
  38. func chartFullName(releaseName string) string {
  39. if strings.Contains(releaseName, externalSecretsChartName) {
  40. return releaseName
  41. }
  42. return fmt.Sprintf("%s-%s", releaseName, externalSecretsChartName)
  43. }
  44. func waitForExternalSecretWebhookReady(serviceName, namespace string) error {
  45. tr := &http.Transport{
  46. // nolint:gosec
  47. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  48. }
  49. client := &http.Client{Transport: tr}
  50. url := externalSecretWebhookURL(serviceName, namespace)
  51. return wait.PollUntilContextTimeout(webhookReadyContext(), webhookReadyPollInterval, webhookReadyTimeout, true, func(ctx context.Context) (bool, error) {
  52. res, err := client.Post(url, "application/json", bytes.NewBufferString(externalSecretValidationReview))
  53. if err != nil {
  54. return false, nil
  55. }
  56. defer func() {
  57. _ = res.Body.Close()
  58. }()
  59. GinkgoWriter.Printf("webhook res: %d", res.StatusCode)
  60. return res.StatusCode == http.StatusOK, nil
  61. })
  62. }